Keep your app code DRY and copypasta free with computed property macaronis (macros)
Keep your app code DRY and copypasta free with computed property macaronis (macros)
Keep your app code DRY and copypasta free with computed property macaronis (macros) for Ember.js 1.13.x and greater.
Computed property macros (CPM) are great for DRYing up your code, and Ember.js ships with a few handy computed macros. This addon adds a few more functional-style macros, and can be thought of as the "lodash equivalent" of Ember CPM libraries.
Chaining is not supported... yet.
First, import the macro(s) you need, or the whole thing:
…findFromCollectionByKeyReturns the first item with a property matching the passed value from a dependent key.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to find by@param {String} valueKey The key name that returns the value to findEmber.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedId: 1,
selectedItem: findFromCollectionByKey('items', 'id', 'selectedId') // { id: 1, name: 'foo' }
});findFromCollectionByValueReturns the first item with a property matching the passed value.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to find by@param {*} value The value to match`Ember.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedItem: findFromCollectionByValue('items', 'id', 1) // { id: 1, name: 'foo' }
});rejectFromCollectionByKeyReturns an array with the items that do not match the passed value from a dependent key.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to reject by@param {String} valueKey The key name that returns the value to rejectEmber.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedId: 2,
selectedItem: rejectFromCollectionByKey('items', 'id', 'selectedId') // [{ id: 1, name: 'foo' }]
});rejectFromCollectionByValueReturns an array with the items that do not match the passed value.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to reject by@param {*} value The value to rejectEmber.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedItem: rejectFromCollectionByValue('items', 'id', 2) // [{ id: 1, name: 'foo' }]
});filterFromCollectionByKeyReturns an array with just the items with the matched property.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to filter by@param {String} valueKey The key name that returns the value to filterEmber.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedId: 1,
selectedItem: filterFromCollectionByKey('items', 'id', 'selectedId') // [{ id: 1, name: 'foo' }]
});filterFromCollectionByContainsReturns an array with just the items that are contained in another array.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to filter by@param {Array} values The array of values to filterEmber.Object.extend({
items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }],
selectedId: 1,
selectedItem: filterFromCollectionByContains('items', 'id', [1]) // [{ id: 1, name: 'foo' }]
});collectionWithoutKeyReturns an array without an item by dependent key.
@param {String} collectionKey The key name for the collection@param {String} propName The key name for the property to excludeEmber.Object.extend({
items: [1, 2, 3],
selectedItem: 1,
remainingItems: collectionWithoutKey('items', 'selectedItem') // [2, 3]
});reduceCollectionByKeyCombines the values of the enumerator into a single value, using a dependent key.
@param {String} collectionKey The key name for the collection@param {String} dependentKey The key name for the property to reduce@param {*} startValue The initial valueEmber.Object.extend({
items: [{ name: 'foo', age: 2 }, { name: 'bar', age: 5 }],
selectedItem: reduceCollectionByKey('items', 'age', 0) // 7
});isEqualByKeysStrict equality using dependent keys.
@param {String} firstKey The key name for the first property@param {String} secondKey The key name for the second propertyEmber.Object.extend({
employeeId: 1
selectedId: 1,
isSelected: isEqualByKeys('employeeId', 'selectedId') // true
});ifThenElseWithKeysTernary conditional with dependent keys.
@param {String} conditionalKey The key name for the conditional property@param {String} trueKey The key name for the property to return when the conditional is true@param {String} falseKey The key name for the property to return when the conditional is falseEmber.Object.extend({
isSelected: true,
selectedText: 'Is Enabled',
deselectedText: 'Is Disabled',
displayText: ifThenElseWithKeys('isSelected', 'selectedText', 'deselectedText') // 'Is Enabled'
});ifThenElseWithValuesTernary conditional.
@param {String} conditionalKey The key name for the conditional property@param {String} trueValue The value to return when the conditional is true@param {String} falseValue The value to return when the conditional is falseEmber.Object.extend({
isSelected: true,
displayText: ifThenElseWithValues('isSelected', 'Is Enabled', 'Is Disabled') // 'Is Enabled'
});gteGreater than or equal to comparison between two dependent keys.
@param {String} lhsKey The key name for the left hand side of the operator@param {String} rhsKey The key name for the right hand side of the operatorEmber.Object.extend({
first: 5,
second: 2,
isFirstGreaterThanOrEqualToSecond: gte('first', 'second') // true
});gtGreater than comparison between two dependent keys.
@param {String} lhsKey The key name for the left hand side of the operator@param {String} rhsKey The key name for the right hand side of the operatorEmber.Object.extend({
first: 5,
second: 2,
isFirstGreaterThanSecond: gt('first', 'second') // true
});lteLesser than or equal to comparison between two dependent keys.
@param {String} lhsKey The key name for the left hand side of the operator@param {String} rhsKey The key name for the right hand side of the operatorEmber.Object.extend({
first: 5,
second: 2,
isFirstLesserThanOrEqualToSecond: lte('first', 'second') // false
});ltLesser than comparison between two dependent keys.
@param {String} lhsKey The key name for the left hand side of the operator@param {String} rhsKey The key name for the right hand side of the operatorEmber.Object.extend({
first: 5,
second: 2,
isFirstLessThanSecond: lt('first', 'second') // false
});getPropertiesByKeysReturns a POJO containing all the key-values that match the dependent keys.
@param {...rest} dependentKeys Argument list of dependent keysEmber.Object.extend({
age: 5,
name: 'foo',
props: getPropertiesByKeys('age', 'name') // { age: 5, name: 'foo' }
});joinWithReturns a string of values joined together with a separator.
@param {String} seperator Separator to join values with@param {...rest} dependentKeys Argument list of dependent keysEmber.Object.extend({
firstName: 'Derek',
lastName: 'Zoolander',
fullName: joinWith(' ', 'firstName', 'lastName') // 'Derek Zoolander'
});git clone this repositorynpm installbower installember serverember testember test --serverember buildFor more information on using ember-cli, visit http://www.ember-cli.com/.
No open issues yet, or sync has not completed.