Skip to content

Commit

Permalink
Merge pull request #1177 from hsw107/master
Browse files Browse the repository at this point in the history
feat: add uniqueArray helper function
  • Loading branch information
Marak authored Oct 19, 2021
2 parents ad303ba + 5280f7b commit 2b7ce5f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ var Helpers = function (faker) {
return o;
};

/**
* takes an array of strings or function that returns a string
* and outputs a unique array of strings based on that source
* @example uniqueArray(faker.random.word, 50)
* @example uniqueArray(faker.definitions.name.first_name, 6)
* @example uniqueArray(["Hello", "World", "Goodbye"], 2)
* @method faker.helpers.uniqueArray
* @param {string[] | function => string} source
* @param {number} length
* @returns {string[]}
*/
self.uniqueArray = function(source, length) {
if (Array.isArray(source)) {
const set = new Set(source);
const array = Array.from(set);
return faker.helpers.shuffle(array).splice(0, length);
}
const set = new Set();
try {
if (typeof source === "function") {
while (set.size < length) {
set.add(source());
}
}
} finally {
return Array.from(set);
}
};

/**
* mustache
*
Expand Down
48 changes: 48 additions & 0 deletions test/helpers.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,54 @@ describe("helpers.js", function () {
});
});

describe("uniqueArray()", function () {
it("custom array returns unique array", function () {
var input = ["a", "a", "a", "a,", "a", "a", "a", "a", "b"];
var length = 2;
var unique = faker.helpers.uniqueArray(input, length);
assert.strictEqual(unique.length, length);
assert.strictEqual(new Set(unique).size, length);
});

it("definition array returns unique array", function () {
var length = faker.datatype.number({ min: 1, max: 6 });
var unique = faker.helpers.uniqueArray(faker.definitions.hacker.noun, length);
assert.strictEqual(unique.length, length);
assert.strictEqual(new Set(unique).size, length);
});

it("function returns unique array", function () {
var length = faker.datatype.number({ min: 1, max: 6 });
var unique = faker.helpers.uniqueArray(faker.lorem.word, length);
assert.strictEqual(unique.length, length);
assert.strictEqual(new Set(unique).size, length);
});

it("empty array returns empty array", function () {
var input = [];
var length = faker.datatype.number({ min: 1, max: 6 });
var unique = faker.helpers.uniqueArray(input, length);
assert.strictEqual(unique.length, input.length);
assert.strictEqual(new Set(unique).size, input.length);
});

it("length longer than source returns max length", function () {
var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
var length = input.length + 1;
var unique = faker.helpers.uniqueArray(input, length);
assert.strictEqual(unique.length, input.length);
assert.strictEqual(new Set(unique).size, input.length);
});

it("works as expected when seeded", function () {
var input = ["a", "a", "a", "a", "a", "f", "g", "h", "i", "j"];
var length = 5;
faker.seed(100);
var unique = faker.helpers.uniqueArray(input, length);
assert.deepStrictEqual(unique, ["g", "a", "i", "f", "j"]);
});
});

describe("slugify()", function () {
it("removes unwanted characters from URI string", function () {
assert.strictEqual(faker.helpers.slugify("Aiden.Harªann"), "Aiden.Harann");
Expand Down

0 comments on commit 2b7ce5f

Please sign in to comment.