@typed/lambda
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@typed/lambda", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Helpful functions for working in a functional style", | ||
@@ -23,3 +23,3 @@ "main": "./cjs/index.js", | ||
"author": "Tylor Steinberger <tlsteinberger167@gmail.com>", | ||
"license": "MIT", | ||
"license": "Parity Public Licence 3.0 <https://licensezero.com/ids/52afd698-c5c7-4034-b229-ef1243d4caeb/>", | ||
"bugs": { | ||
@@ -30,3 +30,3 @@ "url": "https://github.com/tylors/typed-prelude/issues" | ||
"dependencies": { | ||
"@typed/common": "^1.0.1" | ||
"@typed/common": "^1.0.2" | ||
}, | ||
@@ -40,4 +40,4 @@ "publishConfig": { | ||
"sideEffects": false, | ||
"gitHead": "234e175da43230384b18c53bddce77316bd90100", | ||
"gitHead": "3f92e2f169fdbbcf4c478b22f50324c714ebad52", | ||
"unpkg": "./umd/index.js" | ||
} |
449
umd/index.js
@@ -1,449 +0,2 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory((global.Typed = global.Typed || {}, global.Typed.Lambda = {}))); | ||
}(this, function (exports) { 'use strict'; | ||
/** | ||
* Create a function that always returns a given value | ||
* @param value :: a | ||
* @returns (...* -> a) | ||
*/ | ||
const always = (value) => (..._) => value; | ||
/** | ||
* Allow a fixed length function to be partially applied. | ||
* @param f Function you'd like to curry | ||
*/ | ||
const curry = (f) => { | ||
return curriedN(f.length, f, []); | ||
}; | ||
function curriedN(arity, f, previousArgs) { | ||
if (arity < 2) { | ||
return f; | ||
} | ||
return (...args) => { | ||
const concatArgs = previousArgs.concat(args); | ||
return concatArgs.length >= arity ? f(...concatArgs) : curriedN(arity, f, concatArgs); | ||
}; | ||
} | ||
/** | ||
* Call a function with a list of arguments | ||
* @param args List of arguments | ||
* @param fn Function to call | ||
*/ | ||
const apply = curry((args, fn) => fn(...args)); | ||
const ALL_PROPERTIES_NOT_FOUND = new Error('All Properties Not Found'); | ||
const defaultObject = Object.freeze(Object.create(null)); | ||
/** | ||
* Uses ES2015 Proxy to partially apply a function that takes in an options object | ||
* @param f :: Object a => (a -> b) | ||
*/ | ||
const curryObj = (f) => _curryObj(f, defaultObject); | ||
function _curryObj(f, previousObj) { | ||
return (x) => { | ||
const obj = proxyObject(Object.assign({}, previousObj, x)); | ||
try { | ||
return f(obj); | ||
} | ||
catch (error) { | ||
if (error !== ALL_PROPERTIES_NOT_FOUND) { | ||
throw error; | ||
} | ||
return _curryObj(f, obj); | ||
} | ||
}; | ||
} | ||
function proxyObject(a) { | ||
return new Proxy(a, { | ||
get(target, key) { | ||
if (!target.hasOwnProperty(key)) { | ||
throw ALL_PROPERTIES_NOT_FOUND; | ||
} | ||
return target[key]; | ||
}, | ||
}); | ||
} | ||
/** | ||
* Reverse the first two arguments of a function | ||
* @param fn: (a -> b -> c) | ||
* @returns (b -> a -> c) | ||
*/ | ||
const flip = (fn) => ((a, b, ...args) => apply([b, a, ...args], fn)); | ||
/** | ||
* Identity function | ||
* @param value :: a - Value to return | ||
*/ | ||
const id = (value) => value; | ||
function typeOf(value) { | ||
if (value === null) { | ||
return 'Null'; | ||
} | ||
if (value === void 0) { | ||
return `Undefined`; | ||
} | ||
return Object.prototype.toString.call(value).slice(8, -1); | ||
} | ||
const FUNCTION_NAME_REGEX = /^function\s*([\w$]+)/; | ||
const DEFAULT_MATCH = ['', '']; | ||
/** | ||
* Returns the name of a function. | ||
* @name functionName(fn: Function): string | ||
*/ | ||
function functionName(fn) { | ||
if (fn.name) { | ||
return fn.name; | ||
} | ||
const [, name] = String(fn).match(FUNCTION_NAME_REGEX) || DEFAULT_MATCH; | ||
return name; | ||
} | ||
function includesWith(pred, x, list) { | ||
let idx = 0; | ||
const len = list.length; | ||
while (idx < len) { | ||
if (pred(x, list[idx], idx)) { | ||
return true; | ||
} | ||
idx += 1; | ||
} | ||
return false; | ||
} | ||
function equals(a, b, stackA = [], stackB = []) { | ||
if (Object.is(a, b)) { | ||
return true; | ||
} | ||
const typeA = typeOf(a); | ||
if (typeA !== typeOf(b)) { | ||
return false; | ||
} | ||
if (a == null || b == null) { | ||
return false; | ||
} | ||
switch (typeA) { | ||
case 'Arguments': | ||
case 'Array': | ||
case 'Object': | ||
if (typeof a.constructor === 'function' && functionName(a.constructor) === 'Promise') { | ||
return a === b; | ||
} | ||
break; | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
if (!(typeof a === typeof b && Object.is(a.valueOf(), b.valueOf()))) { | ||
return false; | ||
} | ||
break; | ||
case 'Date': | ||
if (!Object.is(a.valueOf(), b.valueOf())) { | ||
return false; | ||
} | ||
break; | ||
case 'Error': | ||
return a.name === b.name && a.message === b.message; | ||
case 'RegExp': | ||
if (!(a.source === b.source && | ||
a.global === b.global && | ||
a.ignoreCase === b.ignoreCase && | ||
a.multiline === b.multiline && | ||
a.sticky === b.sticky && | ||
a.unicode === b.unicode)) { | ||
return false; | ||
} | ||
break; | ||
} | ||
let idx = stackA.length - 1; | ||
while (idx >= 0) { | ||
if (stackA[idx] === a) { | ||
return stackB[idx] === b; | ||
} | ||
idx -= 1; | ||
} | ||
switch (typeA) { | ||
case 'Map': | ||
if (a.size !== b.size) { | ||
return false; | ||
} | ||
return _uniqContentEquals(a.entries(), b.entries(), stackA.concat([a]), stackB.concat([b])); | ||
case 'Set': | ||
if (a.size !== b.size) { | ||
return false; | ||
} | ||
return _uniqContentEquals(a.values(), b.values(), stackA.concat([a]), stackB.concat([b])); | ||
case 'Arguments': | ||
case 'Array': | ||
case 'Object': | ||
case 'Boolean': | ||
case 'Number': | ||
case 'String': | ||
case 'Date': | ||
case 'Error': | ||
case 'RegExp': | ||
case 'Int8Array': | ||
case 'Uint8Array': | ||
case 'Uint8ClampedArray': | ||
case 'Int16Array': | ||
case 'Uint16Array': | ||
case 'Int32Array': | ||
case 'Uint32Array': | ||
case 'Float32Array': | ||
case 'Float64Array': | ||
case 'ArrayBuffer': | ||
break; | ||
default: | ||
// Values of other types are only equal if identical. | ||
return false; | ||
} | ||
const keysA = Object.keys(a); | ||
if (keysA.length !== Object.keys(b).length) { | ||
return false; | ||
} | ||
const extendedStackA = stackA.concat([a]); | ||
const extendedStackB = stackB.concat([b]); | ||
idx = keysA.length - 1; | ||
while (idx >= 0) { | ||
const key = keysA[idx]; | ||
if (!(Object.prototype.hasOwnProperty.call(b, key) && | ||
equals(b[key], a[key], extendedStackA, extendedStackB))) { | ||
return false; | ||
} | ||
idx -= 1; | ||
} | ||
return true; | ||
} | ||
function _uniqContentEquals(aIterable, bIterable, stackA, stackB) { | ||
const a = Array.from(aIterable); | ||
const b = Array.from(bIterable); | ||
// tslint:disable-next-line:variable-name | ||
function eq(_a, _b) { | ||
return equals(_a, _b, stackA.slice(), stackB.slice()); | ||
} | ||
// if *a* array contains any element that is not included in *b* | ||
// tslint:disable-next-line:no-shadowed-variable | ||
return !includesWith((b, aItem) => !includesWith(eq, aItem, b), b, a); | ||
} | ||
function indexOf(list, a, idx = 0) { | ||
// Array.prototype.indexOf doesn't exist below IE9 | ||
if (typeof list.indexOf === 'function') { | ||
switch (typeof a) { | ||
case 'number': | ||
let inf; | ||
let item; | ||
if (a === 0) { | ||
// manually crawl the list to distinguish between +0 and -0 | ||
inf = 1 / a; | ||
while (idx < list.length) { | ||
item = list[idx]; | ||
if (item === 0 && 1 / item === inf) { | ||
return idx; | ||
} | ||
idx += 1; | ||
} | ||
return -1; | ||
} | ||
else if (a !== a) { | ||
// NaN | ||
while (idx < list.length) { | ||
item = list[idx]; | ||
if (typeof item === 'number' && item !== item) { | ||
return idx; | ||
} | ||
idx += 1; | ||
} | ||
return -1; | ||
} | ||
// non-zero numbers can utilise Set | ||
return list.indexOf(a, idx); | ||
// all these types can utilise Set | ||
case 'string': | ||
case 'boolean': | ||
case 'function': | ||
case 'undefined': | ||
return list.indexOf(a, idx); | ||
case 'object': | ||
if (a === null) { | ||
// null can utilise Set | ||
return list.indexOf(a, idx); | ||
} | ||
} | ||
} | ||
while (idx < list.length) { | ||
if (equals(list[idx], a)) { | ||
return idx; | ||
} | ||
idx += 1; | ||
} | ||
return -1; | ||
} | ||
function mapArrayLike(fn, functor) { | ||
let idx = 0; | ||
const len = functor.length; | ||
const result = Array(len); | ||
while (idx < len) { | ||
result[idx] = fn(functor[idx]); | ||
idx += 1; | ||
} | ||
return result; | ||
} | ||
const STEPS = [ | ||
[/\\/g, '\\\\'], | ||
[/[\b]/g, '\\b'], | ||
[/\f/g, '\\f'], | ||
[/\n/g, '\\n'], | ||
[/\r/g, '\\r'], | ||
[/\t/g, '\\t'], | ||
[/\v/g, '\\v'], | ||
[/\0/g, '\\0'], | ||
]; | ||
const LAST_STEP = [/"/g, '\\"']; | ||
function quote(s) { | ||
const escaped = STEPS.reduce(applyStep, s); | ||
return '"' + applyStep(escaped, LAST_STEP) + '"'; | ||
} | ||
function applyStep(str, step) { | ||
return str.replace(step[0], step[1]); | ||
} | ||
/** | ||
* Convert anything into a string | ||
* @param x :: any | ||
* @returns a string representation of a value | ||
*/ | ||
const toString = (x) => _toString(x, []); | ||
function _toString(x, seen) { | ||
const recur = (y) => { | ||
const xs = seen.concat([x]); | ||
return indexOf(xs, y) > -1 ? '<Circular>' : _toString(y, xs); | ||
}; | ||
const mapPairs = (obj, keys) => { | ||
return mapArrayLike((k) => { | ||
return quote(k) + ': ' + recur(obj[k]); | ||
}, keys.slice().sort()); | ||
}; | ||
switch (Object.prototype.toString.call(x)) { | ||
case '[object Arguments]': | ||
return '(function() { return arguments; }(' + mapArrayLike(recur, x).join(', ') + '))'; | ||
case '[object Array]': | ||
return ('[' + | ||
mapArrayLike(recur, x) | ||
.concat(mapPairs(x, Object.keys(x).filter(k => !/^\d+$/.test(k)))) | ||
.join(', ') + | ||
']'); | ||
case '[object Boolean]': | ||
return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString(); | ||
case '[object Date]': | ||
return ('new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : quote(x.toISOString())) + ')'); | ||
case '[object Null]': | ||
return 'null'; | ||
case '[object Number]': | ||
return typeof x === 'object' | ||
? 'new Number(' + recur(x.valueOf()) + ')' | ||
: 1 / x === -Infinity | ||
? '-0' | ||
: x.toString(10); | ||
case '[object String]': | ||
return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : quote(x); | ||
case '[object Undefined]': | ||
return 'undefined'; | ||
default: | ||
if (typeof x.toString === 'function') { | ||
const repr = x.toString(); | ||
if (repr !== '[object Object]') { | ||
return repr; | ||
} | ||
} | ||
return '{' + mapPairs(x, Object.keys(x)).join(', ') + '}'; | ||
} | ||
} | ||
/** | ||
* Memoize a function | ||
* @param f Function to memoize | ||
*/ | ||
const memoize = (f) => { | ||
const cache = new Map(); | ||
return (...args) => { | ||
const key = args.reduce((x, y) => x + toString(y), ''); | ||
if (cache.has(key)) { | ||
return cache.get(key); | ||
} | ||
let result = f(...args); | ||
if (typeof result === 'function') { | ||
result = memoize(result); | ||
} | ||
cache.set(key, result); | ||
return result; | ||
}; | ||
}; | ||
/** | ||
* Function that does nothing | ||
*/ | ||
const noOp = () => void 0; | ||
/** Left-to-right composition for exactly two function */ | ||
const pipe2 = (f, g) => (x) => g(f(x)); | ||
/** | ||
* Generic Left-to-right composition | ||
*/ | ||
const pipe = ((...fns) => fns.length > 1 ? fns.slice(1).reduce(pipe2, fns[0]) : fns[0]); | ||
/** | ||
* Perform a side-effect with a value and return the given value. | ||
* @param fn :: (a -> *) | ||
* @param value :: a | ||
* @returns a | ||
*/ | ||
const tap = curry((fn, value) => { | ||
fn(value); | ||
return value; | ||
}); | ||
/** | ||
* Convert a function like (a -> b -> c -> d) into ((a, b, c) -> d) | ||
* @param fn :: Function to uncurry | ||
* @returns Function that accepts all arguments at once. | ||
*/ | ||
function uncurry(f) { | ||
if (typeof f !== 'function' || f.length === 0) { | ||
return f; | ||
} | ||
// tslint:disable-next-line:only-arrow-functions | ||
return function () { | ||
let r = f; | ||
// tslint:disable-next-line:prefer-for-of | ||
for (let i = 0; i < arguments.length; i++) { | ||
r = r(arguments[i]); | ||
} | ||
return uncurry(r); | ||
}; | ||
} | ||
exports.always = always; | ||
exports.apply = apply; | ||
exports.curry = curry; | ||
exports.curryObj = curryObj; | ||
exports.flip = flip; | ||
exports.id = id; | ||
exports.memoize = memoize; | ||
exports.noOp = noOp; | ||
exports.pipe = pipe; | ||
exports.pipe2 = pipe2; | ||
exports.tap = tap; | ||
exports.uncurry = uncurry; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e=e||self).Typed=e.Typed||{},e.Typed.Lambda={}))}(this,function(e){"use strict";const t=e=>(function e(t,n,r){if(t<2)return n;return(...c)=>{const o=r.concat(c);return o.length>=t?n(...o):e(t,n,o)}})(e.length,e,[]);const n=t((e,t)=>t(...e)),r=new Error("All Properties Not Found"),c=Object.freeze(Object.create(null));function o(e){return null===e?"Null":void 0===e?"Undefined":Object.prototype.toString.call(e).slice(8,-1)}const u=/^function\s*([\w$]+)/,a=["",""];function i(e,t,n){let r=0;const c=n.length;for(;r<c;){if(e(t,n[r],r))return!0;r+=1}return!1}function s(e,t,n=[],r=[]){if(Object.is(e,t))return!0;const c=o(e);if(c!==o(t))return!1;if(null==e||null==t)return!1;switch(c){case"Arguments":case"Array":case"Object":if("function"==typeof e.constructor&&"Promise"===function(e){if(e.name)return e.name;const[,t]=String(e).match(u)||a;return t}(e.constructor))return e===t;break;case"Boolean":case"Number":case"String":if(typeof e!=typeof t||!Object.is(e.valueOf(),t.valueOf()))return!1;break;case"Date":if(!Object.is(e.valueOf(),t.valueOf()))return!1;break;case"Error":return e.name===t.name&&e.message===t.message;case"RegExp":if(e.source!==t.source||e.global!==t.global||e.ignoreCase!==t.ignoreCase||e.multiline!==t.multiline||e.sticky!==t.sticky||e.unicode!==t.unicode)return!1}let i=n.length-1;for(;i>=0;){if(n[i]===e)return r[i]===t;i-=1}switch(c){case"Map":return e.size===t.size&&f(e.entries(),t.entries(),n.concat([e]),r.concat([t]));case"Set":return e.size===t.size&&f(e.values(),t.values(),n.concat([e]),r.concat([t]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}const l=Object.keys(e);if(l.length!==Object.keys(t).length)return!1;const y=n.concat([e]),g=r.concat([t]);for(i=l.length-1;i>=0;){const n=l[i];if(!Object.prototype.hasOwnProperty.call(t,n)||!s(t[n],e[n],y,g))return!1;i-=1}return!0}function f(e,t,n,r){const c=Array.from(e);function o(e,t){return s(e,t,n.slice(),r.slice())}return!i((e,t)=>!i(o,t,e),Array.from(t),c)}function l(e,t){let n=0;const r=t.length,c=Array(r);for(;n<r;)c[n]=e(t[n]),n+=1;return c}const y=[[/\\/g,"\\\\"],[/[\b]/g,"\\b"],[/\f/g,"\\f"],[/\n/g,"\\n"],[/\r/g,"\\r"],[/\t/g,"\\t"],[/\v/g,"\\v"],[/\0/g,"\\0"]],g=[/"/g,'\\"'];function b(e){return'"'+p(y.reduce(p,e),g)+'"'}function p(e,t){return e.replace(t[0],t[1])}const d=e=>(function e(t,n){const r=r=>{const c=n.concat([t]);return function(e,t,n=0){if("function"==typeof e.indexOf)switch(typeof t){case"number":let r,c;if(0===t){for(r=1/t;n<e.length;){if(0===(c=e[n])&&1/c===r)return n;n+=1}return-1}if(t!=t){for(;n<e.length;){if("number"==typeof(c=e[n])&&c!=c)return n;n+=1}return-1}return e.indexOf(t,n);case"string":case"boolean":case"function":case"undefined":return e.indexOf(t,n);case"object":if(null===t)return e.indexOf(t,n)}for(;n<e.length;){if(s(e[n],t))return n;n+=1}return-1}(c,r)>-1?"<Circular>":e(r,c)};const c=(e,t)=>l(t=>b(t)+": "+r(e[t]),t.slice().sort());switch(Object.prototype.toString.call(t)){case"[object Arguments]":return"(function() { return arguments; }("+l(r,t).join(", ")+"))";case"[object Array]":return"["+l(r,t).concat(c(t,Object.keys(t).filter(e=>!/^\d+$/.test(e)))).join(", ")+"]";case"[object Boolean]":return"object"==typeof t?"new Boolean("+r(t.valueOf())+")":t.toString();case"[object Date]":return"new Date("+(isNaN(t.valueOf())?r(NaN):b(t.toISOString()))+")";case"[object Null]":return"null";case"[object Number]":return"object"==typeof t?"new Number("+r(t.valueOf())+")":1/t==-1/0?"-0":t.toString(10);case"[object String]":return"object"==typeof t?"new String("+r(t.valueOf())+")":b(t);case"[object Undefined]":return"undefined";default:if("function"==typeof t.toString){const e=t.toString();if("[object Object]"!==e)return e}return"{"+c(t,Object.keys(t)).join(", ")+"}"}})(e,[]);const j=e=>{const t=new Map;return(...n)=>{const r=n.reduce((e,t)=>e+d(t),"");if(t.has(r))return t.get(r);let c=e(...n);return"function"==typeof c&&(c=j(c)),t.set(r,c),c}},O=(e,t)=>n=>t(e(n)),m=t((e,t)=>(e(t),t));e.always=e=>(...t)=>e,e.apply=n,e.curry=t,e.curryObj=e=>(function e(t,n){return c=>{const o=function(e){return new Proxy(e,{get(e,t){if(!e.hasOwnProperty(t))throw r;return e[t]}})}(Object.assign({},n,c));try{return t(o)}catch(n){if(n!==r)throw n;return e(t,o)}}})(e,c),e.flip=e=>(t,r,...c)=>n([r,t,...c],e),e.id=e=>e,e.memoize=j,e.noOp=()=>void 0,e.pipe=(...e)=>e.length>1?e.slice(1).reduce(O,e[0]):e[0],e.pipe2=O,e.tap=m,e.uncurry=function e(t){return"function"!=typeof t||0===t.length?t:function(){let n=t;for(let e=0;e<arguments.length;e++)n=n(arguments[e]);return e(n)}},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.js.map |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
192
1
3
440245
2
1
70
1333
Updated@typed/common@^1.0.2