Skip to content

Commit

Permalink
add mapKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Aug 12, 2014
1 parent 547e70f commit 45e7299
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 32 deletions.
6 changes: 6 additions & 0 deletions __tests__/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ describe('Map', () => {
expect(r.toObject()).toEqual({a:'A', b:'B', c:'C'});
});

it('maps keys', () => {
var m = Map({a:'a', b:'b', c:'c'});
var r = m.mapKeys(value => value.toUpperCase());
expect(r.toObject()).toEqual({A:'a', B:'b', C:'c'});
});

it('filters values', () => {
var m = Map({a:1, b:2, c:3, d:4, e:5, f:6});
var r = m.filter(value => value % 2 === 1);
Expand Down
20 changes: 13 additions & 7 deletions dist/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,24 +414,30 @@ export interface Sequence<K, V> {
/**
* Returns a new sequence with values passed through a `mapper` function.
*
* Sequence({a:1,b:2}).map(x => 10 * x) // { a: 10, b: 20 }
*
* Note: if you want to map keys instead of values, consider flipping the
* Sequence before mapping:
*
* Sequence({a:1,b:2}).flip().map(x => x.toUpperCase()).flip() // { A: 1, B: 2 }
* Sequence({ a: 1, b: 2 }).map(x => 10 * x) // { a: 10, b: 20 }
*
*/
map<M>(
mapper: (value?: V, key?: K, seq?: Sequence<K, V>) => M,
thisArg?: any
): Sequence<K, M>;

/**
* Returns a new sequence with keys passed through a `mapper` function.
*
* Sequence({ a: 1, b: 2 }).map(x => x.toUpperCase()) // { A: 1, B: 2 }
*
*/
mapKeys<M>(
mapper: (value?: V, key?: K, seq?: Sequence<K, V>) => M,
thisArg?: any
): Sequence<M, V>;

/**
* Returns a new sequence with only the entries for which the `predicate`
* function returns true.
*
* Sequence({a:1,b:2,c:3,d:4}).map(x => x % 2 === 0) // { b: 2, d: 4 }
* Sequence({a:1,b:2,c:3,d:4}).filter(x => x % 2 === 0) // { b: 2, d: 4 }
*
*/
filter(
Expand Down
11 changes: 11 additions & 0 deletions dist/Immutable.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ var $Sequence = Sequence;
});
return mappedSequence;
},
mapKeys: function(mapper, thisArg) {
var sequence = this;
var mappedSequence = sequence.__makeSequence();
mappedSequence.length = sequence.length;
mappedSequence.__iterateUncached = (function(fn, reverse) {
return sequence.__iterate((function(v, k, c) {
return fn(v, mapper.call(thisArg, k, v, c), c) !== false;
}), reverse);
});
return mappedSequence;
},
filter: function(predicate, thisArg) {
return filterFactory(this, predicate, thisArg, true, false);
},
Expand Down
Loading

0 comments on commit 45e7299

Please sign in to comment.