From 4503265fbf9b2153f2c8786e850005e26a90f767 Mon Sep 17 00:00:00 2001 From: Priyansh Date: Sun, 24 Jan 2021 22:11:02 +0530 Subject: removeExts function - if ignoreext is set to false --- index.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d7b1bd4..e0ab620 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,30 @@ export default class RandomAvatars { - constructor() {} + constructor(string, hashcount=11, ignoreext=true) { + /** + * This creates our avatar. + * Takes in a string and makes an picture out of it. + */ + if (ignoreext) removeExts(this, string); + + this.format = 'png' + } +} + +function removeExts(currentClass, string) { + /** + * Sets the input string to create an avatar + */ + + // If the user hasn't disabled it, we will detect image extensions, such as .png, .jpg, etc. + // We'll remove them from the string before hashing. + // This ensures that /Bear.png and /Bear.bmp will send back the same image, in different formats. + + let input = (String(string)).toLowerCase(); + if (input.endsWith('.png') || input.endsWith('.gif') || input.endsWith('.jpg') || input.endsWith('.bmp') || input.endsWith('.jpeg') || input.endsWith('.ppm') || input.endsWith('.datauri')) { + let format = input.substr(input.lastIndexOf('.') + 1, input.length); + if (format.toLowerCase() === 'jpg') format = 'jpg'; + currentClass.format = format + input = input.substr(0, input.lastIndexOf('.')); + } + return input; } -- cgit v1.2.3