diff options
| author | Priyansh <[email protected]> | 2021-07-08 21:26:21 +0530 |
|---|---|---|
| committer | Priyansh <[email protected]> | 2021-07-08 21:26:21 +0530 |
| commit | fffc7f88ed3dc8e5ac92bcddefb20cc3bad8ceb7 (patch) | |
| tree | 87082b7ab1ba8a22d4fbb5fd9dbd976131026608 /index.js | |
| parent | 80a526a5957785700258a02bc24d21afe2c843e0 (diff) | |
| download | randomavatars-fffc7f88ed3dc8e5ac92bcddefb20cc3bad8ceb7.tar.xz randomavatars-fffc7f88ed3dc8e5ac92bcddefb20cc3bad8ceb7.zip | |
impl: listDir and getListOfFiles
Diffstat (limited to 'index.js')
| -rw-r--r-- | index.js | 110 |
1 files changed, 79 insertions, 31 deletions
@@ -1,3 +1,6 @@ +const fs = require('fs'); +const path = require('path'); +const pathWalker = require('walk'); export default class RandomAvatars { constructor(string, hashcount = 11, ignoreext = true) { /** @@ -16,41 +19,86 @@ export default class RandomAvatars { // 3 = BG this.iter = 4; createhashes(this, hashcount); + this.resourceDir = __dirname + '/'; + // Get the list of Backgrounds and Robosets + this.sets = this.listDirs(resourceDir + 'sets') + this.bgsets = this.listDirs(resourceDir + 'backgrounds'); + + // Get the colors in set 1 + this.colors = this.listDirs(resourceDir + 'sets/set1'); 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('.')); + removeExts(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'; + this.format = format; + input = input.substr(0, input.lastIndexOf('.')); + } + return input; } - return input; -} + + createhashes(count) { + /** + * Breaks up our hash into slots, so we can pull them out later. + * Essentially, it splits our SHA/MD5/etc into X parts. + */ + + for (const i of Array(count).keys()) { + // Get 1/numblocks of the hash + const blocksize = parseInt(this.hexdigest.length/count); + const currentStart = ((i + 1) * blocksize) - blocksize; + const currentEnd = (1 +i) * blocksize; + this.hasharray.push(parseInt(this.hexdigest.substr(currentStart, currentEnd), 16)); + } + this.hasharray = this.hasharray + this.hasharray; + } + + listDirs(path) { + return fs.readdirSync(path, {withFileTypes: true}).filter((dir) => dir.isDirectory()).map((dir) => dir.name); + } + + getListOfFiles(path) { + // Go through each subdirectory of `path`, and choose one file from each to use in our hash. + // Continue to increase self.iter, so we use a different 'slot' of randomness each time. + + const walker = pathWalker.walk(path); + const chosenFiles = []; + + // Get a list of all subdirectories + const directories = []; + walker.on('directory', (root, fileStats, next) => { + if (fileStats.name.substr(0,1) !== '.') { + directories.push(path.join(root, fileStats.name)); + } + next(); + }); + + // Go through each directory in the list, and choose one file from each. + // Add this file to our master list of robotparts. + + for (const directory of directories) { + const filesInDir = []; + for (const imagefile of this.listDirs(directory)) { + filesInDir.push(imagefile); + } + + // Use some of our hash bits to choose which file + const elementInList = this.hasharray[this.iter] % filesInDir.length(); + chosenFiles.push(filesInDir[elementInList]); + this.iter += 1; + } -function createhashes(currentClass, count) { - /** - * Breaks up our hash into slots, so we can pull them out later. - * Essentially, it splits our SHA/MD5/etc into X parts. - */ - - for (const i of Array(count).keys()) { - // Get 1/numblocks of the hash - const blocksize = parseInt(currentClass.hexdigest.length/count); - const currentStart = ((i + 1) * blocksize) - blocksize; - const currentEnd = (1 +i) * blocksize; - currentClass.hasharray.push(parseInt(currentClass.hexdigest.substr(currentStart, currentEnd), 16)); + return chosenFiles; } - currentClass.hasharray = currentClass.hasharray + currentClass.hasharray } |
