name:
src/main/js/utils/Predicates.ts
-rw-r--r--
1552
1import { F7Exception } from "../errors/F7Exception";
2import { Complex } from "../models/common/Types";
3import { Numbers } from "./Numbers";
4
5export class Predicates {
6 /**
7 * Used to filter lists to values that are numeric: they are a Boolean, or a Double.
8 *
9 * @param value - to check
10 * @return - true if value is instance of Double, or Boolean. Otherwise false.
11 */
12 static isNumeric(value: any): boolean {
13 return typeof value === "number" || typeof value === "boolean";
14 }
15
16 /**
17 * Is the value a numeric value (Double or Boolean) or a coercable string?
18 *
19 * @param value - to check
20 * @return - true if value is instance of Double, or Boolean, or is a String that can be converted to Double.
21 */
22 static isCoercableToNumeric(value: any): boolean {
23 return Predicates.isNumeric(value) || Predicates.isCoercableString(value);
24 }
25
26 /**
27 * Is it straight-up a number?
28 * @param value - to check.
29 */
30 static isLiteralNumber(value: any): boolean {
31 return typeof value === "number";
32 }
33
34 /**
35 * Can we coerce this string to a number value?
36 *
37 * @param value - to test.
38 * @return true if we can get a Double from this value.
39 */
40 static isCoercableString(value: any): boolean {
41 if (typeof value === "string") {
42 if (Numbers.toNumberOrNull(value) !== null) {
43 return true;
44 }
45 }
46 return false;
47 }
48
49 /**
50 * Is the value NOT an error.
51 * @param value - value to check
52 */
53 static isNonError(value: Complex) {
54 return !(value instanceof F7Exception);
55 }
56}