-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.js
More file actions
96 lines (96 loc) · 3.13 KB
/
utilities.js
File metadata and controls
96 lines (96 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
import { isDate } from 'node:util/types';
import { VALUE_CREATED, VALUE_DELETED, VALUE_UPDATED } from './types.js';
/**
* Get the unknown value type as a string
* @param unknownValue - A value of unknown type
* @returns The unknown value type as a string
*/
export function getTypeOfUnknown(unknownValue) {
if (unknownValue === null) {
return 'null';
}
else if (unknownValue === undefined) {
return 'undefined';
}
else if (typeof unknownValue === 'symbol') {
return 'symbol';
}
else if (unknownValue instanceof Date) {
return 'date';
}
else if (Array.isArray(unknownValue)) {
return 'array';
}
else if (typeof unknownValue === 'function') {
return 'function';
}
return typeof unknownValue;
}
/**
* Check if the unknown value is blank, which means its either an empty string, undefined or null
* @param potentialBlank - A value of unknown type
* @returns True if the value is blank, false otherwise
*/
export function isBlank(potentialBlank) {
return (potentialBlank === '' ||
potentialBlank === undefined ||
potentialBlank === null);
}
/**
* Check if the unknown value is a simple value, which means its either a boolean, number or string
* @param potentialValue - A value of unknown type
* @returns True if the value is a simple value, false otherwise
*/
export function isValue(potentialValue) {
return (typeof potentialValue === 'boolean' ||
typeof potentialValue === 'number' ||
typeof potentialValue === 'string');
}
/**
* Check if the unknown value is an array
* @param potentialArray - A value of unknown type
* @returns True if the value is an array, false otherwise
*/
export function isArray(potentialArray) {
return !isBlank(potentialArray) && Array.isArray(potentialArray);
}
/**
* Check if the unknown value is a function
* @param potentialFunction - A value of unknown type
* @returns True if the value is a function, false otherwise
*/
export function isFunction(potentialFunction) {
return typeof potentialFunction === 'function';
}
/**
* Check if the unknown value is a symbol
* @param potentialSymbol - A value of unknown type
* @returns True if the value is a symbol, false otherwise
*/
export function isSymbol(potentialSymbol) {
return typeof potentialSymbol === 'symbol';
}
/**
* Check if the unknown value is an object, which means it's not a simple value, array, date, function or symbol
* @param potentialObject - A value of unknown type
* @returns True if the value is an object, false otherwise
*/
export function isObject(potentialObject) {
return (!isValue(potentialObject) &&
!isBlank(potentialObject) &&
!isDate(potentialObject) &&
!isArray(potentialObject) &&
!isFunction(potentialObject) &&
!isSymbol(potentialObject));
}
export function getDifferenceType(valueFrom, valueTo) {
let updateType = VALUE_UPDATED;
if (isBlank(valueFrom)) {
updateType = VALUE_CREATED;
}
else if (isBlank(valueTo)) {
updateType = VALUE_DELETED;
}
return updateType;
}