Skip to content

Commit 2040b69

Browse files
authored
Add rebuild, mapKeys, ,and renameKeys (ramda#3493)
1 parent a4b5b60 commit 2040b69

File tree

15 files changed

+217
-30
lines changed

15 files changed

+217
-30
lines changed

source/chain.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import _curry2 from './internal/_curry2.js';
2-
import _dispatchable from './internal/_dispatchable.js';
3-
import _makeFlat from './internal/_makeFlat.js';
4-
import _xchain from './internal/_xchain.js';
5-
import map from './map.js';
2+
import _chain from './internal/_chain.js';
63

74

85
/**
@@ -31,10 +28,5 @@ import map from './map.js';
3128
*
3229
* R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
3330
*/
34-
var chain = _curry2(_dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
35-
if (typeof monad === 'function') {
36-
return function(x) { return fn(monad(x))(x); };
37-
}
38-
return _makeFlat(false)(map(fn, monad));
39-
}));
31+
var chain = _curry2(_chain);
4032
export default chain;

source/fromPairs.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _curry1 from './internal/_curry1.js';
2-
2+
import _fromPairs from './internal/_fromPairs.js';
33

44
/**
55
* Creates a new object from a list key-value pairs. If a key appears in
@@ -17,13 +17,5 @@ import _curry1 from './internal/_curry1.js';
1717
*
1818
* R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
1919
*/
20-
var fromPairs = _curry1(function fromPairs(pairs) {
21-
var result = {};
22-
var idx = 0;
23-
while (idx < pairs.length) {
24-
result[pairs[idx][0]] = pairs[idx][1];
25-
idx += 1;
26-
}
27-
return result;
28-
});
20+
var fromPairs = _curry1(_fromPairs);
2921
export default fromPairs;

source/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export { default as lte } from './lte.js';
124124
export { default as map } from './map.js';
125125
export { default as mapAccum } from './mapAccum.js';
126126
export { default as mapAccumRight } from './mapAccumRight.js';
127+
export { default as mapKeys } from './mapKeys.js';
127128
export { default as mapObjIndexed } from './mapObjIndexed.js';
128129
export { default as match } from './match.js';
129130
export { default as mathMod } from './mathMod.js';
@@ -190,13 +191,15 @@ export { default as propOr } from './propOr.js';
190191
export { default as propSatisfies } from './propSatisfies.js';
191192
export { default as props } from './props.js';
192193
export { default as range } from './range.js';
194+
export { default as rebuild } from './rebuild.js';
193195
export { default as reduce } from './reduce.js';
194196
export { default as reduceBy } from './reduceBy.js';
195197
export { default as reduceRight } from './reduceRight.js';
196198
export { default as reduceWhile } from './reduceWhile.js';
197199
export { default as reduced } from './reduced.js';
198200
export { default as reject } from './reject.js';
199201
export { default as remove } from './remove.js';
202+
export { default as renameKeys } from './renameKeys.js';
200203
export { default as repeat } from './repeat.js';
201204
export { default as replace } from './replace.js';
202205
export { default as reverse } from './reverse.js';

source/internal/_chain.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import _dispatchable from './_dispatchable.js';
2+
import _makeFlat from './_makeFlat.js';
3+
import _xchain from './_xchain.js';
4+
import map from '../map.js';
5+
6+
7+
export default _dispatchable(['fantasy-land/chain', 'chain'], _xchain, function chain(fn, monad) {
8+
if (typeof monad === 'function') {
9+
return function(x) { return fn(monad(x))(x); };
10+
}
11+
return _makeFlat(false)(map(fn, monad));
12+
});

source/internal/_fromPairs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
export default function fromPairs(pairs) {
3+
var result = {};
4+
var idx = 0;
5+
while (idx < pairs.length) {
6+
result[pairs[idx][0]] = pairs[idx][1];
7+
idx += 1;
8+
}
9+
return result;
10+
}

source/internal/_mapKeys.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import _rebuild from './_rebuild.js';
2+
3+
var mapKeys = function mapKeys(fn, obj) {
4+
return _rebuild(function(k, v) {return [[fn(k), v]];}, obj);
5+
};
6+
7+
export default mapKeys;

source/internal/_rebuild.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import _fromPairs from './_fromPairs.js';
2+
import _toPairs from './_toPairs.js';
3+
import _chain from './_chain.js';
4+
5+
var rebuild = function(convert, obj) {
6+
return _fromPairs(_chain(
7+
function(pair) {return convert(pair[0], pair[1]);},
8+
_toPairs(obj)
9+
));
10+
};
11+
export default rebuild;

source/internal/_toPairs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import _has from './_has.js';
2+
3+
4+
export default function _toPairs(obj) {
5+
var pairs = [];
6+
for (var prop in obj) {
7+
if (_has(prop, obj)) {
8+
pairs[pairs.length] = [prop, obj[prop]];
9+
}
10+
}
11+
return pairs;
12+
}

source/mapKeys.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _curry2 from './internal/_curry2.js';
2+
import _mapKeys from './internal/_mapKeys.js';
3+
4+
/**
5+
* Transforms an object by converting the keys to new values.
6+
*
7+
* **Note** that if multiple keys map to the same new key, the last one processed will dominate.
8+
*
9+
* @func
10+
* @memberOf R
11+
* @category Object
12+
* @sig (String -> String) -> Object -> Object
13+
* @param {Function} fn
14+
* @param {Object} obj
15+
* @return {Object}
16+
* @see R.map, R.rebuild, R.renameKeys
17+
* @example
18+
*
19+
* R.mapKeys(toUpper, {foo: 1, bar: 2, baz: 3}) //=> {FOO: 1, BAR: 2, BAZ: 3}
20+
*/
21+
var mapKeys = _curry2(_mapKeys);
22+
23+
export default mapKeys;

source/rebuild.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _curry2 from './internal/_curry2.js';
2+
import _rebuild from './internal/_rebuild.js';
3+
4+
/**
5+
* Transforms an object into a new one, applying to every key-value pair a
6+
* function creating zero, one, or many new key-value pairs, and combining
7+
* the resulst into a single object.
8+
*
9+
* @func
10+
* @memberOf R
11+
* @category List
12+
* @sig ([String, a] -> [[String, b]]) -> {k: a} -> {k: b}
13+
* @param {Function} convert A function that converts a key and a value to an array of key-value arrays.
14+
* @param {Object} obj The structure to convert
15+
* @return {Array} A new object whose key-value pairs are the result of applying the `convert` function
16+
* to every key-value pair in `obj`.
17+
* @see R.map, R.mapKeys, R.renameKeys
18+
* @example
19+
*
20+
* R.rebuild((k, v) => [[k.toUpperCase(), v * v]], {a: 1, b: 2, c: 3}) //=> {A: 1, B: 4, C: 9}
21+
*/
22+
var rebuild = _curry2(_rebuild);
23+
export default rebuild;

0 commit comments

Comments
 (0)