Comparing version 0.1.8 to 0.1.9
@@ -53,5 +53,5 @@ var canReflect = require("can-reflect"); | ||
}, | ||
"can.getName": function(){ | ||
return canReflect.getName(Type); | ||
}, | ||
"can.getName": function(){ | ||
return canReflect.getName(Type); | ||
}, | ||
"can.isMember": function(value) { | ||
@@ -79,5 +79,5 @@ return value == null || value instanceof Type || isMember(value); | ||
}, | ||
"can.getName": function(){ | ||
return canReflect.getName(Type); | ||
}, | ||
"can.getName": function(){ | ||
return canReflect.getName(Type); | ||
}, | ||
"can.isMember": function(value) { | ||
@@ -95,3 +95,2 @@ return value instanceof Type || isMember(value); | ||
return canReflect.convert(val, Type); | ||
//return canReflect.new(Type, val); | ||
} | ||
@@ -98,0 +97,0 @@ |
@@ -7,28 +7,31 @@ @property {can-type.typeobject} can-type.any Any | ||
Like an [identity function](https://en.wikipedia.org/wiki/Identity_function), `type.Any` is a [can-type.typeobject] that allows any type of a value to be allowed without checking or coercion. | ||
Like an [identity function](https://en.wikipedia.org/wiki/Identity_function), `type.Any` is a [can-type.typeobject] that allows any type of a value to be allowed without checking or coercion. | ||
```js | ||
import { Reflect, type } from "can/ecosystem"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
Reflect.convert(42, type.Any); | ||
// -> 42 | ||
let val = Reflect.convert(42, type.Any); | ||
console.log(val); // -> 42 | ||
Reflect.convert(null, type.Any); | ||
// -> null | ||
val = Reflect.convert(null, type.Any); | ||
console.log(val); // -> null | ||
Reflect.convert([], type.Any); | ||
// -> [] | ||
val = Reflect.convert([], type.Any); | ||
console.log(val); // -> [] | ||
Reflect.convert(new Date(), type.Any); | ||
// Date() | ||
``` | ||
val = Reflect.convert(new Date(), type.Any); | ||
console.log(val); // Date() | ||
``` | ||
@codepen | ||
`type.Any` returns the same instance as passed into [can-reflect.convert] so they are referentially identical. | ||
`type.Any` returns the same instance as passed into [can-reflect.convert] so they are referentially identical. | ||
```js | ||
import { Reflect, type } from "can/ecosystem"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
let today = new Date(); | ||
let today = new Date(); | ||
let val = Reflect.convert(today, type.Any); | ||
``` | ||
let val = Reflect.convert(today, type.Any); | ||
console.log(val); // today | ||
``` | ||
@codepen |
@@ -16,20 +16,2 @@ @page can-type | ||
### type.check | ||
Use [can-type/check] to specify a strongly typed [can-type.typeobject] that verifies the value passed is of the same type. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
const StringType = type.check(String); | ||
let val = Reflect.convert("hello world", StringType); | ||
console.log(val); | ||
// -> hello world | ||
Reflect.convert(42, StringType); | ||
// throws! | ||
``` | ||
@codepen | ||
### type.maybe | ||
@@ -44,13 +26,12 @@ | ||
Reflect.convert(42, NumberType); | ||
// -> 42 | ||
let val = Reflect.convert(42, NumberType); | ||
console.log(val); // -> 42 | ||
Reflect.convert(null, NumberType); | ||
// -> null | ||
val = Reflect.convert(null, NumberType); | ||
console.log(val); // -> null | ||
Reflect.convert(undefined, NumberType); | ||
// -> undefined | ||
val = Reflect.convert(undefined, NumberType); | ||
console.log(val); // -> undefined | ||
Reflect.convert("hello world", NumberType); | ||
// throws! | ||
Reflect.convert("hello world", NumberType); // throws! | ||
``` | ||
@@ -68,4 +49,4 @@ @codepen | ||
Reflect.convert("42", NumberType); | ||
// -> 42 | ||
let val = Reflect.convert("42", NumberType); | ||
console.log(val); // -> 42 | ||
``` | ||
@@ -85,16 +66,32 @@ @codepen | ||
Reflect.convert(date, DateType); | ||
// -> date | ||
let val = Reflect.convert(date, DateType); | ||
console.log(val); // -> date | ||
Reflect.convert(null, DateType); | ||
// -> null | ||
val = Reflect.convert(null, DateType); | ||
console.log(val); // -> null | ||
Reflect.convert(undefined, DateType); | ||
// -> undefined | ||
val = Reflect.convert(undefined, DateType); | ||
console.log(val); // -> undefined | ||
Reflect.convert("12/04/1433", DateType); | ||
// -> Date(1433, 12, 04) | ||
val = Reflect.convert("12/04/1433", DateType); | ||
console.log(val); // -> Date{12/04/1433} | ||
``` | ||
@codepen | ||
### type.check | ||
Use [can-type/check] to specify a strongly typed [can-type.typeobject] that verifies the value passed is of the same type. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
const StringType = type.check(String); | ||
let val = Reflect.convert("hello world", StringType); | ||
console.log(val); // -> hello world | ||
Reflect.convert(42, StringType); // throws! | ||
``` | ||
@codepen | ||
## Creating Models and ViewModels | ||
@@ -124,3 +121,3 @@ | ||
console.log( fib ); // ->Person{ ... } | ||
console.log(fib); // ->Person{ ... } | ||
``` | ||
@@ -183,2 +180,23 @@ @codepen | ||
See [can-stache-define-element] and [can-define-object] for more on its API. | ||
See [can-stache-define-element] and [can-define-object] for more on these APIs. | ||
## How it works | ||
The `can-type` methods work by creating functions that are compatible with [can-reflect.convert canReflect.convert]. | ||
These functions have a [can-symbol/symbols/new] Symbol that points to a function that is responsible for creating an instance of the type. The following is an overview of how this function works: | ||
__1. Determine if value is already the correct type__ | ||
- Maybe types (`type.maybe`, `type.maybeConvert`) will return `true` if the value is `null` or `undefined`. | ||
- Common primitive types (`Number`, `String`, `Boolean`) will return `true` if [typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) returns the correct result. | ||
- Other types will return `true` if the value is an [instanceof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) the type. | ||
- [can-type.typeobject TypeObjects] (or anything with a `can.isMember` Symbol) will return `true` if the `can.isMember` function returns `true`. | ||
- Otherwise, the value is not the correct type. | ||
__2. Handle values of another type__ | ||
If the value is not the correct type: | ||
- `type.maybe` and `type.check` will throw an error. | ||
- `type.convert` and `type.maybeConvert` will convert the value using [can-reflect.convert]. |
@@ -7,16 +7,17 @@ @function can-type/check check | ||
Given a type, returns a [can-type.typeobject] that will check values against that type. Throws if another type is provided as a value. | ||
Given a type, returns a [can-type.typeobject] that will check values against that type. Throws if another type is provided as a value. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
let val = Reflect.convert(new Date(), type.check(Date)); | ||
// Date() | ||
let val = Reflect.convert(new Date(), type.check(Date)); | ||
console.log(val); // Date() | ||
val = Reflect.convert("12/14/1933", type.check(Date)); | ||
// throws for providing an invalid type. | ||
``` | ||
Reflect.convert("12/14/1933", type.check(Date)); | ||
// throws for providing an invalid type. | ||
``` | ||
@codepen | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will strictly enforce values of the provided type. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will strictly enforce values of the provided type. |
@@ -7,16 +7,18 @@ @function can-type/convert convert | ||
Given a type, returns a [can-type.typeobject] that will coerce values to that type. | ||
Given a type, returns a [can-type.typeobject] that will coerce values to that type. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
let val = Reflect.convert(new Date(), type.convert(Date)); | ||
// Date() | ||
let date = new Date(); | ||
let val = Reflect.convert(date, type.convert(Date)); | ||
console.log(val); // date | ||
val = Reflect.convert("12/14/1933", type.convert(Date)); | ||
// Date(1933, 12, 14) | ||
``` | ||
val = Reflect.convert("12/14/1933", type.convert(Date)); | ||
console.log(val); // Date{12/14/1933} | ||
``` | ||
@codepen | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will *coerce* values to the provided type. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will *coerce* values to the provided type. |
@@ -7,22 +7,23 @@ @function can-type/maybe maybe | ||
Given a type, returns a [can-type.typeobject] that will check values against against that type. Throws if the value is not of the provided type or `null` or `undefined`. | ||
Given a type, returns a [can-type.typeobject] that will check values against against that type. Throws if the value is not of the provided type or `null` or `undefined`. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
let val = Reflect.convert(42, type.maybe(Number)); | ||
// -> 42 | ||
let val = Reflect.convert(42, type.maybe(Number)); | ||
console.log(val); // -> 42 | ||
val = Reflect.convert(null), type.maybe(Number)); | ||
// -> null | ||
val = Reflect.convert(null, type.maybe(Number)); | ||
console.log(val); // -> null | ||
val = Reflect.convert(undefined), type.maybe(Number)); | ||
// -> undefined | ||
val = Reflect.convert(undefined, type.maybe(Number)); | ||
console.log(val); // -> undefined | ||
val = Reflect.convert("42", type.maybe(Number)); | ||
// throws for providing an invalid type. | ||
``` | ||
Reflect.convert("42", type.maybe(Number)); | ||
// throws for providing an invalid type. | ||
``` | ||
@codepen | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will enforce conversion to the given type. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will enforce conversion to the given type. |
@@ -7,22 +7,23 @@ @function can-type/maybeConvert maybeConvert | ||
Given a type, returns a [can-type.typeobject] that will check values against that type. Coerces if the value is not of the provided type or `null` or `undefined`. | ||
Given a type, returns a [can-type.typeobject] that will check values against that type. Coerces if the value is not of the provided type or `null` or `undefined`. | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
```js | ||
import { Reflect, type } from "can/everything"; | ||
let val = Reflect.convert(42, type.maybeConvert(Number)); | ||
// -> 42 | ||
let val = Reflect.convert(42, type.maybeConvert(Number)); | ||
console.log(val); // -> 42 | ||
val = Reflect.convert(null), type.maybeConvert(Number)); | ||
// -> null | ||
val = Reflect.convert(null, type.maybeConvert(Number)); | ||
console.log(val); // -> null | ||
val = Reflect.convert(undefined), type.maybeConvert(Number)); | ||
// -> undefined | ||
val = Reflect.convert(undefined, type.maybeConvert(Number)); | ||
console.log(val); // -> undefined | ||
val = Reflect.convert("42", type.maybeConvert(Number)); | ||
// -> 42 | ||
``` | ||
val = Reflect.convert("42", type.maybeConvert(Number)); | ||
console.log(val); // -> 42 | ||
``` | ||
@codepen | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@param {Function} Type A constructor function that values will be checked against. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will enforce conversion to the given type. | ||
@return {can-type.typeobject} A [can-type.typeobject] which will enforce conversion to the given type. |
@@ -26,5 +26,5 @@ @typedef {{}} can-type.typeobject TypeObject | ||
Reflect.convert("12/02/1355", dateType); | ||
// -> Date(1355, 12, 02) | ||
let val = Reflect.convert("12/02/1355", dateType); | ||
console.log(val); // -> Date{12/02/1355} | ||
``` | ||
@codepen |
{ | ||
"name": "can-type", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Type definitions", | ||
@@ -5,0 +5,0 @@ "homepage": "https://canjs.com/doc/can-type.html", |
23782
18
199