Created
July 14, 2015 08:34
-
-
Save japboy/3a402fb3637c0475859c to your computer and use it in GitHub Desktop.
Study of Functional JavaScript
This file contains 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'; | |
import _ from 'underscore'; | |
export let splat = (func) => { | |
let x = (array) => func.apply(null, array); | |
return x; | |
}; | |
export let unsplat = (func) => { | |
let x = (...args) => func.call(null, args); | |
return x; | |
}; | |
export let existy = (x) => null != x; | |
export let truthy = (x) => (false !== x) && existy(x); | |
export let doWhen = (cond, action) => { | |
if (truthy(cond)) return action(); | |
else return undefined; | |
}; | |
export let executeIfHasField = (target, name) => { | |
return doWhen(existy(target[name]), () => _.result(target, name)); | |
}; | |
This file contains 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'; | |
import _ from 'underscore'; | |
import { assert } from 'chai'; | |
import * as fp from './fp'; | |
describe('Test for FP fundamental module', () => { | |
describe('splat', () => { | |
let x = (...args) => _.reduce(args, (memo, val) => memo + val); | |
let y = fp.splat(x); | |
it('should return a function', () => { | |
assert.typeOf(y, 'function'); | |
}); | |
it('should accept an array as multiple arguments', () => { | |
let a = x(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); | |
let b = y(_.range(0, 10)); | |
assert.equal(a, b); | |
}); | |
}); | |
describe('unsplat', () => { | |
let x = (args=[]) => _.reduce(args, (memo, val) => memo + val); | |
let y = fp.unsplat(x); | |
it('should return a function', () => { | |
assert.typeOf(y, 'function'); | |
}); | |
it('should accept multiple arguments as an array', () => { | |
let a = x(_.range(0, 10)); | |
let b = y(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); | |
assert.equal(a, b); | |
}); | |
}); | |
describe('existy', () => { | |
let x = fp.existy; | |
it('should be boolean', () => { | |
assert.typeOf(x(), 'boolean'); | |
}); | |
it('should return false if the value is non-existence', () => { | |
assert.equal(x(null), false); | |
assert.equal(x(undefined), false); | |
assert.equal(x({}.notHere), false); | |
assert.equal(x((function () {})()), false); | |
}); | |
it('should return true if the value is existence', () => { | |
assert.equal(x(Math.sqrt(-1)), true); // NaN | |
assert.equal(x(-1), true); | |
assert.equal(x(0), true); | |
assert.equal(x(1), true); | |
assert.equal(x(Infinity), true); | |
assert.equal(x('Hello.'), true); | |
assert.equal(x({}), true); | |
}); | |
}); | |
describe('truthy', () => { | |
let x = fp.truthy; | |
it('should be boolean', () => { | |
assert.typeOf(x(), 'boolean'); | |
}); | |
it('should return false if the value is not true', () => { | |
assert.equal(x(false), false); | |
assert.equal(x(undefined), false); | |
}); | |
it('should return true if the value is true', () => { | |
assert.equal(x(0), true); | |
assert.equal(x(''), true); | |
}); | |
}); | |
describe('doWhen', () => { | |
let x = fp.doWhen; | |
it('should return undefined if the condition is false', () => { | |
assert.equal(x(0 === 1, () => 'It is true!'), undefined); | |
}); | |
it('should return the result of action argument if the condition is true', () => { | |
assert.equal(x(1 === 1, () => 'It is true!'), 'It is true!'); | |
}); | |
}); | |
describe('executeIfHasField', () => { | |
let x = fp.executeIfHasField; | |
it('should return undefined if the target does not have the child with the name argument', () => { | |
assert.equal(x({}, 'notHere'), undefined); | |
}); | |
it('should return the result if the target has the child with the name argument', () => { | |
assert.equal(x([1, 2, 3], 'reverse').join(), '3,2,1'); | |
assert.equal(x({ foo: 42 }, 'foo'), 42); | |
}); | |
}); | |
}); |
This file contains 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
{ | |
"name": "fp", | |
"version": "0.0.1", | |
"description": "FP fundamental module", | |
"main": "fp.js", | |
"dependencies": { | |
"babel": "^5.6.14", | |
"underscore": "^1.8.3" | |
}, | |
"devDependencies": { | |
"babel": "^5.6.14", | |
"chai": "^3.0.0" | |
}, | |
"scripts": { | |
"test": "mocha --compilers js:babel/register ./*.test.js" | |
}, | |
"author": "Yu Inao", | |
"license": "UNLICENSE" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment