Skip to content

Commit

Permalink
add instanceOf and typeOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Nov 6, 2016
1 parent fd6d9a7 commit 5e09c4b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { nameOfMacro } from 'ember-awesome-macros';
* [`equal`](#equal)
* [`gt`](#gt)
* [`gte`](#gte)
* [`instanceOf`](#instanceof)
* [`lt`](#lt)
* [`lte`](#lte)

Expand All @@ -88,6 +89,7 @@ import { nameOfMacro } from 'ember-awesome-macros';
##### Object
* [`getBy`](#getby)
* [`hash`](#hash)
* [`typeOf`](#typeof)

##### Math
* [`math`](#math)
Expand Down Expand Up @@ -419,6 +421,17 @@ value1: indexOf('array', 2), // 0
value2: indexOf('array', 2, 2) // 3
```

##### `instanceOf`
wraps [`instanceof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) operator

```js
key1: {},
key2: false,
key3: '',
value1: instanceOf('key1', Object), // true
value2: instanceOf(or('key2', 'key3'), String) // true
```

##### `isAny`
wraps [`Ember.Enumerable.isAny`](http://emberjs.com/api/classes/Ember.Enumerable.html#method_isAny), allows composing

Expand Down Expand Up @@ -790,6 +803,17 @@ originalValue: 'TestString',
newValue: toUpper('originalValue') // 'TESTSTRING'
```

##### `typeOf`
wraps [`typeOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeOf) operator

```js
key1: {},
key2: false,
key3: '',
value1: typeOf('key1'), // 'object'
value2: typeOf(or('key2', 'key3')) // 'string'
```

##### `underscore`
wraps [`Ember.String.underscore`](http://emberjs.com/api/classes/Ember.String.html#method_underscore), allows composing

Expand Down
2 changes: 2 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { default as hash } from './hash';
export { default as htmlSafe } from './html-safe';
export { default as includes } from './includes';
export { default as indexOf } from './index-of';
export { default as instanceOf } from './instance-of';
export { default as isAny } from './is-any';
export { default as isEvery } from './is-every';
export { default as isHtmlSafe } from './is-html-safe';
Expand Down Expand Up @@ -64,6 +65,7 @@ export { default as sum } from './sum';
export { default as tag } from './tag';
export { default as toLower } from './to-lower';
export { default as toUpper } from './to-upper';
export { default as typeOf } from './type-of';
export { default as underscore } from './underscore';
export { default as uniqBy } from './uniq-by';
export { default as uniq } from './uniq';
Expand Down
10 changes: 10 additions & 0 deletions addon/instance-of.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import computed from './computed';

export default function(...keys) {
return computed(...keys, (object, constructor) => {
if (constructor === undefined) {
return undefined;
}
return object instanceof constructor;
});
}
7 changes: 7 additions & 0 deletions addon/type-of.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import computed from './computed';

export default function(...keys) {
return computed(...keys, object => {
return typeof object;
});
}
45 changes: 45 additions & 0 deletions tests/integration/instance-of-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { instanceOf, raw } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import compute from '../helpers/compute';

module('Integration | Macro | instance of');

test('lookup: object is instance of Object', function(assert) {
compute({
assert,
computed: instanceOf('key1', 'key2'),
properties: {
key1: {},
key2: Object
},
expected: true
});
});

test('lookup: object is not instance of String', function(assert) {
compute({
assert,
computed: instanceOf('key1', 'key2'),
properties: {
key1: {},
key2: String
},
expected: false
});
});

test('value: object is instance of Object', function(assert) {
compute({
assert,
computed: instanceOf({}, Object),
expected: true
});
});

test('composing: object is instance of Object', function(assert) {
compute({
assert,
computed: instanceOf(raw({}), raw(Object)),
expected: true
});
});
32 changes: 32 additions & 0 deletions tests/integration/type-of-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { typeOf, raw } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import compute from '../helpers/compute';

module('Integration | Macro | type of');

test('lookup: object is type of object', function(assert) {
compute({
assert,
computed: typeOf('key1'),
properties: {
key1: {}
},
expected: 'object'
});
});

test('value: object is type of object', function(assert) {
compute({
assert,
computed: typeOf({}),
expected: 'object'
});
});

test('composing: object is type of object', function(assert) {
compute({
assert,
computed: typeOf(raw({})),
expected: 'object'
});
});

0 comments on commit 5e09c4b

Please sign in to comment.