|
| 1 | +/** |
| 2 | + * Returns true if the passed value is a record (object). |
| 3 | + * |
| 4 | + * @param value The value to check |
| 5 | + */ |
| 6 | +const isRecord = ( |
| 7 | + value: unknown |
| 8 | +): value is Record<string | number, unknown> => { |
| 9 | + return ( |
| 10 | + Object.prototype.toString.call(value) === '[object Object]' && |
| 11 | + !Object.is(value, null) |
| 12 | + ); |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Check if a value is a string. |
| 17 | + * |
| 18 | + * @param value The value to check |
| 19 | + */ |
| 20 | +const isString = (value: unknown): value is string => { |
| 21 | + return typeof value === 'string'; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Check if a value is a number. |
| 26 | + * |
| 27 | + * @param value The value to check |
| 28 | + */ |
| 29 | +const isNumber = (value: unknown): value is number => { |
| 30 | + return typeof value === 'number'; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Check if a value is an integer number. |
| 35 | + * |
| 36 | + * @param value The value to check |
| 37 | + */ |
| 38 | +const isIntegerNumber = (value: unknown): value is number => { |
| 39 | + return isNumber(value) && Number.isInteger(value); |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Check if a value is truthy. |
| 44 | + * |
| 45 | + * @param value The value to check |
| 46 | + */ |
| 47 | +const isTruthy = (value: unknown): boolean => { |
| 48 | + if (isString(value)) { |
| 49 | + return value !== ''; |
| 50 | + } else if (isNumber(value)) { |
| 51 | + return value !== 0; |
| 52 | + } else if (typeof value === 'boolean') { |
| 53 | + return value; |
| 54 | + } else if (Array.isArray(value)) { |
| 55 | + return value.length > 0; |
| 56 | + } else if (isRecord(value)) { |
| 57 | + return Object.keys(value).length > 0; |
| 58 | + } else { |
| 59 | + return false; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Check if a value is null. |
| 65 | + * |
| 66 | + * @param value The value to check |
| 67 | + */ |
| 68 | +const isNull = (value: unknown): value is null => { |
| 69 | + return Object.is(value, null); |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * Check if a value is null or undefined. |
| 74 | + * |
| 75 | + * @param value The value to check |
| 76 | + */ |
| 77 | +const isNullOrUndefined = (value: unknown): value is null | undefined => { |
| 78 | + return isNull(value) || Object.is(value, undefined); |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * Get the type of a value as a string. |
| 83 | + * |
| 84 | + * @param value The value to check |
| 85 | + */ |
| 86 | +const getType = (value: unknown): string => { |
| 87 | + if (Array.isArray(value)) { |
| 88 | + return 'array'; |
| 89 | + } else if (isRecord(value)) { |
| 90 | + return 'object'; |
| 91 | + } else if (isString(value)) { |
| 92 | + return 'string'; |
| 93 | + } else if (isNumber(value)) { |
| 94 | + return 'number'; |
| 95 | + } else if (typeof value === 'boolean') { |
| 96 | + return 'boolean'; |
| 97 | + } else if (isNull(value)) { |
| 98 | + return 'null'; |
| 99 | + } else { |
| 100 | + return 'unknown'; |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Compare two arrays for strict equality. |
| 106 | + * |
| 107 | + * @param left The left array to compare |
| 108 | + * @param right The right array to compare |
| 109 | + */ |
| 110 | +const areArraysEqual = (left: unknown[], right: unknown[]): boolean => { |
| 111 | + if (left.length !== right.length) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return left.every((value, i) => isStrictEqual(value, right[i])); |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * Compare two records for strict equality. |
| 120 | + * |
| 121 | + * @param left The left record to compare |
| 122 | + * @param right The right record to compare |
| 123 | + */ |
| 124 | +const areRecordsEqual = ( |
| 125 | + left: Record<string, unknown>, |
| 126 | + right: Record<string, unknown> |
| 127 | +): boolean => { |
| 128 | + const leftKeys = Object.keys(left); |
| 129 | + const rightKeys = Object.keys(right); |
| 130 | + |
| 131 | + if (leftKeys.length !== rightKeys.length) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + return leftKeys.every((key) => isStrictEqual(left[key], right[key])); |
| 136 | +}; |
| 137 | + |
| 138 | +/** |
| 139 | + * Check if two unknown values are strictly equal. |
| 140 | + * |
| 141 | + * If the values are arrays, then each element is compared, regardless of |
| 142 | + * order. If the values are objects, then each key and value from left |
| 143 | + * is compared to the corresponding key and value from right. If the |
| 144 | + * values are primitives, then they are compared using strict equality. |
| 145 | + * |
| 146 | + * @param left Left side of strict equality comparison |
| 147 | + * @param right Right side of strict equality comparison |
| 148 | + */ |
| 149 | +const isStrictEqual = (left: unknown, right: unknown): boolean => { |
| 150 | + if (left === right) { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + if (typeof left !== typeof right) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + if (Array.isArray(left) && Array.isArray(right)) { |
| 159 | + return areArraysEqual(left, right); |
| 160 | + } |
| 161 | + |
| 162 | + if (isRecord(left) && isRecord(right)) { |
| 163 | + return areRecordsEqual(left, right); |
| 164 | + } |
| 165 | + |
| 166 | + return false; |
| 167 | +}; |
| 168 | + |
| 169 | +export { |
| 170 | + isRecord, |
| 171 | + isString, |
| 172 | + isNumber, |
| 173 | + isIntegerNumber, |
| 174 | + isTruthy, |
| 175 | + isNull, |
| 176 | + isNullOrUndefined, |
| 177 | + getType, |
| 178 | + isStrictEqual, |
| 179 | +}; |
0 commit comments