diff --git a/README.md b/README.md index 77f3cc8..7f5cfd1 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ import { nameOfMacro } from 'ember-awesome-macros'; * [`equal`](#equal) * [`gt`](#gt) * [`gte`](#gte) +* [`instanceOf`](#instanceof) * [`lt`](#lt) * [`lte`](#lte) @@ -88,6 +89,7 @@ import { nameOfMacro } from 'ember-awesome-macros'; ##### Object * [`getBy`](#getby) * [`hash`](#hash) +* [`typeOf`](#typeof) ##### Math * [`math`](#math) @@ -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 @@ -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 diff --git a/addon/index.js b/addon/index.js index 9dfe86c..c8e3fe8 100644 --- a/addon/index.js +++ b/addon/index.js @@ -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'; @@ -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'; diff --git a/addon/instance-of.js b/addon/instance-of.js new file mode 100644 index 0000000..3b52d7b --- /dev/null +++ b/addon/instance-of.js @@ -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; + }); +} diff --git a/addon/type-of.js b/addon/type-of.js new file mode 100644 index 0000000..a1a47c4 --- /dev/null +++ b/addon/type-of.js @@ -0,0 +1,7 @@ +import computed from './computed'; + +export default function(...keys) { + return computed(...keys, object => { + return typeof object; + }); +} diff --git a/tests/integration/instance-of-test.js b/tests/integration/instance-of-test.js new file mode 100644 index 0000000..a728f0e --- /dev/null +++ b/tests/integration/instance-of-test.js @@ -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 + }); +}); diff --git a/tests/integration/type-of-test.js b/tests/integration/type-of-test.js new file mode 100644 index 0000000..8aa6bd9 --- /dev/null +++ b/tests/integration/type-of-test.js @@ -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' + }); +});