ember-array-computed-macros
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -7,2 +7,3 @@ import macroAlias from 'ember-computed-decorators/macro-alias'; | ||
orderBy as _orderBy, | ||
orderByComputedProperty as _orderByComputedProperty, | ||
groupBy as _groupBy, | ||
@@ -25,2 +26,3 @@ filter as _filter, | ||
export var orderBy = macroAlias(_orderBy); | ||
export var orderByComputedProperty = macroAlias(_orderByComputedProperty); | ||
export var groupBy = macroAlias(_groupBy); | ||
@@ -27,0 +29,0 @@ export var filter = macroAlias(_filter); |
@@ -29,4 +29,4 @@ import Ember from 'ember'; | ||
export var orderBy = function(listProperty, ...sortProperties) { | ||
sortProperties = sortProperties.map((sortProperty) => { | ||
function _parseSortProperties(sortProperties) { | ||
return sortProperties.map((sortProperty) => { | ||
let descending = false; | ||
@@ -41,23 +41,47 @@ | ||
}); | ||
} | ||
return computed(`${listProperty}.@each`, function() { | ||
const list = get(this, listProperty); | ||
function _orderBy(list, sortProperties) { | ||
// Bug/workaround: Ember.ArrayProxy has no method sort. | ||
// see: https://github.com/emberjs/ember.js/issues/11936 | ||
if (typeof list.sort !== 'function' && typeof list.toArray === 'function') { | ||
list = list.toArray(); | ||
} | ||
return list.sort((item1, item2) => { | ||
let result = 0; | ||
return list.sort((item1, item2) => { | ||
let result = 0; | ||
sortProperties.forEach(([sortProperty, descending]) => { | ||
if (result === 0) { | ||
result = compare(get(item1, sortProperty), get(item2, sortProperty)); | ||
if (result !== 0 && descending) { | ||
result = result * -1; | ||
} | ||
sortProperties.forEach(([sortProperty, descending]) => { | ||
if (result === 0) { | ||
result = compare(get(item1, sortProperty), get(item2, sortProperty)); | ||
if (result !== 0 && descending) { | ||
result = result * -1; | ||
} | ||
}); | ||
} | ||
}); | ||
return result; | ||
}); | ||
return result; | ||
}); | ||
} | ||
export var orderBy = function(listProperty, ...sortProperties) { | ||
sortProperties = _parseSortProperties(sortProperties); | ||
const computedValueKeys = sortProperties.map((item) => item[0]); | ||
return computed(`${listProperty}.[].{${computedValueKeys.join(',')}}`, function() { | ||
const list = get(this, listProperty); | ||
return _orderBy(list, sortProperties); | ||
}).readOnly(); | ||
}; | ||
export var orderByComputedProperty = function(listProperty, orderProperty) { | ||
return computed(`${listProperty}.[]`, `${orderProperty}.[]`, function() { | ||
const list = get(this, listProperty); | ||
const sortProperties = _parseSortProperties(this.get(orderProperty)); | ||
return _orderBy(list, sortProperties); | ||
}).readOnly(); | ||
}; | ||
export var sumBy = function(listProperty, valueProperty) { | ||
@@ -64,0 +88,0 @@ return computed(`${listProperty}.@each.${valueProperty}`, function() { |
{ | ||
"name": "ember-array-computed-macros", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "This addon is supplemental to the already existing computed macros existing in Ember.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -46,6 +46,24 @@ # ember-array-computed-macros | ||
], | ||
oderedNames: orderBy('names', 'last', 'first'. 'age:desc') | ||
orderedNames: orderBy('names', 'last', 'first'. 'age:desc') | ||
}); | ||
``` | ||
There is also a variant for `groupBy` that works with a dynamic list of | ||
sortProperties called `groupByComputedProperty`: | ||
```js | ||
import Ember from 'ember'; | ||
import { map } from 'ember-array-computed-macros'; | ||
export default Ember.Component.extend({ | ||
names: [ | ||
{ first: 'Tom', last: 'Dale', age: 21 }, | ||
{ first: 'Yehuda', last: 'Katz', age: 42 } | ||
], | ||
nameOrdering: ['last', 'first', 'age:desc'], | ||
orderedNames: orderByComputedProperty('names', 'nameOrdering') | ||
}); | ||
``` | ||
### `groupBy(listProperty, valueProperty)` | ||
@@ -52,0 +70,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13677
190
186