@@ -22,6 +22,20 @@ import {Id, IdOrNull, Ids} from './common.d';
2222 */
2323export type Metric = number ;
2424
25+ /**
26+ * The MetricCallback type describes a function that takes a Metric's Id and a
27+ * callback to loop over each Row within it.
28+ *
29+ * A MetricCallback is provided when using the forEachMetric method, so that you
30+ * can do something based on every Metric in the Metrics object. See that method
31+ * for specific examples.
32+ *
33+ * @param metricId The Id of the Metric that the callback can operate on.
34+ * @param metric The value of the Metric.
35+ * @category Callback
36+ */
37+ export type MetricCallback = ( metricId : Id , metric ?: Metric ) => void ;
38+
2539/**
2640 * The Aggregate type describes a custom function that takes an array or numbers
2741 * and returns an aggregate that is used as a Metric.
@@ -478,6 +492,38 @@ export interface Metrics {
478492 */
479493 getMetricIds ( ) : Ids ;
480494
495+ /**
496+ * The forEachMetric method takes a function that it will then call for each
497+ * Metric in the Metrics object.
498+ *
499+ * This method is useful for iterating over all the Metrics in a functional
500+ * style. The `metricCallback` parameter is a MetricCallback function that
501+ * will called with the Id of each Metric and its value.
502+ *
503+ * @param metricCallback The function that should be called for every Metric.
504+ * @example
505+ * This example iterates over each Metric in a Metrics object.
506+ *
507+ * ```js
508+ * const store = createStore().setTable('species', {
509+ * dog: {price: 5},
510+ * cat: {price: 4},
511+ * worm: {price: 1},
512+ * });
513+ * const metrics = createMetrics(store)
514+ * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
515+ * .setMetricDefinition('lowestPrice', 'species', 'min', 'price');
516+ *
517+ * metrics.forEachMetric((metricId, metric) => {
518+ * console.log([metricId, metric]);
519+ * });
520+ * // -> ['highestPrice', 5]
521+ * // -> ['lowestPrice', 1]
522+ * ```
523+ * @category Iterator
524+ */
525+ forEachMetric ( metricCallback : MetricCallback ) : void ;
526+
481527 /**
482528 * The hasMetric method returns a boolean indicating whether a given Metric
483529 * exists in the Metrics object, and has a value.
0 commit comments