UNPKG

@ch1/utility

Version:

Utility functions I end up adding to all my projects

268 lines (264 loc) 6.62 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); 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 */ function createBetween(random) { return (minimum, maximum) => { const min = Math.ceil(minimum); const max = Math.floor(maximum); return Math.floor(random() * (max - min)) + min; }; } 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); } function identity(thing) { return thing; } 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); } function hasProp(prop, haystack) { return haystack[prop] ? true : false; } function isBoolean(arg) { if (typeof arg === 'boolean') { return true; } return false; } function isFunction(thing) { return typeof thing === 'function'; } function isNaN(thing) { return thing !== thing; } function isNull(thing) { return thing === null; } function isNumber(thing) { return typeof thing === 'number'; } function isObject(thing) { if (!thing) { return false; } return typeof thing === 'object'; } function isString(thing) { return typeof thing === 'string'; } function isUndefined(thing) { return thing === undefined; } function noop() { } function objEach(d, callback) { Object.keys(d).forEach((key, i) => { callback(d[key], key, i, d); }); } function objFilter(d, callback) { return objReduce(d, (state, value, key, index) => { if (callback(value, key, index)) { state[key] = value; } return state; }, {}); } function objReduce(d, callback, init) { return Object.keys(d).reduce((state, key, i) => { return callback(state, d[key], key, i, d); }, init); } function partial(f, ...boundArg) { return (...args) => f(...boundArg, ...args); } function pluck(prop, haystack) { return haystack[prop]; } function toGtZeroIntMax(max, val) { const num = toInt(val); if (num > max) { return max; } if (num < 0) { return 0; } return num; } function toInt(val) { return parseInt(val, 10); } function toIntArray(inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(toInt); } return []; } function toIntArrayMax(max, inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(thing => toIntMax(max, thing)).filter(Boolean); } return []; } function toIntArrayMin(max, inputArr) { if (Array.isArray(inputArr)) { return inputArr.map(thing => toIntMin(max, thing)).filter(Boolean); } return []; } function toIntBetween(min, max, val) { const asInt = toInt(val); if (asInt < min) { return min; } if (asInt > max) { return max; } return asInt; } 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); } function toIntMax(max, val) { const num = toInt(val); if (num > max) { return max; } return num; } function toIntMin(min, val) { const num = toInt(val); if (num < min) { return min; } return num; } function toString(val) { return val + ''; } function toStringArray(input) { if (Array.isArray(input)) { return input.map(toString); } return []; } function toStringArrayMax(max, input) { if (Array.isArray(input)) { return input.map(thing => toStringMax(max, thing)); } return []; } function toStringMax(max, val) { const v = toString(val); return v.length > max ? v.slice(0, max) : v; } 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 */ function zip(keys, values) { return keys.reduce((o, key, i) => { o[key] = values[i]; return o; }, {}); } exports.arrToObj = arrToObj; exports.createBetween = createBetween; exports.deepFreeze = deepFreeze; exports.identity = identity; exports.findCaseInsensitivePropInObj = findCaseInsensitivePropInObj; exports.hasProp = hasProp; exports.isBoolean = isBoolean; exports.isFunction = isFunction; exports.isNaN = isNaN; exports.isNull = isNull; exports.isNumber = isNumber; exports.isObject = isObject; exports.isString = isString; exports.isUndefined = isUndefined; exports.noop = noop; exports.objEach = objEach; exports.objFilter = objFilter; exports.objReduce = objReduce; exports.partial = partial; exports.pluck = pluck; exports.toGtZeroIntMax = toGtZeroIntMax; exports.toInt = toInt; exports.toIntArray = toIntArray; exports.toIntArrayMax = toIntArrayMax; exports.toIntArrayMin = toIntArrayMin; exports.toIntBetween = toIntBetween; exports.toIntBetweenOptional = toIntBetweenOptional; exports.toIntMax = toIntMax; exports.toIntMin = toIntMin; exports.toString = toString; exports.toStringArray = toStringArray; exports.toStringArrayMax = toStringArrayMax; exports.toStringMax = toStringMax; exports.unzip = unzip; exports.zip = zip;