8000 Merge branch 'master' into upgrade-immutable · typed-immutable/typed-immutable@cc6493f · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit cc6493f

Browse files
Merge branch 'master' into upgrade-immutable
2 parents 6a0c3b5 + 3ce7f0e commit cc6493f

File tree

5 files changed

+374
-8
lines changed

5 files changed

+374
-8
lines changed

Readme.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Point({x: "1", y: "2"}) // => TypeError: Invalid value for "x" field:
3737
```
3838

3939

40-
Record types definitions may also be provided a default values for feilds for a convinence of use:
40+
Record types definitions may also be provided a default values for feilds for a convenience of use:
4141

4242
```js
4343
var Point = Record({x: Number(0), y: Number(0)})
@@ -182,7 +182,7 @@ Numbers([1, 2, 3, "4", "5"]) // => TypeError: Invalid value: "4" is not a number
182182
Numbers([1, 2, 3]).push(null) // => TypeError: Invalid value: "null" is not a number
183183
```
184184

185-
Typed lists can also be named for convinience:
185+
Typed lists can also be named for convenience:
186186

187187
```js
188188
var Strings = List(String, "Strings")
@@ -225,6 +225,45 @@ xs.toString() // => Typed.List(Number)([ 1, 2 ])
225225

226226
As you can see from example above original `ps` list was of `Point` records while mapped `xs` list is of numbers and that is refleced in the type of the list. Although given that JS is untyped language theer is no guarantee that mapping function will return values of the same type which makes things little more complex, result of such mapping will be list of union type of all types that mapping funciton produced (see types section for union types).
227227

228+
### Map
229+
230+
You can define a typed map by providing `Map` the type for the key and the type for the value:
231+
232+
```js
233+
var {Map, Record} = require("typed-immutable")
234+
var Product = Record({name: String}, "Product")
235+
236+
var Products = Map(Number, Product)
237+
238+
Products().toString() // ‘Typed.Map(Number, Product)({})’
239+
240+
Products([[1, {name: "Mapper 1000"}]]).toString()
241+
//Typed.Map(Number, Product)({ 1: Product({ "name": "Mapper 1000" }) })
242+
```
243+
244+
Typed maps may contain only entries with key and value that match the specified type:
245+
246+
```js
247+
248+
Products([[1, "Mapper 1000"]])
249+
// => TypeError: Invalid value: Invalid data structure "Mapper 1000" was passed to Product
250+
251+
Products().set("P1", {name: "Mapper 1000"})
252+
// TypeError: Invalid key: "P1" is not a number
253+
254+
// All keys in an object are strings, so this fails too:
255+
Products({1: {name: "Mapper 1000"}}) // TypeError: Invalid key: "1" is not a number
256+
```
257+
258+
Note the last example - all keys in an object are strings so if you instantiate a map from an object the type of your key must be a string (or something that handles strings).
259+
260+
As with other types Typed maps can also be named for convenience:
261+
262+
```js
263+
var Products = Map(Number, Product, "Products")
264+
Products([[1, {name: "Mapper 1000"}]]).toString()
265+
// Products({ 1: Product({ "name": "Mapper 1000" }) })
266+
```
228267

229268
### Types
230269

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export {Record} from "./record"
22
export {List} from "./list"
3+
export {Map} from "./map"
34
export {Typed, typeOf, Type, Any, Union, Maybe} from "./typed"

src/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export const List = function(descriptor, label) {
303303
const type = typeOf(descriptor)
304304

305305
if (type === Any) {
306-
throw TypeError("Typed.List was passed an invalid type descriptor: ${descriptor}")
306+
throw TypeError(`Typed.List was passed an invalid type descriptor: ${descriptor}`)
307307
}
308308

309309
const ListType = function(value) {

src/map.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ export const Map = function(keyDescriptor, valueDescriptor, label) {
249249
const type = new EntryType(keyType, valueType, label)
250250

251251
const MapType = function(value) {
252-
const isThis = this instanceof MapType
253-
const constructor = isThis ? this.constructor : MapType
252+
const isMapType = this instanceof MapType
253+
const constructor = isMapType ? this.constructor : MapType
254254

255255
if (value instanceof constructor) {
256256
return value
@@ -262,9 +262,7 @@ export const Map = function(keyDescriptor, valueDescriptor, label) {
262262
throw result
263263
}
264264

265-
const isCall = isThis && construct.prototype === this
266-
267-
if (!isCall && isThis) {
265+
if (isMapType && !this[$store]) {
268266
this[$store] = result[$store]
269267
this.size = result.size
270268
} else {
@@ -273,6 +271,18 @@ export const Map = function(keyDescriptor, valueDescriptor, label) {
273271

274272
return this
275273
}
274+
275+
MapType.of = (...keyValues) => {
276+
return MapType().withMutations(map => {
277+
for (var i = 0; i < keyValues.length; i += 2) {
278+
if (i + 1 >= keyValues.length) {
279+
throw new Error('Missing value for key: ' + keyValues[i]);
280+
}
281+
map.set(keyValues[i], keyValues[i + 1]);
282+
}
283+
});
284+
}
285+
276286
MapType.prototype = Object.create(MapPrototype, {
277287
constructor: {value: MapType},
278288
[$type]: {value: type},

0 commit comments

Comments
 (0)
0