Skip to content

Commit

Permalink
Adding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
WHenderson committed Jan 1, 2016
1 parent 5ee6b39 commit c01067d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ require(['knockout', 'ko-typed'], function (ko) {
* Custom types
* Overrides

**Example**
```js
var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });

typed(); // good. undefined is of a supported type.
typed(undefined); // good. undefined is of a supported type.
typed('string'); // good. 'string' is of a supported type.

base(10); typed(); // bad. 10 is not of a supported type.
typed(10); // bad. 10 is not of a supported type.
```

See [examples/type](../examples/type) for more examples.
See [test/coverage/type](../test/coverage/type) for detailed tests.

* [extenders-convert](./documentation/extenders-type.md)
Create a computed observable which converts to and from internal and external types.

Expand All @@ -76,6 +92,30 @@ require(['knockout', 'ko-typed'], function (ko) {
* Default conversions
* Overrides

**Example**
```js
var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });
var converted = typed.extend({ convert: true });

converted('');
assert.strictEqual(base(), undefined);
converted(10);
assert.strictEqual(base(), '10');
converted('string');
assert.strictEqual(base(), 'string');

base(undefined);
assert.strictEqual(converted(), undefined);
base('10');
assert.strictEqual(converted(), '10');
base('string');
assert.strictEqual(converted(), 'string');
```

See [examples/convert](../examples/convert) for more examples.
See [test/coverage/convert](../test/coverage/convert) for detailed tests.

* [converters](./documentation/converters.md)
Converters are provides between all common types where conversion is common and unambiguous.

25 changes: 25 additions & 0 deletions documentation/extenders-convert.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ Supports:
Uses the [is-an](https://github.com/WHenderson/is-an) library for generic type matching.
See [Extenders](./extenders.md) for generic information around ko-typed extenders.

### Example

```js
var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });
var converted = typed.extend({ convert: true });

converted('');
assert.strictEqual(base(), undefined);
converted(10);
assert.strictEqual(base(), '10');
converted('string');
assert.strictEqual(base(), 'string');

base(undefined);
assert.strictEqual(converted(), undefined);
base('10');
assert.strictEqual(converted(), '10');
base('string');
assert.strictEqual(converted(), 'string');
```

See [examples/convert](../examples/convert) for more examples.
See [test/coverage/convert](../test/coverage/convert) for detailed tests.

### Syntax

* `options: String|Array` -> `options.type: String|Array`
Expand Down
19 changes: 19 additions & 0 deletions documentation/extenders-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ Supports:
Uses the [is-an](https://github.com/WHenderson/is-an) library for generic type matching.
See [Extenders](./extenders.md) for generic information around ko-typed extenders.

### Example

```js
var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });

typed(); // good. undefined is of a supported type.
typed(undefined); // good. undefined is of a supported type.
typed('string'); // good. 'string' is of a supported type.

base(10); typed(); // bad. 10 is not of a supported type.
typed(10); // bad. 10 is not of a supported type.
```

See [examples/type](../examples/type) for more examples.
See [test/coverage/type](../test/coverage/type) for detailed tests.

### Syntax

```js
Expand All @@ -44,3 +61,5 @@ ko.observable().extend({ type: options })
Multiple `TypeName : Function`'s can be provided in this way.
Each `TypeName` provided this way is added to `options.type`.
* [common options](./extenders.md)


20 changes: 20 additions & 0 deletions examples/convert/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var ko = require('../../index');
var assert = require('chai').assert;

var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });
var converted = typed.extend({ convert: true });

converted('');
assert.strictEqual(base(), undefined);
converted(10);
assert.strictEqual(base(), '10');
converted('string');
assert.strictEqual(base(), 'string');

base(undefined);
assert.strictEqual(converted(), undefined);
base('10');
assert.strictEqual(converted(), '10');
base('string');
assert.strictEqual(converted(), 'string');
35 changes: 35 additions & 0 deletions examples/type/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var ko = require('../../index');
var assert = require('chai').assert;

var base = ko.observable();
var typed = base.extend({ type: 'Undefined|String' });

assert.doesNotThrow(
function () {
typed();
}
);

base(10);
assert.throws(
function () {
typed();
},
TypeError,
'Unexpected internal type. Expected Undefined|String, got Number'
);

assert.doesNotThrow(
function () {
typed(undefined);
typed('string');
}
);

assert.throws(
function () {
typed(10);
},
TypeError,
'Unexpected external type. Expected Undefined|String, received Number'
);

0 comments on commit c01067d

Please sign in to comment.