Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Immutables in records. #40

Merged
merged 1 commit into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support Immutables in records.
  • Loading branch information
lukesneeringer committed Dec 9, 2016
commit 1b01f22c61badf448a36650885ec809c81e7f6fa
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