diff --git a/CHANGELOG.md b/CHANGELOG.md index ed4d6a2..8e81741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +### Release 0.1.0 + +The `typed-immutable` maintainers are pleased to release a feature release +for `typed-immutable`. It includes the following changes: + + * Added support for the use of Immutable objects or classes in records. + [@lukesneeringer, #40] + + +### Release 0.0.9 + +The `typed-immutable` maintainers are pleased to release a minor upgrade +and bugfix release for `typed-immutable`. It includes the following changes: + + * Added support for `Map.merge` [@davecoates, #36] + ### Release 0.0.8 The `typed-immutable` maintainers are pleased to release a minor upgrade diff --git a/package.json b/package.json index 1dc319c..3bc463e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typed-immutable", - "version": "0.0.9", + "version": "0.1.0", "description": "Immutable structurally typed data", "author": "Irakli Gozalishvili (http://jeditoolkit.com)", "homepage": "https://github.com/typed-immutable/typed-immutable", diff --git a/src/typed.js b/src/typed.js index 5057ab1..7185b4a 100644 --- a/src/typed.js +++ b/src/typed.js @@ -110,6 +110,15 @@ export const typeOf = (x, type=typeof(x)) => x === Array ? Typed.Array.prototype : x === Symbol ? Typed.Symbol.prototype : x === Date ? Typed.Date.prototype : + + // support Immutable objects + typeof x.toJS === 'function' ? new Typed.ImmutableClass(x.constructor)(x) : + + // support Immutable classes -- all Immutable classes have `isX` static + // methods, e.g. List.isList, Map.isMap, Set.isSet; using this to + // approximate that + typeof x[`is${x.name}`] === 'function' ? Typed.ImmutableClass(x).prototype : + Any; export const Any = Typed("Any", value => value)() @@ -148,6 +157,19 @@ Typed.Date = Typed("Date", value => { return d }) +Typed.ImmutableClass = cls => Typed(`Immutable.${cls.name}`, value => { + if (value instanceof cls) { + return value + } + else if (typeof value === 'undefined' || value === null) { + return new TypeError(`Expected ${cls.name}; got nothing.`) + } + else { + try { return new cls(value); } + catch (ex) { return ex; } + } +}) + class MaybeType extends Type { constructor(type) {