UNPKG

functional-augments-object

Version:

Augment the Object prototype with methods for functional programming, including filter(), map() and reduce().

58 lines (44 loc) 1.3 kB
'use strict'; const augment = (o, p, m) => Object.defineProperty(o.prototype, p, { configuable: true, enumerable: false, value: m, writable: true }); augment(Object, 'reduce', function reduce(f, init) { if (f === undefined) { throw new ReferenceError('Missing argument, f'); } else if (typeof f !== 'function') { throw new TypeError('Argument is not a function'); } const reducer = (acc, cur) => f(acc, this[cur], cur); let keys = Object.keys(this); if(init === undefined) { init = this[keys.shift()]; } return keys.reduce(reducer, init); }); augment(Object, 'map', function map(f) { if (f === undefined) { throw new ReferenceError('Missing argument, f'); } else if (typeof f !== 'function') { throw new TypeError('Argument is not a function'); } const reducer = (acc, cur, k) => { const v = f(cur, k); if (v !== undefined) { acc[k] = v; } return acc; }; return this.reduce(reducer, {}); }); augment(Object, 'filter', function filter(f) { if (f === undefined) { throw new ReferenceError('Missing argument, f'); } else if (typeof f !== 'function') { throw new TypeError('Argument is not a function'); } return this.map((v, k) => f(v, k) ? v : undefined); });