glitched-writer
Version:
Glitched, text writing module. Highly customizable settings. Decoding, decrypting, scrambling, or simply spelling out text.
135 lines (134 loc) • 5.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.trim = exports.filterHtml = exports.htmlToArray = exports.wordsRgx = exports.isSpecialChar = exports.stringToLetterItems = exports.letterToLetterItem = exports.coinFlip = exports.getRandomFromRange = exports.animateWithClass = exports.isInRange = exports.arrayOfTheSame = exports.promiseWhile = exports.wait = exports.parseCharset = exports.filterDuplicates = exports.getRandom = exports.deleteRandom = exports.clamp = exports.random = void 0;
/* eslint-disable no-unused-vars */
function random(min, max, math) {
const result = Math.random() * (max - min) + min;
if (math) {
// eslint-disable-next-line default-case
switch (math) {
case 'floor':
return Math.floor(result);
case 'round':
return Math.round(result);
case 'ceil':
return Math.ceil(result);
}
}
return result;
}
exports.random = random;
const clamp = (min, value, max) => Math.min(Math.max(value, min), max);
exports.clamp = clamp;
const deleteRandom = (array) => array.splice(random(0, array.length, 'floor'), 1).length > 0;
exports.deleteRandom = deleteRandom;
function getRandom(iterable) {
return iterable[random(0, iterable.length, 'floor')];
}
exports.getRandom = getRandom;
function filterDuplicates(iterable) {
const isString = typeof iterable === 'string', result = [];
new Set(iterable).forEach(x => result.push(x));
return isString ? result.join('') : result;
}
exports.filterDuplicates = filterDuplicates;
function parseCharset(input) {
let result;
// Charset is a string
if (typeof input === 'string')
result = input;
// Charset is an array
else if (input.length)
result = input.join('');
// Charset is a Set
else
result = Array.from(input).join('');
return result;
}
exports.parseCharset = parseCharset;
const wait = (time) => new Promise(resolve => setTimeout(() => resolve(time), time));
exports.wait = wait;
function promiseWhile(conditionFunc, actionPromise) {
const whilst = () => conditionFunc() ? actionPromise().then(whilst) : Promise.resolve();
return whilst();
}
exports.promiseWhile = promiseWhile;
const arrayOfTheSame = (value, length) => new Array(length).fill(value);
exports.arrayOfTheSame = arrayOfTheSame;
const isInRange = (min, value, max) => value >= min && value < max;
exports.isInRange = isInRange;
const animateWithClass = (element, className) => {
element.classList.remove(className);
// eslint-disable-next-line no-void
void element.offsetWidth;
element.classList.add(className);
};
exports.animateWithClass = animateWithClass;
function getRandomFromRange(range, round = true) {
return typeof range === 'number'
? range
: random(...range, round ? 'round' : undefined);
}
exports.getRandomFromRange = getRandomFromRange;
const coinFlip = (p = 0.5) => Math.random() < p;
exports.coinFlip = coinFlip;
const letterToLetterItem = (string) => ({
value: string,
});
exports.letterToLetterItem = letterToLetterItem;
const stringToLetterItems = (string) => [...string].map(exports.letterToLetterItem);
exports.stringToLetterItems = stringToLetterItems;
const isSpecialChar = (l) => ['\t', '\n', '\r', '\f', '\v', '', ' '].includes(l);
exports.isSpecialChar = isSpecialChar;
const findHTMLPattern = '(&(?:[a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});)|(<style.+?>.+?</style>|<script.+?>.+?</script>|<(?:!|/?[a-zA-Z]+).*?/?>)';
exports.wordsRgx = /(&(?:[a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});)|.\w+,*\.*"*\s?|\s+|\S+/gi;
function htmlToArray(string) {
const reg = new RegExp(findHTMLPattern, 'gi'), resultArray = [];
let find, lastIndex = 0;
// eslint-disable-next-line no-cond-assign
while ((find = reg.exec(string))) {
const from = find.index, to = reg.lastIndex, stringBefore = string.slice(lastIndex, from);
lastIndex = to;
stringBefore && resultArray.push(...exports.stringToLetterItems(stringBefore));
const result = {
value: find[0],
type: find[1] !== undefined ? 'html_entity' : 'tag',
};
resultArray.push(result);
}
string.length > lastIndex &&
resultArray.push(...exports.stringToLetterItems(string.slice(lastIndex)));
// return resultArray.map(l =>
// l.type
// ? l
// : {
// value: l.value,
// type: isSpecialChar(l.value) ? 'whitespace' : undefined,
// },
// )
return resultArray;
}
exports.htmlToArray = htmlToArray;
function filterHtml(string) {
const reg = new RegExp(findHTMLPattern, 'g');
return string.replace(reg, '');
}
exports.filterHtml = filterHtml;
function trim(str, l) {
if (!l || l.length > 1 || !str)
return str;
if (l === ' ')
return str.trim();
const reg = new RegExp(`${l}+`, 'g');
let find, result = str;
// eslint-disable-next-line no-cond-assign
while ((find = reg.exec(str))) {
const from = find.index, to = reg.lastIndex, length = to - from;
if (from === 0)
result = result.substring(to);
else if (to === str.length)
result = result.substring(result.length - length);
}
return result;
}
exports.trim = trim;