From 44575f7df6c21455492041073cd0e45d82188e3f Mon Sep 17 00:00:00 2001 From: brian shostak Date: Wed, 15 Dec 2021 14:08:54 -0700 Subject: [PATCH] Added a new function to return a random string of digits. Added tests. --- lib/fakerator.js | 10 ++++++++++ test/specs/random.spec.js | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/lib/fakerator.js b/lib/fakerator.js index fa15216b..79908dd7 100644 --- a/lib/fakerator.js +++ b/lib/fakerator.js @@ -45,6 +45,16 @@ module.exports = function(locale) { return self.random.number(9); }, + stringDigit(length) { + let result = ""; + let options = "0123456789"; + let optionsLength = options.length; + for (let i = 0; i < length; i++) { + result += options.charAt(Math.floor(Math.random() * optionsLength)); + } + return result; + }, + hex(len = 1) { let res = []; for(let i = 0; i < len; i++) diff --git a/test/specs/random.spec.js b/test/specs/random.spec.js index 5e861824..92b50b89 100644 --- a/test/specs/random.spec.js +++ b/test/specs/random.spec.js @@ -84,4 +84,9 @@ describe("Fakerator.random", () => { expect(fakerator.random.string({ min: 5, max: 10 })).to.be.length(5); }); + it("check random.stringDigit function", () => { + expect(fakerator.random.stringDigit(10)).to.be.length(10); + expect(fakerator.random.stringDigit(8)).to.be.a("String").length(8); + }); + }); \ No newline at end of file