Skip to content

Commit

Permalink
Added Random Function
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmesh-hemaram committed Jan 13, 2024
1 parent 4cc00a4 commit e3d5624
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 52 deletions.
57 changes: 5 additions & 52 deletions apps/acf-extension/src/content_scripts/util/value.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,21 @@
import { Logger } from '@dhruv-techapps/core-common';
import RandExp from 'randexp';
import { ConfigError } from '../error';
import Common from '../common';

export const VALUE_MATCHER = {
GOOGLE_SHEETS: /^GoogleSheets::/i,
QUERY_PARAM: /^Query::/i,
FUNC: /^Func::/i,
RANDOM: /<random(\[.+?\])?(\{(\d+),?(\d+)?\})?>/gi,
CHOICE: /<choice(\[.+?\])?(\{(\d+),?(\d+)?\})?>/gi,
RANDOM: /<random(.+)>/gi,
BATCH_REPEAT: /<batchRepeat>/,
};

/*
* Random Number Constant
*/
const CAP_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const SMALL_ALPHA = 'abcdefghijklmnopqrstuvwxyz';
const SPECIAL_CHAR = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
const NUM = '0123456789';

const Value = (() => {
const getRandomValue = (value: string) =>
value.replace(VALUE_MATCHER.RANDOM, (_, range, __, start = 6, end = undefined) => {
let characters;
switch (range) {
case '[A-Z]':
characters = CAP_ALPHA;
break;
case '[a-z]':
characters = SMALL_ALPHA;
break;
case '[^a-z]':
characters = CAP_ALPHA + SPECIAL_CHAR + NUM;
break;
case '[^A-Z]':
characters = SMALL_ALPHA + SPECIAL_CHAR + NUM;
break;
case '[\\d]':
characters = NUM;
break;
case '[\\D]':
characters = CAP_ALPHA + SMALL_ALPHA;
break;
case '[\\w]':
characters = `${CAP_ALPHA + SMALL_ALPHA + NUM}_`;
break;
case '[\\W]':
characters = SPECIAL_CHAR;
break;
case '[.]':
characters = CAP_ALPHA + SMALL_ALPHA + SPECIAL_CHAR + NUM;
break;
default:
characters = range?.match(/\[(.+)\]/)[1] || CAP_ALPHA + SMALL_ALPHA + SPECIAL_CHAR + NUM;
}
const charactersLength = characters.length;
let result = '';
let length = start;
if (end) {
length = Math.floor(Math.random() * Number(end)) + Number(start);
}
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
value.replace(VALUE_MATCHER.RANDOM, (_, regex) => {
Logger.colorDebug('RandExp', regex);
const result = new RandExp(regex).gen();
Logger.colorDebug('GetRandomValue', result);
return result;
});
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"express": "^4.18.1",
"i18next": "^23.7.16",
"i18next-http-backend": "^2.2.1",
"randexp": "^0.5.3",
"react": "18.2.0",
"react-bootstrap": "^2.9.2",
"react-dom": "18.2.0",
Expand Down

0 comments on commit e3d5624

Please sign in to comment.