Skip to content

Commit

Permalink
Merge pull request #40 from typed-immutable/immutables-in-records
Browse files Browse the repository at this point in the history
Support Immutables in records.
  • Loading branch information
lukesneeringer committed Dec 9, 2016
2 parents ff73524 + 1b01f22 commit ba575e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-immutable",
"version": "0.0.9",
"version": "0.1.0",
"description": "Immutable structurally typed data",
"author": "Irakli Gozalishvili <[email protected]> (http:https://jeditoolkit.com)",
"homepage": "https://github.com/typed-immutable/typed-immutable",
Expand Down
22 changes: 22 additions & 0 deletions src/typed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ba575e1

Please sign in to comment.