Codacy style changes for test files
This commit is contained in:
@ -22,8 +22,8 @@ const snippetFiles = [];
|
|||||||
|
|
||||||
const snippetFilesActive = fs.readdirSync(SNIPPETS_ACTIVE, 'utf8').map(fileName => fileName.slice(0, -3));
|
const snippetFilesActive = fs.readdirSync(SNIPPETS_ACTIVE, 'utf8').map(fileName => fileName.slice(0, -3));
|
||||||
const snippetFilesArchive = fs.readdirSync(SNIPPETS_ARCHIVE, 'utf8')
|
const snippetFilesArchive = fs.readdirSync(SNIPPETS_ARCHIVE, 'utf8')
|
||||||
.filter(fileName => !fileName.includes('README')) // -> Filters out main README.md file in Archieve which isn't a snippet
|
.filter(fileName => !fileName.includes('README')) // -> Filters out main README.md file in Archieve which isn't a snippet
|
||||||
.map(fileName => fileName.slice(0, -3));
|
.map(fileName => fileName.slice(0, -3));
|
||||||
|
|
||||||
snippetFiles.push(...snippetFilesActive);
|
snippetFiles.push(...snippetFilesActive);
|
||||||
snippetFiles.push(...snippetFilesArchive);
|
snippetFiles.push(...snippetFilesArchive);
|
||||||
@ -45,19 +45,19 @@ snippetFiles
|
|||||||
const fileCode = fileData.slice(fileData.search(/```\s*js/i), fileData.lastIndexOf('```') + 3);
|
const fileCode = fileData.slice(fileData.search(/```\s*js/i), fileData.lastIndexOf('```') + 3);
|
||||||
// Split code based on code markers
|
// Split code based on code markers
|
||||||
const blockMarkers = fileCode
|
const blockMarkers = fileCode
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map((line, lineIndex) => (line.slice(0, 3) === '```' ? lineIndex : '//CLEAR//'))
|
.map((line, lineIndex) => (line.slice(0, 3) === '```' ? lineIndex : '//CLEAR//'))
|
||||||
.filter(x => !(x === '//CLEAR//'));
|
.filter(x => !(x === '//CLEAR//'));
|
||||||
// Grab snippet function based on code markers
|
// Grab snippet function based on code markers
|
||||||
const fileFunction = fileCode
|
const fileFunction = fileCode
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(line => line.trim())
|
.map(line => line)
|
||||||
.filter((_, i) => blockMarkers[0] < i && i < blockMarkers[1]);
|
.filter((_, i) => blockMarkers[0] < i && i < blockMarkers[1]).concat('');
|
||||||
// Grab snippet example based on code markers
|
// Grab snippet example based on code markers
|
||||||
const fileExample = fileCode
|
const fileExample = fileCode
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(line => line.trim())
|
.map(line => line)
|
||||||
.filter((_, i) => blockMarkers[2] < i && i < blockMarkers[3]);
|
.filter((_, i) => blockMarkers[2] < i && i < blockMarkers[3]).concat('');
|
||||||
|
|
||||||
// Export template for snippetName.js
|
// Export template for snippetName.js
|
||||||
const exportFile = `${fileFunction.join('\n')}\nmodule.exports = ${fileName};`.trim();
|
const exportFile = `${fileFunction.join('\n')}\nmodule.exports = ${fileName};`.trim();
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
const CSVToArray = (data, delimiter = ',', omitFirstRow = false) =>
|
const CSVToArray = (data, delimiter = ',', omitFirstRow = false) =>
|
||||||
data
|
data
|
||||||
.slice(omitFirstRow ? data.indexOf('\n') + 1 : 0)
|
.slice(omitFirstRow ? data.indexOf('\n') + 1 : 0)
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(v => v.split(delimiter));
|
.map(v => v.split(delimiter));
|
||||||
|
|
||||||
module.exports = CSVToArray;
|
module.exports = CSVToArray;
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const CSVToJSON = (data, delimiter = ',') => {
|
const CSVToJSON = (data, delimiter = ',') => {
|
||||||
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
|
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
|
||||||
return data
|
return data
|
||||||
.slice(data.indexOf('\n') + 1)
|
.slice(data.indexOf('\n') + 1)
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(v => {
|
.map(v => {
|
||||||
const values = v.split(delimiter);
|
const values = v.split(delimiter);
|
||||||
return titles.reduce((obj, title, index) => ((obj[title] = values[index]), obj), {});
|
return titles.reduce((obj, title, index) => ((obj[title] = values[index]), obj), {});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = CSVToJSON;
|
module.exports = CSVToJSON;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const JSONToDate = arr => {
|
const JSONToDate = arr => {
|
||||||
const dt = new Date(parseInt(arr.toString().substr(6)));
|
const dt = new Date(parseInt(arr.toString().substr(6)));
|
||||||
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
|
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = JSONToDate;
|
module.exports = JSONToDate;
|
||||||
@ -1,4 +1,5 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const JSONToFile = (obj, filename) =>
|
const JSONToFile = (obj, filename) =>
|
||||||
fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));
|
fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));
|
||||||
|
|
||||||
module.exports = JSONToFile;
|
module.exports = JSONToFile;
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const JSONtoCSV = (arr, columns, delimiter = ',') =>
|
const JSONtoCSV = (arr, columns, delimiter = ',') =>
|
||||||
[
|
[
|
||||||
columns.join(delimiter),
|
columns.join(delimiter),
|
||||||
...arr.map(obj =>
|
...arr.map(obj =>
|
||||||
columns.reduce(
|
columns.reduce(
|
||||||
(acc, key) => `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`,
|
(acc, key) => `${acc}${!acc.length ? '' : delimiter}"${!obj[key] ? '' : obj[key]}"`,
|
||||||
''
|
''
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
|
||||||
module.exports = JSONtoCSV;
|
module.exports = JSONtoCSV;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
||||||
|
|
||||||
module.exports = RGBToHex;
|
module.exports = RGBToHex;
|
||||||
@ -1,10 +1,11 @@
|
|||||||
const URLJoin = (...args) =>
|
const URLJoin = (...args) =>
|
||||||
args
|
args
|
||||||
.join('/')
|
.join('/')
|
||||||
.replace(/[\/]+/g, '/')
|
.replace(/[\/]+/g, '/')
|
||||||
.replace(/^(.+):\//, '$1://')
|
.replace(/^(.+):\//, '$1://')
|
||||||
.replace(/^file:/, 'file:/')
|
.replace(/^file:/, 'file:/')
|
||||||
.replace(/\/(\?|&|#[^!])/g, '$1')
|
.replace(/\/(\?|&|#[^!])/g, '$1')
|
||||||
.replace(/\?/g, '&')
|
.replace(/\?/g, '&')
|
||||||
.replace('&', '?');
|
.replace('&', '?');
|
||||||
|
|
||||||
module.exports = URLJoin;
|
module.exports = URLJoin;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const UUIDGeneratorBrowser = () =>
|
const UUIDGeneratorBrowser = () =>
|
||||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||||
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = UUIDGeneratorBrowser;
|
module.exports = UUIDGeneratorBrowser;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const UUIDGeneratorNode = () =>
|
const UUIDGeneratorNode = () =>
|
||||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||||
(c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
|
(c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = UUIDGeneratorNode;
|
module.exports = UUIDGeneratorNode;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const all = (arr, fn = Boolean) => arr.every(fn);
|
const all = (arr, fn = Boolean) => arr.every(fn);
|
||||||
|
|
||||||
module.exports = all;
|
module.exports = all;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const any = (arr, fn = Boolean) => arr.some(fn);
|
const any = (arr, fn = Boolean) => arr.some(fn);
|
||||||
|
|
||||||
module.exports = any;
|
module.exports = any;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
|
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
|
||||||
|
|
||||||
module.exports = approximatelyEqual;
|
module.exports = approximatelyEqual;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const arrayToCSV = (arr, delimiter = ',') =>
|
const arrayToCSV = (arr, delimiter = ',') =>
|
||||||
arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
|
arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
|
||||||
|
|
||||||
module.exports = arrayToCSV;
|
module.exports = arrayToCSV;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const arrayToHtmlList = (arr, listID) =>
|
const arrayToHtmlList = (arr, listID) =>
|
||||||
(el => (
|
(el => (
|
||||||
(el = document.querySelector('#' + listID)),
|
(el = document.querySelector('#' + listID)),
|
||||||
(el.innerHTML += arr.map(item => `<li>${item}</li>`).join(''))
|
(el.innerHTML += arr.map(item => `<li>${item}</li>`).join(''))
|
||||||
))();
|
))();
|
||||||
|
|
||||||
module.exports = arrayToHtmlList;
|
module.exports = arrayToHtmlList;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
|
const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
|
||||||
|
|
||||||
module.exports = ary;
|
module.exports = ary;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const atob = str => new Buffer(str, 'base64').toString('binary');
|
const atob = str => new Buffer(str, 'base64').toString('binary');
|
||||||
|
|
||||||
module.exports = atob;
|
module.exports = atob;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const attempt = (fn, ...args) => {
|
const attempt = (fn, ...args) => {
|
||||||
try {
|
try {
|
||||||
return fn(...args);
|
return fn(...args);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return e instanceof Error ? e : new Error(e);
|
return e instanceof Error ? e : new Error(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = attempt;
|
module.exports = attempt;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
|
const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
|
||||||
|
|
||||||
module.exports = average;
|
module.exports = average;
|
||||||
@ -1,4 +1,5 @@
|
|||||||
const averageBy = (arr, fn) =>
|
const averageBy = (arr, fn) =>
|
||||||
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
|
||||||
arr.length;
|
arr.length;
|
||||||
|
|
||||||
module.exports = averageBy;
|
module.exports = averageBy;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const bifurcate = (arr, filter) =>
|
const bifurcate = (arr, filter) =>
|
||||||
arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
|
arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
|
||||||
|
|
||||||
module.exports = bifurcate;
|
module.exports = bifurcate;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const bifurcateBy = (arr, fn) =>
|
const bifurcateBy = (arr, fn) =>
|
||||||
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
|
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
|
||||||
|
|
||||||
module.exports = bifurcateBy;
|
module.exports = bifurcateBy;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
|
const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
|
||||||
if (start > end) return -1;
|
if (start > end) return -1;
|
||||||
const mid = Math.floor((start + end) / 2);
|
const mid = Math.floor((start + end) / 2);
|
||||||
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
||||||
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
||||||
return mid;
|
return mid;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = binarySearch;
|
module.exports = binarySearch;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const bind = (fn, context, ...args) =>
|
const bind = (fn, context, ...args) =>
|
||||||
function() {
|
function() {
|
||||||
return fn.apply(context, args.concat(...arguments));
|
return fn.apply(context, args.concat(...arguments));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = bind;
|
module.exports = bind;
|
||||||
@ -1,10 +1,11 @@
|
|||||||
const bindAll = (obj, ...fns) =>
|
const bindAll = (obj, ...fns) =>
|
||||||
fns.forEach(
|
fns.forEach(
|
||||||
fn => (
|
fn => (
|
||||||
(f = obj[fn]),
|
(f = obj[fn]),
|
||||||
(obj[fn] = function() {
|
(obj[fn] = function() {
|
||||||
return f.apply(obj);
|
return f.apply(obj);
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = bindAll;
|
module.exports = bindAll;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const bindKey = (context, fn, ...args) =>
|
const bindKey = (context, fn, ...args) =>
|
||||||
function() {
|
function() {
|
||||||
return context[fn].apply(context, args.concat(...arguments));
|
return context[fn].apply(context, args.concat(...arguments));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = bindKey;
|
module.exports = bindKey;
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const binomialCoefficient = (n, k) => {
|
const binomialCoefficient = (n, k) => {
|
||||||
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
|
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
|
||||||
if (k < 0 || k > n) return 0;
|
if (k < 0 || k > n) return 0;
|
||||||
if (k === 0 || k === n) return 1;
|
if (k === 0 || k === n) return 1;
|
||||||
if (k === 1 || k === n - 1) return n;
|
if (k === 1 || k === n - 1) return n;
|
||||||
if (n - k < k) k = n - k;
|
if (n - k < k) k = n - k;
|
||||||
let res = n;
|
let res = n;
|
||||||
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
|
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
|
||||||
return Math.round(res);
|
return Math.round(res);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = binomialCoefficient;
|
module.exports = binomialCoefficient;
|
||||||
@ -1,4 +1,5 @@
|
|||||||
const bottomVisible = () =>
|
const bottomVisible = () =>
|
||||||
document.documentElement.clientHeight + window.scrollY >=
|
document.documentElement.clientHeight + window.scrollY >=
|
||||||
(document.documentElement.scrollHeight || document.documentElement.clientHeight);
|
(document.documentElement.scrollHeight || document.documentElement.clientHeight);
|
||||||
|
|
||||||
module.exports = bottomVisible;
|
module.exports = bottomVisible;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const btoa = str => new Buffer(str, 'binary').toString('base64');
|
const btoa = str => new Buffer(str, 'binary').toString('base64');
|
||||||
|
|
||||||
module.exports = btoa;
|
module.exports = btoa;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const byteSize = str => new Blob([str]).size;
|
const byteSize = str => new Blob([str]).size;
|
||||||
|
|
||||||
module.exports = byteSize;
|
module.exports = byteSize;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const call = (key, ...args) => context => context[key](...args);
|
const call = (key, ...args) => context => context[key](...args);
|
||||||
|
|
||||||
module.exports = call;
|
module.exports = call;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const capitalize = ([first, ...rest], lowerRest = false) =>
|
const capitalize = ([first, ...rest], lowerRest = false) =>
|
||||||
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
|
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
|
||||||
|
|
||||||
module.exports = capitalize;
|
module.exports = capitalize;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
|
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
|
||||||
|
|
||||||
module.exports = capitalizeEveryWord;
|
module.exports = capitalizeEveryWord;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const castArray = val => (Array.isArray(val) ? val : [val]);
|
const castArray = val => (Array.isArray(val) ? val : [val]);
|
||||||
|
|
||||||
module.exports = castArray;
|
module.exports = castArray;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const next = () => fns[curr++](next);
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = chainAsync;
|
module.exports = chainAsync;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const chunk = (arr, size) =>
|
const chunk = (arr, size) =>
|
||||||
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
|
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
|
||||||
arr.slice(i * size, i * size + size)
|
arr.slice(i * size, i * size + size)
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = chunk;
|
module.exports = chunk;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
|
const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
|
||||||
|
|
||||||
module.exports = clampNumber;
|
module.exports = clampNumber;
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
||||||
Object.keys(obj).forEach(key => {
|
Object.keys(obj).forEach(key => {
|
||||||
if (key === childIndicator) {
|
if (key === childIndicator) {
|
||||||
cleanObj(obj[key], keysToKeep, childIndicator);
|
cleanObj(obj[key], keysToKeep, childIndicator);
|
||||||
} else if (!keysToKeep.includes(key)) {
|
} else if (!keysToKeep.includes(key)) {
|
||||||
delete obj[key];
|
delete obj[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = cleanObj;
|
module.exports = cleanObj;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
|
const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
|
||||||
|
|
||||||
module.exports = cloneRegExp;
|
module.exports = cloneRegExp;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
|
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
|
||||||
|
|
||||||
module.exports = coalesce;
|
module.exports = coalesce;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const coalesceFactory = valid => (...args) => args.find(valid);
|
const coalesceFactory = valid => (...args) => args.find(valid);
|
||||||
|
|
||||||
module.exports = coalesceFactory;
|
module.exports = coalesceFactory;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);
|
const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);
|
||||||
|
|
||||||
module.exports = collatz;
|
module.exports = collatz;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const collectInto = fn => (...args) => fn(args);
|
const collectInto = fn => (...args) => fn(args);
|
||||||
|
|
||||||
module.exports = collectInto;
|
module.exports = collectInto;
|
||||||
@ -1,19 +1,20 @@
|
|||||||
const colorize = (...args) => ({
|
const colorize = (...args) => ({
|
||||||
black: `\x1b[30m${args.join(' ')}`,
|
black: `\x1b[30m${args.join(' ')}`,
|
||||||
red: `\x1b[31m${args.join(' ')}`,
|
red: `\x1b[31m${args.join(' ')}`,
|
||||||
green: `\x1b[32m${args.join(' ')}`,
|
green: `\x1b[32m${args.join(' ')}`,
|
||||||
yellow: `\x1b[33m${args.join(' ')}`,
|
yellow: `\x1b[33m${args.join(' ')}`,
|
||||||
blue: `\x1b[34m${args.join(' ')}`,
|
blue: `\x1b[34m${args.join(' ')}`,
|
||||||
magenta: `\x1b[35m${args.join(' ')}`,
|
magenta: `\x1b[35m${args.join(' ')}`,
|
||||||
cyan: `\x1b[36m${args.join(' ')}`,
|
cyan: `\x1b[36m${args.join(' ')}`,
|
||||||
white: `\x1b[37m${args.join(' ')}`,
|
white: `\x1b[37m${args.join(' ')}`,
|
||||||
bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`,
|
bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`,
|
||||||
bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`,
|
bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`,
|
||||||
bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`,
|
bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`,
|
||||||
bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`,
|
bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`,
|
||||||
bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`,
|
bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`,
|
||||||
bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`,
|
bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`,
|
||||||
bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`,
|
bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`,
|
||||||
bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m`
|
bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m`
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = colorize;
|
module.exports = colorize;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const compact = arr => arr.filter(Boolean);
|
const compact = arr => arr.filter(Boolean);
|
||||||
|
|
||||||
module.exports = compact;
|
module.exports = compact;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
|
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
|
||||||
|
|
||||||
module.exports = compose;
|
module.exports = compose;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
|
const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
|
||||||
|
|
||||||
module.exports = composeRight;
|
module.exports = composeRight;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const converge = (converger, fns) => (...args) => converger(...fns.map(fn => fn.apply(null, args)));
|
const converge = (converger, fns) => (...args) => converger(...fns.map(fn => fn.apply(null, args)));
|
||||||
|
|
||||||
module.exports = converge;
|
module.exports = converge;
|
||||||
@ -1,18 +1,19 @@
|
|||||||
const copyToClipboard = str => {
|
const copyToClipboard = str => {
|
||||||
const el = document.createElement('textarea');
|
const el = document.createElement('textarea');
|
||||||
el.value = str;
|
el.value = str;
|
||||||
el.setAttribute('readonly', '');
|
el.setAttribute('readonly', '');
|
||||||
el.style.position = 'absolute';
|
el.style.position = 'absolute';
|
||||||
el.style.left = '-9999px';
|
el.style.left = '-9999px';
|
||||||
document.body.appendChild(el);
|
document.body.appendChild(el);
|
||||||
const selected =
|
const selected =
|
||||||
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||||
el.select();
|
el.select();
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
document.body.removeChild(el);
|
document.body.removeChild(el);
|
||||||
if (selected) {
|
if (selected) {
|
||||||
document.getSelection().removeAllRanges();
|
document.getSelection().removeAllRanges();
|
||||||
document.getSelection().addRange(selected);
|
document.getSelection().addRange(selected);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = copyToClipboard;
|
module.exports = copyToClipboard;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const countBy = (arr, fn) =>
|
const countBy = (arr, fn) =>
|
||||||
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
|
||||||
acc[val] = (acc[val] || 0) + 1;
|
acc[val] = (acc[val] || 0) + 1;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
module.exports = countBy;
|
module.exports = countBy;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
|
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
|
||||||
|
|
||||||
module.exports = countOccurrences;
|
module.exports = countOccurrences;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
|
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
|
||||||
|
|
||||||
module.exports = countVowels;
|
module.exports = countVowels;
|
||||||
@ -1,12 +1,13 @@
|
|||||||
const counter = (selector, start, end, step = 1, duration = 2000) => {
|
const counter = (selector, start, end, step = 1, duration = 2000) => {
|
||||||
let current = start,
|
let current = start,
|
||||||
_step = (end - start) * step < 0 ? -step : step,
|
_step = (end - start) * step < 0 ? -step : step,
|
||||||
timer = setInterval(() => {
|
timer = setInterval(() => {
|
||||||
current += _step;
|
current += _step;
|
||||||
document.querySelector(selector).innerHTML = current;
|
document.querySelector(selector).innerHTML = current;
|
||||||
if (current >= end) document.querySelector(selector).innerHTML = end;
|
if (current >= end) document.querySelector(selector).innerHTML = end;
|
||||||
if (current >= end) clearInterval(timer);
|
if (current >= end) clearInterval(timer);
|
||||||
}, Math.abs(Math.floor(duration / (end - start))));
|
}, Math.abs(Math.floor(duration / (end - start))));
|
||||||
return timer;
|
return timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = counter;
|
module.exports = counter;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const createElement = str => {
|
const createElement = str => {
|
||||||
const el = document.createElement('div');
|
const el = document.createElement('div');
|
||||||
el.innerHTML = str;
|
el.innerHTML = str;
|
||||||
return el.firstElementChild;
|
return el.firstElementChild;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = createElement;
|
module.exports = createElement;
|
||||||
@ -1,15 +1,16 @@
|
|||||||
const createEventHub = () => ({
|
const createEventHub = () => ({
|
||||||
hub: Object.create(null),
|
hub: Object.create(null),
|
||||||
emit(event, data) {
|
emit(event, data) {
|
||||||
(this.hub[event] || []).forEach(handler => handler(data));
|
(this.hub[event] || []).forEach(handler => handler(data));
|
||||||
},
|
},
|
||||||
on(event, handler) {
|
on(event, handler) {
|
||||||
if (!this.hub[event]) this.hub[event] = [];
|
if (!this.hub[event]) this.hub[event] = [];
|
||||||
this.hub[event].push(handler);
|
this.hub[event].push(handler);
|
||||||
},
|
},
|
||||||
off(event, handler) {
|
off(event, handler) {
|
||||||
const i = (this.hub[event] || []).findIndex(h => h === handler);
|
const i = (this.hub[event] || []).findIndex(h => h === handler);
|
||||||
if (i > -1) this.hub[event].splice(i, 1);
|
if (i > -1) this.hub[event].splice(i, 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = createEventHub;
|
module.exports = createEventHub;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const currentURL = () => window.location.href;
|
const currentURL = () => window.location.href;
|
||||||
|
|
||||||
module.exports = currentURL;
|
module.exports = currentURL;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const curry = (fn, arity = fn.length, ...args) =>
|
const curry = (fn, arity = fn.length, ...args) =>
|
||||||
arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
|
arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
|
||||||
|
|
||||||
module.exports = curry;
|
module.exports = curry;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const debounce = (fn, ms = 0) => {
|
const debounce = (fn, ms = 0) => {
|
||||||
let timeoutId;
|
let timeoutId;
|
||||||
return function(...args) {
|
return function(...args) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
timeoutId = setTimeout(() => fn.apply(this, args), ms);
|
timeoutId = setTimeout(() => fn.apply(this, args), ms);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = debounce;
|
module.exports = debounce;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const decapitalize = ([first, ...rest], upperRest = false) =>
|
const decapitalize = ([first, ...rest], upperRest = false) =>
|
||||||
first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join(''));
|
first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join(''));
|
||||||
|
|
||||||
module.exports = decapitalize;
|
module.exports = decapitalize;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const deepClone = obj => {
|
const deepClone = obj => {
|
||||||
let clone = Object.assign({}, obj);
|
let clone = Object.assign({}, obj);
|
||||||
Object.keys(clone).forEach(
|
Object.keys(clone).forEach(
|
||||||
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
||||||
);
|
);
|
||||||
return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : clone;
|
return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : clone;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = deepClone;
|
module.exports = deepClone;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
|
const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
|
||||||
|
|
||||||
module.exports = deepFlatten;
|
module.exports = deepFlatten;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
|
const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
|
||||||
|
|
||||||
module.exports = defaults;
|
module.exports = defaults;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
|
const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
|
||||||
|
|
||||||
module.exports = defer;
|
module.exports = defer;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const degreesToRads = deg => (deg * Math.PI) / 180.0;
|
const degreesToRads = deg => (deg * Math.PI) / 180.0;
|
||||||
|
|
||||||
module.exports = degreesToRads;
|
module.exports = degreesToRads;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
|
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
|
||||||
|
|
||||||
module.exports = delay;
|
module.exports = delay;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const detectDeviceType = () =>
|
const detectDeviceType = () =>
|
||||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
||||||
? 'Mobile'
|
? 'Mobile'
|
||||||
: 'Desktop';
|
: 'Desktop';
|
||||||
|
|
||||||
module.exports = detectDeviceType;
|
module.exports = detectDeviceType;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const difference = (a, b) => {
|
const difference = (a, b) => {
|
||||||
const s = new Set(b);
|
const s = new Set(b);
|
||||||
return a.filter(x => !s.has(x));
|
return a.filter(x => !s.has(x));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = difference;
|
module.exports = difference;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const differenceBy = (a, b, fn) => {
|
const differenceBy = (a, b, fn) => {
|
||||||
const s = new Set(b.map(v => fn(v)));
|
const s = new Set(b.map(v => fn(v)));
|
||||||
return a.filter(x => !s.has(fn(x)));
|
return a.filter(x => !s.has(fn(x)));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = differenceBy;
|
module.exports = differenceBy;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
|
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
|
||||||
|
|
||||||
module.exports = differenceWith;
|
module.exports = differenceWith;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const dig = (obj, target) =>
|
const dig = (obj, target) =>
|
||||||
target in obj
|
target in obj
|
||||||
? obj[target]
|
? obj[target]
|
||||||
: Object.values(obj).reduce((acc, val) => {
|
: Object.values(obj).reduce((acc, val) => {
|
||||||
if (acc !== undefined) return acc;
|
if (acc !== undefined) return acc;
|
||||||
if (typeof val === 'object') return dig(val, target);
|
if (typeof val === 'object') return dig(val, target);
|
||||||
}, undefined);
|
}, undefined);
|
||||||
|
|
||||||
module.exports = dig;
|
module.exports = dig;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const digitize = n => [...`${n}`].map(i => parseInt(i));
|
const digitize = n => [...`${n}`].map(i => parseInt(i));
|
||||||
|
|
||||||
module.exports = digitize;
|
module.exports = digitize;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
|
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
|
||||||
|
|
||||||
module.exports = distance;
|
module.exports = distance;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const drop = (arr, n = 1) => arr.slice(n);
|
const drop = (arr, n = 1) => arr.slice(n);
|
||||||
|
|
||||||
module.exports = drop;
|
module.exports = drop;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const dropRight = (arr, n = 1) => arr.slice(0, -n);
|
const dropRight = (arr, n = 1) => arr.slice(0, -n);
|
||||||
|
|
||||||
module.exports = dropRight;
|
module.exports = dropRight;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const dropRightWhile = (arr, func) => {
|
const dropRightWhile = (arr, func) => {
|
||||||
while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
|
while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = dropRightWhile;
|
module.exports = dropRightWhile;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const dropWhile = (arr, func) => {
|
const dropWhile = (arr, func) => {
|
||||||
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = dropWhile;
|
module.exports = dropWhile;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const elementContains = (parent, child) => parent !== child && parent.contains(child);
|
const elementContains = (parent, child) => parent !== child && parent.contains(child);
|
||||||
|
|
||||||
module.exports = elementContains;
|
module.exports = elementContains;
|
||||||
@ -1,9 +1,10 @@
|
|||||||
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
|
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
|
||||||
const { top, left, bottom, right } = el.getBoundingClientRect();
|
const { top, left, bottom, right } = el.getBoundingClientRect();
|
||||||
const { innerHeight, innerWidth } = window;
|
const { innerHeight, innerWidth } = window;
|
||||||
return partiallyVisible
|
return partiallyVisible
|
||||||
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
|
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
|
||||||
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
||||||
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
|
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = elementIsVisibleInViewport;
|
module.exports = elementIsVisibleInViewport;
|
||||||
@ -1,18 +1,19 @@
|
|||||||
const elo = ([...ratings], kFactor = 32, selfRating) => {
|
const elo = ([...ratings], kFactor = 32, selfRating) => {
|
||||||
const [a, b] = ratings;
|
const [a, b] = ratings;
|
||||||
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
|
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
|
||||||
const newRating = (rating, i) =>
|
const newRating = (rating, i) =>
|
||||||
(selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
|
(selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
|
||||||
if (ratings.length === 2) {
|
if (ratings.length === 2) {
|
||||||
return [newRating(a, 1), newRating(b, 0)];
|
return [newRating(a, 1), newRating(b, 0)];
|
||||||
}
|
}
|
||||||
for (let i = 0, len = ratings.length; i < len; i++) {
|
for (let i = 0, len = ratings.length; i < len; i++) {
|
||||||
let j = i;
|
let j = i;
|
||||||
while (j < len - 1) {
|
while (j < len - 1) {
|
||||||
j++;
|
j++;
|
||||||
[ratings[i], ratings[j]] = elo([ratings[i], ratings[j]], kFactor);
|
[ratings[i], ratings[j]] = elo([ratings[i], ratings[j]], kFactor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ratings;
|
return ratings;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = elo;
|
module.exports = elo;
|
||||||
@ -1,11 +1,12 @@
|
|||||||
const equals = (a, b) => {
|
const equals = (a, b) => {
|
||||||
if (a === b) return true;
|
if (a === b) return true;
|
||||||
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
||||||
if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
|
if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
|
||||||
if (a === null || a === undefined || b === null || b === undefined) return false;
|
if (a === null || a === undefined || b === null || b === undefined) return false;
|
||||||
if (a.prototype !== b.prototype) return false;
|
if (a.prototype !== b.prototype) return false;
|
||||||
let keys = Object.keys(a);
|
let keys = Object.keys(a);
|
||||||
if (keys.length !== Object.keys(b).length) return false;
|
if (keys.length !== Object.keys(b).length) return false;
|
||||||
return keys.every(k => equals(a[k], b[k]));
|
return keys.every(k => equals(a[k], b[k]));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = equals;
|
module.exports = equals;
|
||||||
@ -1,13 +1,14 @@
|
|||||||
const escapeHTML = str =>
|
const escapeHTML = str =>
|
||||||
str.replace(
|
str.replace(
|
||||||
/[&<>'"]/g,
|
/[&<>'"]/g,
|
||||||
tag =>
|
tag =>
|
||||||
({
|
({
|
||||||
'&': '&',
|
'&': '&',
|
||||||
'<': '<',
|
'<': '<',
|
||||||
'>': '>',
|
'>': '>',
|
||||||
"'": ''',
|
"'": ''',
|
||||||
'"': '"'
|
'"': '"'
|
||||||
}[tag] || tag)
|
}[tag] || tag)
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = escapeHTML;
|
module.exports = escapeHTML;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
|
|
||||||
module.exports = escapeRegExp;
|
module.exports = escapeRegExp;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||||
|
|
||||||
module.exports = everyNth;
|
module.exports = everyNth;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const extendHex = shortHex =>
|
const extendHex = shortHex =>
|
||||||
'#' +
|
'#' +
|
||||||
shortHex
|
shortHex
|
||||||
.slice(shortHex.startsWith('#') ? 1 : 0)
|
.slice(shortHex.startsWith('#') ? 1 : 0)
|
||||||
.split('')
|
.split('')
|
||||||
.map(x => x + x)
|
.map(x => x + x)
|
||||||
.join('');
|
.join('');
|
||||||
|
|
||||||
module.exports = extendHex;
|
module.exports = extendHex;
|
||||||
@ -1,9 +1,10 @@
|
|||||||
const factorial = n =>
|
const factorial = n =>
|
||||||
n < 0
|
n < 0
|
||||||
? (() => {
|
? (() => {
|
||||||
throw new TypeError('Negative numbers are not allowed!');
|
throw new TypeError('Negative numbers are not allowed!');
|
||||||
})()
|
})()
|
||||||
: n <= 1
|
: n <= 1
|
||||||
? 1
|
? 1
|
||||||
: n * factorial(n - 1);
|
: n * factorial(n - 1);
|
||||||
|
|
||||||
module.exports = factorial;
|
module.exports = factorial;
|
||||||
@ -1,20 +1,21 @@
|
|||||||
const factors = (num, primes = false) => {
|
const factors = (num, primes = false) => {
|
||||||
const isPrime = num => {
|
const isPrime = num => {
|
||||||
const boundary = Math.floor(Math.sqrt(num));
|
const boundary = Math.floor(Math.sqrt(num));
|
||||||
for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
|
for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
|
||||||
return num >= 2;
|
return num >= 2;
|
||||||
};
|
};
|
||||||
const isNeg = num < 0;
|
const isNeg = num < 0;
|
||||||
num = isNeg ? -num : num;
|
num = isNeg ? -num : num;
|
||||||
let array = Array.from({ length: num - 1 })
|
let array = Array.from({ length: num - 1 })
|
||||||
.map((val, i) => (num % (i + 2) === 0 ? i + 2 : false))
|
.map((val, i) => (num % (i + 2) === 0 ? i + 2 : false))
|
||||||
.filter(val => val);
|
.filter(val => val);
|
||||||
if (isNeg)
|
if (isNeg)
|
||||||
array = array.reduce((acc, val) => {
|
array = array.reduce((acc, val) => {
|
||||||
acc.push(val);
|
acc.push(val);
|
||||||
acc.push(-val);
|
acc.push(-val);
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
return primes ? array.filter(isPrime) : array;
|
return primes ? array.filter(isPrime) : array;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = factors;
|
module.exports = factors;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const fibonacci = n =>
|
const fibonacci = n =>
|
||||||
Array.from({ length: n }).reduce(
|
Array.from({ length: n }).reduce(
|
||||||
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
module.exports = fibonacci;
|
module.exports = fibonacci;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const fibonacciCountUntilNum = num =>
|
const fibonacciCountUntilNum = num =>
|
||||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
|
|
||||||
module.exports = fibonacciCountUntilNum;
|
module.exports = fibonacciCountUntilNum;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const fibonacciUntilNum = num => {
|
const fibonacciUntilNum = num => {
|
||||||
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
return Array.from({ length: n }).reduce(
|
return Array.from({ length: n }).reduce(
|
||||||
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fibonacciUntilNum;
|
module.exports = fibonacciUntilNum;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
||||||
|
|
||||||
module.exports = filterNonUnique;
|
module.exports = filterNonUnique;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const filterNonUniqueBy = (arr, fn) =>
|
const filterNonUniqueBy = (arr, fn) =>
|
||||||
arr.filter((v, i) => arr.every((x, j) => (i === j) === fn(v, x, i, j)));
|
arr.filter((v, i) => arr.every((x, j) => (i === j) === fn(v, x, i, j)));
|
||||||
|
|
||||||
module.exports = filterNonUniqueBy;
|
module.exports = filterNonUniqueBy;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
|
const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
|
||||||
|
|
||||||
module.exports = findKey;
|
module.exports = findKey;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const findLast = (arr, fn) => arr.filter(fn).pop();
|
const findLast = (arr, fn) => arr.filter(fn).pop();
|
||||||
|
|
||||||
module.exports = findLast;
|
module.exports = findLast;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const findLastIndex = (arr, fn) =>
|
const findLastIndex = (arr, fn) =>
|
||||||
arr
|
arr
|
||||||
.map((val, i) => [i, val])
|
.map((val, i) => [i, val])
|
||||||
.filter(([i, val]) => fn(val, i, arr))
|
.filter(([i, val]) => fn(val, i, arr))
|
||||||
.pop()[0];
|
.pop()[0];
|
||||||
|
|
||||||
module.exports = findLastIndex;
|
module.exports = findLastIndex;
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const findLastKey = (obj, fn) =>
|
const findLastKey = (obj, fn) =>
|
||||||
Object.keys(obj)
|
Object.keys(obj)
|
||||||
.reverse()
|
.reverse()
|
||||||
.find(key => fn(obj[key], key, obj));
|
.find(key => fn(obj[key], key, obj));
|
||||||
|
|
||||||
module.exports = findLastKey;
|
module.exports = findLastKey;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
const flatten = (arr, depth = 1) =>
|
const flatten = (arr, depth = 1) =>
|
||||||
arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []);
|
arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []);
|
||||||
|
|
||||||
module.exports = flatten;
|
module.exports = flatten;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
const flattenObject = (obj, prefix = '') =>
|
const flattenObject = (obj, prefix = '') =>
|
||||||
Object.keys(obj).reduce((acc, k) => {
|
Object.keys(obj).reduce((acc, k) => {
|
||||||
const pre = prefix.length ? prefix + '.' : '';
|
const pre = prefix.length ? prefix + '.' : '';
|
||||||
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
|
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
|
||||||
else acc[pre + k] = obj[k];
|
else acc[pre + k] = obj[k];
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
module.exports = flattenObject;
|
module.exports = flattenObject;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const flip = fn => (first, ...rest) => fn(...rest, first);
|
const flip = fn => (first, ...rest) => fn(...rest, first);
|
||||||
|
|
||||||
module.exports = flip;
|
module.exports = flip;
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const forEachRight = (arr, callback) =>
|
const forEachRight = (arr, callback) =>
|
||||||
arr
|
arr
|
||||||
.slice(0)
|
.slice(0)
|
||||||
.reverse()
|
.reverse()
|
||||||
.forEach(callback);
|
.forEach(callback);
|
||||||
|
|
||||||
module.exports = forEachRight;
|
module.exports = forEachRight;
|
||||||
@ -1,2 +1,3 @@
|
|||||||
const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj));
|
const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj));
|
||||||
|
|
||||||
module.exports = forOwn;
|
module.exports = forOwn;
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user