-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.ts
More file actions
132 lines (120 loc) · 3.52 KB
/
utilities.ts
File metadata and controls
132 lines (120 loc) · 3.52 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
import { isDate } from 'node:util/types'
import {
type DifferenceType,
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: unknown
):
| 'array'
| 'boolean'
| 'date'
| 'function'
| 'null'
| 'number'
| 'object'
| 'string'
| 'symbol'
| 'undefined' {
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 as 'boolean' | 'number' | 'object' | 'string'
}
/**
* 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: unknown): boolean {
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: unknown
): potentialValue is boolean | number | string {
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: unknown): potentialArray is unknown[] {
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: unknown
): potentialFunction is Function {
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: unknown): potentialSymbol is symbol {
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: unknown): potentialObject is object {
return (
!isValue(potentialObject) &&
!isBlank(potentialObject) &&
!isDate(potentialObject) &&
!isArray(potentialObject) &&
!isFunction(potentialObject) &&
!isSymbol(potentialObject)
)
}
export function getDifferenceType(
valueFrom: unknown,
valueTo: unknown
): DifferenceType {
let updateType: DifferenceType = VALUE_UPDATED
if (isBlank(valueFrom)) {
updateType = VALUE_CREATED
} else if (isBlank(valueTo)) {
updateType = VALUE_DELETED
}
return updateType
}