open-nomad/ui/app/utils/properties/sum-aggregation.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

20 lines
540 B
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { computed } from '@ember/object';
2017-09-19 14:47:10 +00:00
// An Ember.Computed property for summating all properties from a
// set of objects.
//
// ex. list: [ { foo: 1 }, { foo: 3 } ]
// sum: sumAggregationProperty('list', 'foo') // 4
export default function sumAggregationProperty(listKey, propKey) {
2021-12-28 14:45:20 +00:00
return computed(`${listKey}.@each.${propKey}`, function () {
return this.get(listKey)
.mapBy(propKey)
.reduce((sum, count) => sum + count, 0);
2017-09-19 14:47:10 +00:00
});
}