diff --git a/lib/utility.d.ts b/lib/utility.d.ts index d6c9c8f..c7df1e7 100644 --- a/lib/utility.d.ts +++ b/lib/utility.d.ts @@ -7,6 +7,12 @@ export declare class Utility { * @param setting The setting that needs to be supplied as the value for that key. */ resolveSettings(key: string, setting: string | CharacterSets | BackgroundSets | undefined): string; + /** + * Validates dimensions and returns dimension is correct format. + * @param width Width of the image + * @param height Height of the image + */ + containsOnlyNumbers(width: number, height: number): boolean; /** * Validates dimensions and returns dimension is correct format. * @param width Width of the image diff --git a/lib/utility.js b/lib/utility.js index f292a64..afe7dd7 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -15,6 +15,15 @@ class Utility { } return `${key}=${setting}&`; } + /** + * Validates dimensions and returns dimension is correct format. + * @param width Width of the image + * @param height Height of the image + */ + containsOnlyNumbers(width, height) { + const onlyNumbersRegEx = /^\d+$/; + return onlyNumbersRegEx.test(width.toString()) && onlyNumbersRegEx.test(height.toString()); + } /** * Validates dimensions and returns dimension is correct format. * @param width Width of the image @@ -24,6 +33,9 @@ class Utility { if (!width || !height) { return ""; } + if (!this.containsOnlyNumbers(width, height)) { + return ""; + } return `${width}x${height}`; } /** diff --git a/src/utility.ts b/src/utility.ts index b5b8471..a2b5bb5 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -18,6 +18,16 @@ export class Utility { return `${key}=${setting}&`; } + /** + * Validates dimensions and returns dimension is correct format. + * @param width Width of the image + * @param height Height of the image + */ + public containsOnlyNumbers(width: number, height: number): boolean { + const onlyNumbersRegEx = /^\d+$/; + return onlyNumbersRegEx.test(width.toString()) && onlyNumbersRegEx.test(height.toString()); + } + /** * Validates dimensions and returns dimension is correct format. * @param width Width of the image @@ -27,6 +37,11 @@ export class Utility { if (!width || !height) { return ""; } + + if (!this.containsOnlyNumbers(width, height)) { + return ""; + } + return `${width}x${height}`; } diff --git a/tests/robohash-avatars.test.js b/tests/robohash-avatars.test.js index 8942296..9d5d62f 100644 --- a/tests/robohash-avatars.test.js +++ b/tests/robohash-avatars.test.js @@ -67,7 +67,7 @@ describe("generateAvatar()", function () { assert.equal(actual, "https://robohash.org/tonystark?set=set4&bgset=bg1&size=400x400"); }); - it("allz inputs supplied", function () { + it("valid username but other undefined inputs supplied", function () { console.log("\t", "[test]", this.test.title); var actual = avatars.generateAvatar({ username: "tonystark", @@ -78,5 +78,64 @@ describe("generateAvatar()", function () { }); assert.equal(actual, "https://robohash.org/tonystark"); }); + + it("all undefined inputs supplied", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: undefined, + background: undefined, + characters: undefined, + height: undefined, + width: undefined + }); + assert.equal(actual, "https://robohash.org/RobohashAvatarNPM"); + }); + + it("username and character set supplied", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: "peterparker", + characters: avatars.CharacterSets.Monsters, + }); + assert.equal(actual, "https://robohash.org/peterparker?set=set2"); + }); + + it("width but no height supplied", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: "peterparker", + width: 400 + }); + assert.equal(actual, "https://robohash.org/peterparker"); + }); + + it("height but no width supplied", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: "peterparker", + height: 400 + }); + assert.equal(actual, "https://robohash.org/peterparker"); + }); + + it("height and width supplied as string", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: "peterparker", + height: "400", + width: "600" + }); + assert.equal(actual, "https://robohash.org/peterparker?size=600x400"); + }); + + it("height and width supplied as string alphabets", function () { + console.log("\t", "[test]", this.test.title); + var actual = avatars.generateAvatar({ + username: "peterparker", + height: "abc", + width: "ghi" + }); + assert.equal(actual, "https://robohash.org/peterparker"); + }); }); \ No newline at end of file