Last active
October 22, 2015 01:32
-
-
Save aaronjensen/77074fdd5ac5b2ca3d96 to your computer and use it in GitHub Desktop.
updeep using rollup, commonjs build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var forEach = require('lodash/collection/forEach'); | |
| forEach = 'default' in forEach ? forEach['default'] : forEach; | |
| var mapArray = require('lodash/collection/map'); | |
| mapArray = 'default' in mapArray ? mapArray['default'] : mapArray; | |
| var mapObject = require('lodash/object/mapValues'); | |
| mapObject = 'default' in mapObject ? mapObject['default'] : mapObject; | |
| var _omit = require('lodash/object/omit'); | |
| _omit = 'default' in _omit ? _omit['default'] : _omit; | |
| var _reject = require('lodash/collection/reject'); | |
| _reject = 'default' in _reject ? _reject['default'] : _reject; | |
| var isPlainObject = require('lodash/lang/isPlainObject'); | |
| isPlainObject = 'default' in isPlainObject ? isPlainObject['default'] : isPlainObject; | |
| var babelHelpers__extends = Object.assign || function (target) { | |
| for (var i = 1; i < arguments.length; i++) { | |
| var source = arguments[i]; | |
| for (var key in source) { | |
| if (Object.prototype.hasOwnProperty.call(source, key)) { | |
| target[key] = source[key]; | |
| } | |
| } | |
| } | |
| return target; | |
| }; | |
| var _ = '@@updeep/placeholder'; | |
| function countArguments(args, max) { | |
| var n = args.length; | |
| if (n > max) n = max; | |
| while (args[n - 1] === _) { | |
| n--; | |
| } | |
| return n; | |
| } | |
| function curry1(fn) { | |
| return function curried(a) { | |
| for (var _len = arguments.length, _ref = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | |
| _ref[_key - 1] = arguments[_key]; | |
| } | |
| var b = _ref[0], | |
| c = _ref[1]; | |
| var n = countArguments(arguments); | |
| if (n >= 1) return fn(a, b, c); | |
| return curried; | |
| }; | |
| } | |
| function curry2(fn) { | |
| return function curried(a, b) { | |
| for (var _len2 = arguments.length, _ref2 = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | |
| _ref2[_key2 - 2] = arguments[_key2]; | |
| } | |
| var c = _ref2[0], | |
| d = _ref2[1]; | |
| var n = countArguments(arguments, 2); | |
| if (b === _ || c === _ || d === _) { | |
| throw new Error('Can only use placeholder on first argument of this function.'); | |
| } | |
| if (n >= 2) { | |
| if (a === _) return curry1(function (a, c, d) { | |
| return fn(a, b, c, d); | |
| }); | |
| return fn(a, b, c, d); | |
| } | |
| if (n === 1) return curry1(function (b, c, d) { | |
| return fn(a, b, c, d); | |
| }); | |
| return curried; | |
| }; | |
| } | |
| function curry3(fn) { | |
| return function curried(a, b, c) { | |
| for (var _len3 = arguments.length, _ref3 = Array(_len3 > 3 ? _len3 - 3 : 0), _key3 = 3; _key3 < _len3; _key3++) { | |
| _ref3[_key3 - 3] = arguments[_key3]; | |
| } | |
| var d = _ref3[0], | |
| e = _ref3[1]; | |
| var n = countArguments(arguments, 3); | |
| if (c === _ || d === _ || e === _) { | |
| throw new Error('Can only use placeholder on first or second argument of this function.'); | |
| } | |
| if (n >= 3) { | |
| if (a === _) { | |
| if (b === _) return curry2(function (a, b, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| return curry1(function (a, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| } | |
| if (b === _) return curry1(function (b, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| return fn(a, b, c, d, e); | |
| } | |
| if (n === 2) { | |
| if (a === _) return curry2(function (a, c, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| return curry1(function (c, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| } | |
| if (n === 1) return curry2(function (b, c, d, e) { | |
| return fn(a, b, c, d, e); | |
| }); | |
| return curried; | |
| }; | |
| } | |
| function curry4(fn) { | |
| return function curried(a, b, c, d) { | |
| for (var _len4 = arguments.length, _ref4 = Array(_len4 > 4 ? _len4 - 4 : 0), _key4 = 4; _key4 < _len4; _key4++) { | |
| _ref4[_key4 - 4] = arguments[_key4]; | |
| } | |
| var e = _ref4[0], | |
| f = _ref4[1]; | |
| var n = countArguments(arguments, 4); | |
| if (d === _ || e === _ || f === _) { | |
| throw new Error('Can only use placeholder on first, second or third argument of this function.'); | |
| } | |
| if (n >= 4) { | |
| if (a === _) { | |
| if (b === _) { | |
| if (c === _) return curry3(function (a, b, c, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry2(function (a, b, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (c === _) return curry2(function (a, c, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry1(function (a, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (b === _) { | |
| if (c === _) return curry2(function (b, c, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry1(function (b, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (c === _) return curry1(function (c, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return fn(a, b, c, d, e, f); | |
| } | |
| if (n === 3) { | |
| if (a === _) { | |
| if (b === _) return curry3(function (a, b, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry2(function (a, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (b === _) return curry2(function (b, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry1(function (d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (n === 2) { | |
| if (a === _) return curry3(function (a, c, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curry2(function (c, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| } | |
| if (n === 1) return curry3(function (b, c, d, e, f) { | |
| return fn(a, b, c, d, e, f); | |
| }); | |
| return curried; | |
| }; | |
| } | |
| function curry(fn) { | |
| var length = arguments.length <= 1 || arguments[1] === undefined ? fn.length : arguments[1]; | |
| return (function () { | |
| return [fn, curry1, curry2, curry3, curry4][length](fn); | |
| })(); | |
| } | |
| function isFreezable(object) { | |
| if (object === null) return false; | |
| return Array.isArray(object) || typeof object === 'object'; | |
| } | |
| function needsFreezing(object) { | |
| return isFreezable(object) && !Object.isFrozen(object); | |
| } | |
| function recur(object) { | |
| Object.freeze(object); | |
| Object.keys(object).forEach(function (key) { | |
| var value = object[key]; | |
| if (needsFreezing(value)) { | |
| recur(value); | |
| } | |
| }); | |
| return object; | |
| } | |
| /** | |
| * Deeply freeze a plain javascript object. | |
| * | |
| * If `process.env.NODE_ENV === 'production'`, this returns the original object | |
| * witout freezing. | |
| * | |
| * @function | |
| * @sig a -> a | |
| * @param {object} object Object to freeze. | |
| * @return {object} Frozen object, unless in production, then the same object. | |
| */ | |
| function freeze(object) { | |
| if (process.env.NODE_ENV === 'production') { | |
| return object; | |
| } | |
| if (needsFreezing(object)) { | |
| recur(object); | |
| } | |
| return object; | |
| } | |
| function wrap(func) { | |
| var length = arguments.length <= 1 || arguments[1] === undefined ? func.length : arguments[1]; | |
| return (function () { | |
| return curry(function () { | |
| return freeze(func.apply(undefined, arguments)); | |
| }, length); | |
| })(); | |
| } | |
| function isEmpty(object) { | |
| return !Object.keys(object).length; | |
| } | |
| function isInt(value) { | |
| if (isNaN(value)) { | |
| return false; | |
| } | |
| var x = parseFloat(value); | |
| return (x | 0) === x; | |
| } | |
| function isArrayUpdate(updates) { | |
| for (var _iterator = Object.keys(updates), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | |
| var _ref; | |
| if (_isArray) { | |
| if (_i >= _iterator.length) break; | |
| _ref = _iterator[_i++]; | |
| } else { | |
| _i = _iterator.next(); | |
| if (_i.done) break; | |
| _ref = _i.value; | |
| } | |
| var updateKey = _ref; | |
| if (!isInt(updateKey)) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| function arrayOrObject(updates) { | |
| if (!isEmpty(updates) && isArrayUpdate(updates)) { | |
| return []; | |
| } | |
| return {}; | |
| } | |
| function defaultObject(object, updates) { | |
| if (typeof object === 'undefined' || object === null) { | |
| return arrayOrObject(updates); | |
| } | |
| return object; | |
| } | |
| function reduce(object, callback, initialValue) { | |
| return Object.keys(object).reduce(function (acc, key) { | |
| return callback(acc, object[key], key); | |
| }, initialValue); | |
| } | |
| function resolveUpdates(updates, object) { | |
| return reduce(updates, function (acc, value, key) { | |
| var updatedValue = value; | |
| if (!Array.isArray(value) && value !== null && typeof value === 'object') { | |
| updatedValue = update(value, object[key]); | |
| } else if (typeof value === 'function') { | |
| updatedValue = value(object[key]); | |
| } | |
| if (object[key] !== updatedValue) { | |
| acc[key] = updatedValue; | |
| } | |
| return acc; | |
| }, {}); | |
| } | |
| function updateArray(updates, object) { | |
| var newArray = [].concat(object); | |
| Object.keys(updates).forEach(function (key) { | |
| newArray[key] = updates[key]; | |
| }); | |
| return newArray; | |
| } | |
| /** | |
| * Recursively update an object or array. | |
| * | |
| * Can update with values: | |
| * update({ foo: 3 }, { foo: 1, bar: 2 }); | |
| * // => { foo: 3, bar: 2 } | |
| * | |
| * Or with a function: | |
| * update({ foo: x => (x + 1) }, { foo: 2 }); | |
| * // => { foo: 3 } | |
| * | |
| * @function | |
| * @name update | |
| * @param {Object|Function} updates | |
| * @param {Object|Array} object to update | |
| * @return {Object|Array} new object with modifications | |
| */ | |
| function update(updates, object) { | |
| if (typeof updates === 'function') { | |
| for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | |
| args[_key - 2] = arguments[_key]; | |
| } | |
| return updates.apply(undefined, [object].concat(args)); | |
| } | |
| if (!isPlainObject(updates)) { | |
| return updates; | |
| } | |
| var defaultedObject = defaultObject(object, updates); | |
| var resolvedUpdates = resolveUpdates(updates, defaultedObject); | |
| if (isEmpty(resolvedUpdates)) { | |
| return defaultedObject; | |
| } | |
| if (Array.isArray(defaultedObject)) { | |
| return updateArray(resolvedUpdates, defaultedObject); | |
| } | |
| return babelHelpers__extends({}, defaultedObject, resolvedUpdates); | |
| } | |
| var update$1 = wrap(update, 2); | |
| /** | |
| * Returns a function that always returns the supplied value. | |
| * | |
| * Useful for replacing an object outright rather than merging it. | |
| * | |
| * @function | |
| * @sig a -> (* -> a) | |
| * @memberOf u | |
| * @param {*} value what to return from returned function. | |
| * @return {function} a new function that will always return value. | |
| * | |
| * @example | |
| * var alwaysFour = u.constant(4); | |
| * expect(alwaysFour(32)).toEqual(4); | |
| * | |
| * @example | |
| * var user = { | |
| * name: 'Mitch', | |
| * favorites: { | |
| * band: 'Nirvana', | |
| * movie: 'The Matrix' | |
| * } | |
| * }; | |
| * | |
| * var newFavorites = { | |
| * band: 'Coldplay' | |
| * }; | |
| * | |
| * var result = u({ favorites: u.constant(newFavorites) }, user); | |
| * | |
| * expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } }); | |
| */ | |
| function constant(value) { | |
| var frozen = freeze(value); | |
| return function () { | |
| return frozen; | |
| }; | |
| } | |
| function updateIfElse(predicate, trueUpdates, falseUpdates, object) { | |
| var test = typeof predicate === 'function' ? predicate(object) : predicate; | |
| var updates = test ? trueUpdates : falseUpdates; | |
| return update$1(updates, object); | |
| } | |
| var ifElse = wrap(updateIfElse); | |
| var _if = curry(function (predicate, trueUpdates, object) { | |
| return ifElse(predicate, trueUpdates, function (x) { | |
| return x; | |
| }, object); | |
| }); | |
| function splitPath(path) { | |
| return Array.isArray(path) ? path : _reject(path.split('.'), function (x) { | |
| return !x; | |
| }); | |
| } | |
| function is(path, predicate, object) { | |
| var parts = splitPath(path); | |
| var rest = object; | |
| var part = undefined; | |
| for (var _iterator = parts, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | |
| if (_isArray) { | |
| if (_i >= _iterator.length) break; | |
| part = _iterator[_i++]; | |
| } else { | |
| _i = _iterator.next(); | |
| if (_i.done) break; | |
| part = _i.value; | |
| } | |
| if (typeof rest === 'undefined') return false; | |
| rest = rest[part]; | |
| } | |
| if (typeof predicate === 'function') { | |
| return predicate(rest); | |
| } | |
| return predicate === rest; | |
| } | |
| var is$1 = curry(is); | |
| function shallowEqual(object, otherObject) { | |
| var equal = true; | |
| forEach(otherObject, function (value, key) { | |
| if (value !== object[key]) { | |
| equal = false; | |
| // exit early | |
| return false; | |
| } | |
| }); | |
| return equal; | |
| } | |
| function map(iteratee, object) { | |
| var updater = typeof iteratee === 'function' ? iteratee : update$1(iteratee); | |
| var mapper = Array.isArray(object) ? mapArray : mapObject; | |
| var newObject = mapper(object, updater); | |
| var equal = shallowEqual(object, newObject); | |
| return equal ? object : newObject; | |
| } | |
| var map$1 = wrap(map); | |
| function omit(predicate, collection) { | |
| return _omit(collection, predicate); | |
| } | |
| var omit$1 = wrap(omit); | |
| function reject(predicate, collection) { | |
| return _reject(collection, predicate); | |
| } | |
| var reject$1 = wrap(reject); | |
| var wildcard = '*'; | |
| function reducePath(acc, key) { | |
| var _ref; | |
| if (key === wildcard) { | |
| return function (value) { | |
| var _update; | |
| return Object.prototype.hasOwnProperty.call(value, wildcard) ? | |
| // If we actually have wildcard as a property, update that | |
| update$1((_update = {}, _update[wildcard] = acc, _update), value) : | |
| // Otherwise map over all properties | |
| map$1(acc, value); | |
| }; | |
| } | |
| return (_ref = {}, _ref[key] = acc, _ref); | |
| } | |
| function updateIn(path, value, object) { | |
| var parts = splitPath(path); | |
| var updates = parts.reduceRight(reducePath, value); | |
| return update$1(updates, object); | |
| } | |
| var updateIn$1 = curry(updateIn); | |
| function withDefault(defaultValue, updates, object) { | |
| if (typeof object === 'undefined') { | |
| return update$1(updates, defaultValue); | |
| } | |
| return update$1(updates, object); | |
| } | |
| var withDefault$1 = curry(withDefault); | |
| var u = update$1; | |
| u._ = _; | |
| u.constant = constant; | |
| u['if'] = _if; | |
| u.ifElse = ifElse; | |
| u.is = is$1; | |
| u.freeze = freeze; | |
| u.map = map$1; | |
| u.omit = omit$1; | |
| u.reject = reject$1; | |
| u.update = update$1; | |
| u.updateIn = updateIn$1; | |
| u.withDefault = withDefault$1; | |
| module.exports = u; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment