File tree Expand file tree Collapse file tree 4 files changed +33
-4
lines changed Expand file tree Collapse file tree 4 files changed +33
-4
lines changed Original file line number Diff line number Diff line change 55import type { CommonMetricData } from "../index.js" ;
66import type { JSONValue } from "../../utils.js" ;
77import { MetricType } from "../index.js" ;
8- import { isUndefined , isNumber } from "../../utils.js" ;
8+ import { isUndefined , isInteger } from "../../utils.js" ;
99import { Context } from "../../context.js" ;
1010import { Metric } from "../metric.js" ;
1111import { ErrorType } from "../../error/error_type.js" ;
@@ -16,7 +16,7 @@ export class CounterMetric extends Metric<number, number> {
1616 }
1717
1818 validate ( v : unknown ) : v is number {
19- if ( ! isNumber ( v ) ) {
19+ if ( ! isInteger ( v ) ) {
2020 return false ;
2121 }
2222
Original file line number Diff line number Diff line change 44
55import type { CommonMetricData } from "../index.js" ;
66import { MetricType } from "../index.js" ;
7- import { isNumber } from "../../utils.js" ;
7+ import { isInteger } from "../../utils.js" ;
88import { Context } from "../../context.js" ;
99import { Metric } from "../metric.js" ;
1010import { ErrorType } from "../../error/error_type.js" ;
@@ -15,7 +15,7 @@ export class QuantityMetric extends Metric<number, number> {
1515 }
1616
1717 validate ( v : unknown ) : v is number {
18- if ( ! isNumber ( v ) ) {
18+ if ( ! isInteger ( v ) ) {
1919 return false ;
2020 }
2121
Original file line number Diff line number Diff line change @@ -102,6 +102,21 @@ export function isNumber(v: unknown): v is number {
102102 return ( ( typeof v === "number" || ( typeof v === "object" && v !== null && v . constructor === Number ) ) && ! isNaN ( v ) ) ;
103103}
104104
105+ /**
106+ * Checks whether or not `v` is an integer.
107+ *
108+ * @param v The value to verify.
109+ *
110+ * @returns A special Typescript value (which compiles down to a boolean)
111+ * stating whether `v` is a number.
112+ */
113+ export function isInteger ( v : unknown ) : v is number {
114+ // Decided against using `Number.isInteger` here,
115+ // because that fails if you pass it a number created through `new Number(...)`.
116+ return isNumber ( v ) && v % 1 === 0 ;
117+ }
118+
119+
105120/**
106121 * Generates a pipeline-friendly string
107122 * that replaces non alphanumeric characters with dashes.
Original file line number Diff line number Diff line change @@ -79,6 +79,20 @@ describe("utils", function() {
7979 assert . strictEqual ( utils . isNumber ( new Number ( 10 ) ) , true ) ;
8080 } ) ;
8181
82+ it ( "isInteger validates correctly" , function ( ) {
83+ // Invalid values
84+ assert . strictEqual ( utils . isInteger ( undefined ) , false ) ;
85+ assert . strictEqual ( utils . isInteger ( "10" ) , false ) ;
86+ assert . strictEqual ( utils . isInteger ( { } ) , false ) ;
87+ assert . strictEqual ( utils . isInteger ( NaN ) , false ) ;
88+ assert . strictEqual ( utils . isInteger ( 0.1 ) , false ) ;
89+
90+ // Valid values
91+ assert . strictEqual ( utils . isInteger ( 10 ) , true ) ;
92+ assert . strictEqual ( utils . isInteger ( - 10 ) , true ) ;
93+ assert . strictEqual ( utils . isInteger ( new Number ( 10 ) ) , true ) ;
94+ } ) ;
95+
8296 it ( "sanitizeApplicationId works correctly" , function ( ) {
8397 assert . strictEqual ( utils . sanitizeApplicationId ( "org.mozilla.test-app" ) , "org-mozilla-test-app" ) ;
8498 assert . strictEqual ( utils . sanitizeApplicationId ( "org.mozilla..test---app" ) , "org-mozilla-test-app" ) ;
You can’t perform that action at this time.
0 commit comments