UNPKG

foop

Version:

interfaces that describe their intentions.

119 lines (101 loc) 3.15 kB
// nth arg // toArr // // anyKeyVal // // // until // or(where, flip2(where)) // // dot stuff wrap // !dottable // dumping here // https://github.com/andrewplummer/Sugar/blob/master/lib/number.js#L64 const identity = require('../fp/identity') const arity = require('../fp/arity') const mapArray = require('../loop/flipped/mapArrayFlipped') const argsToArr = require('../cast/argumentsToArray') const ENV_DEBUG = true // const at = obj => propOrIndexAkaKey => obj[propOrIndexAkaKey] // const evolutionAt = at(evolution, identity) // @TODO need `length` arg for safety // @curried 2 // could pass in an array that could `evolve` ? // or object with indexes to prevent `holes` ? const _coerceArgs = function(fn, evolution) { // const fnLength = fn.length return arity(fn.length, function() { if (ENV_DEBUG) { console.log('coerce_arguments_before', arguments, '\n') } let args = argsToArr.apply(null, arguments) let evolved = mapArray(args, index => { const arg = args[index] const evolver = evolution[index] console.log({arg, index, evolver}) if (evolver) { return evolver(arg) } else { return arg } }) if (ENV_DEBUG) { console.log('coerce_arguments_after', evolved, '\n') } return fn.apply(this, evolved) }) } // const nativeToPairs = new Map() // nativeToPairs // .set(Function, [isFunction, toFunction]) // .set(Array, [isArray, toArray]) // .set(String, [isString, toString]) // .set(Date, [isDate, toDate]) // .set(Number, [isNumber, toNumber]) // .set(Object, [isObj, toObj]) // .set(Boolean, [isBoolean, toBoolean]) // .set(RegExp, [isRegExp, toRegExp]) // .set(Map, [isMap, toMap]) // .set(Set, [isSet, toSet]) // nativeToPairs.set(null, [isNull, toNumber]) // // const coerceNative = function(fn, evolutions) { // return arity(fn.length, function() {}) // } // const coerceArgs = curry(2, _coerceArgs) const coerceArgs = _coerceArgs // toPlainObject const toLength = require('./toLength') const toObject = require('./toObj') const toNumber = require('./toNumber') const toInteger = require('./toInteger') const toBoolean = require('./toBoolean') const toArray = require('./toArray') const toString = require('./toString') // https://github.com/the-grid/finitedomain/blob/master/src/trie.js#L78 // https://github.com/the-grid/finitedomain/blob/master/src/domain.js#L1192 // http://jscoercion.qfox.nl/ // // @TODO coerce returned // @TODO if obj, check .cast or .toArray etc // @TODO coerce nested // @TODO coerceWhen or `coerceFromTo` function example( strictArray, strictString, strictNumber, strictBoolean, stringOrNumber ) { var hack = strictArray.slice() hack.push(strictString) if (strictBoolean) { hack = hack.concat([strictNumber, stringOrNumber]) } arguments[arguments.length + 1] = hack return argsToArr.apply(null, arguments) } // http://airbnb.io/projects/hypernova/ // could use the property order and then it is a schema that could be a fn... const result = coerceArgs(example, [toArray, toString, toNumber, toBoolean]) console.log(result('eh', 10, '100', 0))