@zambezi/fun
Advanced tools
Comparing version 2.0.0-2 to 2.0.0
173
dist/fun.js
@@ -7,2 +7,27 @@ (function (global, factory) { | ||
function functor(v) { | ||
return typeof v === 'function' ? v : function () { | ||
return v; | ||
}; | ||
} | ||
function and() { | ||
var funs = Array.from(arguments); | ||
return function and() { | ||
var args = arguments, | ||
context = this; | ||
var result = void 0; | ||
funs.every(call); | ||
return result; | ||
function call(fun) { | ||
return result = fun.apply(context, args); | ||
} | ||
}; | ||
} | ||
function batch() { | ||
@@ -30,12 +55,6 @@ var funcs = arguments, | ||
function functor(v) { | ||
return typeof v === 'function' ? v : function () { | ||
return v; | ||
}; | ||
function modulo(a, n) { | ||
return a - n * Math.floor(a / n); | ||
} | ||
function identity(v) { | ||
return v; | ||
} | ||
function negate(f) { | ||
@@ -47,4 +66,25 @@ return function not() { | ||
function or() { | ||
var funs = Array.from(arguments); | ||
return function or() { | ||
var args = arguments, | ||
context = this; | ||
var result = void 0; | ||
funs.some(call); | ||
return result; | ||
function call(fun) { | ||
return result = fun.apply(context, args); | ||
} | ||
}; | ||
} | ||
function property(k) { | ||
if (!k) return identity; | ||
if (!k) return function (d) { | ||
return d; | ||
}; | ||
var components = k.split('.'); | ||
@@ -62,15 +102,124 @@ return function prop(d) { | ||
a.splice.apply(a, [0, a.length].concat(b)); | ||
return a; | ||
} | ||
function someResult() { | ||
var funs = Array.from(arguments); | ||
return function someResult() { | ||
var args = arguments, | ||
context = this; | ||
var result = void 0; | ||
funs.some(run); | ||
return result; | ||
function run(fun) { | ||
result = fun.apply(context, args); | ||
return !(result === undefined); | ||
} | ||
}; | ||
} | ||
function trampoline(fun) { | ||
var result = isFunction(fun) ? fun.apply(fun, Array.from(arguments).slice(1)) : fun; | ||
while (isFunction(result)) { | ||
result = result(); | ||
}return result; | ||
} | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
} | ||
function trampolineFilter(list, predicate) { | ||
var length = list.length, | ||
i = 0; | ||
if (length == 0) return function () { | ||
return []; | ||
}; | ||
return function () { | ||
return step([], list[i], i); | ||
}; | ||
function step(acc, item, i) { | ||
if (i == length) return acc; | ||
if (predicate(item, i, list)) acc.push(item); | ||
i++; | ||
return function () { | ||
return step(acc, list[i], i); | ||
}; | ||
} | ||
} | ||
function trampolineMergeSort(a) { | ||
var comparator = arguments.length <= 1 || arguments[1] === undefined ? ascending : arguments[1]; | ||
return continuedSort(a, function (d) { | ||
return d; | ||
}); | ||
function continuedSort(a, continuation) { | ||
var n = a.length, | ||
left, | ||
right, | ||
pivot; | ||
if (n <= 1) return continuation(a); | ||
pivot = Math.floor(n / 2); | ||
left = a.slice(0, pivot); | ||
right = a.slice(pivot); | ||
return continuedSort.bind(null, left, function processLeft(leftR) { | ||
return continuedSort.bind(null, right, function processRight(rightR) { | ||
return continuation(fastMerge(leftR, rightR)); | ||
}); | ||
}); | ||
} | ||
function fastMerge(a, b) { | ||
var result = [], | ||
aLength = a.length, | ||
bLength = b.length; | ||
while (aLength || bLength) { | ||
if (!aLength) { | ||
result.push(b.pop()); | ||
} else if (!bLength || comparator(a[aLength - 1], b[bLength - 1]) > 0) { | ||
result.push(a.pop()); | ||
} else { | ||
result.push(b.pop()); | ||
} | ||
aLength = a.length; | ||
bLength = b.length; | ||
} | ||
result.reverse(); | ||
return result; | ||
} | ||
} | ||
function ascending(a, b) { | ||
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; | ||
} | ||
exports.and = and; | ||
exports.batch = batch; | ||
exports.compareWith = compareWith; | ||
exports.functor = functor; | ||
exports.identity = identity; | ||
exports.modulo = modulo; | ||
exports.negate = negate; | ||
exports.or = or; | ||
exports.property = property; | ||
exports.replaceArrayContents = replaceArrayContents; | ||
exports.someResult = someResult; | ||
exports.trampoline = trampoline; | ||
exports.trampolineFilter = trampolineFilter; | ||
exports.trampolineMergeSort = trampolineMergeSort; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=fun.js.map |
{ | ||
"name": "@zambezi/fun", | ||
"version": "2.0.0-2", | ||
"version": "2.0.0", | ||
"description": "Functional goodies from the Zambezi team", | ||
@@ -23,11 +23,12 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"babel-register": "^6.9.0", | ||
"babelrc-rollup": "^2.0.0", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.5.3", | ||
"rimraf": "^2.5.3", | ||
"rollup": "^0.26.3", | ||
"rollup-plugin-babel": "^2.3.9" | ||
"babel-preset-es2015": "6.9.0", | ||
"babel-preset-es2015-rollup": "1.1.1", | ||
"babel-register": "6.9.0", | ||
"babelrc-rollup": "2.0.0", | ||
"d3-array": "1.0.1", | ||
"mkdirp": "0.5.1", | ||
"mocha": "2.5.3", | ||
"rimraf": "2.5.3", | ||
"rollup": "0.26.3", | ||
"rollup-plugin-babel": "2.3.9" | ||
}, | ||
@@ -34,0 +35,0 @@ "dependencies": { |
@@ -1,15 +0,35 @@ | ||
![Logo](logo.svg?raw=true) | ||
# Fun! | ||
# What is fun? | ||
What is fun? | ||
------------ | ||
A functional utility belt, for Javascript. It's rather lovely, you should try it you'll like it. | ||
A functional utility belt, for JavaScript. It's rather lovely, you should try it you'll like it. | ||
# Why fun? | ||
Why fun? | ||
-------- | ||
There are plenty of very good libraries to aid with functional programming in Javascript. This project sits at the seams of those projects, and rather than compete with them fun complements them. | ||
# Installation | ||
Installation | ||
------------ | ||
TBD | ||
```bash | ||
npm install --save-dev @zambezi/fun | ||
``` | ||
Usage | ||
----- | ||
For usage, have a look at documentation for the individual functions, listed in the [SUMMARY](SUMMARY.md) | ||
Found an issue, or want to contribute? | ||
-------------------------------------- | ||
If you find an issue, want to start a discussion on something related to this project, or have suggestions on how to improve it? Please [create an issue](../../issues/new)! | ||
See an error and want to fix it? Want to add a file or otherwise make some changes? All contributions are welcome! Please refer to the [contribution guidelines](CONTRIBUTING.md) for more information. | ||
License | ||
------- | ||
Please refer to the [license](LICENSE.md) for more information on licensing and copyright information. |
@@ -0,7 +1,13 @@ | ||
export { and } from './and' | ||
export { batch } from './batch' | ||
export { compareWith } from './compare-with' | ||
export { functor } from './functor' | ||
export { identity } from './identity' | ||
export { modulo } from './modulo' | ||
export { negate } from './negate' | ||
export { or } from './or' | ||
export { property } from './property' | ||
export { replaceArrayContents } from './replace-array-contents' | ||
export { someResult } from './some-result' | ||
export { trampoline } from './trampoline' | ||
export { trampolineFilter } from './trampoline-filter' | ||
export { trampolineMergeSort } from './trampoline-merge-sort' |
@@ -1,5 +0,3 @@ | ||
import { identity } from './identity' | ||
export function property(k) { | ||
if (!k) return identity | ||
if (!k) return d => d | ||
const components = k.split('.') | ||
@@ -6,0 +4,0 @@ return function prop(d) { |
export function replaceArrayContents(a, b) { | ||
a.splice.apply(a, [0, a.length].concat(b)) | ||
return a | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
34721
40
357
0
36
10
1