refactor: migrated all the generators

pull/243/head
Josh Moore 1 year ago
parent 0721510b31
commit d04b4f9bb5

@ -1,2 +1,49 @@
import fs from 'fs-extra';
import { randomBytes } from 'crypto';
import cryptoRandomString from 'crypto-random-string';
export const random = ({ length }: { length: number }) => cryptoRandomString({ length, type: 'alphanumeric' });
import { path } from '@tycrek/joint';
type Length = { length: number, gfyLength?: number };
// todo: load gfy length from config file
const MIN_LENGTH_GFY = 2;
/**
* Random generator
*/
export const random = ({ length }: Length) => cryptoRandomString({ length, type: 'alphanumeric' });
/**
* Timestamp generator
*/
export const timestamp = () => `${Date.now()}`;
/**
* Charset generator
*/
export const charset = ({ length, charset }: { length: number, charset: string[] }): string =>
[...randomBytes(length)].map((byte) => charset[Number(byte) % charset.length]).join('').slice(1).concat(charset[0]);
/**
* ZWS generator
*/
export const zws = ({ length }: Length) => charset({ length, charset: ['\u200B', '\u200C', '\u200D', '\u2060'] });
/**
* Gfycat generator
*/
export const gfycat = ({ gfyLength }: Length) => {
const count = gfyLength ?? MIN_LENGTH_GFY;
const getWord = (list: string[], delim = '') =>
list[Math.floor(Math.random() * list.length)].concat(delim);
const adjectives = fs.readFileSync(path.join('./common/gfycat/adjectives.txt')).toString().split('\n');
const animals = fs.readFileSync(path.join('./common/gfycat/animals.txt')).toString().split('\n');
let gfycat = '';
for (let i = 0; i < (count < MIN_LENGTH_GFY ? MIN_LENGTH_GFY : count); i++)
gfycat += getWord(adjectives, '-');
return gfycat.concat(getWord(animals));
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,23 +0,0 @@
import fs from 'fs-extra';
// Don't trigger circular dependency during setup
if (require !== undefined && !require?.main?.filename.includes('setup.js'))
var MIN_LENGTH = require('../setup').gfyIdSize; // skipcq: JS-0239, JS-0102
function getWord(list: string[], delim = '') {
return list[Math.floor(Math.random() * list.length)].concat(delim);
}
function genString(count = MIN_LENGTH) {
// For some reason these 3 lines MUST be inside the function
const { path } = require('../utils');
const adjectives = fs.readFileSync(path('./gfycat/adjectives.txt')).toString().split('\n');
const animals = fs.readFileSync(path('./gfycat/animals.txt')).toString().split('\n');
let gfycat = '';
for (let i = 0; i < (count < MIN_LENGTH ? MIN_LENGTH : count); i++)
gfycat += getWord(adjectives, '-');
return gfycat.concat(getWord(animals));
};
export default ({ gfyLength }: { gfyLength: number }) => genString(gfyLength);

@ -1,2 +0,0 @@
import { randomBytes } from 'crypto';
export default (length: number, charset: string[]): string => [...randomBytes(length)].map((byte) => charset[Number(byte) % charset.length]).join('').slice(1).concat(charset[0]);

@ -1,2 +0,0 @@
import { nanoid } from 'nanoid';
export default ({ length }: { length?: number }) => nanoid(length);

@ -1,2 +0,0 @@
import cryptoRandomString from 'crypto-random-string';
export default ({ length }: { length: number }) => cryptoRandomString({ length, type: 'alphanumeric' });

@ -1 +0,0 @@
export default () => `${Date.now()}`;

@ -1,4 +0,0 @@
import lengthGen from './lengthGen';
const zeroWidthChars = ['\u200B', '\u200C', '\u200D', '\u2060'];
export default ({ length }: { length: number }) => lengthGen(length, zeroWidthChars);
export const checkIfZws = (str: string) => str.split('').every(char => zeroWidthChars.includes(char));
Loading…
Cancel
Save