1 | ;
|
2 | /*
|
3 | * Copyright 2020 Palantir Technologies, Inc. All rights reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.uniqueId = exports.countDecimalPlaces = exports.clamp = exports.approxEqual = exports.arrayLengthCompare = exports.isNodeEnv = void 0;
|
19 | var errors_1 = require("../errors");
|
20 | /** Returns whether `process.env.NODE_ENV` exists and equals `env`. */
|
21 | function isNodeEnv(env) {
|
22 | return typeof process !== "undefined" && process.env && process.env.NODE_ENV === env;
|
23 | }
|
24 | exports.isNodeEnv = isNodeEnv;
|
25 | /**
|
26 | * Returns the difference in length between two arrays. A `null` argument is
|
27 | * considered an empty list. The return value will be positive if `a` is longer
|
28 | * than `b`, negative if the opposite is true, and zero if their lengths are
|
29 | * equal.
|
30 | */
|
31 | function arrayLengthCompare(a, b) {
|
32 | if (a === void 0) { a = []; }
|
33 | if (b === void 0) { b = []; }
|
34 | return a.length - b.length;
|
35 | }
|
36 | exports.arrayLengthCompare = arrayLengthCompare;
|
37 | /**
|
38 | * Returns true if the two numbers are within the given tolerance of each other.
|
39 | * This is useful to correct for floating point precision issues, less useful
|
40 | * for integers.
|
41 | */
|
42 | function approxEqual(a, b, tolerance) {
|
43 | if (tolerance === void 0) { tolerance = 0.00001; }
|
44 | return Math.abs(a - b) <= tolerance;
|
45 | }
|
46 | exports.approxEqual = approxEqual;
|
47 | /**
|
48 | * Clamps the given number between min and max values. Returns value if within
|
49 | * range, or closest bound.
|
50 | */
|
51 | function clamp(val, min, max) {
|
52 | if (val == null) {
|
53 | return val;
|
54 | }
|
55 | if (max < min) {
|
56 | throw new Error(errors_1.CLAMP_MIN_MAX);
|
57 | }
|
58 | return Math.min(Math.max(val, min), max);
|
59 | }
|
60 | exports.clamp = clamp;
|
61 | /** Returns the number of decimal places in the given number. */
|
62 | function countDecimalPlaces(num) {
|
63 | if (!isFinite(num)) {
|
64 | return 0;
|
65 | }
|
66 | var e = 1;
|
67 | var p = 0;
|
68 | while (Math.round(num * e) / e !== num) {
|
69 | e *= 10;
|
70 | p++;
|
71 | }
|
72 | return p;
|
73 | }
|
74 | exports.countDecimalPlaces = countDecimalPlaces;
|
75 | var uniqueCountForNamespace = new Map();
|
76 | /** Generate a unique ID within a given namespace, using a simple counter-based implementation to avoid collisions. */
|
77 | function uniqueId(namespace) {
|
78 | var _a;
|
79 | var curCount = (_a = uniqueCountForNamespace.get(namespace)) !== null && _a !== void 0 ? _a : 0;
|
80 | uniqueCountForNamespace.set(namespace, curCount + 1);
|
81 | return namespace + "-" + curCount;
|
82 | }
|
83 | exports.uniqueId = uniqueId;
|
84 | //# sourceMappingURL=jsUtils.js.map |
\ | No newline at end of file |