@nodeswork/utils
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -1,36 +0,3 @@ | ||
export interface MetricsValue<V> { | ||
operand: string; | ||
value: V; | ||
} | ||
export interface Operator<T> { | ||
(val1: T, val2: T): T; | ||
} | ||
export declare class MetricsOperator { | ||
private operators; | ||
constructor(); | ||
registerAggregator<T>(operand: string, oper: Operator<T>): void; | ||
operate<V>(values: MetricsValue<V>[]): MetricsValue<V>; | ||
} | ||
export declare const operator: MetricsOperator; | ||
export declare const SUM = "sum"; | ||
export declare function Sum(val: number): MetricsValue<number>; | ||
export declare const COUNT = "count"; | ||
export declare function Count(val: number): MetricsValue<number>; | ||
export declare const AVERAGE = "avg"; | ||
export declare function Average(val: any, weight?: number): MetricsValue<AverageValue>; | ||
export interface AverageValue { | ||
v: any; | ||
w: number; | ||
} | ||
export declare const MAX = "max"; | ||
export declare function Max(val: number): MetricsValue<number>; | ||
export declare const MIN = "min"; | ||
export declare function Min(val: number): MetricsValue<number>; | ||
export declare const LAST = "last"; | ||
export declare function Last(val: any): MetricsValue<any>; | ||
export declare const FIRST = "first"; | ||
export declare function First(val: any): MetricsValue<any>; | ||
export declare const COLLECT = "collect"; | ||
export declare function Collect(val: any): MetricsValue<any[]>; | ||
export declare const UNION = "union"; | ||
export declare function Union(val: any): MetricsValue<any[]>; | ||
export * from './built-in.operators'; | ||
export * from './def'; | ||
export * from './operators'; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("underscore"); | ||
const error_1 = require("../error"); | ||
class MetricsOperator { | ||
constructor() { | ||
this.operators = {}; | ||
} | ||
registerAggregator(operand, oper) { | ||
this.operators[operand] = oper; | ||
} | ||
operate(values) { | ||
if (values == null || values.length === 0) { | ||
return null; | ||
} | ||
let res = null; | ||
for (const value of values) { | ||
if (res == null) { | ||
res = value; | ||
} | ||
else { | ||
const oper = this.operators[value.operand]; | ||
if (oper == null) { | ||
throw error_1.NodesworkError.internalServerError(`Unknown operator ${value.operand}`); | ||
} | ||
else if (value.operand != res.operand) { | ||
throw error_1.NodesworkError.internalServerError(`Inconsistent operands ${value.operand} vs ${res.operand}`); | ||
} | ||
else { | ||
res = oper(res, value); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
} | ||
exports.MetricsOperator = MetricsOperator; | ||
exports.operator = new MetricsOperator(); | ||
exports.SUM = 'sum'; | ||
function Sum(val) { | ||
return { operand: exports.SUM, value: val }; | ||
} | ||
exports.Sum = Sum; | ||
exports.operator.registerAggregator(exports.SUM, (v1, v2) => v1 + v2); | ||
exports.COUNT = 'count'; | ||
function Count(val) { | ||
return { operand: exports.COUNT, value: val }; | ||
} | ||
exports.Count = Count; | ||
exports.operator.registerAggregator(exports.COUNT, (v1, v2) => v1 + v2); | ||
exports.AVERAGE = 'avg'; | ||
function Average(val, weight = 1) { | ||
return { operand: exports.AVERAGE, value: { v: val, w: weight } }; | ||
} | ||
exports.Average = Average; | ||
exports.operator.registerAggregator(exports.AVERAGE, (v1, v2) => { | ||
return { | ||
v: v1.v * v1.w + v2.v * v2.w, | ||
w: v1.w + v2.w, | ||
}; | ||
}); | ||
exports.MAX = 'max'; | ||
function Max(val) { | ||
return { operand: exports.MAX, value: val }; | ||
} | ||
exports.Max = Max; | ||
exports.operator.registerAggregator(exports.MAX, (v1, v2) => Math.max(v1, v2)); | ||
exports.MIN = 'min'; | ||
function Min(val) { | ||
return { operand: exports.MIN, value: val }; | ||
} | ||
exports.Min = Min; | ||
exports.operator.registerAggregator(exports.MIN, (v1, v2) => Math.min(v1, v2)); | ||
exports.LAST = 'last'; | ||
function Last(val) { | ||
return { operand: exports.LAST, value: val }; | ||
} | ||
exports.Last = Last; | ||
exports.operator.registerAggregator(exports.LAST, (v1, v2) => v2); | ||
exports.FIRST = 'first'; | ||
function First(val) { | ||
return { operand: exports.FIRST, value: val }; | ||
} | ||
exports.First = First; | ||
exports.operator.registerAggregator(exports.FIRST, (v1, v2) => v1); | ||
exports.COLLECT = 'collect'; | ||
function Collect(val) { | ||
return { operand: exports.COLLECT, value: [val] }; | ||
} | ||
exports.Collect = Collect; | ||
exports.operator.registerAggregator(exports.COLLECT, (v1, v2) => v1.concat(v2)); | ||
exports.UNION = 'union'; | ||
function Union(val) { | ||
return { operand: exports.UNION, value: [val] }; | ||
} | ||
exports.Union = Union; | ||
exports.operator.registerAggregator(exports.UNION, (v1, v2) => _.union(v1, v2)); | ||
__export(require("./built-in.operators")); | ||
__export(require("./operators")); |
{ | ||
"name": "@nodeswork/utils", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Utilities used across nodeswork repos.", | ||
@@ -41,4 +41,6 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@types/object-hash": "^1.1.0", | ||
"@types/underscore": "^1.8.1", | ||
"@types/validator": "^6.2.0", | ||
"object-hash": "^1.1.8", | ||
"underscore": "^1.8.3", | ||
@@ -45,0 +47,0 @@ "validator": "^8.0.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
52227
30
985
6
+ Added@types/object-hash@^1.1.0
+ Addedobject-hash@^1.1.8
+ Added@types/object-hash@1.3.4(transitive)
+ Addedobject-hash@1.3.1(transitive)