// ...................................... //// compose // ...................................... const compose = (...fns) => (arg) => fns.reduceRight((acc, fn) => (fn ? fn(acc) : acc), arg); // ...................................... //// partition // ...................................... const accept = ([x, ...xs], fn, index = 0) => { if (undef(x)) return []; if (fn(x, index)) { return [x, ...accept(xs, fn, (index += 1))]; } else { return [...accept(xs, fn, (index += 1))]; } }; const reject = ([x, ...xs], fn, index = 0) => { if (undef(x)) return []; if (!fn(x, index)) { return [x, ...reject(xs, fn, (index += 1))]; } else { return [...reject(xs, fn, (index += 1))]; } }; // ...................................... //// partition // ...................................... const partition = (xs, fn) => [accept(xs, fn), reject(xs, fn)]; const def = (x) => typeof x !== 'undefined' && x !== null; const undef = (x) => !def(x); //--- pattern const labelPattern = /label:\s*([^\s;\n{]+)\s*(;|$)/g; const lineBreakPattern = /(\r\n|\n|\r)/gm; const removeCommentPattern = /\/\*[\s\S]*?\*\//g; const inLineCommentsPattern = /(\/\/).*?__\$__/gm; //--- regex const newRegexExp = (pattern, flag = '') => { const regex = new RegExp(pattern, flag); regex.lastIndex = 0; return regex; }; const regExp = { regex: (pattern, flag) => newRegexExp(pattern, flag), match: (str, pattern, flag) => str.match(regExp.regex(pattern, flag)) || [], test: (str, pattern, flag) => regExp.regex(pattern, flag).test(str), exec: (str, pattern, flag) => regExp.regex(pattern, flag).exec(str) || [], }; //--- object const ownkey = (obj) => Object.keys(obj)[0]; const ownValue = (obj) => Object.values(obj)[0]; const arrayOwnProperties = (obj) => isArray(obj) ? obj : Object.keys(obj).map((m) => ({ [m]: obj[m] })); //--- remove const removeDoubleSpace = (x) => isArray(x) ? x.map((str) => str.replace(/\s{2,}/g, ' ')) : x.replace(/\s{2,}/g, ' '); const removeLabel = (str) => str.replace(labelPattern, ''); const removeSpace = (str) => str.split(/\s+/).join(''); const removeLineBreak = (str) => str.replaceAll(lineBreakPattern, ''); //--- is const isArray = (x) => Array.isArray(x); const isObject = (x) => x != null && !Array.isArray(x) && typeof x === 'object'; const isString = (x) => def(x) && typeof x === 'string'; const isFunc = (x) => def(x) && typeof x === 'function'; const isBrowser = typeof document !== 'undefined'; const isPlainObject = (x) => x !== null && typeof x === 'object' && x.constructor.name === Object.name && !('props' in x && x.$$typeof); const isFalsish = (x) => x === undefined || x === null || x === false || x === ''; //--- Memoize const memoize = (func, src) => { const cache = {}; return (...args) => { const key = JSON.stringify(args); if (!cache[key]) { cache[key] = func(...args); } console.log('memodized...', `[ ${src} ]`); return cache[key]; }; }; export { arrayOwnProperties, compose, def, inLineCommentsPattern, isArray, isBrowser, isFalsish, isFunc, isObject, isPlainObject, isString, labelPattern, lineBreakPattern, memoize, newRegexExp, ownValue, ownkey, partition, regExp, removeCommentPattern, removeDoubleSpace, removeLabel, removeLineBreak, removeSpace, undef };