diff --git a/source/composeWith.js b/source/composeWith.js index 70e417162..33e43db6b 100644 --- a/source/composeWith.js +++ b/source/composeWith.js @@ -21,7 +21,7 @@ import reverse from './reverse'; * const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res)); * * composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) //=> 2 - * composeWhileNotNil([R.inc, R.prop('age')])({}) //=> null + * composeWhileNotNil([R.inc, R.prop('age')])({}) //=> undefined * * @symb R.composeWith(f)([g, h, i])(...args) = f(g, f(h, f(i, ...args))) */ diff --git a/source/index.js b/source/index.js index bf6d735ea..a58838dd3 100644 --- a/source/index.js +++ b/source/index.js @@ -139,6 +139,7 @@ export { default as mergeWithKey } from './mergeWithKey'; export { default as min } from './min'; export { default as minBy } from './minBy'; export { default as modulo } from './modulo'; +export { default as move } from './move'; export { default as multiply } from './multiply'; export { default as nAry } from './nAry'; export { default as negate } from './negate'; diff --git a/source/memoizeWith.js b/source/memoizeWith.js index 3ff8b4680..44b389d37 100644 --- a/source/memoizeWith.js +++ b/source/memoizeWith.js @@ -4,11 +4,10 @@ import _has from './internal/_has'; /** - * A customisable version of [`R.memoize`](#memoize). `memoizeWith` takes an - * additional function that will be applied to a given argument set and used to - * create the cache key under which the results of the function to be memoized - * will be stored. Care must be taken when implementing key generation to avoid - * clashes that may overwrite previous entries erroneously. + * Takes an additional function that will be applied to a given argument set + * and used to create the cache key under which the results of the function to be + * memoized will be stored. Care must be taken when implementing key generation + * to avoid clashes that may overwrite previous entries erroneously. * * * @func diff --git a/source/move.js b/source/move.js new file mode 100644 index 000000000..09ec40536 --- /dev/null +++ b/source/move.js @@ -0,0 +1,36 @@ +import _curry3 from './internal/_curry3'; + +/** + * Move an item, at index `from`, to index `to`, in a list of elements. + * A new list will be created containing the new elements order. + * + * @func + * @memberOf R + * @category List + * @sig Number -> Number -> [a] -> [a] + * @param {Number} from The source index + * @param {Number} to The destination index + * @param {Array} list The list which will serve to realise the move + * @return {Array} The new list reordered + * @example + * + * R.move(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['b', 'c', 'a', 'd', 'e', 'f'] + * R.move(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'a', 'b', 'c', 'd', 'e'] list rotation + */ +var move = _curry3(function(from, to, list) { + var length = list.length; + var result = list.slice(); + var positiveFrom = from < 0 ? length + from : from; + var positiveTo = to < 0 ? length + to : to; + var item = result.splice(positiveFrom, 1); + + return positiveFrom < 0 || positiveFrom >= list.length + || positiveTo < 0 || positiveTo >= list.length + ? list + : [] + .concat(result.slice(0, positiveTo)) + .concat(item) + .concat(result.slice(positiveTo, list.length)); +}); + +export default move; diff --git a/source/none.js b/source/none.js index fbf10461f..58a1aa47d 100644 --- a/source/none.js +++ b/source/none.js @@ -28,7 +28,7 @@ import all from './all'; * R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true * R.none(isOdd, [1, 3, 5, 7, 8, 11]); //=> false */ -const none = _curry2(function none(fn, input) { +var none = _curry2(function none(fn, input) { return all(_complement(fn), input); }); export default none; diff --git a/source/otherwise.js b/source/otherwise.js index a8c836bef..fa9e32553 100644 --- a/source/otherwise.js +++ b/source/otherwise.js @@ -23,12 +23,13 @@ import _assertPromise from './internal/_assertPromise'; * * //recoverFromFailure :: String -> Promise ({firstName, lastName}) * var recoverFromFailure = R.pipe( - * failedFetch(12345), + * failedFetch, * R.otherwise(useDefault), - * R.then(R.pick(['firstName', 'lastName'])) + * R.then(R.pick(['firstName', 'lastName'])), * ); + * recoverFromFailure(12345).then(console.log) */ -const otherwise = _curry2(function otherwise(f, p) { +var otherwise = _curry2(function otherwise(f, p) { _assertPromise('otherwise', p); return p.then(null, f); diff --git a/source/pipeWith.js b/source/pipeWith.js index 3c9b398fe..3d3d58d75 100644 --- a/source/pipeWith.js +++ b/source/pipeWith.js @@ -32,8 +32,8 @@ var pipeWith = _curry2(function pipeWith(xf, list) { return identity; } - const headList = head(list); - const tailList = tail(list); + var headList = head(list); + var tailList = tail(list); return _arity(headList.length, function() { return _reduce( diff --git a/source/then.js b/source/then.js index faf06f227..c2e8c663d 100644 --- a/source/then.js +++ b/source/then.js @@ -27,7 +27,7 @@ import _assertPromise from './internal/_assertPromise'; * R.then(R.pick(['firstName', 'lastName'])) * ); */ -const then = _curry2(function then(f, p) { +var then = _curry2(function then(f, p) { _assertPromise('then', p); return p.then(f); diff --git a/source/thunkify.js b/source/thunkify.js index b2a4a4558..7c9a7789e 100644 --- a/source/thunkify.js +++ b/source/thunkify.js @@ -20,7 +20,7 @@ import _curry1 from './internal/_curry1'; */ var thunkify = _curry1(function thunkify(fn) { return curryN(fn.length, function createThunk() { - const fnArgs = arguments; + var fnArgs = arguments; return function invokeThunk() { return fn.apply(this, fnArgs); }; diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 000000000..3751f9cea --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,9 @@ +{ + "rules": { + "no-restricted-properties": ["error", { + "object": "describe", + "property": "only" + }], + "no-restricted-modules": ["error", ".."] + } +} diff --git a/test/composeWith.js b/test/composeWith.js index d7beebaa2..d05d05129 100644 --- a/test/composeWith.js +++ b/test/composeWith.js @@ -1,4 +1,4 @@ -var R = require('..'); +var R = require('../source'); var eq = require('./shared/eq'); diff --git a/test/move.js b/test/move.js new file mode 100644 index 000000000..5c302e813 --- /dev/null +++ b/test/move.js @@ -0,0 +1,22 @@ +var R = require('../source'); +var eq = require('./shared/eq'); + +var list = ['a', 'b', 'c', 'd', 'e', 'f']; + +describe('move', function() { + it('moves an element from an index to another', function() { + eq(R.move(0, 1, list), ['b', 'a', 'c', 'd', 'e', 'f']); + eq(R.move(2, 1, list), ['a', 'c', 'b', 'd', 'e', 'f']); + eq(R.move(-1, 0, list), ['f', 'a', 'b', 'c', 'd', 'e']); + eq(R.move(0, -1, list), ['b', 'c', 'd', 'e', 'f', 'a']); + }); + + it('does nothing when indexes are outside the list outbounds', function() { + eq(R.move(-20, 2, list), list); + eq(R.move(20, 2, list), list); + eq(R.move(2, 20, list), list); + eq(R.move(2, -20, list), list); + eq(R.move(20, 20, list), list); + eq(R.move(-20, -20, list), list); + }); +}); diff --git a/test/otherwise.js b/test/otherwise.js index 4303b007e..68156473b 100644 --- a/test/otherwise.js +++ b/test/otherwise.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var R = require('..'); +var R = require('../source'); var eq = require('./shared/eq'); diff --git a/test/pipeWith.js b/test/pipeWith.js index a38af7184..a907b05a1 100644 --- a/test/pipeWith.js +++ b/test/pipeWith.js @@ -1,4 +1,4 @@ -var R = require('..'); +var R = require('../source'); var eq = require('./shared/eq'); diff --git a/test/then.js b/test/then.js index 976512530..edf63e9fc 100644 --- a/test/then.js +++ b/test/then.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var R = require('..'); +var R = require('../source'); var eq = require('./shared/eq');