diff --git a/Readme.md b/Readme.md index d4dfb34..7eb3c6e 100644 --- a/Readme.md +++ b/Readme.md @@ -368,6 +368,8 @@ v2.toString() // => Typed.Record({value: Any})({ "value": "hello" }) v3.toString() // => Typed.Record({value: Any})({ "value": Typed.Record({value: Any})({ "value": "hello" }) }) ``` +## Contribution +- Run `npm start` before `npm test` as the tests are ran on built code ## License diff --git a/src/list.js b/src/list.js index c210f89..978272b 100644 --- a/src/list.js +++ b/src/list.js @@ -289,6 +289,25 @@ class TypedList extends BaseTypeInferedList { } } } + flatMap(mapper, context) { + if (this.size === 0) { + return this + } else { + const result = TypeInferedList.from(this).flatMap(mapper, context) + if (this[$store] === result[$store]) { + return this + } + if (result[$type] === this[$type]) { + const list = construct(this) + list[$store] = result[$store] + list.size = result.size + return list + } else { + return result + } + } + } + } export const List = function(descriptor, label) { diff --git a/src/test/list.js b/src/test/list.js index b9feece..facddff 100644 --- a/src/test/list.js +++ b/src/test/list.js @@ -996,3 +996,22 @@ test('empty list optimization', assert => { assert.equal(Points([Point({x: 1})]).clear(), Points([Point({x: 1}), Point({y: 2})]).clear()) }) + +test('flatMap', assert => { + var numbers = NumberList.of(97, 98, 99); + var letters = numbers.flatMap(v => Immutable.fromJS([ + String.fromCharCode(v), + String.fromCharCode(v).toUpperCase(), + ])) + + assert.deepEqual(letters.toArray(), ['a','A','b','B','c','C']) + + var letters = numbers.flatMap(v => [ + String.fromCharCode(v), + String.fromCharCode(v).toUpperCase(), + ]) + + assert.deepEqual(letters.toArray(), ['a','A','b','B','c','C']) + +}) +