diff options
| author | Hanna Walter <[email protected]> | 2021-05-28 12:32:17 -0600 |
|---|---|---|
| committer | Hanna Walter <[email protected]> | 2021-05-28 12:32:17 -0600 |
| commit | 5280f7bfec0ebe0af4e9e7f611c9b30a7c819867 (patch) | |
| tree | 0790e0028a7b509c2e20425831df09d4e3962ae1 | |
| parent | 437f5e00294b5a1bd6772216f77e415ebb3b0943 (diff) | |
| download | faker-5280f7bfec0ebe0af4e9e7f611c9b30a7c819867.tar.xz faker-5280f7bfec0ebe0af4e9e7f611c9b30a7c819867.zip | |
feat: add uniqueArray helper function
| -rw-r--r-- | lib/helpers.js | 29 | ||||
| -rw-r--r-- | test/helpers.unit.js | 48 |
2 files changed, 77 insertions, 0 deletions
diff --git a/lib/helpers.js b/lib/helpers.js index 68e895e7..4ecdaab3 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -216,6 +216,35 @@ var Helpers = function (faker) { }; /** + * 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 * * @method faker.helpers.mustache diff --git a/test/helpers.unit.js b/test/helpers.unit.js index ec32dd02..c2f0adee 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -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"); |
