Comparing version 0.4.1 to 0.5.0
156
build/fn.js
@@ -17,3 +17,3 @@ (function(root, factory) { | ||
fn.toArray = function (collection) { | ||
return [].slice.call(collection); | ||
return [].slice.call(collection); | ||
}; | ||
@@ -24,97 +24,97 @@ | ||
fn.op = { | ||
'+': function (value1, value2) { | ||
return value1 + value2; | ||
}, | ||
'-': function (value1, value2) { | ||
return value1 - value2; | ||
}, | ||
'*': function (value1, value2) { | ||
return value1 * value2; | ||
}, | ||
'/': function (value1, value2) { | ||
return value1 / value2; | ||
}, | ||
'==': function (value1, value2) { | ||
return value1 == value2; | ||
}, | ||
'===': function (value1, value2) { | ||
return value1 === value2; | ||
} | ||
'+': function (value1, value2) { | ||
return value1 + value2; | ||
}, | ||
'-': function (value1, value2) { | ||
return value1 - value2; | ||
}, | ||
'*': function (value1, value2) { | ||
return value1 * value2; | ||
}, | ||
'/': function (value1, value2) { | ||
return value1 / value2; | ||
}, | ||
'==': function (value1, value2) { | ||
return value1 == value2; | ||
}, | ||
'===': function (value1, value2) { | ||
return value1 === value2; | ||
} | ||
}; | ||
fn.is = function (value, type) { | ||
// If the value is null or undefined, return the stringified name, | ||
// otherwise get the [[Class]] and compare to the relevant part of the value: | ||
var valueType = value == null ? | ||
'' + value : | ||
({}).toString.call(value).slice(8, -1).toLowerCase(); | ||
// If the value is null or undefined, return the stringified name, | ||
// otherwise get the [[Class]] and compare to the relevant part of the value: | ||
var valueType = value == null ? | ||
'' + value : | ||
({}).toString.call(value).slice(8, -1).toLowerCase(); | ||
return type === valueType; | ||
return type === valueType; | ||
}; | ||
fn.apply = function (handler, args) { | ||
return handler.apply(null, args); | ||
return handler.apply(null, args); | ||
}; | ||
fn.concat = function () { | ||
var args = fn.toArray(arguments); | ||
var args = fn.toArray(arguments); | ||
return args[0].concat.apply(args[0], args.slice(1)); | ||
return args[0].concat.apply(args[0], args.slice(1)); | ||
}; | ||
fn.partial = function () { | ||
var args = fn.toArray(arguments); | ||
var args = fn.toArray(arguments); | ||
return function () { | ||
return fn.apply(args[0], fn.concat(args.slice(1), fn.toArray(arguments)) ); | ||
}; | ||
return function () { | ||
return fn.apply(args[0], fn.concat(args.slice(1), fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
fn.curry = function (handler, arity) { | ||
arity = arity || handler.length; | ||
arity = arity || handler.length; | ||
return function curry() { | ||
var args = fn.toArray(arguments); | ||
return function curry() { | ||
var args = fn.toArray(arguments); | ||
if (args.length >= arity) { | ||
return handler.apply(null, args); | ||
} | ||
if (args.length >= arity) { | ||
return handler.apply(null, args); | ||
} | ||
return function () { | ||
return curry.apply(null, args.concat(fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
return function () { | ||
return curry.apply(null, args.concat(fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
}; | ||
fn.properties = function (object) { | ||
var accumulator = []; | ||
var accumulator = []; | ||
for (var property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
accumulator.push(property); | ||
} | ||
} | ||
for (var property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
accumulator.push(property); | ||
} | ||
} | ||
return accumulator; | ||
return accumulator; | ||
}; | ||
fn.each = function (handler, collection, params) { | ||
for (var index = 0, collectionLength = collection.length; index < collectionLength; index++) { | ||
fn.apply(handler, fn.concat([ collection[index], index, collection ], params)); | ||
} | ||
for (var index = 0, collectionLength = collection.length; index < collectionLength; index++) { | ||
fn.apply(handler, fn.concat([ collection[index], index, collection ], params)); | ||
} | ||
}; | ||
fn.reduce = function (handler, collection, accumulator, params) { | ||
fn.each(function (value, index) { | ||
accumulator = fn.apply(handler, fn.concat([ accumulator, value, index ], params)); | ||
}, collection); | ||
fn.each(function (value, index) { | ||
accumulator = fn.apply(handler, fn.concat([ accumulator, value, index ], params)); | ||
}, collection); | ||
return accumulator; | ||
return accumulator; | ||
}; | ||
fn.filter = function (expression, collection) { | ||
return fn.reduce(function (accumulator, item, index) { | ||
expression(item, index) && accumulator.push(item); | ||
return accumulator; | ||
}, collection, []); | ||
return fn.reduce(function (accumulator, item, index) { | ||
expression(item, index) && accumulator.push(item); | ||
return accumulator; | ||
}, collection, []); | ||
}; | ||
@@ -126,30 +126,40 @@ | ||
fn.map = function (handler, collection, params) { | ||
return fn.reduce(function (accumulator, value, index) { | ||
accumulator.push( fn.apply(handler, fn.concat([ value, index, collection ], params)) ); | ||
return accumulator; | ||
}, collection, []); | ||
return fn.reduce(function (accumulator, value, index) { | ||
accumulator.push( fn.apply(handler, fn.concat([ value, index, collection ], params)) ); | ||
return accumulator; | ||
}, collection, []); | ||
}; | ||
fn.reverse = function (collection) { | ||
return fn.cloneArray(collection).reverse(); | ||
return fn.cloneArray(collection).reverse(); | ||
}; | ||
fn.pipeline = function () { | ||
var functions = fn.toArray(arguments); | ||
var functions = fn.toArray(arguments); | ||
return function () { | ||
return fn.reduce(function (args, func) { | ||
return [ fn.apply(func, args) ]; | ||
}, functions, fn.toArray(arguments))[0]; | ||
}; | ||
return function () { | ||
return fn.reduce(function (args, func) { | ||
return [ fn.apply(func, args) ]; | ||
}, functions, fn.toArray(arguments))[0]; | ||
}; | ||
}; | ||
fn.compose = function () { | ||
return fn.apply(fn.pipeline, fn.reverse(arguments)); | ||
return fn.apply(fn.pipeline, fn.reverse(arguments)); | ||
}; | ||
fn.prop = fn.curry(function (name, object) { | ||
return object[name]; | ||
return object[name]; | ||
}); | ||
fn.merge = function () { | ||
return fn.reduce(function (accumulator, value) { | ||
fn.each(function (property) { | ||
accumulator[property] = value[property]; | ||
}, fn.properties(value)); | ||
return accumulator; | ||
}, fn.toArray(arguments), {}); | ||
}; | ||
return fn; | ||
})); |
{ | ||
"name": "fn.js", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Functional programming strategy library for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "build/fn.js", |
158
src/index.js
@@ -6,3 +6,3 @@ 'use strict'; | ||
fn.toArray = function (collection) { | ||
return [].slice.call(collection); | ||
return [].slice.call(collection); | ||
}; | ||
@@ -13,97 +13,97 @@ | ||
fn.op = { | ||
'+': function (value1, value2) { | ||
return value1 + value2; | ||
}, | ||
'-': function (value1, value2) { | ||
return value1 - value2; | ||
}, | ||
'*': function (value1, value2) { | ||
return value1 * value2; | ||
}, | ||
'/': function (value1, value2) { | ||
return value1 / value2; | ||
}, | ||
'==': function (value1, value2) { | ||
return value1 == value2; | ||
}, | ||
'===': function (value1, value2) { | ||
return value1 === value2; | ||
} | ||
'+': function (value1, value2) { | ||
return value1 + value2; | ||
}, | ||
'-': function (value1, value2) { | ||
return value1 - value2; | ||
}, | ||
'*': function (value1, value2) { | ||
return value1 * value2; | ||
}, | ||
'/': function (value1, value2) { | ||
return value1 / value2; | ||
}, | ||
'==': function (value1, value2) { | ||
return value1 == value2; | ||
}, | ||
'===': function (value1, value2) { | ||
return value1 === value2; | ||
} | ||
}; | ||
fn.is = function (value, type) { | ||
// If the value is null or undefined, return the stringified name, | ||
// otherwise get the [[Class]] and compare to the relevant part of the value: | ||
var valueType = value == null ? | ||
'' + value : | ||
({}).toString.call(value).slice(8, -1).toLowerCase(); | ||
// If the value is null or undefined, return the stringified name, | ||
// otherwise get the [[Class]] and compare to the relevant part of the value: | ||
var valueType = value == null ? | ||
'' + value : | ||
({}).toString.call(value).slice(8, -1).toLowerCase(); | ||
return type === valueType; | ||
return type === valueType; | ||
}; | ||
fn.apply = function (handler, args) { | ||
return handler.apply(null, args); | ||
return handler.apply(null, args); | ||
}; | ||
fn.concat = function () { | ||
var args = fn.toArray(arguments); | ||
var args = fn.toArray(arguments); | ||
return args[0].concat.apply(args[0], args.slice(1)); | ||
return args[0].concat.apply(args[0], args.slice(1)); | ||
}; | ||
fn.partial = function () { | ||
var args = fn.toArray(arguments); | ||
var args = fn.toArray(arguments); | ||
return function () { | ||
return fn.apply(args[0], fn.concat(args.slice(1), fn.toArray(arguments)) ); | ||
}; | ||
return function () { | ||
return fn.apply(args[0], fn.concat(args.slice(1), fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
fn.curry = function (handler, arity) { | ||
arity = arity || handler.length; | ||
arity = arity || handler.length; | ||
return function curry() { | ||
var args = fn.toArray(arguments); | ||
return function curry() { | ||
var args = fn.toArray(arguments); | ||
if (args.length >= arity) { | ||
return handler.apply(null, args); | ||
} | ||
if (args.length >= arity) { | ||
return handler.apply(null, args); | ||
} | ||
return function () { | ||
return curry.apply(null, args.concat(fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
return function () { | ||
return curry.apply(null, args.concat(fn.toArray(arguments)) ); | ||
}; | ||
}; | ||
}; | ||
fn.properties = function (object) { | ||
var accumulator = []; | ||
var accumulator = []; | ||
for (var property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
accumulator.push(property); | ||
} | ||
} | ||
for (var property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
accumulator.push(property); | ||
} | ||
} | ||
return accumulator; | ||
return accumulator; | ||
}; | ||
fn.each = function (handler, collection, params) { | ||
for (var index = 0, collectionLength = collection.length; index < collectionLength; index++) { | ||
fn.apply(handler, fn.concat([ collection[index], index, collection ], params)); | ||
} | ||
for (var index = 0, collectionLength = collection.length; index < collectionLength; index++) { | ||
fn.apply(handler, fn.concat([ collection[index], index, collection ], params)); | ||
} | ||
}; | ||
fn.reduce = function (handler, collection, accumulator, params) { | ||
fn.each(function (value, index) { | ||
accumulator = fn.apply(handler, fn.concat([ accumulator, value, index ], params)); | ||
}, collection); | ||
fn.each(function (value, index) { | ||
accumulator = fn.apply(handler, fn.concat([ accumulator, value, index ], params)); | ||
}, collection); | ||
return accumulator; | ||
return accumulator; | ||
}; | ||
fn.filter = function (expression, collection) { | ||
return fn.reduce(function (accumulator, item, index) { | ||
expression(item, index) && accumulator.push(item); | ||
return accumulator; | ||
}, collection, []); | ||
return fn.reduce(function (accumulator, item, index) { | ||
expression(item, index) && accumulator.push(item); | ||
return accumulator; | ||
}, collection, []); | ||
}; | ||
@@ -115,28 +115,38 @@ | ||
fn.map = function (handler, collection, params) { | ||
return fn.reduce(function (accumulator, value, index) { | ||
accumulator.push( fn.apply(handler, fn.concat([ value, index, collection ], params)) ); | ||
return accumulator; | ||
}, collection, []); | ||
return fn.reduce(function (accumulator, value, index) { | ||
accumulator.push( fn.apply(handler, fn.concat([ value, index, collection ], params)) ); | ||
return accumulator; | ||
}, collection, []); | ||
}; | ||
fn.reverse = function (collection) { | ||
return fn.cloneArray(collection).reverse(); | ||
return fn.cloneArray(collection).reverse(); | ||
}; | ||
fn.pipeline = function () { | ||
var functions = fn.toArray(arguments); | ||
var functions = fn.toArray(arguments); | ||
return function () { | ||
return fn.reduce(function (args, func) { | ||
return [ fn.apply(func, args) ]; | ||
}, functions, fn.toArray(arguments))[0]; | ||
}; | ||
return function () { | ||
return fn.reduce(function (args, func) { | ||
return [ fn.apply(func, args) ]; | ||
}, functions, fn.toArray(arguments))[0]; | ||
}; | ||
}; | ||
fn.compose = function () { | ||
return fn.apply(fn.pipeline, fn.reverse(arguments)); | ||
return fn.apply(fn.pipeline, fn.reverse(arguments)); | ||
}; | ||
fn.prop = fn.curry(function (name, object) { | ||
return object[name]; | ||
}); | ||
return object[name]; | ||
}); | ||
fn.merge = function () { | ||
return fn.reduce(function (accumulator, value) { | ||
fn.each(function (property) { | ||
accumulator[property] = value[property]; | ||
}, fn.properties(value)); | ||
return accumulator; | ||
}, fn.toArray(arguments), {}); | ||
}; |
@@ -7,15 +7,15 @@ var fn = require('../build/fn'); | ||
var args = [ 1, 'string', true ]; | ||
var func = function (a, b, c) { | ||
return [ a, b, c ]; | ||
}; | ||
var args = [ 1, 'string', true ]; | ||
var func = function (a, b, c) { | ||
return [ a, b, c ]; | ||
}; | ||
it('should apply arguments to a function', function () { | ||
var result = fn.apply(func, args); | ||
it('should apply arguments to a function', function () { | ||
var result = fn.apply(func, args); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
}); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
}); | ||
}); |
@@ -7,13 +7,13 @@ var fn = require('../build/fn'); | ||
it('should clone an array', function () { | ||
var arr = [ 1, 'string', true ]; | ||
var clone = fn.cloneArray(arr); | ||
it('should clone an array', function () { | ||
var arr = [ 1, 'string', true ]; | ||
var clone = fn.cloneArray(arr); | ||
expect(arr).to.not.equal(clone); | ||
expect(clone).to.not.equal(arr); | ||
expect(arr[0]).to.equal(clone[0]); | ||
expect(arr[1]).to.equal(clone[1]); | ||
expect(arr[2]).to.equal(clone[2]); | ||
}); | ||
expect(arr).to.not.equal(clone); | ||
expect(clone).to.not.equal(arr); | ||
expect(arr[0]).to.equal(clone[0]); | ||
expect(arr[1]).to.equal(clone[1]); | ||
expect(arr[2]).to.equal(clone[2]); | ||
}); | ||
}); |
@@ -7,27 +7,27 @@ var fn = require('../build/fn'); | ||
it('should return a new function', function () { | ||
it('should return a new function', function () { | ||
var func = fn.compose( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
var func = fn.compose( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
expect(func).to.be.a('function'); | ||
}); | ||
expect(func).to.be.a('function'); | ||
}); | ||
it('should pass return values from right to left', function () { | ||
var func = fn.compose( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
it('should pass return values from right to left', function () { | ||
var func = fn.compose( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
var result = func(7); | ||
var result = func(7); | ||
expect(result).to.equal(297); | ||
}); | ||
expect(result).to.equal(297); | ||
}); | ||
}); |
@@ -7,42 +7,42 @@ var fn = require('../build/fn'); | ||
it('should combine two arrays into one', function () { | ||
var arr1 = [ 1, 'string', true ]; | ||
var arr2 = [ 2, 'newstring', false ]; | ||
it('should combine two arrays into one', function () { | ||
var arr1 = [ 1, 'string', true ]; | ||
var arr2 = [ 2, 'newstring', false ]; | ||
var result = fn.concat(arr1, arr2); | ||
var result = fn.concat(arr1, arr2); | ||
expect(result).to.have.length(6); | ||
expect(result).to.not.equal(arr1); | ||
expect(result).to.not.equal(arr2); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(2); | ||
expect(result[4]).to.equal('newstring'); | ||
expect(result[5]).to.equal(false); | ||
}); | ||
expect(result).to.have.length(6); | ||
expect(result).to.not.equal(arr1); | ||
expect(result).to.not.equal(arr2); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(2); | ||
expect(result[4]).to.equal('newstring'); | ||
expect(result[5]).to.equal(false); | ||
}); | ||
it('should combine three arrays into one', function () { | ||
var arr1 = [ 1, 'string', true ]; | ||
var arr2 = [ 2, 'newstring', false ]; | ||
var arr3 = [ 3, 'laststring', true ]; | ||
it('should combine three arrays into one', function () { | ||
var arr1 = [ 1, 'string', true ]; | ||
var arr2 = [ 2, 'newstring', false ]; | ||
var arr3 = [ 3, 'laststring', true ]; | ||
var result = fn.concat(arr1, arr2, arr3); | ||
var result = fn.concat(arr1, arr2, arr3); | ||
expect(result).to.have.length(9); | ||
expect(result).to.not.equal(arr1); | ||
expect(result).to.not.equal(arr2); | ||
expect(result).to.not.equal(arr3); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(2); | ||
expect(result[4]).to.equal('newstring'); | ||
expect(result[5]).to.equal(false); | ||
expect(result[6]).to.equal(3); | ||
expect(result[7]).to.equal('laststring'); | ||
expect(result[8]).to.equal(true); | ||
}); | ||
expect(result).to.have.length(9); | ||
expect(result).to.not.equal(arr1); | ||
expect(result).to.not.equal(arr2); | ||
expect(result).to.not.equal(arr3); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(2); | ||
expect(result[4]).to.equal('newstring'); | ||
expect(result[5]).to.equal(false); | ||
expect(result[6]).to.equal(3); | ||
expect(result[7]).to.equal('laststring'); | ||
expect(result[8]).to.equal(true); | ||
}); | ||
}); |
@@ -7,14 +7,14 @@ var fn = require('../build/fn'); | ||
it('should recursively apply arguments to a function', function () { | ||
var add = fn.curry(function (a, b, c) { | ||
return a + b + c; | ||
}); | ||
it('should recursively apply arguments to a function', function () { | ||
var add = fn.curry(function (a, b, c) { | ||
return a + b + c; | ||
}); | ||
var add37 = add(37); | ||
var add37 = add(37); | ||
expect(add37(2, 3)).to.equal(42); | ||
expect(add37(5)(9)).to.equal(51); | ||
expect(add37(10)()()()()()()(20)).to.equal(67); | ||
}); | ||
expect(add37(2, 3)).to.equal(42); | ||
expect(add37(5)(9)).to.equal(51); | ||
expect(add37(10)()()()()()()(20)).to.equal(67); | ||
}); | ||
}); |
@@ -7,60 +7,60 @@ var fn = require('../build/fn'); | ||
it('should iterate over all the elements in a collection', function () { | ||
var collection = [1, 'string', true]; | ||
var count = 0; | ||
it('should iterate over all the elements in a collection', function () { | ||
var collection = [1, 'string', true]; | ||
var count = 0; | ||
fn.each(function () { | ||
count++; | ||
return false; | ||
}, collection); | ||
fn.each(function () { | ||
count++; | ||
return false; | ||
}, collection); | ||
expect(count).to.equal(3); | ||
}); | ||
expect(count).to.equal(3); | ||
}); | ||
it('should be passed a value as the first argument', function () { | ||
var collection = [1, 'string', true]; | ||
var values = []; | ||
it('should be passed a value as the first argument', function () { | ||
var collection = [1, 'string', true]; | ||
var values = []; | ||
fn.each(function (value) { | ||
values.push(value); | ||
}, collection); | ||
fn.each(function (value) { | ||
values.push(value); | ||
}, collection); | ||
expect(values).to.have.length(3); | ||
expect(values[0]).to.equal(1); | ||
expect(values[1]).to.equal('string'); | ||
expect(values[2]).to.equal(true); | ||
}); | ||
expect(values).to.have.length(3); | ||
expect(values[0]).to.equal(1); | ||
expect(values[1]).to.equal('string'); | ||
expect(values[2]).to.equal(true); | ||
}); | ||
it('should be passed an index as the second argument', function () { | ||
var collection = [1, 'string', true]; | ||
var indices = []; | ||
it('should be passed an index as the second argument', function () { | ||
var collection = [1, 'string', true]; | ||
var indices = []; | ||
fn.each(function (value, index) { | ||
indices.push(index); | ||
}, collection); | ||
fn.each(function (value, index) { | ||
indices.push(index); | ||
}, collection); | ||
expect(indices).to.have.length(3); | ||
expect(indices[0]).to.equal(0); | ||
expect(indices[1]).to.equal(1); | ||
expect(indices[2]).to.equal(2); | ||
}); | ||
expect(indices).to.have.length(3); | ||
expect(indices[0]).to.equal(0); | ||
expect(indices[1]).to.equal(1); | ||
expect(indices[2]).to.equal(2); | ||
}); | ||
it('should be passed the collection as the third argument', function () { | ||
var collection = [1, 'string', true]; | ||
it('should be passed the collection as the third argument', function () { | ||
var collection = [1, 'string', true]; | ||
fn.each(function (value, index, copy) { | ||
expect(copy).to.equal(collection); | ||
}, collection); | ||
}); | ||
fn.each(function (value, index, copy) { | ||
expect(copy).to.equal(collection); | ||
}, collection); | ||
}); | ||
it('should apply additional arguments to the handler', function () { | ||
var collection = [1, 2, 3]; | ||
it('should apply additional arguments to the handler', function () { | ||
var collection = [1, 2, 3]; | ||
fn.each(function (value, index, collection, four, five, six) { | ||
expect(four).to.equal(4); | ||
expect(five).to.equal(5); | ||
expect(six).to.equal(6); | ||
}, collection, [4, 5, 6]); | ||
}); | ||
fn.each(function (value, index, collection, four, five, six) { | ||
expect(four).to.equal(4); | ||
expect(five).to.equal(5); | ||
expect(six).to.equal(6); | ||
}, collection, [4, 5, 6]); | ||
}); | ||
}); |
@@ -7,17 +7,17 @@ var fn = require('../build/fn'); | ||
it('should reduce the elements of an array based on an evaluating expression', function () { | ||
var collection = [1, 'string', true, undefined, [], {}, NaN]; | ||
it('should reduce the elements of an array based on an evaluating expression', function () { | ||
var collection = [1, 'string', true, undefined, [], {}, NaN]; | ||
var result = fn.filter(function (value) { | ||
return !!value; | ||
}, collection); | ||
var result = fn.filter(function (value) { | ||
return !!value; | ||
}, collection); | ||
expect(result).to.have.length(5); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.be.true; | ||
expect(result[3]).to.be.an('array'); | ||
expect(result[4]).to.be.an('object'); | ||
}); | ||
expect(result).to.have.length(5); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.be.true; | ||
expect(result[3]).to.be.an('array'); | ||
expect(result[4]).to.be.an('object'); | ||
}); | ||
}); |
120
tests/is.js
@@ -7,82 +7,82 @@ var fn = require('../build/fn'); | ||
it('should match a string to a string', function () { | ||
expect( fn.is('string', 'string') ).to.be.true; | ||
}); | ||
it('should match a string to a string', function () { | ||
expect( fn.is('string', 'string') ).to.be.true; | ||
}); | ||
it('should not match a string to a number', function () { | ||
expect( fn.is('string', 'number') ).to.be.false; | ||
}); | ||
it('should not match a string to a number', function () { | ||
expect( fn.is('string', 'number') ).to.be.false; | ||
}); | ||
it('should match a number to a number', function () { | ||
expect( fn.is(123, 'number') ).to.be.true; | ||
}); | ||
it('should match a number to a number', function () { | ||
expect( fn.is(123, 'number') ).to.be.true; | ||
}); | ||
it('should not match a number to a string', function () { | ||
expect( fn.is(123, 'string') ).to.be.false; | ||
}); | ||
it('should not match a number to a string', function () { | ||
expect( fn.is(123, 'string') ).to.be.false; | ||
}); | ||
it('should match true to a boolean', function () { | ||
expect( fn.is(true, 'boolean') ).to.be.true; | ||
}); | ||
it('should match true to a boolean', function () { | ||
expect( fn.is(true, 'boolean') ).to.be.true; | ||
}); | ||
it('should not match true to a string', function () { | ||
expect( fn.is(true, 'string') ).to.be.false; | ||
}); | ||
it('should not match true to a string', function () { | ||
expect( fn.is(true, 'string') ).to.be.false; | ||
}); | ||
it('should match an array to array', function () { | ||
expect( fn.is([], 'array') ).to.be.true; | ||
}); | ||
it('should match an array to array', function () { | ||
expect( fn.is([], 'array') ).to.be.true; | ||
}); | ||
it('should not match an array to object', function () { | ||
expect( fn.is([], 'object') ).to.be.false; | ||
}); | ||
it('should not match an array to object', function () { | ||
expect( fn.is([], 'object') ).to.be.false; | ||
}); | ||
it('should match an object to object', function () { | ||
expect( fn.is({}, 'object') ).to.be.true; | ||
}); | ||
it('should match an object to object', function () { | ||
expect( fn.is({}, 'object') ).to.be.true; | ||
}); | ||
it('should not match an object to array', function () { | ||
expect( fn.is({}, 'array') ).to.be.false; | ||
}); | ||
it('should not match an object to array', function () { | ||
expect( fn.is({}, 'array') ).to.be.false; | ||
}); | ||
it('should match a Date to date', function () { | ||
expect( fn.is(new Date(), 'date') ).to.be.true; | ||
}); | ||
it('should match a Date to date', function () { | ||
expect( fn.is(new Date(), 'date') ).to.be.true; | ||
}); | ||
it('should not match a Date to number', function () { | ||
expect( fn.is(new Date(), 'number') ).to.be.false; | ||
}); | ||
it('should not match a Date to number', function () { | ||
expect( fn.is(new Date(), 'number') ).to.be.false; | ||
}); | ||
it('should match undefined to undefined', function () { | ||
expect( fn.is(undefined, 'undefined') ).to.be.true; | ||
}); | ||
it('should match undefined to undefined', function () { | ||
expect( fn.is(undefined, 'undefined') ).to.be.true; | ||
}); | ||
it('should not match undefined to null', function () { | ||
expect( fn.is(undefined, 'null') ).to.be.false; | ||
}); | ||
it('should not match undefined to null', function () { | ||
expect( fn.is(undefined, 'null') ).to.be.false; | ||
}); | ||
it('should match null to null', function () { | ||
expect( fn.is(null, 'null') ).to.be.true; | ||
}); | ||
it('should match null to null', function () { | ||
expect( fn.is(null, 'null') ).to.be.true; | ||
}); | ||
it('should not match null to undefined', function () { | ||
expect( fn.is(null, 'undefined') ).to.be.false; | ||
}); | ||
it('should not match null to undefined', function () { | ||
expect( fn.is(null, 'undefined') ).to.be.false; | ||
}); | ||
it('should match function to function', function () { | ||
expect( fn.is(function () {}, 'function') ).to.be.true; | ||
}); | ||
it('should match function to function', function () { | ||
expect( fn.is(function () {}, 'function') ).to.be.true; | ||
}); | ||
it('should not match function to object', function () { | ||
expect( fn.is(function () {}, 'object') ).to.be.false; | ||
}); | ||
it('should not match function to object', function () { | ||
expect( fn.is(function () {}, 'object') ).to.be.false; | ||
}); | ||
it('should match a Regex to regexp', function () { | ||
expect( fn.is(new RegExp(), 'regexp') ).to.be.true; | ||
}); | ||
it('should match a Regex to regexp', function () { | ||
expect( fn.is(new RegExp(), 'regexp') ).to.be.true; | ||
}); | ||
it('should not match a Regex to object', function () { | ||
expect( fn.is(new RegExp(), 'object') ).to.be.false; | ||
}); | ||
it('should not match a Regex to object', function () { | ||
expect( fn.is(new RegExp(), 'object') ).to.be.false; | ||
}); | ||
}); |
@@ -7,34 +7,34 @@ var fn = require('../build/fn'); | ||
it('should return a new array of values', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
it('should return a new array of values', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
var result = fn.map(function (value) { | ||
return value * 2; | ||
}, values); | ||
var result = fn.map(function (value) { | ||
return value * 2; | ||
}, values); | ||
expect(result).to.have.length(9); | ||
expect(result[0]).to.equal(2); | ||
expect(result[1]).to.equal(4); | ||
expect(result[2]).to.equal(6); | ||
expect(result[3]).to.equal(8); | ||
expect(result[4]).to.equal(10); | ||
expect(result[5]).to.equal(12); | ||
expect(result[6]).to.equal(14); | ||
expect(result[7]).to.equal(16); | ||
expect(result[8]).to.equal(18); | ||
}); | ||
expect(result).to.have.length(9); | ||
expect(result[0]).to.equal(2); | ||
expect(result[1]).to.equal(4); | ||
expect(result[2]).to.equal(6); | ||
expect(result[3]).to.equal(8); | ||
expect(result[4]).to.equal(10); | ||
expect(result[5]).to.equal(12); | ||
expect(result[6]).to.equal(14); | ||
expect(result[7]).to.equal(16); | ||
expect(result[8]).to.equal(18); | ||
}); | ||
it('should receive an index', function () { | ||
var values = [ 1, 2, 3 ]; | ||
it('should receive an index', function () { | ||
var values = [ 1, 2, 3 ]; | ||
var result = fn.map(function (value, index) { | ||
return index; | ||
}, values); | ||
var result = fn.map(function (value, index) { | ||
return index; | ||
}, values); | ||
expect(result).to.have.length(3); | ||
expect(result[0]).to.equal(0); | ||
expect(result[1]).to.equal(1); | ||
expect(result[2]).to.equal(2); | ||
}); | ||
expect(result).to.have.length(3); | ||
expect(result[0]).to.equal(0); | ||
expect(result[1]).to.equal(1); | ||
expect(result[2]).to.equal(2); | ||
}); | ||
}); |
118
tests/op.js
@@ -7,70 +7,70 @@ var fn = require('../build/fn'); | ||
describe('+', function () { | ||
it('should add two numbers', function () { | ||
expect( fn.op['+'](1, 2) ).to.equal(3); | ||
}); | ||
describe('+', function () { | ||
it('should add two numbers', function () { | ||
expect( fn.op['+'](1, 2) ).to.equal(3); | ||
}); | ||
it('should concatenate two strings', function () { | ||
expect( fn.op['+']('hello', 'world') ).to.equal('helloworld'); | ||
}); | ||
}); | ||
it('should concatenate two strings', function () { | ||
expect( fn.op['+']('hello', 'world') ).to.equal('helloworld'); | ||
}); | ||
}); | ||
describe('-', function () { | ||
it('should subtract two numbers', function () { | ||
expect( fn.op['-'](3, 2) ).to.equal(1); | ||
}); | ||
}); | ||
describe('-', function () { | ||
it('should subtract two numbers', function () { | ||
expect( fn.op['-'](3, 2) ).to.equal(1); | ||
}); | ||
}); | ||
describe('*', function () { | ||
it('should multiply two numbers', function () { | ||
expect( fn.op['*'](3, 2) ).to.equal(6); | ||
}); | ||
}); | ||
describe('*', function () { | ||
it('should multiply two numbers', function () { | ||
expect( fn.op['*'](3, 2) ).to.equal(6); | ||
}); | ||
}); | ||
describe('/', function () { | ||
it('should divide two numbers', function () { | ||
expect( fn.op['/'](6, 2) ).to.equal(3); | ||
}); | ||
}); | ||
describe('/', function () { | ||
it('should divide two numbers', function () { | ||
expect( fn.op['/'](6, 2) ).to.equal(3); | ||
}); | ||
}); | ||
describe('==', function () { | ||
it('should weakly compare two values', function () { | ||
expect( fn.op['=='](3, 3) ).to.be.true; | ||
expect( fn.op['=='](false, 0) ).to.be.true; | ||
expect( fn.op['=='](false, 1) ).to.be.false; | ||
expect( fn.op['=='](null, undefined) ).to.be.true; | ||
expect( fn.op['=='](1/0, Infinity) ).to.be.true; | ||
expect( fn.op['==']('hello', 'hello') ).to.be.true; | ||
expect( fn.op['==']({}, {}) ).to.be.false; | ||
}); | ||
}); | ||
describe('==', function () { | ||
it('should weakly compare two values', function () { | ||
expect( fn.op['=='](3, 3) ).to.be.true; | ||
expect( fn.op['=='](false, 0) ).to.be.true; | ||
expect( fn.op['=='](false, 1) ).to.be.false; | ||
expect( fn.op['=='](null, undefined) ).to.be.true; | ||
expect( fn.op['=='](1/0, Infinity) ).to.be.true; | ||
expect( fn.op['==']('hello', 'hello') ).to.be.true; | ||
expect( fn.op['==']({}, {}) ).to.be.false; | ||
}); | ||
}); | ||
describe('===', function () { | ||
it('should strongly compare two values', function () { | ||
expect( fn.op['==='](3, 3) ).to.be.true; | ||
expect( fn.op['==='](false, 0) ).to.be.false; | ||
expect( fn.op['==='](false, 1) ).to.be.false; | ||
expect( fn.op['==='](null, undefined) ).to.be.false; | ||
expect( fn.op['==='](1/0, Infinity) ).to.be.true; | ||
expect( fn.op['===']('hello', 'hello') ).to.be.true; | ||
expect( fn.op['===']({}, {}) ).to.be.false; | ||
}); | ||
}); | ||
describe('===', function () { | ||
it('should strongly compare two values', function () { | ||
expect( fn.op['==='](3, 3) ).to.be.true; | ||
expect( fn.op['==='](false, 0) ).to.be.false; | ||
expect( fn.op['==='](false, 1) ).to.be.false; | ||
expect( fn.op['==='](null, undefined) ).to.be.false; | ||
expect( fn.op['==='](1/0, Infinity) ).to.be.true; | ||
expect( fn.op['===']('hello', 'hello') ).to.be.true; | ||
expect( fn.op['===']({}, {}) ).to.be.false; | ||
}); | ||
}); | ||
describe('++', function () { | ||
it('should increment a number', function () { | ||
expect( fn.op['++'](6) ).to.equal(7); | ||
expect( fn.op['++'](10) ).to.equal(11); | ||
expect( fn.op['++'](0) ).to.equal(1); | ||
}); | ||
}); | ||
describe('++', function () { | ||
it('should increment a number', function () { | ||
expect( fn.op['++'](6) ).to.equal(7); | ||
expect( fn.op['++'](10) ).to.equal(11); | ||
expect( fn.op['++'](0) ).to.equal(1); | ||
}); | ||
}); | ||
describe('--', function () { | ||
it('should decrement a number', function () { | ||
expect( fn.op['--'](6) ).to.equal(5); | ||
expect( fn.op['--'](10) ).to.equal(9); | ||
expect( fn.op['--'](0) ).to.equal(-1); | ||
}); | ||
}); | ||
describe('--', function () { | ||
it('should decrement a number', function () { | ||
expect( fn.op['--'](6) ).to.equal(5); | ||
expect( fn.op['--'](10) ).to.equal(9); | ||
expect( fn.op['--'](0) ).to.equal(-1); | ||
}); | ||
}); | ||
}); |
@@ -7,18 +7,18 @@ var fn = require('../build/fn'); | ||
it('should partially apply arguments to a function', function () { | ||
var func = function (a, b, c, d, e) { | ||
return [ a, b, c, d, e ]; | ||
}; | ||
it('should partially apply arguments to a function', function () { | ||
var func = function (a, b, c, d, e) { | ||
return [ a, b, c, d, e ]; | ||
}; | ||
var partialFunc = fn.partial(func, 1, 'string', true); | ||
var result = partialFunc(3, false); | ||
var partialFunc = fn.partial(func, 1, 'string', true); | ||
var result = partialFunc(3, false); | ||
expect(result.length).to.equal(5); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(3); | ||
expect(result[4]).to.equal(false); | ||
}); | ||
expect(result.length).to.equal(5); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
expect(result[3]).to.equal(3); | ||
expect(result[4]).to.equal(false); | ||
}); | ||
}); |
@@ -7,27 +7,27 @@ var fn = require('../build/fn'); | ||
it('should return a new function', function () { | ||
it('should return a new function', function () { | ||
var func = fn.pipeline( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
var func = fn.pipeline( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
expect(func).to.be.a('function'); | ||
}); | ||
expect(func).to.be.a('function'); | ||
}); | ||
it('should pass return values from left to right', function () { | ||
var func = fn.pipeline( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
it('should pass return values from left to right', function () { | ||
var func = fn.pipeline( | ||
fn.partial( fn.op['+'], 3 ), | ||
fn.partial( fn.op['*'], 6 ), | ||
function (num) { | ||
return Math.pow(num, 2); | ||
}); | ||
var result = func(7); | ||
var result = func(7); | ||
expect(result).to.equal(3600); | ||
}); | ||
expect(result).to.equal(3600); | ||
}); | ||
}); |
@@ -7,34 +7,34 @@ var fn = require('../build/fn'); | ||
it('should extract a value from an object by property name', function () { | ||
var obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
}; | ||
it('should extract a value from an object by property name', function () { | ||
var obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3 | ||
}; | ||
expect(fn.prop('a', obj)).to.equal(1); | ||
expect(fn.prop('b', obj)).to.equal(2); | ||
expect(fn.prop('c', obj)).to.equal(3); | ||
}); | ||
expect(fn.prop('a', obj)).to.equal(1); | ||
expect(fn.prop('b', obj)).to.equal(2); | ||
expect(fn.prop('c', obj)).to.equal(3); | ||
}); | ||
it('should curry properties until object is received', function () { | ||
var obj1 = { | ||
a: 1 | ||
}; | ||
it('should curry properties until object is received', function () { | ||
var obj1 = { | ||
a: 1 | ||
}; | ||
var obj2 = { | ||
a: 10 | ||
}; | ||
var obj2 = { | ||
a: 10 | ||
}; | ||
var obj3 = { | ||
a: 100 | ||
}; | ||
var obj3 = { | ||
a: 100 | ||
}; | ||
var getA = fn.prop('a'); | ||
var getA = fn.prop('a'); | ||
expect(getA(obj1)).to.equal(1); | ||
expect(getA(obj2)).to.equal(10); | ||
expect(getA(obj3)).to.equal(100); | ||
}); | ||
expect(getA(obj1)).to.equal(1); | ||
expect(getA(obj2)).to.equal(10); | ||
expect(getA(obj3)).to.equal(100); | ||
}); | ||
}); |
@@ -7,52 +7,52 @@ var fn = require('../build/fn'); | ||
it('should fold an array of numbers into a single number', function () { | ||
var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
it('should fold an array of numbers into a single number', function () { | ||
var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
var result = fn.reduce(function (accumulator, value) { | ||
return accumulator + value; | ||
}, numbers, 0); | ||
var result = fn.reduce(function (accumulator, value) { | ||
return accumulator + value; | ||
}, numbers, 0); | ||
expect(result).to.equal(45); | ||
}); | ||
expect(result).to.equal(45); | ||
}); | ||
it('should fold an array of strings into a single string', function () { | ||
var strings = [ 'hello', 'world', 'foo', 'bar', 'baz' ]; | ||
it('should fold an array of strings into a single string', function () { | ||
var strings = [ 'hello', 'world', 'foo', 'bar', 'baz' ]; | ||
var result = fn.reduce(function (accumulator, value) { | ||
return accumulator + value; | ||
}, strings, ''); | ||
var result = fn.reduce(function (accumulator, value) { | ||
return accumulator + value; | ||
}, strings, ''); | ||
expect(result).to.equal('helloworldfoobarbaz'); | ||
}); | ||
expect(result).to.equal('helloworldfoobarbaz'); | ||
}); | ||
it('should fold an array of arrays into a single array', function () { | ||
var arrays = [ [1, 2], [3, 4], [5, 6] ]; | ||
it('should fold an array of arrays into a single array', function () { | ||
var arrays = [ [1, 2], [3, 4], [5, 6] ]; | ||
var result = fn.reduce(function (accumulator, value) { | ||
return fn.concat(accumulator, value); | ||
}, arrays, []); | ||
var result = fn.reduce(function (accumulator, value) { | ||
return fn.concat(accumulator, value); | ||
}, arrays, []); | ||
expect(result).to.have.length(6); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal(2); | ||
expect(result[2]).to.equal(3); | ||
expect(result[3]).to.equal(4); | ||
expect(result[4]).to.equal(5); | ||
expect(result[5]).to.equal(6); | ||
}); | ||
expect(result).to.have.length(6); | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal(2); | ||
expect(result[2]).to.equal(3); | ||
expect(result[3]).to.equal(4); | ||
expect(result[4]).to.equal(5); | ||
expect(result[5]).to.equal(6); | ||
}); | ||
it('should fold an array of objects into a single array', function () { | ||
var people = [ { name: 'Bill' }, { name: 'Jim' }, { name: 'Steve' } ]; | ||
it('should fold an array of objects into a single array', function () { | ||
var people = [ { name: 'Bill' }, { name: 'Jim' }, { name: 'Steve' } ]; | ||
var result = fn.reduce(function (accumulator, value) { | ||
accumulator.push(value.name); | ||
return accumulator; | ||
}, people, []); | ||
var result = fn.reduce(function (accumulator, value) { | ||
accumulator.push(value.name); | ||
return accumulator; | ||
}, people, []); | ||
expect(result).to.have.length(3); | ||
expect(result[0]).to.equal('Bill'); | ||
expect(result[1]).to.equal('Jim'); | ||
expect(result[2]).to.equal('Steve'); | ||
}); | ||
expect(result).to.have.length(3); | ||
expect(result[0]).to.equal('Bill'); | ||
expect(result[1]).to.equal('Jim'); | ||
expect(result[2]).to.equal('Steve'); | ||
}); | ||
}); |
@@ -7,27 +7,27 @@ var fn = require('../build/fn'); | ||
it('should contain an array of reversed values', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
it('should contain an array of reversed values', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
var result = fn.reverse(values); | ||
var result = fn.reverse(values); | ||
expect(result).to.have.length(9); | ||
expect(result[0]).to.equal(9); | ||
expect(result[1]).to.equal(8); | ||
expect(result[2]).to.equal(7); | ||
expect(result[3]).to.equal(6); | ||
expect(result[4]).to.equal(5); | ||
expect(result[5]).to.equal(4); | ||
expect(result[6]).to.equal(3); | ||
expect(result[7]).to.equal(2); | ||
expect(result[8]).to.equal(1); | ||
}); | ||
expect(result).to.have.length(9); | ||
expect(result[0]).to.equal(9); | ||
expect(result[1]).to.equal(8); | ||
expect(result[2]).to.equal(7); | ||
expect(result[3]).to.equal(6); | ||
expect(result[4]).to.equal(5); | ||
expect(result[5]).to.equal(4); | ||
expect(result[6]).to.equal(3); | ||
expect(result[7]).to.equal(2); | ||
expect(result[8]).to.equal(1); | ||
}); | ||
it('should not be the same array', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
it('should not be the same array', function () { | ||
var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; | ||
var result = fn.reverse(values); | ||
var result = fn.reverse(values); | ||
expect(result).to.not.equal(values); | ||
}); | ||
expect(result).to.not.equal(values); | ||
}); | ||
}); |
@@ -7,15 +7,15 @@ var fn = require('../build/fn'); | ||
var func = function () { | ||
return arguments; | ||
}; | ||
var func = function () { | ||
return arguments; | ||
}; | ||
it('should convert arguments to an array', function () { | ||
var result = fn.toArray( func(1, 'string', true) ); | ||
it('should convert arguments to an array', function () { | ||
var result = fn.toArray( func(1, 'string', true) ); | ||
expect( fn.is(result, 'array')).to.be.true; | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
}); | ||
expect( fn.is(result, 'array')).to.be.true; | ||
expect(result[0]).to.equal(1); | ||
expect(result[1]).to.equal('string'); | ||
expect(result[2]).to.equal(true); | ||
}); | ||
}); |
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
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
26
830
35805