open-nomad/ui/app/utils/properties/sum-aggregation.js
2023-04-10 15:36:59 +00:00

20 lines
540 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { computed } from '@ember/object';
// 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) {
return computed(`${listKey}.@each.${propKey}`, function () {
return this.get(listKey)
.mapBy(propKey)
.reduce((sum, count) => sum + count, 0);
});
}