UNPKG

@ch1/utility

Version:

Utility functions I end up adding to all my projects

228 lines 5.68 kB
export function arrToObj(arr, prop, aggregate) { return arr.reduce((state, el, i) => { let index; if (!prop) { index = i; } else { index = el[prop]; } if (aggregate) { if (!state[index]) { state[index] = []; } state[index].push(el); } else { state[index] = el; } return state; }, {}); } /** assumes number is between 0 - 1 inclusive of 0 but not 1 */ export function createBetween(random) { return (minimum, maximum) => { const min = Math.ceil(minimum); const max = Math.floor(maximum); return Math.floor(random() * (max - min)) + min; }; } export function deepFreeze(obj) { if (Array.isArray(obj)) { return Object.freeze(obj.map(deepFreeze)); } if (isObject(obj)) { for (let i in obj) { if (isObject(obj[i])) { if (!Object.isFrozen(obj[i])) { obj[i] = deepFreeze(obj[i]); } } } } return Object.freeze(obj); } export function identity(thing) { return thing; } export function findCaseInsensitivePropInObj(obj, prop) { const lProp = prop.toLowerCase(); return objReduce(obj, (obj, el, objProp) => { if (obj) { return obj; } if (lProp === objProp.toLowerCase()) { return el; } return false; }, false); } export function hasProp(prop, haystack) { return haystack[prop] ? true : false; } export function isBoolean(arg) { if (typeof arg === 'boolean') { return true; } return false; } export function isFunction(thing) { return typeof thing === 'function'; } export function isNaN(thing) { return thing !== thing; } export function isNull(thing) { return thing === null; } export function isNumber(thing) { return typeof thing === 'number'; } export function isObject(thing) { if (!thing) { return false; } return typeof thing === 'object'; } export function isString(thing) { return typeof thing === 'string'; } export function isUndefined(thing) { return thing === undefined; } export function noop() { } export function objEach(d, callback) { Object.keys(d).forEach((key, i) => { callback(d[key], key, i, d); }); } export function objFilter(d, callback) { return objReduce(d, (state, value, key, index) => { if (callback(value, key, index)) { state[key] = value; } return state; }, {}); } export function objReduce(d, callback, init) { return Object.keys(d).reduce((state, key, i) => { return callback(state, d[key], key, i, d); }, init); } export function partial(f, ...boundArg) { return (...args) => f(...boundArg, ...args); } export function pluck(prop, haystack) { return haystack[prop]; } export function toGtZeroIntMax(max, val) { const num = toInt(val); if (num > max) { return max; } if (num < 0) { return 0; } return num; } export function toInt(val) { return parseInt(val, 10); } export function toIntArray(inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(toInt); } return []; } export function toIntArrayMax(max, inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(thing => toIntMax(max, thing)).filter(Boolean); } return []; } export function toIntArrayMin(max, inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(thing => toIntMin(max, thing)).filter(Boolean); } return []; } export function toIntBetween(min, max, val) { const asInt = toInt(val); if (asInt < min) { return min; } if (asInt > max) { return max; } return asInt; } export function toIntBetweenOptional(min, max, val) { if (min === undefined && max === undefined) { return toInt(val); } if (min === undefined && max !== undefined) { return toIntMax(max, val); } if (max === undefined && min !== undefined) { return toIntMin(min, val); } return toIntBetween(min, max, val); } export function toIntMax(max, val) { const num = toInt(val); if (num > max) { return max; } return num; } export function toIntMin(min, val) { const num = toInt(val); if (num < min) { return min; } return num; } export function toString(val) { return val + ''; } export function toStringArray(input) { if (Array.isArray(input)) { return input.map(toString); } return []; } export function toStringArrayMax(max, input) { if (Array.isArray(input)) { return input.map(thing => toStringMax(max, thing)); } return []; } export function toStringMax(max, val) { const v = toString(val); return v.length > max ? v.slice(0, max) : v; } export function unzip(dictionary) { return Object.keys(dictionary).reduce((s, val) => { s.keys.push(val); s.values.push(dictionary[val]); return s; }, { keys: [], values: [] }); } /** * If keys/values have different lengths the expect behavior is to "underflow" * values. Non values will be initialized to undefined. Non keys will be * ignored. * * If there are duplicate keys the last assignment "wins", this would be the * key with the highest index in the given keys array */ export function zip(keys, values) { return keys.reduce((o, key, i) => { o[key] = values[i]; return o; }, {}); } //# sourceMappingURL=utility.js.map