Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer committed Jul 28, 2016
2 parents 8c93559 + 02c1062 commit 8478aa6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

})

0 comments on commit 8478aa6

Please sign in to comment.