├── .gitattributes ├── src ├── internal │ ├── _add.js │ ├── _identity.js │ ├── _multiply.js │ ├── _quote.js │ ├── _nth.js │ ├── _has.js │ ├── _isTransformer.js │ ├── _complement.js │ ├── _createMapEntry.js │ ├── _append.js │ ├── _prepend.js │ ├── _contains.js │ ├── _forceReduced.js │ ├── _pluck.js │ ├── _assoc.js │ ├── _reduced.js │ ├── _xfBase.js │ ├── _dissoc.js │ ├── _map.js │ ├── _chain.js │ ├── _all.js │ ├── _any.js │ ├── _xchain.js │ ├── _isThenable.js │ ├── _forEach.js │ ├── _containsWith.js │ ├── _filter.js │ ├── _isInteger.js │ ├── _indexOf.js │ ├── _filterIndexed.js │ ├── _composeL.js │ ├── _eq.js │ ├── _createPartialApplicator.js │ ├── _assocPath.js │ ├── _cloneRegExp.js │ ├── _functionsWith.js │ ├── _xwrap.js │ ├── _dissocPath.js │ ├── _curry1.js │ ├── _isArray.js │ ├── _xmap.js │ ├── _createComposer.js │ ├── _xfilter.js │ ├── _xdrop.js │ ├── _xtakeWhile.js │ ├── _xtake.js │ ├── _xdropWhile.js │ ├── _xfindLast.js │ ├── _path.js │ ├── _toISOString.js │ ├── _concat.js │ ├── _makeFlat.js │ ├── _extend.js │ ├── _xfindLastIndex.js │ ├── _xall.js │ ├── _xany.js │ ├── _xfind.js │ ├── _hasMethod.js │ ├── _predicateWrap.js │ ├── _checkForMethod.js │ └── _xfindIndex.js ├── dec.js ├── inc.js ├── negate.js ├── T.js ├── F.js ├── toLower.js ├── toUpper.js ├── max.js ├── min.js ├── sum.js ├── mean.js ├── last.js ├── product.js ├── path.js ├── nthChar.js ├── init.js ├── head.js ├── nthArg.js ├── isNil.js ├── identity.js ├── length.js ├── lt.js ├── nthCharCode.js ├── prop.js ├── gt.js ├── add.js ├── maxBy.js ├── minBy.js ├── of.js ├── test.js ├── uniq.js ├── unnest.js ├── always.js ├── dissoc.js ├── lte.js ├── flatten.js ├── gte.js ├── tap.js ├── prepend.js ├── divide.js ├── not.js ├── nth.js ├── apply.js ├── pluck.js ├── mergeAll.js ├── multiply.js ├── union.js ├── reverse.js ├── split.js ├── createMapEntry.js ├── join.js ├── subtract.js ├── isEmpty.js ├── contains.js ├── tail.js ├── dissocPath.js ├── clone.js ├── range.js ├── defaultTo.js ├── __.js ├── functions.js ├── zipObj.js ├── median.js ├── repeat.js ├── assoc.js ├── eqProps.js ├── isSet.js ├── omit.js ├── comparator.js ├── append.js ├── fromPairs.js ├── intersection.js ├── values.js ├── modulo.js ├── or.js ├── none.js ├── ap.js ├── and.js ├── commute.js ├── keysIn.js ├── valuesIn.js ├── bind.js ├── containsWith.js ├── functionsIn.js ├── indexOf.js ├── merge.js ├── splitEvery.js ├── chain.js ├── complement.js ├── update.js ├── dropRepeats.js ├── is.js ├── toPairs.js ├── unapply.js ├── has.js ├── allPass.js ├── assocPath.js ├── construct.js ├── intersperse.js ├── once.js ├── hasIn.js ├── lensIndex.js ├── project.js ├── equals.js ├── match.js ├── replace.js ├── anyPass.js ├── pick.js ├── reduced.js ├── scan.js └── toPairsIn.js ├── .jshintignore ├── .travis.yml ├── CHANGELOG.md ├── lib ├── sauce │ ├── server.js │ ├── linux.js │ ├── browsers.js │ ├── ios.js │ ├── conf.js │ ├── apple.js │ ├── windows.js │ └── android.js ├── test │ ├── require-shim.js │ └── index.html └── bench │ ├── curry.bench.js │ ├── concat.bench.js │ ├── map.bench.js │ ├── each.bench.js │ ├── indexof.bench.js │ ├── find.bench.js │ ├── reduce.bench.js │ ├── filter.bench.js │ ├── maxWith.bench.js │ ├── findIndex.bench.js │ └── isset.bench.js ├── .gitignore ├── test ├── helpers │ └── listXf.js ├── product.js ├── toLower.js ├── toUpper.js ├── T.js ├── F.js ├── not.js ├── join.js ├── add.js ├── multiply.js ├── reverse.js ├── mapObj.js ├── minBy.js ├── prop.js ├── comparator.js ├── maxBy.js ├── createMapEntry.js ├── of.js ├── negate.js ├── split.js ├── identity.js ├── inc.js ├── dec.js ├── isNil.js ├── fromPairs.js ├── sum.js ├── nthChar.js ├── intersectionWith.js ├── unionWith.js ├── divide.js ├── match.js ├── subtract.js ├── nthCharCode.js ├── head.js ├── last.js ├── zip.js ├── repeat.js ├── binary.js ├── toPairs.js ├── unary.js ├── tail.js ├── append.js ├── max.js ├── min.js ├── prepend.js ├── flip.js ├── apply.js ├── pickAll.js ├── complement.js ├── mean.js ├── tap.js ├── test.js ├── mergeAll.js ├── unfold.js ├── internal │ └── _curry2.js ├── partition.js ├── times.js ├── partial.js ├── intersperse.js ├── shared │ └── internal │ │ └── util.js ├── containsWith.js ├── nthArg.js ├── partialRight.js ├── toPairsIn.js ├── omit.js ├── ap.js ├── init.js ├── insertAll.js ├── median.js ├── reduced.js ├── sort.js ├── functionsIn.js ├── pluck.js ├── replace.js ├── takeWhile.js ├── range.js ├── zipObj.js ├── zipWith.js ├── always.js ├── call.js ├── hasIn.js ├── functions.js ├── lt.js ├── xprod.js ├── gt.js ├── pipeK.js ├── gte.js ├── props.js ├── lte.js ├── composeK.js ├── isSet.js ├── aperture.js └── keysIn.js ├── .jshintrc ├── testem.json ├── .npmignore ├── scripts ├── doc-blob ├── template.js ├── BOOKMARKLET.tmpl.md ├── header ├── testfiles └── prepublish ├── .editorconfig ├── bower.json └── CONTRIBUTING.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js text eol=lf 2 | -------------------------------------------------------------------------------- /src/internal/_add.js: -------------------------------------------------------------------------------- 1 | module.exports = function _add(a, b) { return a + b; }; 2 | -------------------------------------------------------------------------------- /src/internal/_identity.js: -------------------------------------------------------------------------------- 1 | module.exports = function _identity(x) { return x; }; 2 | -------------------------------------------------------------------------------- /src/internal/_multiply.js: -------------------------------------------------------------------------------- 1 | module.exports = function _multiply(a, b) { return a * b; }; 2 | -------------------------------------------------------------------------------- /src/internal/_quote.js: -------------------------------------------------------------------------------- 1 | module.exports = function _quote(s) { 2 | return '"' + s.replace(/"/g, '\\"') + '"'; 3 | }; 4 | -------------------------------------------------------------------------------- /src/internal/_nth.js: -------------------------------------------------------------------------------- 1 | module.exports = function _nth(n, list) { 2 | return n < 0 ? list[list.length + n] : list[n]; 3 | }; 4 | -------------------------------------------------------------------------------- /src/internal/_has.js: -------------------------------------------------------------------------------- 1 | module.exports = function _has(prop, obj) { 2 | return Object.prototype.hasOwnProperty.call(obj, prop); 3 | }; 4 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | **/*.min.js 2 | test/index.html 3 | test/bundle.js 4 | dist/** 5 | lib/test/** 6 | node_modules/** 7 | bower_components/** 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - 'iojs-v1.5.1' 6 | before_script: 7 | - make clean && make dist/ramda.js 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | See the [upgrade guides][1]. 4 | 5 | 6 | [1]: https://github.com/ramda/ramda/issues?q=label%3A%22upgrade+guide%22 7 | -------------------------------------------------------------------------------- /src/internal/_isTransformer.js: -------------------------------------------------------------------------------- 1 | module.exports = function _isTransformer(obj) { 2 | return typeof obj['@@transducer/step'] === 'function'; 3 | }; 4 | -------------------------------------------------------------------------------- /src/internal/_complement.js: -------------------------------------------------------------------------------- 1 | module.exports = function _complement(f) { 2 | return function() { 3 | return !f.apply(this, arguments); 4 | }; 5 | }; 6 | -------------------------------------------------------------------------------- /src/internal/_createMapEntry.js: -------------------------------------------------------------------------------- 1 | module.exports = function _createMapEntry(key, val) { 2 | var obj = {}; 3 | obj[key] = val; 4 | return obj; 5 | }; 6 | -------------------------------------------------------------------------------- /src/internal/_append.js: -------------------------------------------------------------------------------- 1 | var _concat = require('./_concat'); 2 | 3 | 4 | module.exports = function _append(el, list) { 5 | return _concat(list, [el]); 6 | }; 7 | -------------------------------------------------------------------------------- /src/internal/_prepend.js: -------------------------------------------------------------------------------- 1 | var _concat = require('./_concat'); 2 | 3 | 4 | module.exports = function _prepend(el, list) { 5 | return _concat([el], list); 6 | }; 7 | -------------------------------------------------------------------------------- /src/internal/_contains.js: -------------------------------------------------------------------------------- 1 | var _indexOf = require('./_indexOf'); 2 | 3 | 4 | module.exports = function _contains(a, list) { 5 | return _indexOf(list, a, 0) >= 0; 6 | }; 7 | -------------------------------------------------------------------------------- /lib/sauce/server.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | server: { 3 | options: { 4 | hostname: 'localhost', 5 | port: 3210, 6 | base: '.' 7 | } 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /src/internal/_forceReduced.js: -------------------------------------------------------------------------------- 1 | module.exports = function _forceReduced(x) { 2 | return { 3 | '@@transducer/value': x, 4 | '@@transducer/reduced': true 5 | }; 6 | }; 7 | -------------------------------------------------------------------------------- /src/internal/_pluck.js: -------------------------------------------------------------------------------- 1 | var map = require('../map'); 2 | var prop = require('../prop'); 3 | 4 | 5 | module.exports = function _pluck(p, list) { 6 | return map(prop(p), list); 7 | }; 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /lib/bench/report/ 2 | /lib/docs/public/ 3 | /node_modules/ 4 | /index.js 5 | /npm-debug.log 6 | /ramda.js.tmp 7 | /lib/test/bundle.js 8 | /tmp 9 | /bower_components/ 10 | /dist/gh-pages/ 11 | -------------------------------------------------------------------------------- /src/internal/_assoc.js: -------------------------------------------------------------------------------- 1 | module.exports = function _assoc(prop, val, obj) { 2 | var result = {}; 3 | for (var p in obj) { 4 | result[p] = obj[p]; 5 | } 6 | result[prop] = val; 7 | return result; 8 | }; 9 | -------------------------------------------------------------------------------- /src/internal/_reduced.js: -------------------------------------------------------------------------------- 1 | module.exports = function _reduced(x) { 2 | return x && x['@@transducer/reduced'] ? x : 3 | { 4 | '@@transducer/value': x, 5 | '@@transducer/reduced': true 6 | }; 7 | }; 8 | -------------------------------------------------------------------------------- /src/internal/_xfBase.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | init: function() { 3 | return this.xf['@@transducer/init'](); 4 | }, 5 | result: function(result) { 6 | return this.xf['@@transducer/result'](result); 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /src/internal/_dissoc.js: -------------------------------------------------------------------------------- 1 | module.exports = function _dissoc(prop, obj) { 2 | var result = {}; 3 | for (var p in obj) { 4 | if (p !== prop) { 5 | result[p] = obj[p]; 6 | } 7 | } 8 | return result; 9 | }; 10 | -------------------------------------------------------------------------------- /src/internal/_map.js: -------------------------------------------------------------------------------- 1 | module.exports = function _map(fn, list) { 2 | var idx = 0, len = list.length, result = []; 3 | while (idx < len) { 4 | result[idx] = fn(list[idx]); 5 | idx += 1; 6 | } 7 | return result; 8 | }; 9 | -------------------------------------------------------------------------------- /test/helpers/listXf.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | '@@transducer/init': function() { return []; }, 3 | '@@transducer/step': function(acc, x) { return acc.concat([x]); }, 4 | '@@transducer/result': function(x) { return x; } 5 | }; 6 | -------------------------------------------------------------------------------- /src/internal/_chain.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var map = require('../map'); 3 | var unnest = require('../unnest'); 4 | 5 | 6 | module.exports = _curry2(function _chain(f, list) { 7 | return unnest(map(f, list)); 8 | }); 9 | -------------------------------------------------------------------------------- /src/internal/_all.js: -------------------------------------------------------------------------------- 1 | module.exports = function _all(fn, list) { 2 | var idx = 0; 3 | while (idx < list.length) { 4 | if (!fn(list[idx])) { 5 | return false; 6 | } 7 | idx += 1; 8 | } 9 | return true; 10 | }; 11 | -------------------------------------------------------------------------------- /src/internal/_any.js: -------------------------------------------------------------------------------- 1 | module.exports = function _any(fn, list) { 2 | var idx = 0; 3 | while (idx < list.length) { 4 | if (fn(list[idx])) { 5 | return true; 6 | } 7 | idx += 1; 8 | } 9 | return false; 10 | }; 11 | -------------------------------------------------------------------------------- /src/internal/_xchain.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _flatCat = require('./_flatCat'); 3 | var map = require('../map'); 4 | 5 | 6 | module.exports = _curry2(function _xchain(f, xf) { 7 | return map(f, _flatCat(xf)); 8 | }); 9 | -------------------------------------------------------------------------------- /lib/sauce/linux.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | { 3 | browserName: 'firefox', 4 | version: '33', 5 | platform: 'Linux' 6 | }, 7 | { 8 | browserName: 'chrome', 9 | version: '38', 10 | platform: 'Linux' 11 | } 12 | ]; 13 | -------------------------------------------------------------------------------- /src/internal/_isThenable.js: -------------------------------------------------------------------------------- 1 | var _hasMethod = require('./_hasMethod'); 2 | 3 | 4 | /** 5 | * Tests if a value is a thenable (promise). 6 | */ 7 | module.exports = function _isThenable(value) { 8 | return _hasMethod('then', value); 9 | }; 10 | -------------------------------------------------------------------------------- /src/internal/_forEach.js: -------------------------------------------------------------------------------- 1 | module.exports = function _forEach(fn, list) { 2 | var idx = 0, len = list.length; 3 | while (idx < len) { 4 | fn(list[idx]); 5 | idx += 1; 6 | } 7 | // i can't bear not to return *something* 8 | return list; 9 | }; 10 | -------------------------------------------------------------------------------- /lib/sauce/browsers.js: -------------------------------------------------------------------------------- 1 | var windows = require('./windows'); 2 | var apple = require('./apple'); 3 | var linux = require('./linux'); 4 | var android = require('./android'); 5 | var ios = require('./ios'); 6 | 7 | module.exports = windows.concat(apple, android, ios, linux); 8 | -------------------------------------------------------------------------------- /src/internal/_containsWith.js: -------------------------------------------------------------------------------- 1 | module.exports = function _containsWith(pred, x, list) { 2 | var idx = 0, len = list.length; 3 | while (idx < len) { 4 | if (pred(x, list[idx])) { 5 | return true; 6 | } 7 | idx += 1; 8 | } 9 | return false; 10 | }; 11 | -------------------------------------------------------------------------------- /test/product.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('product', function() { 7 | it('multiplies together the array of numbers supplied', function() { 8 | assert.strictEqual(R.product([1, 2, 3, 4]), 24); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/toLower.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('toLower', function() { 7 | it('returns the lower-case equivalent of the input string', function() { 8 | assert.strictEqual(R.toLower('XYZ'), 'xyz'); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/toUpper.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('toUpper', function() { 7 | it('returns the upper-case equivalent of the input string', function() { 8 | assert.strictEqual(R.toUpper('abc'), 'ABC'); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/internal/_filter.js: -------------------------------------------------------------------------------- 1 | module.exports = function _filter(fn, list) { 2 | var idx = 0, len = list.length, result = []; 3 | while (idx < len) { 4 | if (fn(list[idx])) { 5 | result[result.length] = list[idx]; 6 | } 7 | idx += 1; 8 | } 9 | return result; 10 | }; 11 | -------------------------------------------------------------------------------- /src/internal/_isInteger.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Determine if the passed argument is an integer. 3 | * 4 | * @private 5 | * @param {*} n 6 | * @category Type 7 | * @return {Boolean} 8 | */ 9 | module.exports = Number.isInteger || function _isInteger(n) { 10 | return (n << 0) === n; 11 | }; 12 | -------------------------------------------------------------------------------- /lib/test/require-shim.js: -------------------------------------------------------------------------------- 1 | function require(path) { 2 | switch (path.substr(path.lastIndexOf('/') + 1)) { 3 | case 'assert': 4 | return assert; 5 | case '..': 6 | return R; 7 | default: 8 | throw new Error('Unexpected require path "' + path + '"'); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/internal/_indexOf.js: -------------------------------------------------------------------------------- 1 | var equals = require('../equals'); 2 | 3 | 4 | module.exports = function _indexOf(list, item, from) { 5 | var idx = from; 6 | while (idx < list.length) { 7 | if (equals(list[idx], item)) { 8 | return idx; 9 | } 10 | idx += 1; 11 | } 12 | return -1; 13 | }; 14 | -------------------------------------------------------------------------------- /test/T.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('T', function() { 7 | it('always returns true', function() { 8 | assert.strictEqual(R.T(), true); 9 | assert.strictEqual(R.T(10), true); 10 | assert.strictEqual(R.T(true), true); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/internal/_filterIndexed.js: -------------------------------------------------------------------------------- 1 | module.exports = function _filterIndexed(fn, list) { 2 | var idx = 0, len = list.length, result = []; 3 | while (idx < len) { 4 | if (fn(list[idx], idx, list)) { 5 | result[result.length] = list[idx]; 6 | } 7 | idx += 1; 8 | } 9 | return result; 10 | }; 11 | -------------------------------------------------------------------------------- /test/F.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('F', function() { 7 | it('always returns false', function() { 8 | assert.strictEqual(R.F(), false); 9 | assert.strictEqual(R.F(10), false); 10 | assert.strictEqual(R.F(true), false); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /test/not.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('not', function() { 7 | it('reverses argument', function() { 8 | assert.strictEqual(R.not(false), true); 9 | assert.strictEqual(R.not(1), false); 10 | assert.strictEqual(R.not(''), true); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "evil": true, 3 | "eqeqeq": true, 4 | "eqnull": true, 5 | "globals": { 6 | "Symbol": true 7 | }, 8 | "newcap": false, 9 | "plusplus": true, 10 | "predef": ["beforeEach", "console", "define", "describe", "it", "module", "process", "require"], 11 | "undef": true, 12 | "unused": true 13 | } 14 | -------------------------------------------------------------------------------- /src/dec.js: -------------------------------------------------------------------------------- 1 | var add = require('./add'); 2 | 3 | 4 | /** 5 | * Decrements its argument. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Math 10 | * @sig Number -> Number 11 | * @param {Number} n 12 | * @return {Number} 13 | * @example 14 | * 15 | * R.dec(42); //=> 41 16 | */ 17 | module.exports = add(-1); 18 | -------------------------------------------------------------------------------- /src/inc.js: -------------------------------------------------------------------------------- 1 | var add = require('./add'); 2 | 3 | 4 | /** 5 | * Increments its argument. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Math 10 | * @sig Number -> Number 11 | * @param {Number} n 12 | * @return {Number} 13 | * @example 14 | * 15 | * R.inc(42); //=> 43 16 | */ 17 | module.exports = add(1); 18 | -------------------------------------------------------------------------------- /testem.json: -------------------------------------------------------------------------------- 1 | { 2 | "framework": "mocha", 3 | "cwd": ".", 4 | "src_files": [ 5 | "ramda.js", 6 | "test/*.js" 7 | ], 8 | "serve_files": [ 9 | "lib/test/require-shim.js", 10 | "ramda.js", 11 | "lib/test/expect.js", 12 | "lib/test/assert-shim.js", 13 | "test/*.js" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /lib/sauce/ios.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | { 3 | browserName: 'ipad', 4 | version: '8.1', 5 | 'device-orientation': 'portrait', 6 | platform: 'OS X 10.9' 7 | }, 8 | { 9 | browserName: 'ipad', 10 | version: '4.3', 11 | 'device-orientation': 'portrait', 12 | platform: 'OS X 10.6' 13 | } 14 | ]; 15 | -------------------------------------------------------------------------------- /test/join.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('join', function() { 7 | it("concatenates a list's elements to a string, with an seperator string between elements", function() { 8 | var list = [1, 2, 3, 4]; 9 | assert.strictEqual(R.join('~', list), '1~2~3~4'); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /lib/sauce/conf.js: -------------------------------------------------------------------------------- 1 | var envvar = require('envvar'); 2 | var browsers = require('./browsers'); 3 | module.exports = { 4 | all: { 5 | options: { 6 | urls: ['localhost:3210/test/index.html'], 7 | build: envvar.number('CI_BUILD_NUMBER', 0), 8 | testname: 'Ramda Sauce Unit Test', 9 | browsers: browsers 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components/ 2 | /dist/gh-pages/ 3 | /lib/bench/ 4 | /lib/doc/ 5 | /lib/sauce/ 6 | /scripts/ 7 | **/test/ 8 | /.editorconfig 9 | /.gitattributes 10 | /.npmignore 11 | /.travis.yml 12 | /.jscsrc 13 | /.jshintrc 14 | /CONTRIBUTING.md 15 | /Makefile 16 | /bower.json 17 | /index.js 18 | /npm-debug.log 19 | /ramda.js.tmp 20 | /testem.json 21 | /tmp 22 | -------------------------------------------------------------------------------- /test/add.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('add', function() { 7 | it('adds together two numbers', function() { 8 | assert.strictEqual(R.add(3, 7), 10); 9 | }); 10 | 11 | it('is curried', function() { 12 | var incr = R.add(1); 13 | assert.strictEqual(incr(42), 43); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /scripts/doc-blob: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'); 4 | var YAML = require('js-yaml'); 5 | 6 | var DOC_FILE = 'doc.yaml'; 7 | var DATA_FILE = 'docs/data.json'; 8 | 9 | var text = fs.readFileSync(DOC_FILE, 'utf-8'); 10 | var docs = YAML.safeLoad(text); 11 | var data = JSON.stringify(docs, null, 4); 12 | 13 | fs.writeFileSync(DATA_FILE, data, 'utf-8'); 14 | -------------------------------------------------------------------------------- /scripts/template.js: -------------------------------------------------------------------------------- 1 | ;(function() { 2 | 3 | 'use strict'; 4 | 5 | /* global R */ 6 | 7 | /* TEST_ENTRY_POINT */ 8 | 9 | if (typeof exports === 'object') { 10 | module.exports = R; 11 | } else if (typeof define === 'function' && define.amd) { 12 | define(function() { return R; }); 13 | } else { 14 | this.R = R; 15 | } 16 | 17 | }.call(this)); 18 | -------------------------------------------------------------------------------- /src/internal/_composeL.js: -------------------------------------------------------------------------------- 1 | var _compose = require('./_compose'); 2 | var lens = require('../lens'); 3 | 4 | module.exports = function _composeL(innerLens, outerLens) { 5 | return lens(_compose(innerLens, outerLens), function(x, source) { 6 | var newInnerValue = innerLens.set(x, outerLens(source)); 7 | return outerLens.set(newInnerValue, source); 8 | }); 9 | }; 10 | -------------------------------------------------------------------------------- /test/multiply.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('multiply', function() { 7 | it('adds together two numbers', function() { 8 | assert.strictEqual(R.multiply(6, 7), 42); 9 | }); 10 | 11 | it('is curried', function() { 12 | var dbl = R.multiply(2); 13 | assert.strictEqual(dbl(15), 30); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /test/reverse.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('reverse', function() { 7 | it('reverses arrays', function() { 8 | assert.deepEqual(R.reverse([1, 2, 3, 4]), [4, 3, 2, 1]); 9 | }); 10 | 11 | it('returns the empty list to itself', function() { 12 | assert.deepEqual(R.reverse([]), []); 13 | }); 14 | 15 | }); 16 | -------------------------------------------------------------------------------- /scripts/BOOKMARKLET.tmpl.md: -------------------------------------------------------------------------------- 1 | # Ramda Bookmarklet 2 | 3 | Use this bookmarklet to load Ramda on any website. To "install": 4 | 5 | 1. Create a new bookmark in your bookmarks bar (what it points to doesn't 6 | matter, any page) 7 | 8 | 2. Edit the new bookmark and replace the `URL` with the following: 9 | 10 | ```javascript 11 | javascript:{{{bookmarklet}}} 12 | ``` 13 | -------------------------------------------------------------------------------- /src/negate.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Negates its argument. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Math 10 | * @sig Number -> Number 11 | * @param {Number} n 12 | * @return {Number} 13 | * @example 14 | * 15 | * R.negate(42); //=> -42 16 | */ 17 | module.exports = _curry1(function negate(n) { return -n; }); 18 | -------------------------------------------------------------------------------- /scripts/header: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | cat < true 11 | * @see R.always 12 | * @return {Boolean} `true`. 13 | * @example 14 | * 15 | * R.T(); //=> true 16 | */ 17 | module.exports = always(true); 18 | -------------------------------------------------------------------------------- /src/F.js: -------------------------------------------------------------------------------- 1 | var always = require('./always'); 2 | 3 | 4 | /** 5 | * A function that always returns `false`. Any passed in parameters are ignored. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Function 10 | * @sig * -> false 11 | * @see R.always 12 | * @return {Boolean} false 13 | * @example 14 | * 15 | * R.F(); //=> false 16 | */ 17 | module.exports = always(false); 18 | -------------------------------------------------------------------------------- /test/mapObj.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('mapObj', function() { 7 | var square = function(n) {return n * n;}; 8 | 9 | it('runs the given function over each of the object properties', function() { 10 | var obj = {a: 1, b: 2, c: 3}; 11 | assert.deepEqual(R.mapObj(square, obj), {a: 1, b: 4, c: 9}); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/minBy.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('minBy', function() { 7 | 8 | it('returns the smaller value as determined by the function', function() { 9 | assert.strictEqual(R.minBy(function(n) { return n * n; }, -3, 2), 2); 10 | assert.deepEqual(R.minBy(R.prop('x'), {x: 3, y: 1}, {x: 5, y: 10}), {x: 3, y: 1}); 11 | }); 12 | 13 | }); 14 | -------------------------------------------------------------------------------- /test/prop.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('prop', function() { 7 | var fred = {name: 'Fred', age: 23}; 8 | 9 | it('returns a function that fetches the appropriate property', function() { 10 | var nm = R.prop('name'); 11 | assert.strictEqual(typeof nm, 'function'); 12 | assert.strictEqual(nm(fred), 'Fred'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /test/comparator.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('comparator', function() { 7 | it('builds a comparator function for sorting out of a simple predicate that reports whether the first param is smaller', function() { 8 | assert.deepEqual([3, 1, 8, 1, 2, 5].sort(R.comparator(function(a, b) {return a < b;})), [1, 1, 2, 3, 5, 8]); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/maxBy.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('maxBy', function() { 7 | 8 | it('returns the larger value as determined by the function', function() { 9 | assert.strictEqual(R.maxBy(function(n) { return n * n; }, -3, 2), -3); 10 | assert.deepEqual(R.maxBy(R.prop('x'), {x: 3, y: 1}, {x: 5, y: 10}), {x: 5, y: 10}); 11 | }); 12 | 13 | }); 14 | -------------------------------------------------------------------------------- /src/internal/_eq.js: -------------------------------------------------------------------------------- 1 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Polyfill 2 | module.exports = function _eq(x, y) { 3 | // SameValue algorithm 4 | if (x === y) { // Steps 1-5, 7-10 5 | // Steps 6.b-6.e: +0 != -0 6 | return x !== 0 || 1 / x === 1 / y; 7 | } else { 8 | // Step 6.a: NaN == NaN 9 | return x !== x && y !== y; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /src/internal/_createPartialApplicator.js: -------------------------------------------------------------------------------- 1 | var _arity = require('./_arity'); 2 | var _slice = require('./_slice'); 3 | 4 | 5 | module.exports = function _createPartialApplicator(concat) { 6 | return function(fn) { 7 | var args = _slice(arguments, 1); 8 | return _arity(Math.max(0, fn.length - args.length), function() { 9 | return fn.apply(this, concat(args, arguments)); 10 | }); 11 | }; 12 | }; 13 | -------------------------------------------------------------------------------- /scripts/testfiles: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | var fs = require('fs'); 6 | var path = require('path'); 7 | var R = require('ramda'); 8 | 9 | 10 | R.pipe( 11 | fs.readdirSync, 12 | R.filter(R.test(/[.]js$/)), 13 | R.reject(R.eq('bundle.js')), 14 | R.reject(R.eq('test.examplesRunner.js')), 15 | R.map(R.partial(path.resolve, 'test')), 16 | R.join('\n'), 17 | console.log 18 | )('test'); 19 | -------------------------------------------------------------------------------- /test/createMapEntry.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('createMapEntry', function() { 7 | it('creates an object containing a single key:value pair', function() { 8 | assert.deepEqual(R.createMapEntry('foo', 42), {foo: 42}); 9 | }); 10 | 11 | it('is curried', function() { 12 | assert.deepEqual(R.createMapEntry('foo')(42), {foo: 42}); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/internal/_assocPath.js: -------------------------------------------------------------------------------- 1 | var _assoc = require('./_assoc'); 2 | var _slice = require('./_slice'); 3 | 4 | 5 | module.exports = function _assocPath(path, val, obj) { 6 | switch (path.length) { 7 | case 0: 8 | return obj; 9 | case 1: 10 | return _assoc(path[0], val, obj); 11 | default: 12 | return _assoc(path[0], _assocPath(_slice(path, 1), val, Object(obj[path[0]])), obj); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /test/of.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('of', function() { 7 | it('returns its argument as an Array', function() { 8 | assert.deepEqual(R.of(100), [100]); 9 | assert.deepEqual(R.of([100]), [[100]]); 10 | assert.deepEqual(R.of(null), [null]); 11 | assert.deepEqual(R.of(undefined), [undefined]); 12 | assert.deepEqual(R.of([]), [[]]); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/toLower.js: -------------------------------------------------------------------------------- 1 | var invoker = require('./invoker'); 2 | 3 | 4 | /** 5 | * The lower case version of a string. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category String 10 | * @sig String -> String 11 | * @param {String} str The string to lower case. 12 | * @return {String} The lower case version of `str`. 13 | * @example 14 | * 15 | * R.toLower('XYZ'); //=> 'xyz' 16 | */ 17 | module.exports = invoker(0, 'toLowerCase'); 18 | -------------------------------------------------------------------------------- /src/toUpper.js: -------------------------------------------------------------------------------- 1 | var invoker = require('./invoker'); 2 | 3 | 4 | /** 5 | * The upper case version of a string. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category String 10 | * @sig String -> String 11 | * @param {String} str The string to upper case. 12 | * @return {String} The upper case version of `str`. 13 | * @example 14 | * 15 | * R.toUpper('abc'); //=> 'ABC' 16 | */ 17 | module.exports = invoker(0, 'toUpperCase'); 18 | -------------------------------------------------------------------------------- /src/internal/_cloneRegExp.js: -------------------------------------------------------------------------------- 1 | module.exports = function _cloneRegExp(pattern) { 2 | return new RegExp(pattern.source, (pattern.global ? 'g' : '') + 3 | (pattern.ignoreCase ? 'i' : '') + 4 | (pattern.multiline ? 'm' : '') + 5 | (pattern.sticky ? 'y' : '') + 6 | (pattern.unicode ? 'u' : '')); 7 | }; 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.js] 12 | indent_style = space 13 | indent_size = 2 14 | max_line_length = 100 15 | 16 | [*.json] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [*.md] 21 | # Two trailing spaces => newline in GitHub-flavored markdown 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /lib/sauce/apple.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | { 3 | browserName: 'safari', 4 | version: '5', 5 | platform: 'OS X 10.6' 6 | }, 7 | { 8 | browserName: 'safari', 9 | version: '6', 10 | platform: 'OS X 10.8' 11 | }, 12 | { 13 | browserName: 'safari', 14 | version: '7', 15 | platform: 'OS X 10.9' 16 | }, 17 | { 18 | browserName: 'safari', 19 | version: '8', 20 | platform: 'OS X 10.10' 21 | } 22 | ]; 23 | -------------------------------------------------------------------------------- /test/negate.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('negate', function() { 7 | 8 | it('negates its argument', function() { 9 | assert.strictEqual(R.negate(-Infinity), Infinity); 10 | assert.strictEqual(R.negate(-1), 1); 11 | assert.strictEqual(R.negate(0), 0); 12 | assert.strictEqual(R.negate(1), -1); 13 | assert.strictEqual(R.negate(Infinity), -Infinity); 14 | }); 15 | 16 | }); 17 | -------------------------------------------------------------------------------- /scripts/prepublish: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | README="$(cat README.md)" 5 | README="${README//"$PREVIOUS_VERSION/ramda.min.js"/"$VERSION/ramda.min.js"}" 6 | README="${README//"${PREVIOUS_VERSION%.*}/ramda.min.js"/"${VERSION%.*}/ramda.min.js"}" 7 | echo "$README" >README.md 8 | git add README.md 9 | 10 | rm -f dist/ramda{,.min}.js 11 | make dist/ramda{,.min}.js 12 | git add dist/ramda{,.min}.js 13 | 14 | npm run bookmarklet 15 | git add BOOKMARKLET.md 16 | -------------------------------------------------------------------------------- /src/internal/_functionsWith.js: -------------------------------------------------------------------------------- 1 | var _filter = require('./_filter'); 2 | 3 | 4 | /** 5 | * @private 6 | * @param {Function} fn The strategy for extracting function names from an object 7 | * @return {Function} A function that takes an object and returns an array of function names. 8 | */ 9 | module.exports = function _functionsWith(fn) { 10 | return function(obj) { 11 | return _filter(function(key) { return typeof obj[key] === 'function'; }, fn(obj)); 12 | }; 13 | }; 14 | -------------------------------------------------------------------------------- /test/split.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('split', function() { 7 | it('splits a string into an array', function() { 8 | assert.deepEqual(R.split('.', 'a.b.c.xyz.d'), ['a', 'b', 'c', 'xyz', 'd']); 9 | }); 10 | 11 | it('the split string can be arbitrary', function() { 12 | assert.deepEqual(R.split('at', 'The Cat in the Hat sat on the mat'), ['The C', ' in the H', ' s', ' on the m', '']); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /lib/bench/curry.bench.js: -------------------------------------------------------------------------------- 1 | var curry = require('../..').curry; 2 | 3 | function mult(x, y) { return x * y; } 4 | var mult4 = curry(mult)(4); 5 | function manmult(x) { 6 | return function(y) { 7 | return x * y; 8 | }; 9 | } 10 | var manual = manmult(4); 11 | 12 | module.exports = { 13 | name: 'curry', 14 | tests: { 15 | 'mult4(100)': function() { 16 | mult4(100); 17 | }, 18 | 'manual(100)': function() { 19 | manual(100); 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /test/identity.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('identity', function() { 7 | it('returns its first argument', function() { 8 | assert.strictEqual(R.identity(undefined), undefined); 9 | assert.strictEqual(R.identity('foo'), 'foo'); 10 | assert.strictEqual(R.identity('foo', 'bar'), 'foo'); 11 | }); 12 | 13 | it('has length 1', function() { 14 | assert.strictEqual(R.identity.length, 1); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /test/inc.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('inc', function() { 7 | 8 | it('increments its argument', function() { 9 | assert.strictEqual(R.inc(-1), 0); 10 | assert.strictEqual(R.inc(0), 1); 11 | assert.strictEqual(R.inc(1), 2); 12 | assert.strictEqual(R.inc(12.34), 13.34); 13 | assert.strictEqual(R.inc(-Infinity), -Infinity); 14 | assert.strictEqual(R.inc(Infinity), Infinity); 15 | }); 16 | 17 | }); 18 | -------------------------------------------------------------------------------- /src/max.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns the larger of its two arguments. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Relation 10 | * @sig Ord a => a -> a -> a 11 | * @param {*} a 12 | * @param {*} b 13 | * @return {*} 14 | * @see R.maxBy 15 | * @example 16 | * 17 | * R.max(789, 123); //=> 789 18 | * R.max('a', 'b'); //=> 'b' 19 | */ 20 | module.exports = _curry2(function max(a, b) { return b > a ? b : a; }); 21 | -------------------------------------------------------------------------------- /src/min.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns the smaller of its two arguments. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Relation 10 | * @sig Ord a => a -> a -> a 11 | * @param {*} a 12 | * @param {*} b 13 | * @return {*} 14 | * @see R.minBy 15 | * @example 16 | * 17 | * R.min(789, 123); //=> 123 18 | * R.min('a', 'b'); //=> 'a' 19 | */ 20 | module.exports = _curry2(function min(a, b) { return b < a ? b : a; }); 21 | -------------------------------------------------------------------------------- /test/dec.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('dec', function() { 7 | 8 | it('decrements its argument', function() { 9 | assert.strictEqual(R.dec(-1), -2); 10 | assert.strictEqual(R.dec(0), -1); 11 | assert.strictEqual(R.dec(1), 0); 12 | assert.strictEqual(R.dec(12.34), 11.34); 13 | assert.strictEqual(R.dec(-Infinity), -Infinity); 14 | assert.strictEqual(R.dec(Infinity), Infinity); 15 | }); 16 | 17 | }); 18 | -------------------------------------------------------------------------------- /src/internal/_xwrap.js: -------------------------------------------------------------------------------- 1 | module.exports = (function() { 2 | function XWrap(fn) { 3 | this.f = fn; 4 | } 5 | XWrap.prototype['@@transducer/init'] = function() { 6 | throw new Error('init not implemented on XWrap'); 7 | }; 8 | XWrap.prototype['@@transducer/result'] = function(acc) { return acc; }; 9 | XWrap.prototype['@@transducer/step'] = function(acc, x) { 10 | return this.f(acc, x); 11 | }; 12 | 13 | return function _xwrap(fn) { return new XWrap(fn); }; 14 | }()); 15 | -------------------------------------------------------------------------------- /test/isNil.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var R = require('..'); 3 | 4 | describe('isNil', function() { 5 | it('tests a value for `null` or `undefined`', function() { 6 | assert.strictEqual(R.isNil(void 0), true); 7 | assert.strictEqual(R.isNil(null), true); 8 | assert.strictEqual(R.isNil([]), false); 9 | assert.strictEqual(R.isNil({}), false); 10 | assert.strictEqual(R.isNil(0), false); 11 | assert.strictEqual(R.isNil(''), false); 12 | }); 13 | 14 | }); 15 | -------------------------------------------------------------------------------- /lib/bench/concat.bench.js: -------------------------------------------------------------------------------- 1 | var concat = require('../..').concat; 2 | 3 | var s1 = [8, 2, 85, 2, 34, 3, 23]; 4 | var s2 = [247, 57, 8, 0, 6, 5, 46, 54, 643]; 5 | var concatS1 = concat(s1); 6 | 7 | module.exports = { 8 | name: 'concat', 9 | tests: { 10 | 'concat(s1, s2)': function() { 11 | concat(s1, s2); 12 | }, 13 | 'concat(s1)(s2)': function() { 14 | concat(s1)(s2); 15 | }, 16 | 'concatS1(s2)': function() { 17 | concatS1(s2); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /lib/bench/map.bench.js: -------------------------------------------------------------------------------- 1 | var map = require('../..').map; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | function sq(x) { return x * x; } 5 | var mapSq = map(sq); 6 | 7 | module.exports = { 8 | name: 'map', 9 | tests: { 10 | 'map(sq, nums)': function() { 11 | map(sq, nums); 12 | }, 13 | 'map(sq)(nums)': function() { 14 | map(sq)(nums); 15 | }, 16 | 'mapSq(nums)': function() { 17 | mapSq(nums); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/sum.js: -------------------------------------------------------------------------------- 1 | var _add = require('./internal/_add'); 2 | var reduce = require('./reduce'); 3 | 4 | 5 | /** 6 | * Adds together all the elements of a list. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Math 11 | * @sig [Number] -> Number 12 | * @param {Array} list An array of numbers 13 | * @return {Number} The sum of all the numbers in the list. 14 | * @see R.reduce 15 | * @example 16 | * 17 | * R.sum([2,4,6,8,100,1]); //=> 121 18 | */ 19 | module.exports = reduce(_add, 0); 20 | -------------------------------------------------------------------------------- /test/fromPairs.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('fromPairs', function() { 7 | it('combines an array of two-element arrays into an object', function() { 8 | assert.deepEqual(R.fromPairs([['a', 1], ['b', 2], ['c', 3]]), {a: 1, b: 2, c: 3}); 9 | }); 10 | it('skips empty Arrays and non-Array elements', function() { 11 | assert.deepEqual(R.fromPairs([['a', 1], 'x', [], ['b', 2], {}, ['c', 3]]), {a: 1, b: 2, c: 3}); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/sum.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('sum', function() { 7 | it('adds together the array of numbers supplied', function() { 8 | assert.strictEqual(R.sum([1, 2, 3, 4]), 10); 9 | }); 10 | 11 | it('does not save the state of the accumulator', function() { 12 | assert.strictEqual(R.sum([1, 2, 3, 4]), 10); 13 | assert.strictEqual(R.sum([1]), 1); 14 | assert.strictEqual(R.sum([5, 5, 5, 5, 5]), 25); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /lib/bench/each.bench.js: -------------------------------------------------------------------------------- 1 | var forEach = require('../..').forEach; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | 5 | module.exports = { 6 | name: 'forEach', 7 | tests: { 8 | 'forEach(x2, nums)': function() { 9 | var result = []; 10 | forEach(function(x) { result.push(x); }, nums); 11 | }, 12 | 'forEach(x2)(nums)': function() { 13 | var result = []; 14 | forEach(function(x) { result.push(x); })(nums); 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /lib/bench/indexof.bench.js: -------------------------------------------------------------------------------- 1 | var indexOf = require('../..').indexOf; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | var idxOf23 = indexOf(23); 5 | 6 | module.exports = { 7 | name: 'indexOf', 8 | tests: { 9 | 'indexOf(sq, nums)': function() { 10 | indexOf(23, nums); 11 | }, 12 | 'indexOf(sq)(nums)': function() { 13 | indexOf(23)(nums); 14 | }, 15 | 'idxOf23(nums)': function() { 16 | idxOf23(nums); 17 | } 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /test/nthChar.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('nthChar', function() { 7 | it('returns the nth character of the given string', function() { 8 | assert.strictEqual(R.nthChar(2, 'Ramda'), 'm'); 9 | }); 10 | 11 | it('accepts negative offsets', function() { 12 | assert.strictEqual(R.nthChar(-2, 'Ramda'), 'd'); 13 | }); 14 | 15 | it('is curried', function() { 16 | assert.strictEqual(R.nthChar(2)('Ramda'), 'm'); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/mean.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var sum = require('./sum'); 3 | 4 | 5 | /** 6 | * Returns the mean of the given list of numbers. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Math 11 | * @sig [Number] -> Number 12 | * @param {Array} list 13 | * @return {Number} 14 | * @example 15 | * 16 | * R.mean([2, 7, 9]); //=> 6 17 | * R.mean([]); //=> NaN 18 | */ 19 | module.exports = _curry1(function mean(list) { 20 | return sum(list) / list.length; 21 | }); 22 | -------------------------------------------------------------------------------- /test/intersectionWith.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('intersectionWith', function() { 7 | var Ro = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; 8 | var So = [{a: 3}, {a: 4}, {a: 5}, {a: 6}]; 9 | var eqA = function(r, s) { return r.a === s.a; }; 10 | it('combines two lists into the set of all their elements based on the passed-in equality predicate', function() { 11 | assert.deepEqual(R.intersectionWith(eqA, Ro, So), [{a: 3}, {a: 4}]); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/internal/_dissocPath.js: -------------------------------------------------------------------------------- 1 | var _assoc = require('./_assoc'); 2 | var _dissoc = require('./_dissoc'); 3 | var _slice = require('./_slice'); 4 | 5 | 6 | module.exports = function _dissocPath(path, obj) { 7 | switch (path.length) { 8 | case 0: 9 | return obj; 10 | case 1: 11 | return _dissoc(path[0], obj); 12 | default: 13 | var head = path[0]; 14 | var tail = _slice(path, 1); 15 | return obj[head] == null ? obj : _assoc(head, _dissocPath(tail, obj[head]), obj); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /src/last.js: -------------------------------------------------------------------------------- 1 | var nth = require('./nth'); 2 | 3 | 4 | /** 5 | * Returns the last element from a list. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category List 10 | * @see R.init, R.head, R.tail 11 | * @sig [a] -> a | Undefined 12 | * @param {Array} list The array to consider. 13 | * @return {*} The last element of the list, or `undefined` if the list is empty. 14 | * @example 15 | * 16 | * R.last(['fi', 'fo', 'fum']); //=> 'fum' 17 | * R.last([]); //=> undefined 18 | */ 19 | module.exports = nth(-1); 20 | -------------------------------------------------------------------------------- /test/unionWith.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('unionWith', function() { 7 | var Ro = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; 8 | var So = [{a: 3}, {a: 4}, {a: 5}, {a: 6}]; 9 | var eqA = function(r, s) { return r.a === s.a; }; 10 | it('combines two lists into the set of all their elements based on the passed-in equality predicate', function() { 11 | assert.deepEqual(R.unionWith(eqA, Ro, So), [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}, {a: 6}]); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/product.js: -------------------------------------------------------------------------------- 1 | var _multiply = require('./internal/_multiply'); 2 | var reduce = require('./reduce'); 3 | 4 | 5 | /** 6 | * Multiplies together all the elements of a list. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Math 11 | * @sig [Number] -> Number 12 | * @param {Array} list An array of numbers 13 | * @return {Number} The product of all the numbers in the list. 14 | * @see R.reduce 15 | * @example 16 | * 17 | * R.product([2,4,6,8,100,1]); //=> 38400 18 | */ 19 | module.exports = reduce(_multiply, 1); 20 | -------------------------------------------------------------------------------- /lib/bench/find.bench.js: -------------------------------------------------------------------------------- 1 | var find = require('../..').find; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | function isZero(x) { return x === 0; } 5 | var findZero = find(isZero); 6 | 7 | module.exports = { 8 | name: 'find', 9 | tests: { 10 | 'find(isZero, nums)': function() { 11 | find(isZero, nums); 12 | }, 13 | 'find(isZero)(nums)': function() { 14 | find(isZero)(nums); 15 | }, 16 | 'findZero(nums)': function() { 17 | findZero(nums); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/path.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _path = require('./internal/_path'); 3 | 4 | 5 | /** 6 | * Retrieve the value at a given path. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig [String] -> {k: v} -> v | Undefined 12 | * @param {Array} path The path to use. 13 | * @return {*} The data at `path`. 14 | * @example 15 | * 16 | * R.path(['a', 'b'], {a: {b: 2}}); //=> 2 17 | * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined 18 | */ 19 | module.exports = _curry2(_path); 20 | -------------------------------------------------------------------------------- /src/nthChar.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns the nth character of the given string. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category String 10 | * @sig Number -> String -> String 11 | * @param {Number} n 12 | * @param {String} str 13 | * @return {String} 14 | * @example 15 | * 16 | * R.nthChar(2, 'Ramda'); //=> 'm' 17 | * R.nthChar(-2, 'Ramda'); //=> 'd' 18 | */ 19 | module.exports = _curry2(function nthChar(n, str) { 20 | return str.charAt(n < 0 ? str.length + n : n); 21 | }); 22 | -------------------------------------------------------------------------------- /test/divide.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('divide', function() { 7 | it('divides two numbers', function() { 8 | assert.strictEqual(R.divide(28, 7), 4); 9 | }); 10 | 11 | it('is curried', function() { 12 | var into28 = R.divide(28); 13 | assert.strictEqual(into28(7), 4); 14 | }); 15 | 16 | it('behaves right curried when passed `R.__` for its first argument', function() { 17 | var half = R.divide(R.__, 2); 18 | assert.strictEqual(half(40), 20); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /lib/bench/reduce.bench.js: -------------------------------------------------------------------------------- 1 | var reduce = require('../..').reduce; 2 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 3 | function add(acc, x) { return acc + x; } 4 | var reduceAdd = reduce(add, 0); 5 | 6 | module.exports = { 7 | name: 'reduce', 8 | tests: { 9 | 'reduce(add, 0, nums)': function() { 10 | reduce(add, 0, nums); 11 | }, 12 | 'reduce(add, 0)(nums)': function() { 13 | reduce(add, 0)(nums); 14 | }, 15 | 'reduceAdd(nums)': function() { 16 | reduceAdd(nums); 17 | } 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /src/internal/_curry1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Optimized internal two-arity curry function. 3 | * 4 | * @private 5 | * @category Function 6 | * @param {Function} fn The function to curry. 7 | * @return {Function} The curried function. 8 | */ 9 | module.exports = function _curry1(fn) { 10 | return function f1(a) { 11 | if (arguments.length === 0) { 12 | return f1; 13 | } else if (a != null && a['@@functional/placeholder'] === true) { 14 | return f1; 15 | } else { 16 | return fn.apply(this, arguments); 17 | } 18 | }; 19 | }; 20 | -------------------------------------------------------------------------------- /src/init.js: -------------------------------------------------------------------------------- 1 | var slice = require('./slice'); 2 | 3 | 4 | /** 5 | * Returns all but the last element of a list. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category List 10 | * @see R.last, R.head, R.tail 11 | * @sig [a] -> [a] 12 | * @param {Array} list The array to consider. 13 | * @return {Array} A new array containing all but the last element of the input list, or an 14 | * empty list if the input list is empty. 15 | * @example 16 | * 17 | * R.init(['fi', 'fo', 'fum']); //=> ['fi', 'fo'] 18 | */ 19 | module.exports = slice(0, -1); 20 | -------------------------------------------------------------------------------- /test/match.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('match', function() { 7 | var re = /[A-Z]\d\d\-[a-zA-Z]+/; 8 | 9 | it('determines whether a string matches a regex', function() { 10 | assert.strictEqual(R.match(re, 'B17-afn').length, 1); 11 | assert.deepEqual(R.match(re, 'B1-afn'), []); 12 | }); 13 | 14 | it('is curried', function() { 15 | var format = R.match(re); 16 | assert.strictEqual(format('B17-afn').length, 1); 17 | assert.deepEqual(format('B1-afn'), []); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /lib/bench/filter.bench.js: -------------------------------------------------------------------------------- 1 | var filter = require('../..').filter; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | function isEven(x) { return x % 2 === 0; } 5 | var filterEven = filter(isEven); 6 | 7 | module.exports = { 8 | name: 'filter', 9 | tests: { 10 | 'filter(isEven, nums)': function() { 11 | filter(isEven, nums); 12 | }, 13 | 'filter(isEven)(nums)': function() { 14 | filter(isEven)(nums); 15 | }, 16 | 'filterEven(nums)': function() { 17 | filterEven(nums); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/internal/_isArray.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Tests whether or not an object is an array. 3 | * 4 | * @private 5 | * @param {*} val The object to test. 6 | * @return {Boolean} `true` if `val` is an array, `false` otherwise. 7 | * @example 8 | * 9 | * _isArray([]); //=> true 10 | * _isArray(null); //=> false 11 | * _isArray({}); //=> false 12 | */ 13 | module.exports = Array.isArray || function _isArray(val) { 14 | return (val != null && 15 | val.length >= 0 && 16 | Object.prototype.toString.call(val) === '[object Array]'); 17 | }; 18 | -------------------------------------------------------------------------------- /src/internal/_xmap.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XMap(f, xf) { 7 | this.xf = xf; 8 | this.f = f; 9 | } 10 | XMap.prototype['@@transducer/init'] = _xfBase.init; 11 | XMap.prototype['@@transducer/result'] = _xfBase.result; 12 | XMap.prototype['@@transducer/step'] = function(result, input) { 13 | return this.xf['@@transducer/step'](result, this.f(input)); 14 | }; 15 | 16 | return _curry2(function _xmap(f, xf) { return new XMap(f, xf); }); 17 | })(); 18 | -------------------------------------------------------------------------------- /test/subtract.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('subtract', function() { 7 | it('subtracts two numbers', function() { 8 | assert.strictEqual(R.subtract(22, 7), 15); 9 | }); 10 | 11 | it('is curried', function() { 12 | var ninesCompl = R.subtract(9); 13 | assert.strictEqual(ninesCompl(6), 3); 14 | }); 15 | 16 | it('behaves right curried when passed `R.__` for its first argument', function() { 17 | var minus5 = R.subtract(R.__, 5); 18 | assert.strictEqual(minus5(17), 12); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/head.js: -------------------------------------------------------------------------------- 1 | var nth = require('./nth'); 2 | 3 | 4 | /** 5 | * Returns the first element in a list. 6 | * In some libraries this function is named `first`. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @see R.tail, R.init, R.last 12 | * @sig [a] -> a | Undefined 13 | * @param {Array} list The array to consider. 14 | * @return {*} The first element of the list, or `undefined` if the list is empty. 15 | * @example 16 | * 17 | * R.head(['fi', 'fo', 'fum']); //=> 'fi' 18 | * R.head([]); //=> undefined 19 | */ 20 | module.exports = nth(0); 21 | -------------------------------------------------------------------------------- /src/internal/_createComposer.js: -------------------------------------------------------------------------------- 1 | var _arity = require('./_arity'); 2 | 3 | 4 | /* 5 | * Returns a function that makes a multi-argument version of compose from 6 | * either _compose or _composeP. 7 | */ 8 | module.exports = function _createComposer(composeFunction) { 9 | return function() { 10 | var fn = arguments[arguments.length - 1]; 11 | var length = fn.length; 12 | var idx = arguments.length - 2; 13 | while (idx >= 0) { 14 | fn = composeFunction(arguments[idx], fn); 15 | idx -= 1; 16 | } 17 | return _arity(length, fn); 18 | }; 19 | }; 20 | -------------------------------------------------------------------------------- /test/nthCharCode.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('nthCharCode', function() { 7 | it('returns the character code of the nth character of the given string', function() { 8 | assert.strictEqual(R.nthCharCode(2, 'Ramda'), 'm'.charCodeAt(0)); 9 | }); 10 | 11 | it('accepts negative offsets', function() { 12 | assert.strictEqual(R.nthCharCode(-2, 'Ramda'), 'd'.charCodeAt(0)); 13 | }); 14 | 15 | it('is curried', function() { 16 | assert.strictEqual(R.nthCharCode(2)('Ramda'), 'm'.charCodeAt(0)); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /lib/bench/maxWith.bench.js: -------------------------------------------------------------------------------- 1 | var R = require('../..'); 2 | var maxBy = R.maxBy; 3 | var computer = R.prop('val'); 4 | var maxVal = maxBy(computer); 5 | var vals = R.map(function(n) { return {key: n * 5, val: String.fromCharCode(n * 5)}; }, R.range(1, 100)); 6 | 7 | module.exports = { 8 | name: 'maxBy', 9 | tests: { 10 | 'maxBy(computer, nums)': function() { 11 | maxBy(computer, vals); 12 | }, 13 | 'maxBy(computer)(vals)': function() { 14 | maxBy(computer)(vals); 15 | }, 16 | 'maxVal(vals)': function() { 17 | maxVal(vals); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/nthArg.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _nth = require('./internal/_nth'); 3 | 4 | 5 | /** 6 | * Returns a function which returns its nth argument. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Function 11 | * @sig Number -> *... -> * 12 | * @param {Number} n 13 | * @return {Function} 14 | * @example 15 | * 16 | * R.nthArg(1)('a', 'b', 'c'); //=> 'b' 17 | * R.nthArg(-1)('a', 'b', 'c'); //=> 'c' 18 | */ 19 | module.exports = _curry1(function nthArg(n) { 20 | return function() { 21 | return _nth(n, arguments); 22 | }; 23 | }); 24 | -------------------------------------------------------------------------------- /lib/bench/findIndex.bench.js: -------------------------------------------------------------------------------- 1 | var findIndex = require('../..').findIndex; 2 | 3 | var nums = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | function isZero(x) { return x === 0; } 5 | var findIndexZero = findIndex(isZero); 6 | 7 | module.exports = { 8 | name: 'findIndex', 9 | tests: { 10 | 'findIndex(isZero, nums)': function() { 11 | findIndex(isZero, nums); 12 | }, 13 | 'findIndex(isZero)(nums)': function() { 14 | findIndex(isZero)(nums); 15 | }, 16 | 'findIndexZero(nums)': function() { 17 | findIndexZero(nums); 18 | } 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/isNil.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Checks if the input value is `null` or `undefined`. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Type 10 | * @sig * -> Boolean 11 | * @param {*} x The value to test. 12 | * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`. 13 | * @example 14 | * 15 | * R.isNil(null); //=> true 16 | * R.isNil(undefined); //=> true 17 | * R.isNil(0); //=> false 18 | * R.isNil([]); //=> false 19 | */ 20 | module.exports = _curry1(function isNil(x) { return x == null; }); 21 | -------------------------------------------------------------------------------- /test/head.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('head', function() { 7 | it('returns undefined for an empty list', function() { 8 | assert.strictEqual(typeof(R.head([])), 'undefined'); 9 | }); 10 | it('returns the first element of a list', function() { 11 | assert.strictEqual(R.head(['a', 'b', 'c', 'd']), 'a'); 12 | }); 13 | it('throws if applied to null or undefined', function() { 14 | assert.throws(function() { R.head(null); }, TypeError); 15 | assert.throws(function() { R.head(undefined); }, TypeError); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /test/last.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('last', function() { 7 | it('returns undefined for an empty list', function() { 8 | assert.strictEqual(typeof(R.last([])), 'undefined'); 9 | }); 10 | it('returns the first element of a list', function() { 11 | assert.strictEqual(R.last(['a', 'b', 'c', 'd']), 'd'); 12 | }); 13 | it('throws if applied to null or undefined', function() { 14 | assert.throws(function() { R.last(null); }, TypeError); 15 | assert.throws(function() { R.last(undefined); }, TypeError); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /test/zip.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('zip', function() { 7 | it('returns an array of "tuples"', function() { 8 | var a = [1, 2, 3], b = [100, 200, 300]; 9 | assert.deepEqual(R.zip(a, b), [[1, 100], [2, 200], [3, 300]]); 10 | }); 11 | 12 | it('returns a list as long as the shorter of the lists input', function() { 13 | var a = [1, 2, 3], b = [100, 200, 300, 400], c = [10, 20]; 14 | assert.deepEqual(R.zip(a, b), [[1, 100], [2, 200], [3, 300]]); 15 | assert.deepEqual(R.zip(a, c), [[1, 10], [2, 20]]); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /test/repeat.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('repeat', function() { 7 | it('returns a lazy list of identical values', function() { 8 | assert.deepEqual(R.repeat(0, 5), [0, 0, 0, 0, 0]); 9 | }); 10 | 11 | it('can accept any value, including `null`', function() { 12 | assert.deepEqual(R.repeat(null, 3), [null, null, null]); 13 | }); 14 | 15 | it('is curried', function() { 16 | var makeFoos = R.repeat('foo'); 17 | assert.deepEqual(makeFoos(0), []); 18 | assert.deepEqual(makeFoos(3), ['foo', 'foo', 'foo']); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/identity.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _identity = require('./internal/_identity'); 3 | 4 | 5 | /** 6 | * A function that does nothing but return the parameter supplied to it. Good as a default 7 | * or placeholder function. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Function 12 | * @sig a -> a 13 | * @param {*} x The value to return. 14 | * @return {*} The input value, `x`. 15 | * @example 16 | * 17 | * R.identity(1); //=> 1 18 | * 19 | * var obj = {}; 20 | * R.identity(obj) === obj; //=> true 21 | */ 22 | module.exports = _curry1(_identity); 23 | -------------------------------------------------------------------------------- /src/internal/_xfilter.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XFilter(f, xf) { 7 | this.xf = xf; 8 | this.f = f; 9 | } 10 | XFilter.prototype['@@transducer/init'] = _xfBase.init; 11 | XFilter.prototype['@@transducer/result'] = _xfBase.result; 12 | XFilter.prototype['@@transducer/step'] = function(result, input) { 13 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : result; 14 | }; 15 | 16 | return _curry2(function _xfilter(f, xf) { return new XFilter(f, xf); }); 17 | })(); 18 | -------------------------------------------------------------------------------- /src/length.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var is = require('./is'); 3 | 4 | 5 | /** 6 | * Returns the number of elements in the array by returning `list.length`. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig [a] -> Number 12 | * @param {Array} list The array to inspect. 13 | * @return {Number} The length of the array. 14 | * @example 15 | * 16 | * R.length([]); //=> 0 17 | * R.length([1, 2, 3]); //=> 3 18 | */ 19 | module.exports = _curry1(function length(list) { 20 | return list != null && is(Number, list.length) ? list.length : NaN; 21 | }); 22 | -------------------------------------------------------------------------------- /src/lt.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns `true` if the first argument is less than the second; 6 | * `false` otherwise. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord a => a -> a -> Boolean 12 | * @param {*} a 13 | * @param {*} b 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.lt(2, 1); //=> false 18 | * R.lt(2, 2); //=> false 19 | * R.lt(2, 3); //=> true 20 | * R.lt('a', 'z'); //=> true 21 | * R.lt('z', 'a'); //=> false 22 | */ 23 | module.exports = _curry2(function lt(a, b) { return a < b; }); 24 | -------------------------------------------------------------------------------- /src/nthCharCode.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns the character code of the nth character of the given string. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category String 10 | * @sig Number -> String -> Number 11 | * @param {Number} n 12 | * @param {String} str 13 | * @return {Number} 14 | * @example 15 | * 16 | * R.nthCharCode(2, 'Ramda'); //=> 'm'.charCodeAt(0) 17 | * R.nthCharCode(-2, 'Ramda'); //=> 'd'.charCodeAt(0) 18 | */ 19 | module.exports = _curry2(function nthCharCode(n, str) { 20 | return str.charCodeAt(n < 0 ? str.length + n : n); 21 | }); 22 | -------------------------------------------------------------------------------- /src/prop.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns a function that when supplied an object returns the indicated property of that object, if it exists. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Object 10 | * @sig s -> {s: a} -> a | Undefined 11 | * @param {String} p The property name 12 | * @param {Object} obj The object to query 13 | * @return {*} The value at `obj.p`. 14 | * @example 15 | * 16 | * R.prop('x', {x: 100}); //=> 100 17 | * R.prop('x', {}); //=> undefined 18 | */ 19 | module.exports = _curry2(function prop(p, obj) { return obj[p]; }); 20 | -------------------------------------------------------------------------------- /src/gt.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns `true` if the first argument is greater than the second; 6 | * `false` otherwise. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord a => a -> a -> Boolean 12 | * @param {*} a 13 | * @param {*} b 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.gt(2, 1); //=> true 18 | * R.gt(2, 2); //=> false 19 | * R.gt(2, 3); //=> false 20 | * R.gt('a', 'z'); //=> false 21 | * R.gt('z', 'a'); //=> true 22 | */ 23 | module.exports = _curry2(function gt(a, b) { return a > b; }); 24 | -------------------------------------------------------------------------------- /test/binary.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('binary', function() { 7 | it('turns multiple-argument function into binary one', function() { 8 | R.binary(function(x, y, z) { 9 | assert.strictEqual(arguments.length, 2); 10 | assert.strictEqual(typeof z, 'undefined'); 11 | })(10, 20, 30); 12 | }); 13 | 14 | it('initial arguments are passed through normally', function() { 15 | R.binary(function(x, y, z) { 16 | assert.strictEqual(x, 10); 17 | assert.strictEqual(y, 20); 18 | void z; 19 | })(10, 20, 30); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /src/add.js: -------------------------------------------------------------------------------- 1 | var _add = require('./internal/_add'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Adds two numbers (or strings). Equivalent to `a + b` but curried. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Math 11 | * @sig Number -> Number -> Number 12 | * @sig String -> String -> String 13 | * @param {Number|String} a The first value. 14 | * @param {Number|String} b The second value. 15 | * @return {Number|String} The result of `a + b`. 16 | * @example 17 | * 18 | * R.add(2, 3); //=> 5 19 | * R.add(7)(10); //=> 17 20 | */ 21 | module.exports = _curry2(_add); 22 | -------------------------------------------------------------------------------- /src/maxBy.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | 3 | 4 | /** 5 | * Takes a function and two values, and returns whichever value produces 6 | * the larger result when passed to the provided function. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord b => (a -> b) -> a -> a -> a 12 | * @param {Function} f 13 | * @param {*} a 14 | * @param {*} b 15 | * @return {*} 16 | * @see R.max 17 | * @example 18 | * 19 | * R.maxBy(function(n) { return n * n; }, -3, 2); //=> -3 20 | */ 21 | module.exports = _curry3(function maxBy(f, a, b) { 22 | return f(b) > f(a) ? b : a; 23 | }); 24 | -------------------------------------------------------------------------------- /src/minBy.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | 3 | 4 | /** 5 | * Takes a function and two values, and returns whichever value produces 6 | * the smaller result when passed to the provided function. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord b => (a -> b) -> a -> a -> a 12 | * @param {Function} f 13 | * @param {*} a 14 | * @param {*} b 15 | * @return {*} 16 | * @see R.min 17 | * @example 18 | * 19 | * R.minBy(function(n) { return n * n; }, -3, 2); //=> 2 20 | */ 21 | module.exports = _curry3(function minBy(f, a, b) { 22 | return f(b) < f(a) ? b : a; 23 | }); 24 | -------------------------------------------------------------------------------- /src/of.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Returns a singleton array containing the value provided. 6 | * 7 | * Note this `of` is different from the ES6 `of`; See 8 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Function 13 | * @sig a -> [a] 14 | * @param {*} x any value 15 | * @return {Array} An array wrapping `x`. 16 | * @example 17 | * 18 | * R.of(null); //=> [null] 19 | * R.of([42]); //=> [[42]] 20 | */ 21 | module.exports = _curry1(function of(x) { return [x]; }); 22 | -------------------------------------------------------------------------------- /test/toPairs.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('toPairs', function() { 7 | it('converts an object into an array of two-element [key, value] arrays', function() { 8 | assert.deepEqual(R.toPairs({a: 1, b: 2, c: 3}), [['a', 1], ['b', 2], ['c', 3]]); 9 | }); 10 | it("only iterates the object's own properties", function() { 11 | var F = function() { 12 | this.x = 1; 13 | this.y = 2; 14 | }; 15 | F.prototype.protoProp = 'you can\'t see me'; 16 | var f = new F(); 17 | assert.deepEqual(R.toPairs(f), [['x', 1], ['y', 2]]); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /src/test.js: -------------------------------------------------------------------------------- 1 | var _cloneRegExp = require('./internal/_cloneRegExp'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Determines whether a given string matches a given regular expression. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category String 11 | * @sig RegExp -> String -> Boolean 12 | * @param {RegExp} pattern 13 | * @param {String} str 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.test(/^x/, 'xyz'); //=> true 18 | * R.test(/^y/, 'xyz'); //=> false 19 | */ 20 | module.exports = _curry2(function test(pattern, str) { 21 | return _cloneRegExp(pattern).test(str); 22 | }); 23 | -------------------------------------------------------------------------------- /test/unary.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('unary', function() { 7 | it('turns multiple-argument function into unary one', function() { 8 | R.unary(function(x, y, z) { 9 | assert.strictEqual(arguments.length, 1); 10 | assert.strictEqual(typeof y, 'undefined'); 11 | assert.strictEqual(typeof z, 'undefined'); 12 | })(10, 20, 30); 13 | }); 14 | 15 | it('initial argument is passed through normally', function() { 16 | R.unary(function(x, y, z) { 17 | assert.strictEqual(x, 10); 18 | void z; 19 | })(10, 20, 30); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /src/uniq.js: -------------------------------------------------------------------------------- 1 | var equals = require('./equals'); 2 | var uniqWith = require('./uniqWith'); 3 | 4 | 5 | /** 6 | * Returns a new list containing only one copy of each element in the original list. 7 | * `R.equals` is used to determine equality. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [a] -> [a] 13 | * @param {Array} list The array to consider. 14 | * @return {Array} The list of unique items. 15 | * @example 16 | * 17 | * R.uniq([1, 1, 2, 1]); //=> [1, 2] 18 | * R.uniq([1, '1']); //=> [1, '1'] 19 | * R.uniq([[42], [42]]); //=> [[42]] 20 | */ 21 | module.exports = uniqWith(equals); 22 | -------------------------------------------------------------------------------- /src/unnest.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _makeFlat = require('./internal/_makeFlat'); 3 | 4 | 5 | /** 6 | * Returns a new list by pulling every item at the first level of nesting out, and putting 7 | * them in a new array. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [a] -> [b] 13 | * @param {Array} list The array to consider. 14 | * @return {Array} The flattened list. 15 | * @example 16 | * 17 | * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]] 18 | * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6] 19 | */ 20 | module.exports = _curry1(_makeFlat(false)); 21 | -------------------------------------------------------------------------------- /test/tail.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('tail', function() { 7 | it('returns an empty list for an empty list', function() { 8 | assert.deepEqual(R.tail([]), []); 9 | }); 10 | it('returns a new list containing all the elements after the first element of a list', function() { 11 | assert.deepEqual(R.tail(['a', 'b', 'c', 'd']), ['b', 'c', 'd']); 12 | }); 13 | it('throws if applied to null or undefined', function() { 14 | assert.throws(function() { R.tail(null); }, TypeError); 15 | assert.throws(function() { R.tail(undefined); }, TypeError); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /lib/test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ramda Tests 6 | 7 | 8 | 9 |

Tests for ramda

10 |
11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/always.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Returns a function that always returns the given value. Note that for non-primitives the value 6 | * returned is a reference to the original value. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Function 11 | * @sig a -> (* -> a) 12 | * @param {*} val The value to wrap in a function 13 | * @return {Function} A Function :: * -> val. 14 | * @example 15 | * 16 | * var t = R.always('Tee'); 17 | * t(); //=> 'Tee' 18 | */ 19 | module.exports = _curry1(function always(val) { 20 | return function() { 21 | return val; 22 | }; 23 | }); 24 | -------------------------------------------------------------------------------- /src/dissoc.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _dissoc = require('./internal/_dissoc'); 3 | 4 | 5 | /** 6 | * Returns a new object that does not contain a `prop` property. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig String -> {k: v} -> {k: v} 12 | * @param {String} prop the name of the property to dissociate 13 | * @param {Object} obj the object to clone 14 | * @return {Object} a new object similar to the original but without the specified property 15 | * @example 16 | * 17 | * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3} 18 | */ 19 | module.exports = _curry2(_dissoc); 20 | -------------------------------------------------------------------------------- /src/lte.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns `true` if the first argument is less than or equal to the second; 6 | * `false` otherwise. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord a => a -> a -> Boolean 12 | * @param {Number} a 13 | * @param {Number} b 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.lte(2, 1); //=> false 18 | * R.lte(2, 2); //=> true 19 | * R.lte(2, 3); //=> true 20 | * R.lte('a', 'z'); //=> true 21 | * R.lte('z', 'a'); //=> false 22 | */ 23 | module.exports = _curry2(function lte(a, b) { return a <= b; }); 24 | -------------------------------------------------------------------------------- /test/append.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('append', function() { 7 | it('adds the element to the end of the list', function() { 8 | assert.deepEqual(R.append('z', ['x', 'y']), ['x', 'y', 'z']); 9 | assert.deepEqual(R.append(['a', 'z'], ['x', 'y']), ['x', 'y', ['a', 'z']]); 10 | }); 11 | 12 | it('works on empty list', function() { 13 | assert.deepEqual(R.append(1, []), [1]); 14 | }); 15 | 16 | it('is curried', function() { 17 | assert.strictEqual(typeof R.append(4), 'function'); 18 | assert.deepEqual(R.append(1)([4, 3, 2]), [4, 3, 2, 1]); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/flatten.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _makeFlat = require('./internal/_makeFlat'); 3 | 4 | 5 | /** 6 | * Returns a new list by pulling every item out of it (and all its sub-arrays) and putting 7 | * them in a new array, depth-first. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [a] -> [b] 13 | * @param {Array} list The array to consider. 14 | * @return {Array} The flattened list. 15 | * @example 16 | * 17 | * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]); 18 | * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 19 | */ 20 | module.exports = _curry1(_makeFlat(true)); 21 | -------------------------------------------------------------------------------- /src/gte.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns `true` if the first argument is greater than or equal to the second; 6 | * `false` otherwise. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Relation 11 | * @sig Ord a => a -> a -> Boolean 12 | * @param {Number} a 13 | * @param {Number} b 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.gte(2, 1); //=> true 18 | * R.gte(2, 2); //=> true 19 | * R.gte(2, 3); //=> false 20 | * R.gte('a', 'z'); //=> false 21 | * R.gte('z', 'a'); //=> true 22 | */ 23 | module.exports = _curry2(function gte(a, b) { return a >= b; }); 24 | -------------------------------------------------------------------------------- /src/tap.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Runs the given function with the supplied object, then returns the object. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Function 10 | * @sig (a -> *) -> a -> a 11 | * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away. 12 | * @param {*} x 13 | * @return {*} `x`. 14 | * @example 15 | * 16 | * var sayX = function(x) { console.log('x is ' + x); }; 17 | * R.tap(sayX, 100); //=> 100 18 | * //-> 'x is 100' 19 | */ 20 | module.exports = _curry2(function tap(fn, x) { 21 | fn(x); 22 | return x; 23 | }); 24 | -------------------------------------------------------------------------------- /test/max.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('max', function() { 7 | 8 | it('returns the larger of its two arguments', function() { 9 | assert.strictEqual(R.max(-7, 7), 7); 10 | assert.strictEqual(R.max(7, -7), 7); 11 | }); 12 | 13 | it('works for any orderable type', function() { 14 | var d1 = new Date('2001-01-01'); 15 | var d2 = new Date('2002-02-02'); 16 | 17 | assert.strictEqual(R.max(d1, d2), d2); 18 | assert.strictEqual(R.max(d2, d1), d2); 19 | assert.strictEqual(R.max('a', 'b'), 'b'); 20 | assert.strictEqual(R.max('b', 'a'), 'b'); 21 | }); 22 | 23 | }); 24 | -------------------------------------------------------------------------------- /test/min.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('min', function() { 7 | 8 | it('returns the smaller of its two arguments', function() { 9 | assert.strictEqual(R.min(-7, 7), -7); 10 | assert.strictEqual(R.min(7, -7), -7); 11 | }); 12 | 13 | it('works for any orderable type', function() { 14 | var d1 = new Date('2001-01-01'); 15 | var d2 = new Date('2002-02-02'); 16 | 17 | assert.strictEqual(R.min(d1, d2), d1); 18 | assert.strictEqual(R.min(d2, d1), d1); 19 | assert.strictEqual(R.min('a', 'b'), 'a'); 20 | assert.strictEqual(R.min('b', 'a'), 'a'); 21 | }); 22 | 23 | }); 24 | -------------------------------------------------------------------------------- /test/prepend.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('prepend', function() { 7 | it('adds the element to the beginning of the list', function() { 8 | assert.deepEqual(R.prepend('x', ['y', 'z']), ['x', 'y', 'z']); 9 | assert.deepEqual(R.prepend(['a', 'z'], ['x', 'y']), [['a', 'z'], 'x', 'y']); 10 | }); 11 | 12 | it('works on empty list', function() { 13 | assert.deepEqual(R.prepend(1, []), [1]); 14 | }); 15 | 16 | it('is curried', function() { 17 | assert.strictEqual(typeof R.prepend(4), 'function'); 18 | assert.deepEqual(R.prepend(4)([3, 2, 1]), [4, 3, 2, 1]); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/internal/_xdrop.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XDrop(n, xf) { 7 | this.xf = xf; 8 | this.n = n; 9 | } 10 | XDrop.prototype['@@transducer/init'] = _xfBase.init; 11 | XDrop.prototype['@@transducer/result'] = _xfBase.result; 12 | XDrop.prototype['@@transducer/step'] = function(result, input) { 13 | if (this.n > 0) { 14 | this.n -= 1; 15 | return result; 16 | } 17 | return this.xf['@@transducer/step'](result, input); 18 | }; 19 | 20 | return _curry2(function _xdrop(n, xf) { return new XDrop(n, xf); }); 21 | })(); 22 | -------------------------------------------------------------------------------- /src/prepend.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _prepend = require('./internal/_prepend'); 3 | 4 | 5 | /** 6 | * Returns a new list with the given element at the front, followed by the contents of the 7 | * list. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig a -> [a] -> [a] 13 | * @param {*} el The item to add to the head of the output list. 14 | * @param {Array} list The array to add to the tail of the output list. 15 | * @return {Array} A new array. 16 | * @example 17 | * 18 | * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum'] 19 | */ 20 | module.exports = _curry2(_prepend); 21 | -------------------------------------------------------------------------------- /test/flip.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('flip', function() { 7 | it('returns a function which inverts the first two arguments to the supplied function', function() { 8 | var f = function(a, b, c) {return a + ' ' + b + ' ' + c;}; 9 | var g = R.flip(f); 10 | assert.strictEqual(f('a', 'b', 'c'), 'a b c'); 11 | assert.strictEqual(g('a', 'b', 'c'), 'b a c'); 12 | }); 13 | 14 | it('returns a curried function', function() { 15 | var f = function(a, b, c) {return a + ' ' + b + ' ' + c;}; 16 | var g = R.flip(f)('a'); 17 | assert.strictEqual(g('b', 'c'), 'b a c'); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /src/divide.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Divides two numbers. Equivalent to `a / b`. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Math 10 | * @sig Number -> Number -> Number 11 | * @param {Number} a The first value. 12 | * @param {Number} b The second value. 13 | * @return {Number} The result of `a / b`. 14 | * @example 15 | * 16 | * R.divide(71, 100); //=> 0.71 17 | * 18 | * var half = R.divide(R.__, 2); 19 | * half(42); //=> 21 20 | * 21 | * var reciprocal = R.divide(1); 22 | * reciprocal(4); //=> 0.25 23 | */ 24 | module.exports = _curry2(function divide(a, b) { return a / b; }); 25 | -------------------------------------------------------------------------------- /src/not.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * A function that returns the `!` of its argument. It will return `true` when 6 | * passed false-y value, and `false` when passed a truth-y one. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Logic 11 | * @sig * -> Boolean 12 | * @param {*} a any value 13 | * @return {Boolean} the logical inverse of passed argument. 14 | * @see R.complement 15 | * @example 16 | * 17 | * R.not(true); //=> false 18 | * R.not(false); //=> true 19 | * R.not(0); => true 20 | * R.not(1); => false 21 | */ 22 | module.exports = _curry1(function not(a) { 23 | return !a; 24 | }); 25 | -------------------------------------------------------------------------------- /test/apply.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('apply', function() { 7 | it('applies function to argument list', function() { 8 | assert.strictEqual(R.apply(Math.max, [1, 2, 3, -99, 42, 6, 7]), 42); 9 | }); 10 | 11 | it('is curried', function() { 12 | assert.strictEqual(R.apply(Math.max)([1, 2, 3, -99, 42, 6, 7]), 42); 13 | }); 14 | 15 | it('provides no way to specify context', function() { 16 | var obj = {method: function() { return this === obj; }}; 17 | assert.strictEqual(R.apply(obj.method, []), false); 18 | assert.strictEqual(R.apply(R.bind(obj.method, obj), []), true); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/nth.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _nth = require('./internal/_nth'); 3 | 4 | 5 | /** 6 | * Returns the nth element in a list. 7 | * If n is negative the element at index length + n is returned. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig Number -> [a] -> a | Undefined 13 | * @param {Number} idx 14 | * @param {Array} list 15 | * @return {*} The nth element of the list. 16 | * @example 17 | * 18 | * var list = ['foo', 'bar', 'baz', 'quux']; 19 | * R.nth(1, list); //=> 'bar' 20 | * R.nth(-1, list); //=> 'quux' 21 | * R.nth(-99, list); //=> undefined 22 | */ 23 | module.exports = _curry2(_nth); 24 | -------------------------------------------------------------------------------- /src/apply.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Applies function `fn` to the argument list `args`. This is useful for 6 | * creating a fixed-arity function from a variadic function. `fn` should 7 | * be a bound function if context is significant. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Function 12 | * @sig (*... -> a) -> [*] -> a 13 | * @param {Function} fn 14 | * @param {Array} args 15 | * @return {*} 16 | * @example 17 | * 18 | * var nums = [1, 2, 3, -99, 42, 6, 7]; 19 | * R.apply(Math.max, nums); //=> 42 20 | */ 21 | module.exports = _curry2(function apply(fn, args) { 22 | return fn.apply(this, args); 23 | }); 24 | -------------------------------------------------------------------------------- /src/internal/_xtakeWhile.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XTakeWhile(f, xf) { 8 | this.xf = xf; 9 | this.f = f; 10 | } 11 | XTakeWhile.prototype['@@transducer/init'] = _xfBase.init; 12 | XTakeWhile.prototype['@@transducer/result'] = _xfBase.result; 13 | XTakeWhile.prototype['@@transducer/step'] = function(result, input) { 14 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result); 15 | }; 16 | 17 | return _curry2(function _xtakeWhile(f, xf) { return new XTakeWhile(f, xf); }); 18 | })(); 19 | -------------------------------------------------------------------------------- /src/pluck.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _pluck = require('./internal/_pluck'); 3 | 4 | 5 | /** 6 | * Returns a new list by plucking the same named property off all objects in the list supplied. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig k -> [{k: v}] -> [v] 12 | * @param {Number|String} key The key name to pluck off of each object. 13 | * @param {Array} list The array to consider. 14 | * @return {Array} The list of values for the given key. 15 | * @example 16 | * 17 | * R.pluck('a')([{a: 1}, {a: 2}]); //=> [1, 2] 18 | * R.pluck(0)([[1, 2], [3, 4]]); //=> [1, 3] 19 | */ 20 | module.exports = _curry2(_pluck); 21 | -------------------------------------------------------------------------------- /test/pickAll.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('pickAll', function() { 7 | var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}; 8 | it('copies the named properties of an object to the new object', function() { 9 | assert.deepEqual(R.pickAll(['a', 'c', 'f'], obj), {a: 1, c: 3, f: 6}); 10 | }); 11 | 12 | it('includes properties not present on the input object', function() { 13 | assert.deepEqual(R.pickAll(['a', 'c', 'g'], obj), {a: 1, c: 3, g: undefined}); 14 | }); 15 | 16 | it('is curried', function() { 17 | var copyAB = R.pickAll(['a', 'b']); 18 | assert.deepEqual(copyAB(obj), {a: 1, b: 2}); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/mergeAll.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var merge = require('./merge'); 3 | var reduce = require('./reduce'); 4 | 5 | 6 | /** 7 | * Merges a list of objects together into one object. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [{k: v}] -> {k: v} 13 | * @param {Array} list An array of objects 14 | * @return {Object} A merged object. 15 | * @see R.reduce 16 | * @example 17 | * 18 | * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3} 19 | * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2} 20 | */ 21 | module.exports = _curry1(function mergeAll(list) { 22 | return reduce(merge, {}, list); 23 | }); 24 | -------------------------------------------------------------------------------- /test/complement.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('complement', function() { 7 | it('creates boolean-returning function that reverses another', function() { 8 | var even = function(x) {return x % 2 === 0;}; 9 | var f = R.complement(even); 10 | assert.strictEqual(f(8), false); 11 | assert.strictEqual(f(13), true); 12 | }); 13 | 14 | it('accepts a function that take multiple parameters', function() { 15 | var between = function(a, b, c) {return a < b && b < c;}; 16 | var f = R.complement(between); 17 | assert.strictEqual(f(4, 5, 11), false); 18 | assert.strictEqual(f(12, 2, 6), true); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/mean.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('mean', function() { 7 | 8 | it('returns mean of a nonempty list', function() { 9 | assert.strictEqual(R.mean([2]), 2); 10 | assert.strictEqual(R.mean([2, 7]), 4.5); 11 | assert.strictEqual(R.mean([2, 7, 9]), 6); 12 | assert.strictEqual(R.mean([2, 7, 9, 10]), 7); 13 | }); 14 | 15 | it('returns NaN for an empty list', function() { 16 | assert.strictEqual(R.identical(NaN, R.mean([])), true); 17 | }); 18 | 19 | it('handles array-like object', function() { 20 | assert.strictEqual(R.mean((function() { return arguments; }(1, 2, 3))), 2); 21 | }); 22 | 23 | }); 24 | -------------------------------------------------------------------------------- /src/multiply.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _multiply = require('./internal/_multiply'); 3 | 4 | 5 | /** 6 | * Multiplies two numbers. Equivalent to `a * b` but curried. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Math 11 | * @sig Number -> Number -> Number 12 | * @param {Number} a The first value. 13 | * @param {Number} b The second value. 14 | * @return {Number} The result of `a * b`. 15 | * @example 16 | * 17 | * var double = R.multiply(2); 18 | * var triple = R.multiply(3); 19 | * double(3); //=> 6 20 | * triple(4); //=> 12 21 | * R.multiply(2, 5); //=> 10 22 | */ 23 | module.exports = _curry2(_multiply); 24 | -------------------------------------------------------------------------------- /test/tap.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('tap', function() { 7 | it('returns a function that always returns its argument', function() { 8 | var f = R.tap(R.identity); 9 | assert.strictEqual(typeof f, 'function'); 10 | assert.strictEqual(f(100), 100); 11 | }); 12 | 13 | it("may take a function as the first argument that executes with tap's argument", function() { 14 | var sideEffect = 0; 15 | assert.strictEqual(sideEffect, 0); 16 | var rv = R.tap(function(x) { sideEffect = 'string ' + x; }, 200); 17 | assert.strictEqual(rv, 200); 18 | assert.strictEqual(sideEffect, 'string 200'); 19 | }); 20 | 21 | }); 22 | -------------------------------------------------------------------------------- /lib/bench/isset.bench.js: -------------------------------------------------------------------------------- 1 | var isSet = require('../..').isSet; 2 | 3 | var unsortedBag = [8, 2, 85, 2, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 4 | var unsortedSet = [8, 2, 85, 34, 3, 23, 247, 57, 8, 0, 6, 5, 46, 54, 643]; 5 | var sortedSet = unsortedSet.sort(); 6 | var sortedBag = unsortedBag.sort(); 7 | 8 | module.exports = { 9 | name: 'isSet', 10 | tests: { 11 | 'isSet(unsortedBag)': function() { 12 | isSet(unsortedBag); 13 | }, 14 | 'isSet(unsortedSet)': function() { 15 | isSet(unsortedSet); 16 | }, 17 | 'isSet(sortedBag)': function() { 18 | isSet(sortedBag); 19 | }, 20 | 'isSet(sortedSet)': function() { 21 | isSet(sortedSet); 22 | }, 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('test', function() { 7 | it('returns true if string matches pattern', function() { 8 | assert.strictEqual(R.test(/^x/, 'xyz'), true); 9 | }); 10 | 11 | it('returns false if string does not match pattern', function() { 12 | assert.strictEqual(R.test(/^y/, 'xyz'), false); 13 | }); 14 | 15 | it('is referentially transparent', function() { 16 | var pattern = /x/g; 17 | assert.strictEqual(pattern.lastIndex, 0); 18 | assert.strictEqual(R.test(pattern, 'xyz'), true); 19 | assert.strictEqual(pattern.lastIndex, 0); 20 | assert.strictEqual(R.test(pattern, 'xyz'), true); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ramda", 3 | "main": "dist/ramda.js", 4 | "version": "0.15.1", 5 | "homepage": "https://github.com/ramda/ramda", 6 | "authors": [ 7 | "(Scott Sauyet )", 8 | "(Michael Hurley )", 9 | "(David Chambers )" 10 | ], 11 | "description": "A practical functional library for Javascript programmers.", 12 | "moduleType": [ 13 | "amd", 14 | "globals", 15 | "node" 16 | ], 17 | "keywords": [ 18 | "functional" 19 | ], 20 | "license": "MIT", 21 | "ignore": [ 22 | "**/.*", 23 | "node_modules", 24 | "bower_components", 25 | "test", 26 | "tests", 27 | "examples", 28 | "logo" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /src/union.js: -------------------------------------------------------------------------------- 1 | var _concat = require('./internal/_concat'); 2 | var _curry2 = require('./internal/_curry2'); 3 | var compose = require('./compose'); 4 | var uniq = require('./uniq'); 5 | 6 | 7 | /** 8 | * Combines two lists into a set (i.e. no duplicates) composed of the 9 | * elements of each list. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Relation 14 | * @sig [a] -> [a] -> [a] 15 | * @param {Array} as The first list. 16 | * @param {Array} bs The second list. 17 | * @return {Array} The first and second lists concatenated, with 18 | * duplicates removed. 19 | * @example 20 | * 21 | * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4] 22 | */ 23 | module.exports = _curry2(compose(uniq, _concat)); 24 | -------------------------------------------------------------------------------- /src/internal/_xtake.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XTake(n, xf) { 8 | this.xf = xf; 9 | this.n = n; 10 | } 11 | XTake.prototype['@@transducer/init'] = _xfBase.init; 12 | XTake.prototype['@@transducer/result'] = _xfBase.result; 13 | XTake.prototype['@@transducer/step'] = function(result, input) { 14 | if (this.n === 0) { 15 | return _reduced(result); 16 | } else { 17 | this.n -= 1; 18 | return this.xf['@@transducer/step'](result, input); 19 | } 20 | }; 21 | 22 | return _curry2(function _xtake(n, xf) { return new XTake(n, xf); }); 23 | })(); 24 | -------------------------------------------------------------------------------- /test/mergeAll.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('mergeAll', function() { 7 | it('merges a list of objects together into one object', function() { 8 | assert.deepEqual(R.mergeAll([{foo:1}, {bar:2}, {baz:3}]), {foo:1, bar:2, baz:3}); 9 | }); 10 | 11 | it('gives precedence to later objects in the list', function() { 12 | assert.deepEqual(R.mergeAll([{foo:1}, {foo:2}, {bar:2}]), {foo:2, bar:2}); 13 | }); 14 | 15 | it('ignores inherited properties', function() { 16 | function Foo() {} 17 | Foo.prototype.bar = 42; 18 | var foo = new Foo(); 19 | var res = R.mergeAll([foo, {fizz: 'buzz'}]); 20 | assert.deepEqual(res, {fizz: 'buzz'}); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/internal/_xdropWhile.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XDropWhile(f, xf) { 7 | this.xf = xf; 8 | this.f = f; 9 | } 10 | XDropWhile.prototype['@@transducer/init'] = _xfBase.init; 11 | XDropWhile.prototype['@@transducer/result'] = _xfBase.result; 12 | XDropWhile.prototype['@@transducer/step'] = function(result, input) { 13 | if (this.f) { 14 | if (this.f(input)) { 15 | return result; 16 | } 17 | this.f = null; 18 | } 19 | return this.xf['@@transducer/step'](result, input); 20 | }; 21 | 22 | return _curry2(function _xdropWhile(f, xf) { return new XDropWhile(f, xf); }); 23 | })(); 24 | -------------------------------------------------------------------------------- /src/reverse.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _slice = require('./internal/_slice'); 3 | 4 | 5 | /** 6 | * Returns a new list with the same elements as the original list, just 7 | * in the reverse order. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [a] -> [a] 13 | * @param {Array} list The list to reverse. 14 | * @return {Array} A copy of the list in reverse order. 15 | * @example 16 | * 17 | * R.reverse([1, 2, 3]); //=> [3, 2, 1] 18 | * R.reverse([1, 2]); //=> [2, 1] 19 | * R.reverse([1]); //=> [1] 20 | * R.reverse([]); //=> [] 21 | */ 22 | module.exports = _curry1(function reverse(list) { 23 | return _slice(list).reverse(); 24 | }); 25 | -------------------------------------------------------------------------------- /src/split.js: -------------------------------------------------------------------------------- 1 | var invoker = require('./invoker'); 2 | 3 | 4 | /** 5 | * Splits a string into an array of strings based on the given 6 | * separator. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category String 11 | * @sig String -> String -> [String] 12 | * @param {String} sep The separator string. 13 | * @param {String} str The string to separate into an array. 14 | * @return {Array} The array of strings from `str` separated by `str`. 15 | * @example 16 | * 17 | * var pathComponents = R.split('/'); 18 | * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node'] 19 | * 20 | * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd'] 21 | */ 22 | module.exports = invoker(1, 'split'); 23 | -------------------------------------------------------------------------------- /test/unfold.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('unfold', function() { 7 | it('unfolds simple functions with a starting point to create a list', function() { 8 | assert.deepEqual(R.unfold(function(n) {if (n > 0) {return [n, n - 1];}}, 10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); 9 | }); 10 | 11 | it('is cool!', function() { 12 | var fib = function(n) { 13 | var count = 0; 14 | return function(pair) { 15 | count += 1; 16 | if (count <= n) { 17 | return [pair[0], [pair[1], pair[0] + pair[1]]]; 18 | } 19 | }; 20 | }; 21 | assert.deepEqual(R.unfold(fib(10), [0, 1]), [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]); 22 | }); 23 | 24 | }); 25 | -------------------------------------------------------------------------------- /src/createMapEntry.js: -------------------------------------------------------------------------------- 1 | var _createMapEntry = require('./internal/_createMapEntry'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Creates an object containing a single key:value pair. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig String -> a -> {String:a} 12 | * @param {String} key 13 | * @param {*} val 14 | * @return {Object} 15 | * @example 16 | * 17 | * var matchPhrases = R.compose( 18 | * R.createMapEntry('must'), 19 | * R.map(R.createMapEntry('match_phrase')) 20 | * ); 21 | * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]} 22 | */ 23 | module.exports = _curry2(_createMapEntry); 24 | -------------------------------------------------------------------------------- /src/internal/_xfindLast.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XFindLast(f, xf) { 7 | this.xf = xf; 8 | this.f = f; 9 | } 10 | XFindLast.prototype['@@transducer/init'] = _xfBase.init; 11 | XFindLast.prototype['@@transducer/result'] = function(result) { 12 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last)); 13 | }; 14 | XFindLast.prototype['@@transducer/step'] = function(result, input) { 15 | if (this.f(input)) { 16 | this.last = input; 17 | } 18 | return result; 19 | }; 20 | 21 | return _curry2(function _xfindLast(f, xf) { return new XFindLast(f, xf); }); 22 | })(); 23 | -------------------------------------------------------------------------------- /src/join.js: -------------------------------------------------------------------------------- 1 | var invoker = require('./invoker'); 2 | 3 | 4 | /** 5 | * Returns a string made by inserting the `separator` between each 6 | * element and concatenating all the elements into a single string. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig String -> [a] -> String 12 | * @param {Number|String} separator The string used to separate the elements. 13 | * @param {Array} xs The elements to join into a string. 14 | * @return {String} str The string made by concatenating `xs` with `separator`. 15 | * @example 16 | * 17 | * var spacer = R.join(' '); 18 | * spacer(['a', 2, 3.4]); //=> 'a 2 3.4' 19 | * R.join('|', [1, 2, 3]); //=> '1|2|3' 20 | */ 21 | module.exports = invoker(1, 'join'); 22 | -------------------------------------------------------------------------------- /test/internal/_curry2.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var _ = require('../../src/__'); 4 | var _curry2 = require('../../src/internal/_curry2'); 5 | 6 | 7 | describe('_curry2', function() { 8 | it('supports R.__ placeholder', function() { 9 | var f = function(a, b) { return [a, b]; }; 10 | var g = _curry2(f); 11 | 12 | assert.deepEqual(g(1)(2), [1, 2]); 13 | assert.deepEqual(g(1, 2), [1, 2]); 14 | 15 | assert.deepEqual(g(_, 2)(1), [1, 2]); 16 | assert.deepEqual(g(1, _)(2), [1, 2]); 17 | 18 | assert.deepEqual(g(_, _)(1)(2), [1, 2]); 19 | assert.deepEqual(g(_, _)(1, 2), [1, 2]); 20 | assert.deepEqual(g(_, _)(_)(1, 2), [1, 2]); 21 | assert.deepEqual(g(_, _)(_, 2)(1), [1, 2]); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/partition.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('partition', function() { 7 | it('splits a list into two lists according to a predicate', function() { 8 | var pred = function(n) { return n % 2; }; 9 | assert.deepEqual(R.partition(pred, []), [[], []]); 10 | assert.deepEqual(R.partition(pred, [0, 2, 4, 6]), [[], [0, 2, 4, 6]]); 11 | assert.deepEqual(R.partition(pred, [1, 3, 5, 7]), [[1, 3, 5, 7], []]); 12 | assert.deepEqual(R.partition(pred, [0, 1, 2, 3]), [[1, 3], [0, 2]]); 13 | }); 14 | 15 | it('is curried', function() { 16 | var polarize = R.partition(Boolean); 17 | assert.deepEqual(polarize([true, 0, 1, null]), [[true, 1], [0, null]]); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /test/times.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('times', function() { 7 | it('takes a map func', function() { 8 | assert.deepEqual(R.times(R.identity, 5), [0, 1, 2, 3, 4]); 9 | assert.deepEqual(R.times(function(x) { 10 | return x * 2; 11 | }, 5), [0, 2, 4, 6, 8]); 12 | }); 13 | 14 | it('is curried', function() { 15 | var mapid = R.times(R.identity); 16 | assert.deepEqual(mapid(5), [0, 1, 2, 3, 4]); 17 | }); 18 | 19 | it('throws if second argument is not a valid array length', function() { 20 | assert.throws(function() { R.times(3)('cheers!'); }, RangeError); 21 | assert.throws(function() { R.times(R.identity, -1); }, RangeError); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /src/subtract.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Subtracts two numbers. Equivalent to `a - b` but curried. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Math 10 | * @sig Number -> Number -> Number 11 | * @param {Number} a The first value. 12 | * @param {Number} b The second value. 13 | * @return {Number} The result of `a - b`. 14 | * @example 15 | * 16 | * R.subtract(10, 8); //=> 2 17 | * 18 | * var minus5 = R.subtract(R.__, 5); 19 | * minus5(17); //=> 12 20 | * 21 | * var complementaryAngle = R.subtract(90); 22 | * complementaryAngle(30); //=> 60 23 | * complementaryAngle(72); //=> 18 24 | */ 25 | module.exports = _curry2(function subtract(a, b) { return a - b; }); 26 | -------------------------------------------------------------------------------- /test/partial.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('partial', function() { 7 | var disc = function(a, b, c) { // note disc(3, 7, 4) => 1 8 | return b * b - 4 * a * c; 9 | }; 10 | 11 | it('caches the initially supplied left-most parameters in the generated function', function() { 12 | var f = R.partial(disc, 3); 13 | assert.strictEqual(f(7, 4), 1); 14 | var g = R.partial(disc, 3, 7); 15 | assert.strictEqual(g(4), 1); 16 | }); 17 | 18 | it('correctly reports the arity of the new function', function() { 19 | var f = R.partial(disc, 3); 20 | assert.strictEqual(f.length, 2); 21 | var g = R.partial(disc, 3, 7); 22 | assert.strictEqual(g.length, 1); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/isEmpty.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Reports whether the list has zero elements. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Logic 10 | * @sig [a] -> Boolean 11 | * @param {Array} list 12 | * @return {Boolean} 13 | * @example 14 | * 15 | * R.isEmpty([1, 2, 3]); //=> false 16 | * R.isEmpty([]); //=> true 17 | * R.isEmpty(''); //=> true 18 | * R.isEmpty(null); //=> false 19 | * R.isEmpty(R.keys({})); //=> true 20 | * R.isEmpty({}); //=> false ({} does not have a length property) 21 | * R.isEmpty({length: 0}); //=> true 22 | */ 23 | module.exports = _curry1(function isEmpty(list) { 24 | return Object(list).length === 0; 25 | }); 26 | -------------------------------------------------------------------------------- /test/intersperse.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('intersperse', function() { 7 | it('interposes a separator between list items', function() { 8 | assert.deepEqual(R.intersperse('n', ['ba', 'a', 'a']), ['ba', 'n', 'a', 'n', 'a']); 9 | assert.deepEqual(R.intersperse('bar', ['foo']), ['foo']); 10 | assert.deepEqual(R.intersperse('bar', []), []); 11 | }); 12 | 13 | it('dispatches', function() { 14 | var obj = {intersperse: function(x) { return 'override ' + x; }}; 15 | assert.strictEqual(R.intersperse('x', obj), 'override x'); 16 | }); 17 | 18 | it('is curried', function() { 19 | assert.deepEqual(R.intersperse('n')(['ba', 'a', 'a']), ['ba', 'n', 'a', 'n', 'a']); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /src/contains.js: -------------------------------------------------------------------------------- 1 | var _contains = require('./internal/_contains'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Returns `true` if the specified value is equal, in `R.equals` terms, 7 | * to at least one element of the given list; `false` otherwise. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig a -> [a] -> Boolean 13 | * @param {Object} a The item to compare against. 14 | * @param {Array} list The array to consider. 15 | * @return {Boolean} `true` if the item is in the list, `false` otherwise. 16 | * 17 | * @example 18 | * 19 | * R.contains(3, [1, 2, 3]); //=> true 20 | * R.contains(4, [1, 2, 3]); //=> false 21 | * R.contains([42], [[42]]); //=> true 22 | */ 23 | module.exports = _curry2(_contains); 24 | -------------------------------------------------------------------------------- /src/internal/_path.js: -------------------------------------------------------------------------------- 1 | /** 2 | * internal path function 3 | * Takes an array, paths, indicating the deep set of keys 4 | * to find. 5 | * 6 | * @private 7 | * @memberOf R 8 | * @category Object 9 | * @param {Array} paths An array of strings to map to object properties 10 | * @param {Object} obj The object to find the path in 11 | * @return {Array} The value at the end of the path or `undefined`. 12 | * @example 13 | * 14 | * _path(['a', 'b'], {a: {b: 2}}); //=> 2 15 | */ 16 | module.exports = function _path(paths, obj) { 17 | if (obj == null) { 18 | return; 19 | } else { 20 | var val = obj; 21 | for (var idx = 0, len = paths.length; idx < len && val != null; idx += 1) { 22 | val = val[paths[idx]]; 23 | } 24 | return val; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /test/shared/internal/util.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 3 | baseMap: function(f) { 4 | return f(this.value); 5 | }, 6 | 7 | extend: function(Child, Parent) { 8 | function Ctor() { 9 | this.constructor = Child; 10 | } 11 | Ctor.prototype = Parent.prototype; 12 | Child.prototype = new Ctor(); 13 | Child.super_ = Parent.prototype; 14 | }, 15 | 16 | identity: function(x) { return x; }, 17 | 18 | notImplemented: function(str) { 19 | return function() { 20 | throw new Error(str + ' is not implemented'); 21 | }; 22 | }, 23 | 24 | notCallable: function(fn) { 25 | return function() { 26 | throw new Error(fn + ' cannot be called directly'); 27 | }; 28 | }, 29 | 30 | returnThis: function() { return this; } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /src/tail.js: -------------------------------------------------------------------------------- 1 | var _checkForMethod = require('./internal/_checkForMethod'); 2 | var _slice = require('./internal/_slice'); 3 | 4 | 5 | /** 6 | * Returns all but the first element of a list. If the list provided has the `tail` method, 7 | * it will instead return `list.tail()`. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @see R.head, R.init, R.last 13 | * @sig [a] -> [a] 14 | * @param {Array} list The array to consider. 15 | * @return {Array} A new array containing all but the first element of the input list, or an 16 | * empty list if the input list is empty. 17 | * @example 18 | * 19 | * R.tail(['fi', 'fo', 'fum']); //=> ['fo', 'fum'] 20 | */ 21 | module.exports = _checkForMethod('tail', function(list) { 22 | return _slice(list, 1); 23 | }); 24 | -------------------------------------------------------------------------------- /test/containsWith.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('containsWith', function() { 7 | var Ro = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; 8 | var So = [{a: 3}, {a: 4}, {a: 5}, {a: 6}]; 9 | var eqA = function(r, s) { return r.a === s.a; }; 10 | 11 | it('determines if an element is the list based on the predicate', function() { 12 | assert.strictEqual(R.containsWith(eqA, {a: 3}, So), true); 13 | assert.strictEqual(R.containsWith(eqA, {a: 3000}, So), false); 14 | }); 15 | it('is curried', function() { 16 | assert.strictEqual(typeof R.containsWith(eqA), 'function'); 17 | assert.strictEqual(typeof R.containsWith(eqA)({a: 3}), 'function'); 18 | assert.strictEqual(R.containsWith(eqA)({a: 3})(Ro), true); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/nthArg.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('nthArg', function() { 7 | it('returns a function which returns its nth argument', function() { 8 | assert.strictEqual(R.nthArg(0)('foo', 'bar'), 'foo'); 9 | assert.strictEqual(R.nthArg(1)('foo', 'bar'), 'bar'); 10 | assert.strictEqual(R.nthArg(2)('foo', 'bar'), undefined); 11 | }); 12 | 13 | it('accepts negative offsets', function() { 14 | assert.strictEqual(R.nthArg(-1)('foo', 'bar'), 'bar'); 15 | assert.strictEqual(R.nthArg(-2)('foo', 'bar'), 'foo'); 16 | assert.strictEqual(R.nthArg(-3)('foo', 'bar'), undefined); 17 | }); 18 | 19 | it('returns a function with length 0', function() { 20 | assert.strictEqual(R.nthArg(2).length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/partialRight.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('partialRight', function() { 7 | var disc = function(a, b, c) { // note disc(3, 7, 4) => 1 8 | return b * b - 4 * a * c; 9 | }; 10 | 11 | it('caches the initially supplied right-most parameters in the generated function', function() { 12 | var f = R.partialRight(disc, 4); 13 | assert.strictEqual(f(3, 7), 1); 14 | var g = R.partialRight(disc, 7, 4); 15 | assert.strictEqual(g(3), 1); 16 | }); 17 | 18 | it('correctly reports the arity of the new function', function() { 19 | var f = R.partialRight(disc, 4); 20 | assert.strictEqual(f.length, 2); 21 | var g = R.partialRight(disc, 7, 4); 22 | assert.strictEqual(g.length, 1); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/dissocPath.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _dissocPath = require('./internal/_dissocPath'); 3 | 4 | 5 | /** 6 | * Makes a shallow clone of an object, omitting the property at the 7 | * given path. Note that this copies and flattens prototype properties 8 | * onto the new object as well. All non-primitive properties are copied 9 | * by reference. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Object 14 | * @sig [String] -> {k: v} -> {k: v} 15 | * @param {Array} path the path to set 16 | * @param {Object} obj the object to clone 17 | * @return {Object} a new object without the property at path 18 | * @example 19 | * 20 | * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}} 21 | */ 22 | module.exports = _curry2(_dissocPath); 23 | -------------------------------------------------------------------------------- /test/toPairsIn.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('toPairsIn', function() { 7 | it('converts an object into an array of two-element [key, value] arrays', function() { 8 | assert.deepEqual(R.toPairsIn({a: 1, b: 2, c: 3}), [['a', 1], ['b', 2], ['c', 3]]); 9 | }); 10 | it("iterates properties on the object's prototype chain", function() { 11 | function sortPairs(a, b) { 12 | return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0; 13 | } 14 | var F = function() { 15 | this.x = 1; 16 | this.y = 2; 17 | }; 18 | F.prototype.protoProp = 'you can see me'; 19 | var f = new F(); 20 | assert.deepEqual(R.toPairsIn(f).sort(sortPairs), [['protoProp', 'you can see me'], ['x', 1], ['y', 2]]); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/clone.js: -------------------------------------------------------------------------------- 1 | var _baseCopy = require('./internal/_baseCopy'); 2 | var _curry1 = require('./internal/_curry1'); 3 | 4 | 5 | /** 6 | * Creates a deep copy of the value which may contain (nested) `Array`s and 7 | * `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are 8 | * not copied, but assigned by their reference. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {*} -> {*} 14 | * @param {*} value The object or array to clone 15 | * @return {*} A new object or array. 16 | * @example 17 | * 18 | * var objects = [{}, {}, {}]; 19 | * var objectsClone = R.clone(objects); 20 | * objects[0] === objectsClone[0]; //=> false 21 | */ 22 | module.exports = _curry1(function clone(value) { 23 | return _baseCopy(value, [], []); 24 | }); 25 | -------------------------------------------------------------------------------- /src/range.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns a list of numbers from `from` (inclusive) to `to` 6 | * (exclusive). 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig Number -> Number -> [Number] 12 | * @param {Number} from The first number in the list. 13 | * @param {Number} to One more than the last number in the list. 14 | * @return {Array} The list of numbers in tthe set `[a, b)`. 15 | * @example 16 | * 17 | * R.range(1, 5); //=> [1, 2, 3, 4] 18 | * R.range(50, 53); //=> [50, 51, 52] 19 | */ 20 | module.exports = _curry2(function range(from, to) { 21 | var result = []; 22 | var n = from; 23 | while (n < to) { 24 | result[result.length] = n; 25 | n += 1; 26 | } 27 | return result; 28 | }); 29 | -------------------------------------------------------------------------------- /src/defaultTo.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns the second argument if it is not null or undefined. If it is null 6 | * or undefined, the first (default) argument is returned. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Logic 11 | * @sig a -> b -> a | b 12 | * @param {a} val The default value. 13 | * @param {b} val The value to return if it is not null or undefined 14 | * @return {*} The the second value or the default value 15 | * @example 16 | * 17 | * var defaultTo42 = defaultTo(42); 18 | * 19 | * defaultTo42(null); //=> 42 20 | * defaultTo42(undefined); //=> 42 21 | * defaultTo42('Ramda'); //=> 'Ramda' 22 | */ 23 | module.exports = _curry2(function defaultTo(d, v) { 24 | return v == null ? d : v; 25 | }); 26 | -------------------------------------------------------------------------------- /test/omit.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('omit', function() { 7 | var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}; 8 | 9 | it('copies an object omitting the listed properties', function() { 10 | assert.deepEqual(R.omit(['a', 'c', 'f'], obj), {b: 2, d: 4, e: 5}); 11 | }); 12 | 13 | it('includes prototype properties', function() { 14 | var F = function(param) {this.x = param;}; 15 | F.prototype.y = 40; F.prototype.z = 50; 16 | var obj = new F(30); 17 | obj.v = 10; obj.w = 20; 18 | assert.deepEqual(R.omit(['w', 'x', 'y'], obj), {v: 10, z: 50}); 19 | }); 20 | 21 | it('is curried', function() { 22 | var skipAB = R.omit(['a', 'b']); 23 | assert.deepEqual(skipAB(obj), {c: 3, d: 4, e: 5, f: 6}); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/__.js: -------------------------------------------------------------------------------- 1 | /** 2 | * A special placeholder value used to specify "gaps" within curried functions, 3 | * allowing partial application of any combination of arguments, 4 | * regardless of their positions. 5 | * 6 | * If `g` is a curried ternary function and `_` is `R.__`, the following are equivalent: 7 | * 8 | * - `g(1, 2, 3)` 9 | * - `g(_, 2, 3)(1)` 10 | * - `g(_, _, 3)(1)(2)` 11 | * - `g(_, _, 3)(1, 2)` 12 | * - `g(_, 2, _)(1, 3)` 13 | * - `g(_, 2)(1)(3)` 14 | * - `g(_, 2)(1, 3)` 15 | * - `g(_, 2)(_, 3)(1)` 16 | * 17 | * @constant 18 | * @memberOf R 19 | * @category Function 20 | * @example 21 | * 22 | * var greet = R.replace('{name}', R.__, 'Hello, {name}!'); 23 | * greet('Alice'); //=> 'Hello, Alice!' 24 | */ 25 | module.exports = {'@@functional/placeholder': true}; 26 | -------------------------------------------------------------------------------- /lib/sauce/windows.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | { 3 | browserName: 'internet explorer', 4 | version: '11', 5 | platform: 'Windows 8.1' 6 | }, 7 | { 8 | browserName: 'internet explorer', 9 | version: '10', 10 | platform: 'Windows 8' 11 | }, 12 | { 13 | browserName: 'internet explorer', 14 | version: '9', 15 | platform: 'Windows 7' 16 | }, 17 | { 18 | browserName: 'internet explorer', 19 | version: '8', 20 | platform: 'Windows 7' 21 | }, 22 | { 23 | browserName: 'internet explorer', 24 | version: '7', 25 | platform: 'XP' 26 | }, 27 | { 28 | browserName: 'chrome', 29 | version: '38', 30 | platform: 'Windows 8.1' 31 | }, 32 | { 33 | browserName: 'firefox', 34 | version: '33', 35 | platform: 'Windows 8.1' 36 | } 37 | ]; 38 | -------------------------------------------------------------------------------- /test/ap.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('ap', function() { 7 | function mult2(x) { return x * 2; } 8 | function plus3(x) { return x + 3; } 9 | 10 | it('applies a list of functions to a list of values', function() { 11 | assert.deepEqual(R.ap([mult2, plus3], [1, 2, 3]), [2, 4, 6, 4, 5, 6]); 12 | }); 13 | 14 | it('dispatches to the passed object\'s ap method when values is a non-Array object', function() { 15 | var obj = {ap: function(n) { return 'called ap with ' + n; }}; 16 | assert.deepEqual(R.ap(obj, 10), obj.ap(10)); 17 | }); 18 | 19 | it('is curried', function() { 20 | var val = R.ap([mult2, plus3]); 21 | assert.strictEqual(typeof val, 'function'); 22 | assert.deepEqual(val([1, 2, 3]), [2, 4, 6, 4, 5, 6]); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/init.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('init', function() { 7 | it('returns an empty list for an empty list', function() { 8 | assert.deepEqual(R.init([]), []); 9 | }); 10 | it('returns a new list containing all the elements except the last element of a list', function() { 11 | assert.deepEqual(R.init(['a', 'b', 'c', 'd']), ['a', 'b', 'c']); 12 | }); 13 | it('throws if applied to null or undefined', function() { 14 | assert.throws(function() { R.init(null); }, TypeError); 15 | assert.throws(function() { R.init(undefined); }, TypeError); 16 | }); 17 | it('handles array-like object', function() { 18 | var args = (function() { return arguments; }(1, 2, 3, 4, 5)); 19 | assert.deepEqual(R.init(args), [1, 2, 3, 4]); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/insertAll.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('insertAll', function() { 7 | it('inserts a list of elements into the given list', function() { 8 | var list = ['a', 'b', 'c', 'd', 'e']; 9 | assert.deepEqual(R.insertAll(2, ['x', 'y', 'z'], list), ['a', 'b', 'x', 'y', 'z', 'c', 'd', 'e']); 10 | }); 11 | 12 | it('appends to the end of the list if the index is too large', function() { 13 | var list = ['a', 'b', 'c', 'd', 'e']; 14 | assert.deepEqual(R.insertAll(8, ['p', 'q', 'r'], list), ['a', 'b', 'c', 'd', 'e', 'p', 'q', 'r']); 15 | }); 16 | 17 | it('is curried', function() { 18 | var list = ['a', 'b', 'c', 'd', 'e']; 19 | assert.deepEqual(R.insertAll(8)(['p', 'q', 'r'], list), ['a', 'b', 'c', 'd', 'e', 'p', 'q', 'r']); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/median.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('median', function() { 7 | 8 | it('returns middle value of an odd-length list', function() { 9 | assert.strictEqual(R.median([2]), 2); 10 | assert.strictEqual(R.median([2, 9, 7]), 7); 11 | }); 12 | 13 | it('returns mean of two middle values of a nonempty even-length list', function() { 14 | assert.strictEqual(R.median([7, 2]), 4.5); 15 | assert.strictEqual(R.median([7, 2, 10, 9]), 8); 16 | }); 17 | 18 | it('returns NaN for an empty list', function() { 19 | assert.strictEqual(R.identical(NaN, R.median([])), true); 20 | }); 21 | 22 | it('handles array-like object', function() { 23 | assert.strictEqual(R.median((function() { return arguments; }(1, 2, 3))), 2); 24 | }); 25 | 26 | }); 27 | -------------------------------------------------------------------------------- /src/internal/_toISOString.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Polyfill from . 3 | */ 4 | module.exports = (function() { 5 | var pad = function pad(n) { return (n < 10 ? '0' : '') + n; }; 6 | 7 | return typeof Date.prototype.toISOString === 'function' ? 8 | function _toISOString(d) { 9 | return d.toISOString(); 10 | } : 11 | function _toISOString(d) { 12 | return ( 13 | d.getUTCFullYear() + '-' + 14 | pad(d.getUTCMonth() + 1) + '-' + 15 | pad(d.getUTCDate()) + 'T' + 16 | pad(d.getUTCHours()) + ':' + 17 | pad(d.getUTCMinutes()) + ':' + 18 | pad(d.getUTCSeconds()) + '.' + 19 | (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z' 20 | ); 21 | }; 22 | }()); 23 | -------------------------------------------------------------------------------- /src/functions.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _functionsWith = require('./internal/_functionsWith'); 3 | var keys = require('./keys'); 4 | 5 | 6 | /** 7 | * Returns a list of function names of object's own functions 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Object 12 | * @sig {*} -> [String] 13 | * @param {Object} obj The objects with functions in it 14 | * @return {Array} A list of the object's own properties that map to functions. 15 | * @example 16 | * 17 | * R.functions(R); // returns list of ramda's own function names 18 | * 19 | * var F = function() { this.x = function(){}; this.y = 1; } 20 | * F.prototype.z = function() {}; 21 | * F.prototype.a = 100; 22 | * R.functions(new F()); //=> ["x"] 23 | */ 24 | module.exports = _curry1(_functionsWith(keys)); 25 | -------------------------------------------------------------------------------- /src/zipObj.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Creates a new object out of a list of keys and a list of values. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category List 10 | * @sig [String] -> [*] -> {String: *} 11 | * @param {Array} keys The array that will be properties on the output object. 12 | * @param {Array} values The list of values on the output object. 13 | * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`. 14 | * @example 15 | * 16 | * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3} 17 | */ 18 | module.exports = _curry2(function zipObj(keys, values) { 19 | var idx = 0, len = keys.length, out = {}; 20 | while (idx < len) { 21 | out[keys[idx]] = values[idx]; 22 | idx += 1; 23 | } 24 | return out; 25 | }); 26 | -------------------------------------------------------------------------------- /test/reduced.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('reduced', function() { 7 | it('wraps a value', function() { 8 | // white box test. 9 | var v = {}; 10 | assert.strictEqual(R.reduced(v)['@@transducer/value'], v); 11 | }); 12 | 13 | it('flags value as reduced', function() { 14 | // white box test. 15 | assert.strictEqual(R.reduced({})['@@transducer/reduced'], true); 16 | }); 17 | 18 | it('short-circuits reduce', function() { 19 | // black box test. 20 | assert.strictEqual( 21 | R.reduce( 22 | function(acc, v) { 23 | var result = acc + v; 24 | if (result >= 10) {result = R.reduced(result);} 25 | return result; 26 | }, 27 | 0, 28 | [1, 2, 3, 4, 5]), 29 | 10); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/internal/_concat.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Private `concat` function to merge two array-like objects. 3 | * 4 | * @private 5 | * @param {Array|Arguments} [set1=[]] An array-like object. 6 | * @param {Array|Arguments} [set2=[]] An array-like object. 7 | * @return {Array} A new, merged array. 8 | * @example 9 | * 10 | * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3] 11 | */ 12 | module.exports = function _concat(set1, set2) { 13 | set1 = set1 || []; 14 | set2 = set2 || []; 15 | var idx; 16 | var len1 = set1.length; 17 | var len2 = set2.length; 18 | var result = []; 19 | 20 | idx = 0; 21 | while (idx < len1) { 22 | result[result.length] = set1[idx]; 23 | idx += 1; 24 | } 25 | idx = 0; 26 | while (idx < len2) { 27 | result[result.length] = set2[idx]; 28 | idx += 1; 29 | } 30 | return result; 31 | }; 32 | -------------------------------------------------------------------------------- /src/median.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _slice = require('./internal/_slice'); 3 | var mean = require('./mean'); 4 | 5 | 6 | /** 7 | * Returns the median of the given list of numbers. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Math 12 | * @sig [Number] -> Number 13 | * @param {Array} list 14 | * @return {Number} 15 | * @example 16 | * 17 | * R.median([2, 9, 7]); //=> 7 18 | * R.median([7, 2, 10, 9]); //=> 8 19 | * R.median([]); //=> NaN 20 | */ 21 | module.exports = _curry1(function median(list) { 22 | var len = list.length; 23 | if (len === 0) { 24 | return NaN; 25 | } 26 | var width = 2 - len % 2; 27 | var idx = (len - width) / 2; 28 | return mean(_slice(list).sort(function(a, b) { 29 | return a < b ? -1 : a > b ? 1 : 0; 30 | }).slice(idx, idx + width)); 31 | }); 32 | -------------------------------------------------------------------------------- /src/repeat.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var always = require('./always'); 3 | var times = require('./times'); 4 | 5 | 6 | /** 7 | * Returns a fixed list of size `n` containing a specified identical value. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig a -> n -> [a] 13 | * @param {*} value The value to repeat. 14 | * @param {Number} n The desired size of the output list. 15 | * @return {Array} A new array containing `n` `value`s. 16 | * @example 17 | * 18 | * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi'] 19 | * 20 | * var obj = {}; 21 | * var repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}] 22 | * repeatedObjs[0] === repeatedObjs[1]; //=> true 23 | */ 24 | module.exports = _curry2(function repeat(value, n) { 25 | return times(always(value), n); 26 | }); 27 | -------------------------------------------------------------------------------- /test/sort.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('sort', function() { 7 | it('sorts the elements of a list', function() { 8 | assert.deepEqual(R.sort(function(a, b) {return a - b;}, [3, 1, 8, 1, 2, 5]), [1, 1, 2, 3, 5, 8]); 9 | }); 10 | 11 | it('does not affect the list passed supplied', function() { 12 | var list = [3, 1, 8, 1, 2, 5]; 13 | assert.deepEqual(R.sort(function(a, b) {return a - b;}, list), [1, 1, 2, 3, 5, 8]); 14 | assert.deepEqual(list, [3, 1, 8, 1, 2, 5]); 15 | }); 16 | 17 | it('is curried', function() { 18 | var sortByLength = R.sort(function(a, b) {return a.length - b.length;}); 19 | assert.deepEqual(sortByLength(['one', 'two', 'three', 'four', 'five', 'six']), 20 | ['one', 'two', 'six', 'four', 'five', 'three']); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/internal/_makeFlat.js: -------------------------------------------------------------------------------- 1 | var isArrayLike = require('../isArrayLike'); 2 | 3 | 4 | /** 5 | * `_makeFlat` is a helper function that returns a one-level or fully recursive function 6 | * based on the flag passed in. 7 | * 8 | * @private 9 | */ 10 | module.exports = function _makeFlat(recursive) { 11 | return function flatt(list) { 12 | var value, result = [], idx = 0, j, ilen = list.length, jlen; 13 | while (idx < ilen) { 14 | if (isArrayLike(list[idx])) { 15 | value = recursive ? flatt(list[idx]) : list[idx]; 16 | j = 0; 17 | jlen = value.length; 18 | while (j < jlen) { 19 | result[result.length] = value[j]; 20 | j += 1; 21 | } 22 | } else { 23 | result[result.length] = list[idx]; 24 | } 25 | idx += 1; 26 | } 27 | return result; 28 | }; 29 | }; 30 | -------------------------------------------------------------------------------- /test/functionsIn.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('functionsIn', function() { 7 | 8 | function F() { 9 | this.sort = function() {}; 10 | this.map = function() {}; 11 | this.obj = {}; 12 | this.num = 4; 13 | } 14 | 15 | F.prototype.x = function() {}; 16 | F.prototype.y = function() {}; 17 | F.prototype.z = {}; 18 | 19 | var f = new F(); 20 | 21 | it('returns list of functions with prototype functions', function() { 22 | assert.deepEqual(R.functionsIn(f).sort(), ['map', 'sort', 'x', 'y']); 23 | assert.strictEqual(R.functionsIn(f).length, 4); 24 | }); 25 | 26 | it('returns an empty array if there are no functions on the object or its prototype chain', function() { 27 | function G() {} 28 | assert.deepEqual(R.functionsIn(new G()), []); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/pluck.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('pluck', function() { 7 | var people = [ 8 | {name: 'Fred', age: 23}, 9 | {name: 'Wilma', age: 21} , 10 | {name: 'Pebbles', age: 2} 11 | ]; 12 | 13 | it('returns a function that maps the appropriate property over an array', function() { 14 | var nm = R.pluck('name'); 15 | assert.strictEqual(typeof nm, 'function'); 16 | assert.deepEqual(nm(people), ['Fred', 'Wilma', 'Pebbles']); 17 | }); 18 | 19 | it('behaves as a transducer when given a transducer in list position', function() { 20 | var numbers = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; 21 | var transducer = R.compose(R.pluck('a'), R.map(R.add(1)), R.take(2)); 22 | assert.deepEqual(R.transduce(transducer, R.flip(R.append), [], numbers), [2, 3]); 23 | }); 24 | 25 | }); 26 | -------------------------------------------------------------------------------- /test/replace.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('replace', function() { 7 | 8 | it('replaces substrings of the input string', function() { 9 | assert.strictEqual(R.replace('1', 'one', '1 two three'), 'one two three'); 10 | }); 11 | 12 | it('replaces regex matches of the input string', function() { 13 | assert.strictEqual(R.replace(/\d+/g, 'num', '1 2 three'), 'num num three'); 14 | }); 15 | 16 | it('is curried up to 3 arguments', function() { 17 | assert.strictEqual(R.replace(null).constructor, Function); 18 | assert.strictEqual(R.replace(null, null).constructor, Function); 19 | 20 | var replaceSemicolon = R.replace(';'); 21 | var removeSemicolon = replaceSemicolon(''); 22 | assert.strictEqual(removeSemicolon('return 42;'), 'return 42'); 23 | }); 24 | 25 | }); 26 | -------------------------------------------------------------------------------- /test/takeWhile.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('takeWhile', function() { 7 | it('continues taking elements while the function reports `true`', function() { 8 | assert.deepEqual(R.takeWhile(function(x) {return x !== 5;}, [1, 3, 5, 7, 9]), [1, 3]); 9 | }); 10 | 11 | it('starts at the right arg and acknowledges undefined', function() { 12 | assert.deepEqual(R.takeWhile(function() { assert(false); }, []), []); 13 | assert.deepEqual(R.takeWhile(function(x) {return x !== void 0;}, [1, 3, void 0, 5, 7]), [1, 3]); 14 | }); 15 | 16 | it('is curried', function() { 17 | var takeUntil7 = R.takeWhile(function(x) {return x !== 7;}); 18 | assert.deepEqual(takeUntil7([1, 3, 5, 7, 9]), [1, 3, 5]); 19 | assert.deepEqual(takeUntil7([2, 4, 6, 8, 10]), [2, 4, 6, 8, 10]); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /src/internal/_extend.js: -------------------------------------------------------------------------------- 1 | var keys = require('../keys'); 2 | 3 | 4 | /** 5 | * Assigns own enumerable properties of the other object to the destination 6 | * object preferring items in other. 7 | * 8 | * @private 9 | * @memberOf R 10 | * @category Object 11 | * @param {Object} destination The destination object. 12 | * @param {Object} other The other object to merge with destination. 13 | * @return {Object} The destination object. 14 | * @example 15 | * 16 | * _extend({ 'name': 'fred', 'age': 10 }, { 'age': 40 }); 17 | * //=> { 'name': 'fred', 'age': 40 } 18 | */ 19 | module.exports = function _extend(destination, other) { 20 | var props = keys(other); 21 | var idx = 0, length = props.length; 22 | while (idx < length) { 23 | destination[props[idx]] = other[props[idx]]; 24 | idx += 1; 25 | } 26 | return destination; 27 | }; 28 | -------------------------------------------------------------------------------- /src/internal/_xfindLastIndex.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _xfBase = require('./_xfBase'); 3 | 4 | 5 | module.exports = (function() { 6 | function XFindLastIndex(f, xf) { 7 | this.xf = xf; 8 | this.f = f; 9 | this.idx = -1; 10 | this.lastIdx = -1; 11 | } 12 | XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init; 13 | XFindLastIndex.prototype['@@transducer/result'] = function(result) { 14 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx)); 15 | }; 16 | XFindLastIndex.prototype['@@transducer/step'] = function(result, input) { 17 | this.idx += 1; 18 | if (this.f(input)) { 19 | this.lastIdx = this.idx; 20 | } 21 | return result; 22 | }; 23 | 24 | return _curry2(function _xfindLastIndex(f, xf) { return new XFindLastIndex(f, xf); }); 25 | })(); 26 | -------------------------------------------------------------------------------- /src/assoc.js: -------------------------------------------------------------------------------- 1 | var _assoc = require('./internal/_assoc'); 2 | var _curry3 = require('./internal/_curry3'); 3 | 4 | 5 | /** 6 | * Makes a shallow clone of an object, setting or overriding the specified 7 | * property with the given value. Note that this copies and flattens 8 | * prototype properties onto the new object as well. All non-primitive 9 | * properties are copied by reference. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Object 14 | * @sig String -> a -> {k: v} -> {k: v} 15 | * @param {String} prop the property name to set 16 | * @param {*} val the new value 17 | * @param {Object} obj the object to clone 18 | * @return {Object} a new object similar to the original except for the specified property. 19 | * @example 20 | * 21 | * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} 22 | */ 23 | module.exports = _curry3(_assoc); 24 | -------------------------------------------------------------------------------- /src/eqProps.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | var equals = require('./equals'); 3 | 4 | 5 | /** 6 | * Reports whether two objects have the same value, in `R.equals` terms, 7 | * for the specified property. Useful as a curried predicate. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Object 12 | * @sig k -> {k: v} -> {k: v} -> Boolean 13 | * @param {String} prop The name of the property to compare 14 | * @param {Object} obj1 15 | * @param {Object} obj2 16 | * @return {Boolean} 17 | * 18 | * @example 19 | * 20 | * var o1 = { a: 1, b: 2, c: 3, d: 4 }; 21 | * var o2 = { a: 10, b: 20, c: 3, d: 40 }; 22 | * R.eqProps('a', o1, o2); //=> false 23 | * R.eqProps('c', o1, o2); //=> true 24 | */ 25 | module.exports = _curry3(function eqProps(prop, obj1, obj2) { 26 | return equals(obj1[prop], obj2[prop]); 27 | }); 28 | -------------------------------------------------------------------------------- /src/isSet.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _indexOf = require('./internal/_indexOf'); 3 | 4 | 5 | /** 6 | * Returns `true` if all elements are unique, in `R.equals` terms, 7 | * otherwise `false`. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig [a] -> Boolean 13 | * @param {Array} list The array to consider. 14 | * @return {Boolean} `true` if all elements are unique, else `false`. 15 | * @example 16 | * 17 | * R.isSet(['1', 1]); //=> true 18 | * R.isSet([1, 1]); //=> false 19 | * R.isSet([[42], [42]]); //=> false 20 | */ 21 | module.exports = _curry1(function isSet(list) { 22 | var len = list.length; 23 | var idx = 0; 24 | while (idx < len) { 25 | if (_indexOf(list, list[idx], idx + 1) >= 0) { 26 | return false; 27 | } 28 | idx += 1; 29 | } 30 | return true; 31 | }); 32 | -------------------------------------------------------------------------------- /src/omit.js: -------------------------------------------------------------------------------- 1 | var _contains = require('./internal/_contains'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Returns a partial copy of an object omitting the keys specified. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig [String] -> {String: *} -> {String: *} 12 | * @param {Array} names an array of String property names to omit from the new object 13 | * @param {Object} obj The object to copy from 14 | * @return {Object} A new object with properties from `names` not on it. 15 | * @example 16 | * 17 | * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3} 18 | */ 19 | module.exports = _curry2(function omit(names, obj) { 20 | var result = {}; 21 | for (var prop in obj) { 22 | if (!_contains(prop, names)) { 23 | result[prop] = obj[prop]; 24 | } 25 | } 26 | return result; 27 | }); 28 | -------------------------------------------------------------------------------- /src/comparator.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Makes a comparator function out of a function that reports whether the first element is less than the second. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category Function 10 | * @sig (a, b -> Boolean) -> (a, b -> Number) 11 | * @param {Function} pred A predicate function of arity two. 12 | * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`. 13 | * @example 14 | * 15 | * var cmp = R.comparator(function(a, b) { 16 | * return a.age < b.age; 17 | * }); 18 | * var people = [ 19 | * // ... 20 | * ]; 21 | * R.sort(cmp, people); 22 | */ 23 | module.exports = _curry1(function comparator(pred) { 24 | return function(a, b) { 25 | return pred(a, b) ? -1 : pred(b, a) ? 1 : 0; 26 | }; 27 | }); 28 | -------------------------------------------------------------------------------- /test/range.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('range', function() { 7 | it('returns list of numbers', function() { 8 | assert.deepEqual(R.range(0, 5), [0, 1, 2, 3, 4]); 9 | assert.deepEqual(R.range(4, 7), [4, 5, 6]); 10 | }); 11 | 12 | it('returns the empty list if the first parameter is not larger than the second', function() { 13 | assert.deepEqual(R.range(7, 3), []); 14 | assert.deepEqual(R.range(5, 5), []); 15 | }); 16 | 17 | it('is curried', function() { 18 | var from10 = R.range(10); 19 | assert.deepEqual(from10(15), [10, 11, 12, 13, 14]); 20 | }); 21 | 22 | it('returns an empty array if from > to', function() { 23 | var result = R.range(10, 5); 24 | assert.deepEqual(result, []); 25 | result.push(5); 26 | assert.deepEqual(R.range(10, 5), []); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /src/append.js: -------------------------------------------------------------------------------- 1 | var _append = require('./internal/_append'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Returns a new list containing the contents of the given list, followed by the given 7 | * element. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig a -> [a] -> [a] 13 | * @param {*} el The element to add to the end of the new list. 14 | * @param {Array} list The list whose contents will be added to the beginning of the output 15 | * list. 16 | * @return {Array} A new list containing the contents of the old list followed by `el`. 17 | * @example 18 | * 19 | * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests'] 20 | * R.append('tests', []); //=> ['tests'] 21 | * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']] 22 | */ 23 | module.exports = _curry2(_append); 24 | -------------------------------------------------------------------------------- /src/fromPairs.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _isArray = require('./internal/_isArray'); 3 | 4 | 5 | /** 6 | * Creates a new object out of a list key-value pairs. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig [[k,v]] -> {k: v} 12 | * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object. 13 | * @return {Object} The object made by pairing up `keys` and `values`. 14 | * @example 15 | * 16 | * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3} 17 | */ 18 | module.exports = _curry1(function fromPairs(pairs) { 19 | var idx = 0, len = pairs.length, out = {}; 20 | while (idx < len) { 21 | if (_isArray(pairs[idx]) && pairs[idx].length) { 22 | out[pairs[idx][0]] = pairs[idx][1]; 23 | } 24 | idx += 1; 25 | } 26 | return out; 27 | }); 28 | -------------------------------------------------------------------------------- /src/intersection.js: -------------------------------------------------------------------------------- 1 | var _contains = require('./internal/_contains'); 2 | var _curry2 = require('./internal/_curry2'); 3 | var _filter = require('./internal/_filter'); 4 | var flip = require('./flip'); 5 | var uniq = require('./uniq'); 6 | 7 | 8 | /** 9 | * Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Relation 14 | * @sig [a] -> [a] -> [a] 15 | * @param {Array} list1 The first list. 16 | * @param {Array} list2 The second list. 17 | * @see R.intersectionWith 18 | * @return {Array} The list of elements found in both `list1` and `list2`. 19 | * @example 20 | * 21 | * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3] 22 | */ 23 | module.exports = _curry2(function intersection(list1, list2) { 24 | return uniq(_filter(flip(_contains)(list1), list2)); 25 | }); 26 | -------------------------------------------------------------------------------- /test/zipObj.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('zipObj', function() { 7 | it('combines an array of keys with an arrau of values into a single object', function() { 8 | assert.deepEqual(R.zipObj(['a', 'b', 'c'], [1, 2, 3]), {a: 1, b: 2, c: 3}); 9 | }); 10 | it('ignores extra values', function() { 11 | assert.deepEqual(R.zipObj(['a', 'b', 'c'], [1, 2, 3, 4, 5, 6, 7]), {a: 1, b: 2, c: 3}); 12 | }); 13 | it('extra keys are undefined', function() { 14 | assert.deepEqual(R.zipObj(['a', 'b', 'c', 'd', 'e', 'f'], [1, 2, 3]), 15 | {a: 1, b: 2, c: 3, d: undefined, e: undefined, f: undefined}); 16 | }); 17 | it('last one in wins when there are duplicate keys', function() { 18 | assert.deepEqual(R.zipObj(['a', 'b', 'c', 'a'], [1, 2, 3, 'LAST']), {a: 'LAST', b: 2, c: 3}); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/values.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var keys = require('./keys'); 3 | 4 | 5 | /** 6 | * Returns a list of all the enumerable own properties of the supplied object. 7 | * Note that the order of the output array is not guaranteed across 8 | * different JS platforms. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {k: v} -> [v] 14 | * @param {Object} obj The object to extract values from 15 | * @return {Array} An array of the values of the object's own properties. 16 | * @example 17 | * 18 | * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3] 19 | */ 20 | module.exports = _curry1(function values(obj) { 21 | var props = keys(obj); 22 | var len = props.length; 23 | var vals = []; 24 | var idx = 0; 25 | while (idx < len) { 26 | vals[idx] = obj[props[idx]]; 27 | idx += 1; 28 | } 29 | return vals; 30 | }); 31 | -------------------------------------------------------------------------------- /test/zipWith.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('zipWith', function() { 7 | var a = [1, 2, 3], b = [100, 200, 300], c = [10, 20, 30, 40, 50, 60]; 8 | var add = function(a, b) { return a + b; }; 9 | var x = function(a, b) { return a * b; }; 10 | var s = function(a, b) { return a + ' cow ' + b; }; 11 | it('returns an array created by applying its passed-in function pair-wise on its passed in arrays', function() { 12 | assert.deepEqual(R.zipWith(add, a, b), [101, 202, 303]); 13 | assert.deepEqual(R.zipWith(x, a, b), [100, 400, 900]); 14 | assert.deepEqual(R.zipWith(s, a, b), ['1 cow 100', '2 cow 200', '3 cow 300']); 15 | }); 16 | 17 | it('returns an array whose length is equal to the shorter of its input arrays', function() { 18 | assert.strictEqual(R.zipWith(add, a, c).length, a.length); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/modulo.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Divides the second parameter by the first and returns the remainder. 6 | * Note that this functions preserves the JavaScript-style behavior for 7 | * modulo. For mathematical modulo see `mathMod` 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Math 12 | * @sig Number -> Number -> Number 13 | * @param {Number} a The value to the divide. 14 | * @param {Number} b The pseudo-modulus 15 | * @return {Number} The result of `b % a`. 16 | * @see R.mathMod 17 | * @example 18 | * 19 | * R.modulo(17, 3); //=> 2 20 | * // JS behavior: 21 | * R.modulo(-17, 3); //=> -2 22 | * R.modulo(17, -3); //=> 2 23 | * 24 | * var isOdd = R.modulo(R.__, 2); 25 | * isOdd(42); //=> 0 26 | * isOdd(21); //=> 1 27 | */ 28 | module.exports = _curry2(function modulo(a, b) { return a % b; }); 29 | -------------------------------------------------------------------------------- /src/or.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _hasMethod = require('./internal/_hasMethod'); 3 | 4 | 5 | /** 6 | * A function that returns the first truthy of two arguments otherwise the 7 | * last argument. Note that this is NOT short-circuited, meaning that if 8 | * expressions are passed they are both evaluated. 9 | * 10 | * Dispatches to the `or` method of the first argument if applicable. 11 | * 12 | * @func 13 | * @memberOf R 14 | * @category Logic 15 | * @sig * -> * -> * 16 | * @param {*} a any value 17 | * @param {*} b any other value 18 | * @return {*} the first truthy argument, otherwise the last argument. 19 | * @example 20 | * 21 | * R.or(false, true); //=> true 22 | * R.or(0, []); //=> [] 23 | * R.or(null, ''); => '' 24 | */ 25 | module.exports = _curry2(function or(a, b) { 26 | return _hasMethod('or', a) ? a.or(b) : a || b; 27 | }); 28 | -------------------------------------------------------------------------------- /src/none.js: -------------------------------------------------------------------------------- 1 | var _any = require('./internal/_any'); 2 | var _complement = require('./internal/_complement'); 3 | var _curry2 = require('./internal/_curry2'); 4 | var _dispatchable = require('./internal/_dispatchable'); 5 | var _xany = require('./internal/_xany'); 6 | 7 | 8 | /** 9 | * Returns `true` if no elements of the list match the predicate, 10 | * `false` otherwise. 11 | * 12 | * @func 13 | * @memberOf R 14 | * @category List 15 | * @sig (a -> Boolean) -> [a] -> Boolean 16 | * @param {Function} fn The predicate function. 17 | * @param {Array} list The array to consider. 18 | * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise. 19 | * @example 20 | * 21 | * R.none(R.isNaN, [1, 2, 3]); //=> true 22 | * R.none(R.isNaN, [1, 2, 3, NaN]); //=> false 23 | */ 24 | module.exports = _curry2(_complement(_dispatchable('any', _xany, _any))); 25 | -------------------------------------------------------------------------------- /test/always.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('always', function() { 7 | it('returns a function that returns the object initially supplied', function() { 8 | var theMeaning = R.always(42); 9 | assert.strictEqual(theMeaning(), 42); 10 | assert.strictEqual(theMeaning(10), 42); 11 | assert.strictEqual(theMeaning(false), 42); 12 | }); 13 | 14 | it('works with various types', function() { 15 | assert.strictEqual(R.always(false)(), false); 16 | assert.strictEqual(R.always('abc')(), 'abc'); 17 | assert.deepEqual(R.always({a: 1, b: 2})(), {a: 1, b: 2}); 18 | var obj = {a: 1, b: 2}; 19 | assert.strictEqual(R.always(obj)(), obj); 20 | var now = new Date(1776, 6, 4); 21 | assert.deepEqual(R.always(now)(), new Date(1776, 6, 4)); 22 | assert.strictEqual(R.always(undefined)(), undefined); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. Fork the project and clone your fork. 4 | 5 | 2. Create a local feature branch: 6 | 7 | $ git checkout -b 8 | 9 | 3. If adding a function `R.foo`, define it in __src/foo.js__ and include unit 10 | tests in __test/foo.js__. If adding an internal function `_foo`, define it 11 | in __src/internal/_foo.js__. 12 | 13 | 4. Make one or more atomic commits. Each commit should have a descriptive 14 | commit message, wrapped at 72 characters. Do not commit changes to 15 | __dist/ramda.js__. 16 | 17 | 5. Run `npm test` (or `make test lint`) and address any errors. Preferably, 18 | fix commits in place using `git rebase` or `git commit --amend` to make the 19 | changes easier to review and to keep the history tidy. 20 | 21 | 6. Push to your fork: 22 | 23 | $ git push origin 24 | 25 | 7. Open a pull request. 26 | -------------------------------------------------------------------------------- /src/ap.js: -------------------------------------------------------------------------------- 1 | var _concat = require('./internal/_concat'); 2 | var _curry2 = require('./internal/_curry2'); 3 | var _hasMethod = require('./internal/_hasMethod'); 4 | var _reduce = require('./internal/_reduce'); 5 | var map = require('./map'); 6 | 7 | 8 | /** 9 | * ap applies a list of functions to a list of values. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Function 14 | * @sig [f] -> [a] -> [f a] 15 | * @param {Array} fns An array of functions 16 | * @param {Array} vs An array of values 17 | * @return {Array} An array of results of applying each of `fns` to all of `vs` in turn. 18 | * @example 19 | * 20 | * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6] 21 | */ 22 | module.exports = _curry2(function ap(fns, vs) { 23 | return _hasMethod('ap', fns) ? fns.ap(vs) : _reduce(function(acc, fn) { 24 | return _concat(acc, map(fn, vs)); 25 | }, [], fns); 26 | }); 27 | -------------------------------------------------------------------------------- /src/internal/_xall.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XAll(f, xf) { 8 | this.xf = xf; 9 | this.f = f; 10 | this.all = true; 11 | } 12 | XAll.prototype['@@transducer/init'] = _xfBase.init; 13 | XAll.prototype['@@transducer/result'] = function(result) { 14 | if (this.all) { 15 | result = this.xf['@@transducer/step'](result, true); 16 | } 17 | return this.xf['@@transducer/result'](result); 18 | }; 19 | XAll.prototype['@@transducer/step'] = function(result, input) { 20 | if (!this.f(input)) { 21 | this.all = false; 22 | result = _reduced(this.xf['@@transducer/step'](result, false)); 23 | } 24 | return result; 25 | }; 26 | 27 | return _curry2(function _xall(f, xf) { return new XAll(f, xf); }); 28 | })(); 29 | -------------------------------------------------------------------------------- /src/internal/_xany.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XAny(f, xf) { 8 | this.xf = xf; 9 | this.f = f; 10 | this.any = false; 11 | } 12 | XAny.prototype['@@transducer/init'] = _xfBase.init; 13 | XAny.prototype['@@transducer/result'] = function(result) { 14 | if (!this.any) { 15 | result = this.xf['@@transducer/step'](result, false); 16 | } 17 | return this.xf['@@transducer/result'](result); 18 | }; 19 | XAny.prototype['@@transducer/step'] = function(result, input) { 20 | if (this.f(input)) { 21 | this.any = true; 22 | result = _reduced(this.xf['@@transducer/step'](result, true)); 23 | } 24 | return result; 25 | }; 26 | 27 | return _curry2(function _xany(f, xf) { return new XAny(f, xf); }); 28 | })(); 29 | -------------------------------------------------------------------------------- /test/call.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('call', function() { 7 | it('returns the result of calling its first argument with the remaining arguments', function() { 8 | assert.strictEqual(R.call(Math.max, 1, 2, 3, -99, 42, 6, 7), 42); 9 | }); 10 | 11 | it('accepts one or more arguments', function() { 12 | var fn = function() { return arguments.length; }; 13 | assert.strictEqual(R.call(fn), 0); 14 | assert.strictEqual(R.call(fn, 'x'), 1); 15 | assert.strictEqual(R.call(fn, 'x', 'y'), 2); 16 | assert.strictEqual(R.call(fn, 'x', 'y', 'z'), 3); 17 | }); 18 | 19 | it('provides no way to specify context', function() { 20 | var obj = {method: function() { return this === obj; }}; 21 | assert.strictEqual(R.call(obj.method), false); 22 | assert.strictEqual(R.call(R.bind(obj.method, obj)), true); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/and.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _hasMethod = require('./internal/_hasMethod'); 3 | 4 | 5 | /** 6 | * A function that returns the first argument if it's falsy otherwise the second 7 | * argument. Note that this is NOT short-circuited, meaning that if expressions 8 | * are passed they are both evaluated. 9 | * 10 | * Dispatches to the `and` method of the first argument if applicable. 11 | * 12 | * @func 13 | * @memberOf R 14 | * @category Logic 15 | * @sig * -> * -> * 16 | * @param {*} a any value 17 | * @param {*} b any other value 18 | * @return {*} the first argument if falsy otherwise the second argument. 19 | * @example 20 | * 21 | * R.and(false, true); //=> false 22 | * R.and(0, []); //=> 0 23 | * R.and(null, ''); => null 24 | */ 25 | module.exports = _curry2(function and(a, b) { 26 | return _hasMethod('and', a) ? a.and(b) : a && b; 27 | }); 28 | -------------------------------------------------------------------------------- /src/commute.js: -------------------------------------------------------------------------------- 1 | var commuteMap = require('./commuteMap'); 2 | var identity = require('./identity'); 3 | 4 | 5 | /** 6 | * Turns a list of Functors into a Functor of a list. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @see R.commuteMap 12 | * @sig Functor f => (x -> f x) -> [f a] -> f [a] 13 | * @param {Function} of A function that returns the data type to return 14 | * @param {Array} list An array of functors of the same type 15 | * @return {*} 16 | * @example 17 | * 18 | * R.commute(R.of, [[1], [2, 3]]); //=> [[1, 2], [1, 3]] 19 | * R.commute(R.of, [[1, 2], [3]]); //=> [[1, 3], [2, 3]] 20 | * R.commute(R.of, [[1], [2], [3]]); //=> [[1, 2, 3]] 21 | * R.commute(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3]) 22 | * R.commute(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing() 23 | */ 24 | module.exports = commuteMap(identity); 25 | -------------------------------------------------------------------------------- /src/keysIn.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Returns a list containing the names of all the 6 | * properties of the supplied object, including prototype properties. 7 | * Note that the order of the output array is not guaranteed to be 8 | * consistent across different JS platforms. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {k: v} -> [k] 14 | * @param {Object} obj The object to extract properties from 15 | * @return {Array} An array of the object's own and prototype properties. 16 | * @example 17 | * 18 | * var F = function() { this.x = 'X'; }; 19 | * F.prototype.y = 'Y'; 20 | * var f = new F(); 21 | * R.keysIn(f); //=> ['x', 'y'] 22 | */ 23 | module.exports = _curry1(function keysIn(obj) { 24 | var prop, ks = []; 25 | for (prop in obj) { 26 | ks[ks.length] = prop; 27 | } 28 | return ks; 29 | }); 30 | -------------------------------------------------------------------------------- /src/valuesIn.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Returns a list of all the properties, including prototype properties, 6 | * of the supplied object. 7 | * Note that the order of the output array is not guaranteed to be 8 | * consistent across different JS platforms. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {k: v} -> [v] 14 | * @param {Object} obj The object to extract values from 15 | * @return {Array} An array of the values of the object's own and prototype properties. 16 | * @example 17 | * 18 | * var F = function() { this.x = 'X'; }; 19 | * F.prototype.y = 'Y'; 20 | * var f = new F(); 21 | * R.valuesIn(f); //=> ['X', 'Y'] 22 | */ 23 | module.exports = _curry1(function valuesIn(obj) { 24 | var prop, vs = []; 25 | for (prop in obj) { 26 | vs[vs.length] = obj[prop]; 27 | } 28 | return vs; 29 | }); 30 | -------------------------------------------------------------------------------- /lib/sauce/android.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | { 3 | browserName: 'android', 4 | version: '4.4', 5 | deviceName: 'Android', 6 | 'device-orientation': 'portrait', 7 | platform: 'Linux' 8 | }, 9 | { 10 | browserName: 'android', 11 | version: '4.3', 12 | deviceName: 'Android', 13 | 'device-orientation': 'portrait', 14 | platform: 'Linux' 15 | }, 16 | { 17 | browserName: 'android', 18 | version: '4.2', 19 | deviceName: 'Android', 20 | 'device-orientation': 'portrait', 21 | platform: 'Linux' 22 | }, 23 | { 24 | browserName: 'android', 25 | version: '4.1', 26 | deviceName: 'Android', 27 | 'device-orientation': 'portrait', 28 | platform: 'Linux' 29 | }, 30 | { 31 | browserName: 'android', 32 | version: '4.0', 33 | deviceName: 'Android', 34 | 'device-orientation': 'portrait', 35 | platform: 'Linux' 36 | } 37 | ]; 38 | -------------------------------------------------------------------------------- /src/bind.js: -------------------------------------------------------------------------------- 1 | var _arity = require('./internal/_arity'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Creates a function that is bound to a context. 7 | * Note: `R.bind` does not provide the additional argument-binding capabilities of 8 | * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind). 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Function 13 | * @category Object 14 | * @see R.partial 15 | * @sig (* -> *) -> {*} -> (* -> *) 16 | * @param {Function} fn The function to bind to context 17 | * @param {Object} thisObj The context to bind `fn` to 18 | * @return {Function} A function that will execute in the context of `thisObj`. 19 | */ 20 | module.exports = _curry2(function bind(fn, thisObj) { 21 | return _arity(fn.length, function() { 22 | return fn.apply(thisObj, arguments); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/containsWith.js: -------------------------------------------------------------------------------- 1 | var _containsWith = require('./internal/_containsWith'); 2 | var _curry3 = require('./internal/_curry3'); 3 | 4 | 5 | /** 6 | * Returns `true` if the `x` is found in the `list`, using `pred` as an 7 | * equality predicate for `x`. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category List 12 | * @sig (a, a -> Boolean) -> a -> [a] -> Boolean 13 | * @param {Function} pred A predicate used to test whether two items are equal. 14 | * @param {*} x The item to find 15 | * @param {Array} list The list to iterate over 16 | * @return {Boolean} `true` if `x` is in `list`, else `false`. 17 | * @example 18 | * 19 | * var xs = [{x: 12}, {x: 11}, {x: 10}]; 20 | * R.containsWith(function(a, b) { return a.x === b.x; }, {x: 10}, xs); //=> true 21 | * R.containsWith(function(a, b) { return a.x === b.x; }, {x: 1}, xs); //=> false 22 | */ 23 | module.exports = _curry3(_containsWith); 24 | -------------------------------------------------------------------------------- /src/functionsIn.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _functionsWith = require('./internal/_functionsWith'); 3 | var keysIn = require('./keysIn'); 4 | 5 | 6 | /** 7 | * Returns a list of function names of object's own and prototype functions 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Object 12 | * @sig {*} -> [String] 13 | * @param {Object} obj The objects with functions in it 14 | * @return {Array} A list of the object's own properties and prototype 15 | * properties that map to functions. 16 | * @example 17 | * 18 | * R.functionsIn(R); // returns list of ramda's own and prototype function names 19 | * 20 | * var F = function() { this.x = function(){}; this.y = 1; } 21 | * F.prototype.z = function() {}; 22 | * F.prototype.a = 100; 23 | * R.functionsIn(new F()); //=> ["x", "z"] 24 | */ 25 | module.exports = _curry1(_functionsWith(keysIn)); 26 | -------------------------------------------------------------------------------- /src/indexOf.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _hasMethod = require('./internal/_hasMethod'); 3 | var _indexOf = require('./internal/_indexOf'); 4 | 5 | 6 | /** 7 | * Returns the position of the first occurrence of an item in an array, 8 | * or -1 if the item is not included in the array. `R.equals` is used to 9 | * determine equality. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category List 14 | * @sig a -> [a] -> Number 15 | * @param {*} target The item to find. 16 | * @param {Array} xs The array to search in. 17 | * @return {Number} the index of the target, or -1 if the target is not found. 18 | * 19 | * @example 20 | * 21 | * R.indexOf(3, [1,2,3,4]); //=> 2 22 | * R.indexOf(10, [1,2,3,4]); //=> -1 23 | */ 24 | module.exports = _curry2(function indexOf(target, xs) { 25 | return _hasMethod('indexOf', xs) ? xs.indexOf(target) : _indexOf(xs, target, 0); 26 | }); 27 | -------------------------------------------------------------------------------- /src/merge.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _extend = require('./internal/_extend'); 3 | 4 | 5 | /** 6 | * Create a new object with the own properties of `a` 7 | * merged with the own properties of object `b`. 8 | * This function will *not* mutate passed-in objects. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {k: v} -> {k: v} -> {k: v} 14 | * @param {Object} a source object 15 | * @param {Object} b object with higher precedence in output 16 | * @return {Object} The destination object. 17 | * @example 18 | * 19 | * R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 }); 20 | * //=> { 'name': 'fred', 'age': 40 } 21 | * 22 | * var resetToDefault = R.merge(R.__, {x: 0}); 23 | * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2} 24 | */ 25 | module.exports = _curry2(function merge(a, b) { 26 | return _extend(_extend({}, a), b); 27 | }); 28 | -------------------------------------------------------------------------------- /src/splitEvery.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var slice = require('./slice'); 3 | 4 | 5 | /** 6 | * Splits a collection into slices of the specified length. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig Number -> [a] -> [[a]] 12 | * @sig Number -> String -> [String] 13 | * @param {Number} n 14 | * @param {Array} list 15 | * @return {Array} 16 | * @example 17 | * 18 | * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]] 19 | * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz'] 20 | */ 21 | module.exports = _curry2(function splitEvery(n, list) { 22 | if (n <= 0) { 23 | throw new Error('First argument to splitEvery must be a positive integer'); 24 | } 25 | var result = []; 26 | var idx = 0; 27 | while (idx < list.length) { 28 | result.push(slice(idx, idx += n, list)); 29 | } 30 | return result; 31 | }); 32 | -------------------------------------------------------------------------------- /src/internal/_xfind.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XFind(f, xf) { 8 | this.xf = xf; 9 | this.f = f; 10 | this.found = false; 11 | } 12 | XFind.prototype['@@transducer/init'] = _xfBase.init; 13 | XFind.prototype['@@transducer/result'] = function(result) { 14 | if (!this.found) { 15 | result = this.xf['@@transducer/step'](result, void 0); 16 | } 17 | return this.xf['@@transducer/result'](result); 18 | }; 19 | XFind.prototype['@@transducer/step'] = function(result, input) { 20 | if (this.f(input)) { 21 | this.found = true; 22 | result = _reduced(this.xf['@@transducer/step'](result, input)); 23 | } 24 | return result; 25 | }; 26 | 27 | return _curry2(function _xfind(f, xf) { return new XFind(f, xf); }); 28 | })(); 29 | -------------------------------------------------------------------------------- /src/chain.js: -------------------------------------------------------------------------------- 1 | var _chain = require('./internal/_chain'); 2 | var _curry2 = require('./internal/_curry2'); 3 | var _dispatchable = require('./internal/_dispatchable'); 4 | var _xchain = require('./internal/_xchain'); 5 | 6 | /** 7 | * `chain` maps a function over a list and concatenates the results. 8 | * This implementation is compatible with the 9 | * Fantasy-land Chain spec, and will work with types that implement that spec. 10 | * `chain` is also known as `flatMap` in some libraries 11 | * 12 | * @func 13 | * @memberOf R 14 | * @category List 15 | * @sig (a -> [b]) -> [a] -> [b] 16 | * @param {Function} fn 17 | * @param {Array} list 18 | * @return {Array} 19 | * @example 20 | * 21 | * var duplicate = function(n) { 22 | * return [n, n]; 23 | * }; 24 | * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3] 25 | */ 26 | module.exports = _curry2(_dispatchable('chain', _xchain, _chain)); 27 | -------------------------------------------------------------------------------- /src/internal/_hasMethod.js: -------------------------------------------------------------------------------- 1 | var _isArray = require('./_isArray'); 2 | 3 | 4 | /** 5 | * Private function that determines whether or not a provided object has a given method. 6 | * Does not ignore methods stored on the object's prototype chain. Used for dynamically 7 | * dispatching Ramda methods to non-Array objects. 8 | * 9 | * @private 10 | * @param {String} methodName The name of the method to check for. 11 | * @param {Object} obj The object to test. 12 | * @return {Boolean} `true` has a given method, `false` otherwise. 13 | * @example 14 | * 15 | * var person = { name: 'John' }; 16 | * person.shout = function() { alert(this.name); }; 17 | * 18 | * _hasMethod('shout', person); //=> true 19 | * _hasMethod('foo', person); //=> false 20 | */ 21 | module.exports = function _hasMethod(methodName, obj) { 22 | return obj != null && !_isArray(obj) && typeof obj[methodName] === 'function'; 23 | }; 24 | -------------------------------------------------------------------------------- /test/hasIn.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('hasIn', function() { 7 | var fred = {name: 'Fred', age: 23}; 8 | var anon = {age: 99}; 9 | 10 | it('returns a function that checks the appropriate property', function() { 11 | var nm = R.hasIn('name'); 12 | assert.strictEqual(typeof nm, 'function'); 13 | assert.strictEqual(nm(fred), true); 14 | assert.strictEqual(nm(anon), false); 15 | }); 16 | 17 | it('checks properties from the prototype chain', function() { 18 | var Person = function() {}; 19 | Person.prototype.age = function() {}; 20 | 21 | var bob = new Person(); 22 | assert.strictEqual(R.hasIn('age', bob), true); 23 | }); 24 | 25 | it('works properly when called with two arguments', function() { 26 | assert.strictEqual(R.hasIn('name', fred), true); 27 | assert.strictEqual(R.hasIn('name', anon), false); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /src/complement.js: -------------------------------------------------------------------------------- 1 | var _complement = require('./internal/_complement'); 2 | var _curry1 = require('./internal/_curry1'); 3 | 4 | 5 | /** 6 | * Takes a function `f` and returns a function `g` such that: 7 | * 8 | * - applying `g` to zero or more arguments will give __true__ if applying 9 | * the same arguments to `f` gives a logical __false__ value; and 10 | * 11 | * - applying `g` to zero or more arguments will give __false__ if applying 12 | * the same arguments to `f` gives a logical __true__ value. 13 | * 14 | * @func 15 | * @memberOf R 16 | * @category Logic 17 | * @sig (*... -> *) -> (*... -> Boolean) 18 | * @param {Function} f 19 | * @return {Function} 20 | * @example 21 | * 22 | * var isEven = function(n) { return n % 2 === 0; }; 23 | * var isOdd = R.complement(isEven); 24 | * isOdd(21); //=> true 25 | * isOdd(42); //=> false 26 | */ 27 | module.exports = _curry1(_complement); 28 | -------------------------------------------------------------------------------- /src/update.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | var adjust = require('./adjust'); 3 | var always = require('./always'); 4 | 5 | /** 6 | * Returns a new copy of the array with the element at the 7 | * provided index replaced with the given value. 8 | * @see R.adjust 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category List 13 | * @sig Number -> a -> [a] -> [a] 14 | * @param {Number} idx The index to update. 15 | * @param {*} x The value to exist at the given index of the returned array. 16 | * @param {Array|Arguments} list The source array-like object to be updated. 17 | * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`. 18 | * @example 19 | * 20 | * R.update(1, 11, [0, 1, 2]); //=> [0, 11, 2] 21 | * R.update(1)(11)([0, 1, 2]); //=> [0, 11, 2] 22 | */ 23 | module.exports = _curry3(function(idx, x, list) { 24 | return adjust(always(x), idx, list); 25 | }); 26 | -------------------------------------------------------------------------------- /test/functions.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('functions', function() { 7 | 8 | function F() { 9 | this.sort = function() {}; 10 | this.map = function() {}; 11 | this.obj = {}; 12 | this.num = 4; 13 | } 14 | 15 | F.prototype.x = function() {}; 16 | F.prototype.y = function() {}; 17 | F.prototype.z = {}; 18 | 19 | var f = new F(); 20 | 21 | it('returns list of functions without prototype functions', function() { 22 | assert.deepEqual(R.functions(f).sort(), ['map', 'sort']); 23 | assert.strictEqual(R.functions(f).length, 2); 24 | assert.deepEqual(R.functions({add: R.add, reduce: R.reduce}).sort(), ['add', 'reduce']); 25 | }); 26 | 27 | it('returns an empty array if there are no functions on the object or its prototype chain', function() { 28 | function G() {} 29 | assert.deepEqual(R.functions(new G()), []); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /test/lt.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('lt', function() { 7 | it('reports whether one item is less than another', function() { 8 | assert.strictEqual(R.lt(3, 5), true); 9 | assert.strictEqual(R.lt(6, 4), false); 10 | assert.strictEqual(R.lt(7.0, 7.0), false); 11 | assert.strictEqual(R.lt('abc', 'xyz'), true); 12 | assert.strictEqual(R.lt('abcd', 'abc'), false); 13 | }); 14 | 15 | it('is curried', function() { 16 | var gt5 = R.lt(5); 17 | assert.strictEqual(gt5(10), true); 18 | assert.strictEqual(gt5(5), false); 19 | assert.strictEqual(gt5(3), false); 20 | }); 21 | 22 | it('behaves right curried when passed `R.__` for its first argument', function() { 23 | var lt5 = R.lt(R.__, 5); 24 | assert.strictEqual(lt5(10), false); 25 | assert.strictEqual(lt5(5), false); 26 | assert.strictEqual(lt5(3), true); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /src/dropRepeats.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _dispatchable = require('./internal/_dispatchable'); 3 | var _xdropRepeatsWith = require('./internal/_xdropRepeatsWith'); 4 | var dropRepeatsWith = require('./dropRepeatsWith'); 5 | var equals = require('./equals'); 6 | 7 | 8 | /** 9 | * Returns a new list without any consecutively repeating elements. 10 | * `R.equals` is used to determine equality. 11 | * 12 | * Acts as a transducer if a transformer is given in list position. 13 | * @see R.transduce 14 | * 15 | * @func 16 | * @memberOf R 17 | * @category List 18 | * @sig [a] -> [a] 19 | * @param {Array} list The array to consider. 20 | * @return {Array} `list` without repeating elements. 21 | * @example 22 | * 23 | * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2] 24 | */ 25 | module.exports = _curry1(_dispatchable('dropRepeats', _xdropRepeatsWith(equals), dropRepeatsWith(equals))); 26 | -------------------------------------------------------------------------------- /src/is.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * See if an object (`val`) is an instance of the supplied constructor. 6 | * This function will check up the inheritance chain, if any. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Type 11 | * @sig (* -> {*}) -> a -> Boolean 12 | * @param {Object} ctor A constructor 13 | * @param {*} val The value to test 14 | * @return {Boolean} 15 | * @example 16 | * 17 | * R.is(Object, {}); //=> true 18 | * R.is(Number, 1); //=> true 19 | * R.is(Object, 1); //=> false 20 | * R.is(String, 's'); //=> true 21 | * R.is(String, new String('')); //=> true 22 | * R.is(Object, new String('')); //=> true 23 | * R.is(Object, 's'); //=> false 24 | * R.is(Number, {}); //=> false 25 | */ 26 | module.exports = _curry2(function is(Ctor, val) { 27 | return val != null && val.constructor === Ctor || val instanceof Ctor; 28 | }); 29 | -------------------------------------------------------------------------------- /src/toPairs.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _has = require('./internal/_has'); 3 | 4 | 5 | /** 6 | * Converts an object into an array of key, value arrays. 7 | * Only the object's own properties are used. 8 | * Note that the order of the output array is not guaranteed to be 9 | * consistent across different JS platforms. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Object 14 | * @sig {String: *} -> [[String,*]] 15 | * @param {Object} obj The object to extract from 16 | * @return {Array} An array of key, value arrays from the object's own properties. 17 | * @example 18 | * 19 | * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]] 20 | */ 21 | module.exports = _curry1(function toPairs(obj) { 22 | var pairs = []; 23 | for (var prop in obj) { 24 | if (_has(prop, obj)) { 25 | pairs[pairs.length] = [prop, obj[prop]]; 26 | } 27 | } 28 | return pairs; 29 | }); 30 | -------------------------------------------------------------------------------- /src/unapply.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _slice = require('./internal/_slice'); 3 | 4 | 5 | /** 6 | * Takes a function `fn`, which takes a single array argument, and returns 7 | * a function which: 8 | * 9 | * - takes any number of positional arguments; 10 | * - passes these arguments to `fn` as an array; and 11 | * - returns the result. 12 | * 13 | * In other words, R.unapply derives a variadic function from a function 14 | * which takes an array. R.unapply is the inverse of R.apply. 15 | * 16 | * @func 17 | * @memberOf R 18 | * @category Function 19 | * @sig ([*...] -> a) -> (*... -> a) 20 | * @param {Function} fn 21 | * @return {Function} 22 | * @see R.apply 23 | * @example 24 | * 25 | * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]' 26 | */ 27 | module.exports = _curry1(function unapply(fn) { 28 | return function() { 29 | return fn(_slice(arguments)); 30 | }; 31 | }); 32 | -------------------------------------------------------------------------------- /src/has.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _has = require('./internal/_has'); 3 | 4 | 5 | /** 6 | * Returns whether or not an object has an own property with 7 | * the specified name 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Object 12 | * @sig s -> {s: x} -> Boolean 13 | * @param {String} prop The name of the property to check for. 14 | * @param {Object} obj The object to query. 15 | * @return {Boolean} Whether the property exists. 16 | * @example 17 | * 18 | * var hasName = R.has('name'); 19 | * hasName({name: 'alice'}); //=> true 20 | * hasName({name: 'bob'}); //=> true 21 | * hasName({}); //=> false 22 | * 23 | * var point = {x: 0, y: 0}; 24 | * var pointHas = R.has(R.__, point); 25 | * pointHas('x'); //=> true 26 | * pointHas('y'); //=> true 27 | * pointHas('z'); //=> false 28 | */ 29 | module.exports = _curry2(_has); 30 | -------------------------------------------------------------------------------- /test/xprod.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('xprod', function() { 7 | var a = [1, 2], b = ['a', 'b', 'c']; 8 | 9 | it('returns an empty list if either input list is empty', function() { 10 | assert.deepEqual(R.xprod([], [1, 2, 3]), []); 11 | assert.deepEqual(R.xprod([1, 2, 3], []), []); 12 | }); 13 | 14 | it('creates the collection of all cross-product pairs of its parameters', function() { 15 | assert.deepEqual(R.xprod(a, b), [[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c']]); 16 | }); 17 | 18 | it('is curried', function() { 19 | var something = R.xprod(b); 20 | assert.deepEqual(something(a), [['a', 1], ['a', 2], ['b', 1], ['b', 2], ['c', 1], ['c', 2]]); 21 | }); 22 | 23 | it('correctly reports the arity of curried versions', function() { 24 | var something = R.xprod(a); 25 | assert.deepEqual(something.length, 1); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/gt.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('gt', function() { 7 | it('reports whether one item is less than another', function() { 8 | assert.strictEqual(R.gt(3, 5), false); 9 | assert.strictEqual(R.gt(6, 4), true); 10 | assert.strictEqual(R.gt(7.0, 7.0), false); 11 | assert.strictEqual(R.gt('abc', 'xyz'), false); 12 | assert.strictEqual(R.gt('abcd', 'abc'), true); 13 | }); 14 | 15 | it('is curried', function() { 16 | var lt20 = R.gt(20); 17 | assert.strictEqual(lt20(10), true); 18 | assert.strictEqual(lt20(20), false); 19 | assert.strictEqual(lt20(25), false); 20 | }); 21 | 22 | it('behaves right curried when passed `R.__` for its first argument', function() { 23 | var gt20 = R.gt(R.__, 20); 24 | assert.strictEqual(gt20(10), false); 25 | assert.strictEqual(gt20(20), false); 26 | assert.strictEqual(gt20(25), true); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /src/internal/_predicateWrap.js: -------------------------------------------------------------------------------- 1 | var _arity = require('./_arity'); 2 | var _pluck = require('./_pluck'); 3 | var _slice = require('./_slice'); 4 | 5 | 6 | /** 7 | * Create a predicate wrapper which will call a pick function (all/any) for each predicate 8 | * 9 | * @private 10 | * @see R.all 11 | * @see R.any 12 | */ 13 | module.exports = function _predicateWrap(predPicker) { 14 | return function(preds) { 15 | var predIterator = function() { 16 | var args = arguments; 17 | return predPicker(function(predicate) { 18 | return predicate.apply(null, args); 19 | }, preds); 20 | }; 21 | return arguments.length > 1 ? 22 | // Call function immediately if given arguments 23 | predIterator.apply(null, _slice(arguments, 1)) : 24 | // Return a function which will call the predicates with the provided arguments 25 | _arity(Math.max.apply(Math, _pluck('length', preds)), predIterator); 26 | }; 27 | }; 28 | -------------------------------------------------------------------------------- /test/pipeK.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | var Identity = function(x) { 7 | this.value = x; 8 | }; 9 | 10 | Identity.prototype.chain = function(f) { 11 | return f(this.value); 12 | }; 13 | 14 | 15 | describe('pipeK', function() { 16 | 17 | it('is a variadic function', function() { 18 | assert.strictEqual(typeof R.pipeK, 'function'); 19 | assert.strictEqual(R.pipeK.length, 0); 20 | }); 21 | 22 | it('performs left-to-right Kleisli composition', function() { 23 | var f = function(x) { return new Identity(x - 1); }; 24 | var g = function(x) { return new Identity(x * x); }; 25 | var h = function(x) { return new Identity(x + 1); }; 26 | 27 | var fn = R.pipeK(f, g, h); 28 | var id = new Identity(8); 29 | 30 | assert.strictEqual(fn(id).value, 50); 31 | assert.strictEqual(fn(id).value, R.pipe(R.chain(f), R.chain(g), R.chain(h))(id).value); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /src/allPass.js: -------------------------------------------------------------------------------- 1 | var _all = require('./internal/_all'); 2 | var _predicateWrap = require('./internal/_predicateWrap'); 3 | var curry = require('./curry'); 4 | 5 | 6 | /** 7 | * Given a list of predicates, returns a new predicate that will be true exactly when all of them are. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Logic 12 | * @sig [(*... -> Boolean)] -> (*... -> Boolean) 13 | * @param {Array} list An array of predicate functions 14 | * @param {*} optional Any arguments to pass into the predicates 15 | * @return {Function} a function that applies its arguments to each of 16 | * the predicates, returning `true` if all are satisfied. 17 | * @example 18 | * 19 | * var gt10 = function(x) { return x > 10; }; 20 | * var even = function(x) { return x % 2 === 0}; 21 | * var f = R.allPass([gt10, even]); 22 | * f(11); //=> false 23 | * f(12); //=> true 24 | */ 25 | module.exports = curry(_predicateWrap(_all)); 26 | -------------------------------------------------------------------------------- /src/assocPath.js: -------------------------------------------------------------------------------- 1 | var _assocPath = require('./internal/_assocPath'); 2 | var _curry3 = require('./internal/_curry3'); 3 | 4 | 5 | /** 6 | * Makes a shallow clone of an object, setting or overriding the nodes 7 | * required to create the given path, and placing the specific value at the 8 | * tail end of that path. Note that this copies and flattens prototype 9 | * properties onto the new object as well. All non-primitive properties 10 | * are copied by reference. 11 | * 12 | * @func 13 | * @memberOf R 14 | * @category Object 15 | * @sig [String] -> a -> {k: v} -> {k: v} 16 | * @param {Array} path the path to set 17 | * @param {*} val the new value 18 | * @param {Object} obj the object to clone 19 | * @return {Object} a new object similar to the original except along the specified path. 20 | * @example 21 | * 22 | * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}} 23 | */ 24 | module.exports = _curry3(_assocPath); 25 | -------------------------------------------------------------------------------- /src/construct.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var constructN = require('./constructN'); 3 | 4 | 5 | /** 6 | * Wraps a constructor function inside a curried function that can be called with the same 7 | * arguments and returns the same type. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Function 12 | * @sig (* -> {*}) -> (* -> {*}) 13 | * @param {Function} Fn The constructor function to wrap. 14 | * @return {Function} A wrapped, curried constructor function. 15 | * @example 16 | * 17 | * // Constructor function 18 | * var Widget = function(config) { 19 | * // ... 20 | * }; 21 | * Widget.prototype = { 22 | * // ... 23 | * }; 24 | * var allConfigs = [ 25 | * // ... 26 | * ]; 27 | * R.map(R.construct(Widget), allConfigs); // a list of Widgets 28 | */ 29 | module.exports = _curry1(function construct(Fn) { 30 | return constructN(Fn.length, Fn); 31 | }); 32 | -------------------------------------------------------------------------------- /test/gte.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('gte', function() { 7 | it('reports whether one item is less than another', function() { 8 | assert.strictEqual(R.gte(3, 5), false); 9 | assert.strictEqual(R.gte(6, 4), true); 10 | assert.strictEqual(R.gte(7.0, 7.0), true); 11 | assert.strictEqual(R.gte('abc', 'xyz'), false); 12 | assert.strictEqual(R.gte('abcd', 'abc'), true); 13 | }); 14 | 15 | it('is curried', function() { 16 | var lte20 = R.gte(20); 17 | assert.strictEqual(lte20(10), true); 18 | assert.strictEqual(lte20(20), true); 19 | assert.strictEqual(lte20(25), false); 20 | }); 21 | 22 | it('behaves right curried when passed `R.__` for its first argument', function() { 23 | var gte20 = R.gte(R.__, 20); 24 | assert.strictEqual(gte20(10), false); 25 | assert.strictEqual(gte20(20), true); 26 | assert.strictEqual(gte20(25), true); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /test/props.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('props', function() { 7 | var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}; 8 | 9 | it('returns empty array if no properties requested', function() { 10 | assert.deepEqual(R.props([], obj), []); 11 | }); 12 | 13 | it('returns values for requested properties', function() { 14 | assert.deepEqual(R.props(['a', 'e'], obj), [1, 5]); 15 | }); 16 | 17 | it('preserves order', function() { 18 | assert.deepEqual(R.props(['f', 'c', 'e'], obj), [6, 3, 5]); 19 | }); 20 | 21 | it('returns undefined for nonexistent properties', function() { 22 | var ps = R.props(['a', 'nonexistent'], obj); 23 | assert.strictEqual(ps.length, 2); 24 | assert.strictEqual(ps[0], 1); 25 | assert.strictEqual(ps[1], void 0); 26 | }); 27 | 28 | it('is curried', function() { 29 | assert.deepEqual(R.props(['a', 'b'])(obj), [1, 2]); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/intersperse.js: -------------------------------------------------------------------------------- 1 | var _checkForMethod = require('./internal/_checkForMethod'); 2 | var _curry2 = require('./internal/_curry2'); 3 | 4 | 5 | /** 6 | * Creates a new list with the separator interposed between elements. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category List 11 | * @sig a -> [a] -> [a] 12 | * @param {*} separator The element to add to the list. 13 | * @param {Array} list The list to be interposed. 14 | * @return {Array} The new list. 15 | * @example 16 | * 17 | * R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a'] 18 | */ 19 | module.exports = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) { 20 | var out = []; 21 | var idx = 0; 22 | var length = list.length; 23 | while (idx < length) { 24 | if (idx === length - 1) { 25 | out.push(list[idx]); 26 | } else { 27 | out.push(list[idx], separator); 28 | } 29 | idx += 1; 30 | } 31 | return out; 32 | })); 33 | -------------------------------------------------------------------------------- /src/once.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Accepts a function `fn` and returns a function that guards invocation of `fn` such that 6 | * `fn` can only ever be called once, no matter how many times the returned function is 7 | * invoked. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Function 12 | * @sig (a... -> b) -> (a... -> b) 13 | * @param {Function} fn The function to wrap in a call-only-once wrapper. 14 | * @return {Function} The wrapped function. 15 | * @example 16 | * 17 | * var addOneOnce = R.once(function(x){ return x + 1; }); 18 | * addOneOnce(10); //=> 11 19 | * addOneOnce(addOneOnce(50)); //=> 11 20 | */ 21 | module.exports = _curry1(function once(fn) { 22 | var called = false, result; 23 | return function() { 24 | if (called) { 25 | return result; 26 | } 27 | called = true; 28 | result = fn.apply(this, arguments); 29 | return result; 30 | }; 31 | }); 32 | -------------------------------------------------------------------------------- /test/lte.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('lte', function() { 7 | it('reports whether one item is less than another', function() { 8 | assert.strictEqual(R.lte(3, 5), true); 9 | assert.strictEqual(R.lte(6, 4), false); 10 | assert.strictEqual(R.lte(7.0, 7.0), true); 11 | assert.strictEqual(R.lte('abc', 'xyz'), true); 12 | assert.strictEqual(R.lte('abcd', 'abc'), false); 13 | }); 14 | 15 | it('is curried', function() { 16 | var gte20 = R.lte(20); 17 | assert.strictEqual(gte20(10), false); 18 | assert.strictEqual(gte20(20), true); 19 | assert.strictEqual(gte20(25), true); 20 | }); 21 | 22 | it('behaves right curried when passed `R.__` for its first argument', function() { 23 | var upTo20 = R.lte(R.__, 20); 24 | assert.strictEqual(upTo20(10), true); 25 | assert.strictEqual(upTo20(20), true); 26 | assert.strictEqual(upTo20(25), false); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /src/hasIn.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns whether or not an object or its prototype chain has 6 | * a property with the specified name 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig s -> {s: x} -> Boolean 12 | * @param {String} prop The name of the property to check for. 13 | * @param {Object} obj The object to query. 14 | * @return {Boolean} Whether the property exists. 15 | * @example 16 | * 17 | * function Rectangle(width, height) { 18 | * this.width = width; 19 | * this.height = height; 20 | * } 21 | * Rectangle.prototype.area = function() { 22 | * return this.width * this.height; 23 | * }; 24 | * 25 | * var square = new Rectangle(2, 2); 26 | * R.hasIn('width', square); //=> true 27 | * R.hasIn('area', square); //=> true 28 | */ 29 | module.exports = _curry2(function(prop, obj) { 30 | return prop in obj; 31 | }); 32 | -------------------------------------------------------------------------------- /src/lensIndex.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var lens = require('./lens'); 3 | var nth = require('./nth'); 4 | var update = require('./update'); 5 | 6 | 7 | /** 8 | * Creates a lens that will focus on index `n` of the source array. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category List 13 | * @see R.lens 14 | * @sig Number -> (a -> b) 15 | * @param {Number} n The index of the array that the returned lens will focus on. 16 | * @return {Function} the returned function has `set` and `map` properties that are 17 | * also curried functions. 18 | * @example 19 | * 20 | * var headLens = R.lensIndex(0); 21 | * headLens([10, 20, 30, 40]); //=> 10 22 | * headLens.set('mu', [10, 20, 30, 40]); //=> ['mu', 20, 30, 40] 23 | * headLens.map(function(x) { return x + 1; }, [10, 20, 30, 40]); //=> [11, 20, 30, 40] 24 | */ 25 | module.exports = _curry1(function lensIndex(n) { 26 | return lens(nth(n), update(n)); 27 | }); 28 | -------------------------------------------------------------------------------- /src/project.js: -------------------------------------------------------------------------------- 1 | var _map = require('./internal/_map'); 2 | var identity = require('./identity'); 3 | var pickAll = require('./pickAll'); 4 | var useWith = require('./useWith'); 5 | 6 | 7 | /** 8 | * Reasonable analog to SQL `select` statement. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @category Relation 14 | * @sig [k] -> [{k: v}] -> [{k: v}] 15 | * @param {Array} props The property names to project 16 | * @param {Array} objs The objects to query 17 | * @return {Array} An array of objects with just the `props` properties. 18 | * @example 19 | * 20 | * var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2}; 21 | * var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7}; 22 | * var kids = [abby, fred]; 23 | * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}] 24 | */ 25 | module.exports = useWith(_map, pickAll, identity); // passing `identity` gives correct arity 26 | -------------------------------------------------------------------------------- /test/composeK.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | var Identity = function(x) { 7 | this.value = x; 8 | }; 9 | 10 | Identity.prototype.chain = function(f) { 11 | return f(this.value); 12 | }; 13 | 14 | 15 | describe('composeK', function() { 16 | 17 | it('is a variadic function', function() { 18 | assert.strictEqual(typeof R.composeK, 'function'); 19 | assert.strictEqual(R.composeK.length, 0); 20 | }); 21 | 22 | it('performs right-to-left Kleisli composition', function() { 23 | var f = function(x) { return new Identity(x - 1); }; 24 | var g = function(x) { return new Identity(x * x); }; 25 | var h = function(x) { return new Identity(x + 1); }; 26 | 27 | var fn = R.composeK(h, g, f); 28 | var id = new Identity(8); 29 | 30 | assert.strictEqual(fn(id).value, 50); 31 | assert.strictEqual(fn(id).value, R.compose(R.chain(h), R.chain(g), R.chain(f))(id).value); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /src/equals.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var _equals = require('./internal/_equals'); 3 | var _hasMethod = require('./internal/_hasMethod'); 4 | 5 | 6 | /** 7 | * Returns `true` if its arguments are equivalent, `false` otherwise. 8 | * Dispatches to an `equals` method if present. Handles cyclical data 9 | * structures. 10 | * 11 | * @func 12 | * @memberOf R 13 | * @category Relation 14 | * @sig a -> b -> Boolean 15 | * @param {*} a 16 | * @param {*} b 17 | * @return {Boolean} 18 | * @example 19 | * 20 | * R.equals(1, 1); //=> true 21 | * R.equals(1, '1'); //=> false 22 | * R.equals([1, 2, 3], [1, 2, 3]); //=> true 23 | * 24 | * var a = {}; a.v = a; 25 | * var b = {}; b.v = b; 26 | * R.equals(a, b); //=> true 27 | */ 28 | module.exports = _curry2(function equals(a, b) { 29 | return _hasMethod('equals', a) ? a.equals(b) : 30 | _hasMethod('equals', b) ? b.equals(a) : _equals(a, b, [], []); 31 | }); 32 | -------------------------------------------------------------------------------- /src/match.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | var compose = require('./internal/compose'); 3 | var defaultTo = require('./defaultTo'); 4 | var invoker = require('./invoker'); 5 | 6 | 7 | /** 8 | * Tests a regular expression against a String. Note that this function 9 | * will return an empty array when there are no matches. This differs 10 | * from [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) 11 | * which returns `null` when there are no matches. 12 | * 13 | * @func 14 | * @memberOf R 15 | * @category String 16 | * @sig RegExp -> String -> [String | Undefined] 17 | * @param {RegExp} rx A regular expression. 18 | * @param {String} str The string to match against 19 | * @return {Array} The list of matches. 20 | * @example 21 | * 22 | * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na'] 23 | */ 24 | module.exports = _curry2(compose(defaultTo([]), invoker(1, 'match'))); 25 | -------------------------------------------------------------------------------- /src/replace.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | 3 | 4 | /** 5 | * Replace a substring or regex match in a string with a replacement. 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category String 10 | * @sig RegExp|String -> String -> String -> String 11 | * @param {RegExp|String} pattern A regular expression or a substring to match. 12 | * @param {String} replacement The string to replace the matches with. 13 | * @param {String} str The String to do the search and replacement in. 14 | * @return {String} The result. 15 | * @example 16 | * 17 | * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo' 18 | * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo' 19 | * 20 | * // Use the "g" (global) flag to replace all occurrences: 21 | * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar' 22 | */ 23 | module.exports = _curry3(function replace(regex, replacement, str) { 24 | return str.replace(regex, replacement); 25 | }); 26 | -------------------------------------------------------------------------------- /test/isSet.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('isSet', function() { 7 | it('returns true if a list is composed of unique elements', function() { 8 | var list = [1, 2, 3, 1, 2, 3, 1, 2, 3]; 9 | assert.strictEqual(R.isSet(list), false); 10 | assert.strictEqual(R.isSet([3, 1, 4, 2, 5, 7, 9]), true); 11 | }); 12 | 13 | it('returns true for an empty array', function() { 14 | assert.strictEqual(R.isSet([]), true); 15 | }); 16 | 17 | it('has R.equals semantics', function() { 18 | function Just(x) { this.value = x; } 19 | Just.prototype.equals = function(x) { 20 | return x instanceof Just && R.equals(x.value, this.value); 21 | }; 22 | 23 | assert.strictEqual(R.isSet([0, -0]), true); 24 | assert.strictEqual(R.isSet([-0, 0]), true); 25 | assert.strictEqual(R.isSet([NaN, NaN]), false); 26 | assert.strictEqual(R.isSet([new Just([42]), new Just([42])]), false); 27 | }); 28 | 29 | }); 30 | -------------------------------------------------------------------------------- /src/anyPass.js: -------------------------------------------------------------------------------- 1 | var _any = require('./internal/_any'); 2 | var _predicateWrap = require('./internal/_predicateWrap'); 3 | var curry = require('./curry'); 4 | 5 | 6 | /** 7 | * Given a list of predicates returns a new predicate that will be true exactly when any one of them is. 8 | * 9 | * @func 10 | * @memberOf R 11 | * @category Logic 12 | * @sig [(*... -> Boolean)] -> (*... -> Boolean) 13 | * @param {Array} list An array of predicate functions 14 | * @param {*} optional Any arguments to pass into the predicates 15 | * @return {Function} A function that applies its arguments to each of the predicates, returning 16 | * `true` if all are satisfied. 17 | * @example 18 | * 19 | * var gt10 = function(x) { return x > 10; }; 20 | * var even = function(x) { return x % 2 === 0}; 21 | * var f = R.anyPass([gt10, even]); 22 | * f(11); //=> true 23 | * f(8); //=> true 24 | * f(9); //=> false 25 | */ 26 | module.exports = curry(_predicateWrap(_any)); 27 | -------------------------------------------------------------------------------- /src/internal/_checkForMethod.js: -------------------------------------------------------------------------------- 1 | var _isArray = require('./_isArray'); 2 | var _slice = require('./_slice'); 3 | 4 | 5 | /** 6 | * Similar to hasMethod, this checks whether a function has a [methodname] 7 | * function. If it isn't an array it will execute that function otherwise it will 8 | * default to the ramda implementation. 9 | * 10 | * @private 11 | * @param {Function} fn ramda implemtation 12 | * @param {String} methodname property to check for a custom implementation 13 | * @return {Object} Whatever the return value of the method is. 14 | */ 15 | module.exports = function _checkForMethod(methodname, fn) { 16 | return function() { 17 | var length = arguments.length; 18 | if (length === 0) { 19 | return fn(); 20 | } 21 | var obj = arguments[length - 1]; 22 | return (_isArray(obj) || typeof obj[methodname] !== 'function') ? 23 | fn.apply(this, arguments) : 24 | obj[methodname].apply(obj, _slice(arguments, 0, length - 1)); 25 | }; 26 | }; 27 | -------------------------------------------------------------------------------- /test/aperture.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('aperture', function() { 7 | var sevenLs = [1, 2, 3, 4, 5, 6, 7]; 8 | it('creates a list of n-tuples from a list', function() { 9 | assert.deepEqual(R.aperture(1, sevenLs), [[1], [2], [3], [4], [5], [6], [7]]); 10 | assert.deepEqual(R.aperture(2, sevenLs), [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]); 11 | assert.deepEqual(R.aperture(3, sevenLs), [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]); 12 | assert.deepEqual(R.aperture(4, [1, 2, 3, 4]), [[1, 2, 3, 4]]); 13 | }); 14 | 15 | it('returns an empty list when `n` > `list.length`', function() { 16 | assert.deepEqual(R.aperture(6, [1, 2, 3]), []); 17 | assert.deepEqual(R.aperture(1, []), []); 18 | }); 19 | 20 | it('is curried', function() { 21 | var pairwise = R.aperture(2); 22 | assert.deepEqual(pairwise(sevenLs), [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/pick.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./internal/_curry2'); 2 | 3 | 4 | /** 5 | * Returns a partial copy of an object containing only the keys specified. If the key does not exist, the 6 | * property is ignored. 7 | * 8 | * @func 9 | * @memberOf R 10 | * @category Object 11 | * @sig [k] -> {k: v} -> {k: v} 12 | * @param {Array} names an array of String property names to copy onto a new object 13 | * @param {Object} obj The object to copy from 14 | * @return {Object} A new object with only properties from `names` on it. 15 | * @example 16 | * 17 | * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4} 18 | * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1} 19 | */ 20 | module.exports = _curry2(function pick(names, obj) { 21 | var result = {}; 22 | var idx = 0; 23 | while (idx < names.length) { 24 | if (names[idx] in obj) { 25 | result[names[idx]] = obj[names[idx]]; 26 | } 27 | idx += 1; 28 | } 29 | return result; 30 | }); 31 | -------------------------------------------------------------------------------- /src/reduced.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | var _reduced = require('./internal/_reduced'); 3 | 4 | /** 5 | * Returns a value wrapped to indicate that it is the final value of the 6 | * reduce and transduce functions. The returned value 7 | * should be considered a black box: the internal structure is not 8 | * guaranteed to be stable. 9 | * 10 | * Note: this optimization is unavailable to functions not explicitly listed 11 | * above. For instance, it is not currently supported by reduceIndexed, 12 | * reduceRight, or reduceRightIndexed. 13 | * 14 | * @func 15 | * @memberOf R 16 | * @category List 17 | * @see R.reduce, R.transduce 18 | * @sig a -> * 19 | * @param {*} x The final value of the reduce. 20 | * @return {*} The wrapped value. 21 | * @example 22 | * 23 | * R.reduce( 24 | * R.pipe(R.add, R.ifElse(R.lte(10), R.reduced, R.identity)), 25 | * 0, 26 | * [1, 2, 3, 4, 5]) // 10 27 | */ 28 | 29 | module.exports = _curry1(_reduced); 30 | -------------------------------------------------------------------------------- /src/scan.js: -------------------------------------------------------------------------------- 1 | var _curry3 = require('./internal/_curry3'); 2 | 3 | 4 | /** 5 | * Scan is similar to reduce, but returns a list of successively reduced values from the left 6 | * 7 | * @func 8 | * @memberOf R 9 | * @category List 10 | * @sig (a,b -> a) -> a -> [b] -> [a] 11 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the 12 | * current element from the array 13 | * @param {*} acc The accumulator value. 14 | * @param {Array} list The list to iterate over. 15 | * @return {Array} A list of all intermediately reduced values. 16 | * @example 17 | * 18 | * var numbers = [1, 2, 3, 4]; 19 | * var factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24] 20 | */ 21 | module.exports = _curry3(function scan(fn, acc, list) { 22 | var idx = 0, len = list.length, result = [acc]; 23 | while (idx < len) { 24 | acc = fn(acc, list[idx]); 25 | result[idx + 1] = acc; 26 | idx += 1; 27 | } 28 | return result; 29 | }); 30 | -------------------------------------------------------------------------------- /src/toPairsIn.js: -------------------------------------------------------------------------------- 1 | var _curry1 = require('./internal/_curry1'); 2 | 3 | 4 | /** 5 | * Converts an object into an array of key, value arrays. 6 | * The object's own properties and prototype properties are used. 7 | * Note that the order of the output array is not guaranteed to be 8 | * consistent across different JS platforms. 9 | * 10 | * @func 11 | * @memberOf R 12 | * @category Object 13 | * @sig {String: *} -> [[String,*]] 14 | * @param {Object} obj The object to extract from 15 | * @return {Array} An array of key, value arrays from the object's own 16 | * and prototype properties. 17 | * @example 18 | * 19 | * var F = function() { this.x = 'X'; }; 20 | * F.prototype.y = 'Y'; 21 | * var f = new F(); 22 | * R.toPairsIn(f); //=> [['x','X'], ['y','Y']] 23 | */ 24 | module.exports = _curry1(function toPairsIn(obj) { 25 | var pairs = []; 26 | for (var prop in obj) { 27 | pairs[pairs.length] = [prop, obj[prop]]; 28 | } 29 | return pairs; 30 | }); 31 | -------------------------------------------------------------------------------- /test/keysIn.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var R = require('..'); 4 | 5 | 6 | describe('keysIn', function() { 7 | var obj = {a: 100, b: [1, 2, 3], c: {x: 200, y: 300}, d: 'D', e: null, f: undefined}; 8 | function C() { this.a = 100; this.b = 200; } 9 | C.prototype.x = function() { return 'x'; }; 10 | C.prototype.y = 'y'; 11 | var cobj = new C(); 12 | 13 | it("returns an array of the given object's keys", function() { 14 | assert.deepEqual(R.keysIn(obj).sort(), ['a', 'b', 'c', 'd', 'e', 'f']); 15 | }); 16 | 17 | it("includes the given object's prototype properties", function() { 18 | assert.deepEqual(R.keysIn(cobj).sort(), ['a', 'b', 'x', 'y']); 19 | }); 20 | 21 | it('works for primitives', function() { 22 | /* jshint elision: true */ 23 | var result = R.map(function(val) { 24 | return R.keys(val); 25 | }, [null, undefined, 55, '', true, false, NaN, Infinity, , []]); 26 | assert.deepEqual(result, R.repeat([], 10)); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /src/internal/_xfindIndex.js: -------------------------------------------------------------------------------- 1 | var _curry2 = require('./_curry2'); 2 | var _reduced = require('./_reduced'); 3 | var _xfBase = require('./_xfBase'); 4 | 5 | 6 | module.exports = (function() { 7 | function XFindIndex(f, xf) { 8 | this.xf = xf; 9 | this.f = f; 10 | this.idx = -1; 11 | this.found = false; 12 | } 13 | XFindIndex.prototype['@@transducer/init'] = _xfBase.init; 14 | XFindIndex.prototype['@@transducer/result'] = function(result) { 15 | if (!this.found) { 16 | result = this.xf['@@transducer/step'](result, -1); 17 | } 18 | return this.xf['@@transducer/result'](result); 19 | }; 20 | XFindIndex.prototype['@@transducer/step'] = function(result, input) { 21 | this.idx += 1; 22 | if (this.f(input)) { 23 | this.found = true; 24 | result = _reduced(this.xf['@@transducer/step'](result, this.idx)); 25 | } 26 | return result; 27 | }; 28 | 29 | return _curry2(function _xfindIndex(f, xf) { return new XFindIndex(f, xf); }); 30 | })(); 31 | --------------------------------------------------------------------------------