what-are-you 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/get-text.d.ts +0 -35
- package/dist/cjs/get-text.d.ts.map +1 -1
- package/dist/cjs/get-text.js +0 -108
- package/dist/cjs/get-text.js.map +1 -1
- package/dist/cjs/index.d.ts +194 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +313 -34
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/is-basic.d.ts +0 -121
- package/dist/cjs/is-basic.d.ts.map +1 -1
- package/dist/cjs/is-basic.js +0 -154
- package/dist/cjs/is-basic.js.map +1 -1
- package/dist/cjs/is-special.d.ts +0 -38
- package/dist/cjs/is-special.d.ts.map +1 -1
- package/dist/cjs/is-special.js +0 -57
- package/dist/cjs/is-special.js.map +1 -1
- package/dist/esm/get-text.d.ts +1 -35
- package/dist/esm/get-text.d.ts.map +1 -1
- package/dist/esm/get-text.js +1 -101
- package/dist/esm/get-text.js.map +1 -1
- package/dist/esm/index.d.ts +194 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +283 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/is-basic.d.ts +1 -121
- package/dist/esm/is-basic.d.ts.map +1 -1
- package/dist/esm/is-basic.js +1 -135
- package/dist/esm/is-basic.js.map +1 -1
- package/dist/esm/is-special.d.ts +1 -38
- package/dist/esm/is-special.d.ts.map +1 -1
- package/dist/esm/is-special.js +1 -50
- package/dist/esm/is-special.js.map +1 -1
- package/package.json +6 -3
- package/src/get-text.ts +0 -111
- package/src/index.ts +300 -36
- package/src/is-basic.ts +0 -143
- package/src/is-special.ts +0 -57
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "what-are-you",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Assortment of type-related checks and information.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"type",
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
"type-check",
|
|
11
11
|
"type-test"
|
|
12
12
|
],
|
|
13
|
-
"repository":
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/GregRos/what-are-you.git"
|
|
16
|
+
},
|
|
14
17
|
"license": "MIT",
|
|
15
18
|
"author": "GregRos",
|
|
16
19
|
"sideEffects": false,
|
|
@@ -84,4 +87,4 @@
|
|
|
84
87
|
},
|
|
85
88
|
"packageManager": "yarn@4.9.1",
|
|
86
89
|
"sourcesRoot": "src"
|
|
87
|
-
}
|
|
90
|
+
}
|
package/src/get-text.ts
CHANGED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isBigInt,
|
|
3
|
-
isFunction,
|
|
4
|
-
isNullish,
|
|
5
|
-
isObject,
|
|
6
|
-
isString,
|
|
7
|
-
isSymbol,
|
|
8
|
-
isThenable
|
|
9
|
-
} from "./is-basic.js"
|
|
10
|
-
import { isAsyncIterable, isDoddle, isIterable, isNextable } from "./is-special.js"
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Gets a short value type name, which can be used in a message.
|
|
14
|
-
*
|
|
15
|
-
* @param value - The value to check.
|
|
16
|
-
*/
|
|
17
|
-
export function getNiceTypeOf(value: any) {
|
|
18
|
-
return value === null ? "null" : typeof value
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Describes an object's class, type, or constructor name to be used in a message.
|
|
22
|
-
*
|
|
23
|
-
* - 📦 For an object, returns the name of its constructor or similar string.
|
|
24
|
-
* - ⚙️ For a function (or class), returns the function's name.
|
|
25
|
-
* - 🔠 For other values, returns the `typeof` string via {@link getNiceTypeOf}.
|
|
26
|
-
*
|
|
27
|
-
* @param something - The value whose class name or type is to be determined.
|
|
28
|
-
*/
|
|
29
|
-
export function getNiceClassName(something: any) {
|
|
30
|
-
if (isFunction(something)) {
|
|
31
|
-
return getFunctionName(something)
|
|
32
|
-
}
|
|
33
|
-
if (!isObject(something)) {
|
|
34
|
-
return getNiceTypeOf(something)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let name: string | undefined
|
|
38
|
-
if (something.constructor) {
|
|
39
|
-
name = getFunctionName(something.constructor)
|
|
40
|
-
}
|
|
41
|
-
const o = "Object"
|
|
42
|
-
return name && name !== o ? name : (getObjectStringTag(something) ?? o)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getObjectStringTag(object: any) {
|
|
46
|
-
return object[Symbol.toStringTag]
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Gets the description of a symbol.
|
|
50
|
-
*
|
|
51
|
-
* @param symbol - The symbol to get the description of.
|
|
52
|
-
*/
|
|
53
|
-
export function getSymbolDescription(symbol: symbol) {
|
|
54
|
-
return `${symbol.description}`
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function _getShortObjectString(object: any) {
|
|
58
|
-
if (isDoddle(object)) {
|
|
59
|
-
return object.toString()
|
|
60
|
-
}
|
|
61
|
-
if (isIterable(object)) {
|
|
62
|
-
return `iterable ${getNiceClassName(object)}`
|
|
63
|
-
} else if (isAsyncIterable(object)) {
|
|
64
|
-
return `async iterable ${getNiceClassName(object)}`
|
|
65
|
-
} else if (isNextable(object)) {
|
|
66
|
-
return `iterator ${getNiceClassName(object)}`
|
|
67
|
-
} else if (isThenable(object)) {
|
|
68
|
-
return `a Promise`
|
|
69
|
-
} else {
|
|
70
|
-
return `object ${getNiceClassName(object)}`
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Gets a short string describing a value, to be used in a message.
|
|
76
|
-
*
|
|
77
|
-
* @param value - The value to describe.
|
|
78
|
-
*/
|
|
79
|
-
export function getNiceValueName(value: any) {
|
|
80
|
-
if (isNullish(value)) {
|
|
81
|
-
return `${value}`
|
|
82
|
-
}
|
|
83
|
-
if (isFunction(value)) {
|
|
84
|
-
return `function ${getFunctionName(value) || "<anonymous>"}`
|
|
85
|
-
}
|
|
86
|
-
if (isBigInt(value)) {
|
|
87
|
-
return `${value}n`
|
|
88
|
-
}
|
|
89
|
-
if (isSymbol(value)) {
|
|
90
|
-
return `Symbol(${getSymbolDescription(value)})`
|
|
91
|
-
}
|
|
92
|
-
if (isString(value)) {
|
|
93
|
-
if (value.length > 30) {
|
|
94
|
-
value = value.slice(0, 30) + "⋯"
|
|
95
|
-
}
|
|
96
|
-
return `"${value}"`
|
|
97
|
-
}
|
|
98
|
-
if (isObject(value)) {
|
|
99
|
-
return _getShortObjectString(value)
|
|
100
|
-
}
|
|
101
|
-
return `${value}`
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Gets the name of a function, or null if it has no name.
|
|
106
|
-
*
|
|
107
|
-
* @param func - The function to get the name of.
|
|
108
|
-
*/
|
|
109
|
-
export function getFunctionName(func: Function) {
|
|
110
|
-
return func.name || undefined
|
|
111
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -1,36 +1,300 @@
|
|
|
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
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is an object.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the value to check.
|
|
5
|
+
* @param value - The value to test.
|
|
6
|
+
*/
|
|
7
|
+
export function isObject<T>(value: T): value is T & {} {
|
|
8
|
+
return typeof value === "object" && value != null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the given value is a function.
|
|
13
|
+
*
|
|
14
|
+
* @param value - The value to check.
|
|
15
|
+
*/
|
|
16
|
+
export function isFunction(value: unknown): value is Function {
|
|
17
|
+
return typeof value === "function"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Checks if the given value is a BigInt.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to check.
|
|
24
|
+
*/
|
|
25
|
+
export const isBigInt = (value: any): value is bigint => {
|
|
26
|
+
return typeof value === "bigint"
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Checks if the given value is a string.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to check.
|
|
33
|
+
*/
|
|
34
|
+
export const isString = (value: any): value is string => {
|
|
35
|
+
return typeof value === "string"
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks if the given value is acceptable as the key of a property.
|
|
39
|
+
*
|
|
40
|
+
* That is, whether it's a string, a number, or a symbol.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to check.
|
|
43
|
+
*/
|
|
44
|
+
export const isPropertyKey = (value: any): value is PropertyKey => {
|
|
45
|
+
return isString(value) || isSymbol(value) || isNumber(value)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks if the given value is null or undefined.
|
|
50
|
+
*
|
|
51
|
+
* @param value - The value to check.
|
|
52
|
+
*/
|
|
53
|
+
export const isNullish = (value: any): value is null | undefined => {
|
|
54
|
+
return value == null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Checks if the given value is an array.
|
|
59
|
+
*
|
|
60
|
+
* @param value - The value to check.
|
|
61
|
+
*/
|
|
62
|
+
export const isArray = Array.isArray
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the given value is a thenable (i.e., a Promise or similar).
|
|
66
|
+
*
|
|
67
|
+
* @template T - Allows asserting the type of the resolved value. Not part of the check.
|
|
68
|
+
* @param what - The value to check.
|
|
69
|
+
*/
|
|
70
|
+
export const isThenable = <T = unknown>(what: unknown): what is PromiseLike<T> => {
|
|
71
|
+
return isObject(what) && isFunction((what as any).then)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks if the given value is a number.
|
|
76
|
+
*
|
|
77
|
+
* @param v - The value to check.
|
|
78
|
+
*/
|
|
79
|
+
export const isNumber = (v: number) => +v === v
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the given value is a number and can be represented exactly in JavaScript.
|
|
82
|
+
*
|
|
83
|
+
* That is, if it's a number in the range (-2⁵³, 2⁵³).
|
|
84
|
+
*
|
|
85
|
+
* @param v - The value to check.
|
|
86
|
+
*/
|
|
87
|
+
export const isInt = Number.isSafeInteger
|
|
88
|
+
/**
|
|
89
|
+
* Checks if the given value is either a {@link isInt safe integer} or Infinity.
|
|
90
|
+
*
|
|
91
|
+
* @param v - The value to check.
|
|
92
|
+
*/
|
|
93
|
+
export const isIntOrInfinity = (v: number) => isInt(v) || v === Infinity
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Checks if the given value is a {@link isInt safe integer} and non-negative.
|
|
97
|
+
*
|
|
98
|
+
* @param v - The value to check.
|
|
99
|
+
* @see {@link isInt}
|
|
100
|
+
*/
|
|
101
|
+
export const isNat = (v: number) => isInt(v) && v >= 0
|
|
102
|
+
/**
|
|
103
|
+
* Checks if the given value is either a non-negative {@link isInt safe integer} or Infinity.
|
|
104
|
+
*
|
|
105
|
+
* @param v - The value to check.
|
|
106
|
+
*/
|
|
107
|
+
export const isNatOrInfinity = (v: number) => isIntOrInfinity(v) && v >= 0
|
|
108
|
+
/**
|
|
109
|
+
* Checks if the given value is a boolean.
|
|
110
|
+
*
|
|
111
|
+
* @param value - The value to check.
|
|
112
|
+
*/
|
|
113
|
+
export const isBool = (value: boolean) => !!value === value
|
|
114
|
+
/**
|
|
115
|
+
* Checks if the given value is not null or undefined.
|
|
116
|
+
*
|
|
117
|
+
* @param value - The value to check.
|
|
118
|
+
*/
|
|
119
|
+
export const isNotNullish = (value: any) => !isNullish(value)
|
|
120
|
+
/**
|
|
121
|
+
* Checks if the given value is an array of two elements.
|
|
122
|
+
*
|
|
123
|
+
* @param value - The value to check.
|
|
124
|
+
*/
|
|
125
|
+
export const isPair = (value: any) => isArray(value) && value.length === 2
|
|
126
|
+
/**
|
|
127
|
+
* Checks if the given value is a {@link isInt safe integer} and positive.
|
|
128
|
+
*
|
|
129
|
+
* @param value - The value to check.
|
|
130
|
+
*/
|
|
131
|
+
export const isPosInt = (value: number) => isInt(value) && value > 0
|
|
132
|
+
/**
|
|
133
|
+
* Checks if the given value is an Error object.
|
|
134
|
+
*
|
|
135
|
+
* @param value - The value to check.
|
|
136
|
+
*/
|
|
137
|
+
export const isError = (value: any) => value instanceof Error
|
|
138
|
+
/**
|
|
139
|
+
* Checks if the given value is a symbol.
|
|
140
|
+
*
|
|
141
|
+
* @param value - The value to check.
|
|
142
|
+
*/
|
|
143
|
+
export const isSymbol = (value: any) => typeof value === "symbol"
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Checks if the given value is array-like. It must be an object and have an integer `length`
|
|
147
|
+
* property.
|
|
148
|
+
*
|
|
149
|
+
* @template T - Allows asserting the element type. Not part of the check.
|
|
150
|
+
* @param value - The value to check.
|
|
151
|
+
*/
|
|
152
|
+
export function isArrayLike<T>(value: any): value is ArrayLike<T> {
|
|
153
|
+
return isObject(value) && !isFunction(value) && isInt(value.length)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Checks if the given value is an object
|
|
158
|
+
*
|
|
159
|
+
* @param value - The value to check.
|
|
160
|
+
*/
|
|
161
|
+
export function isIterable<T>(value: any): value is Iterable<T> {
|
|
162
|
+
return isObject(value) && isFunction(value[Symbol.iterator])
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Checks if the given value is an iterable or async iterable.
|
|
166
|
+
*
|
|
167
|
+
* @template T - Allows asserting the element type. Not part of the check.
|
|
168
|
+
* @param value - The value to check.
|
|
169
|
+
*/
|
|
170
|
+
export function isAnyIterable<T = unknown>(value: any): value is Iterable<T> {
|
|
171
|
+
return isIterable(value) || isAsyncIterable(value)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Checks if the given value is an async iterable.
|
|
176
|
+
*
|
|
177
|
+
* @template T - Allows asserting the element type. Not part of the check.
|
|
178
|
+
* @param value - The value to check.
|
|
179
|
+
*/
|
|
180
|
+
export function isAsyncIterable<T>(value: any): value is AsyncIterable<T> {
|
|
181
|
+
return isObject(value) && isFunction(value[Symbol.asyncIterator])
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function isNextable<T>(value: any): value is Iterator<T> | AsyncIterator<T> {
|
|
185
|
+
// Checks if value is an iterator
|
|
186
|
+
return isObject(value) && isFunction(value.next)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Checks if the given value is a Doddle object.
|
|
191
|
+
*
|
|
192
|
+
* @param value - The value to check.
|
|
193
|
+
* @returns True if the value is a Doddle object, false otherwise.
|
|
194
|
+
*/
|
|
195
|
+
export function isDoddle<T>(value: any): value is {
|
|
196
|
+
pull(): T
|
|
197
|
+
} {
|
|
198
|
+
return isObject(value) && isFunction(value.pull) && isFunction(value.map)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Gets a short value type name, which can be used in a message.
|
|
203
|
+
*
|
|
204
|
+
* @param value - The value to check.
|
|
205
|
+
*/
|
|
206
|
+
export function getNiceTypeOf(value: any) {
|
|
207
|
+
return value === null ? "null" : typeof value
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Describes an object's class, type, or constructor name to be used in a message.
|
|
211
|
+
*
|
|
212
|
+
* - 📦 For an object, returns the name of its constructor or similar string.
|
|
213
|
+
* - ⚙️ For a function (or class), returns the function's name.
|
|
214
|
+
* - 🔠 For other values, returns the `typeof` string via {@link getNiceTypeOf}.
|
|
215
|
+
*
|
|
216
|
+
* @param something - The value whose class name or type is to be determined.
|
|
217
|
+
*/
|
|
218
|
+
export function getNiceClassName(something: any) {
|
|
219
|
+
if (isFunction(something)) {
|
|
220
|
+
return getFunctionName(something)
|
|
221
|
+
}
|
|
222
|
+
if (!isObject(something)) {
|
|
223
|
+
return getNiceTypeOf(something)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let name: string | undefined
|
|
227
|
+
if (something.constructor) {
|
|
228
|
+
name = getFunctionName(something.constructor)
|
|
229
|
+
}
|
|
230
|
+
const o = "Object"
|
|
231
|
+
return name && name !== o ? name : (getObjectStringTag(something) ?? o)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function getObjectStringTag(object: any) {
|
|
235
|
+
return object[Symbol.toStringTag]
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Gets the description of a symbol.
|
|
239
|
+
*
|
|
240
|
+
* @param symbol - The symbol to get the description of.
|
|
241
|
+
*/
|
|
242
|
+
export function getSymbolDescription(symbol: symbol) {
|
|
243
|
+
return `${symbol.description}`
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function _getShortObjectString(object: any) {
|
|
247
|
+
if (isDoddle(object)) {
|
|
248
|
+
return object.toString()
|
|
249
|
+
}
|
|
250
|
+
if (isIterable(object)) {
|
|
251
|
+
return `iterable ${getNiceClassName(object)}`
|
|
252
|
+
} else if (isAsyncIterable(object)) {
|
|
253
|
+
return `async iterable ${getNiceClassName(object)}`
|
|
254
|
+
} else if (isNextable(object)) {
|
|
255
|
+
return `iterator ${getNiceClassName(object)}`
|
|
256
|
+
} else if (isThenable(object)) {
|
|
257
|
+
return `a Promise`
|
|
258
|
+
} else {
|
|
259
|
+
return `object ${getNiceClassName(object)}`
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Gets a short string describing a value, to be used in a message.
|
|
265
|
+
*
|
|
266
|
+
* @param value - The value to describe.
|
|
267
|
+
*/
|
|
268
|
+
export function getNiceValueName(value: any) {
|
|
269
|
+
if (isNullish(value)) {
|
|
270
|
+
return `${value}`
|
|
271
|
+
}
|
|
272
|
+
if (isFunction(value)) {
|
|
273
|
+
return `function ${getFunctionName(value) || "<anonymous>"}`
|
|
274
|
+
}
|
|
275
|
+
if (isBigInt(value)) {
|
|
276
|
+
return `${value}n`
|
|
277
|
+
}
|
|
278
|
+
if (isSymbol(value)) {
|
|
279
|
+
return `Symbol(${getSymbolDescription(value)})`
|
|
280
|
+
}
|
|
281
|
+
if (isString(value)) {
|
|
282
|
+
if (value.length > 30) {
|
|
283
|
+
value = value.slice(0, 30) + "⋯"
|
|
284
|
+
}
|
|
285
|
+
return `"${value}"`
|
|
286
|
+
}
|
|
287
|
+
if (isObject(value)) {
|
|
288
|
+
return _getShortObjectString(value)
|
|
289
|
+
}
|
|
290
|
+
return `${value}`
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Gets the name of a function, or null if it has no name.
|
|
295
|
+
*
|
|
296
|
+
* @param func - The function to get the name of.
|
|
297
|
+
*/
|
|
298
|
+
export function getFunctionName(func: Function) {
|
|
299
|
+
return func.name || undefined
|
|
300
|
+
}
|
package/src/is-basic.ts
CHANGED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if the given value is an object.
|
|
3
|
-
*
|
|
4
|
-
* @template T - The type of the value to check.
|
|
5
|
-
* @param value - The value to test.
|
|
6
|
-
*/
|
|
7
|
-
export function isObject<T>(value: T): value is T & {} {
|
|
8
|
-
return typeof value === "object" && value != null
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Checks if the given value is a function.
|
|
13
|
-
*
|
|
14
|
-
* @param value - The value to check.
|
|
15
|
-
*/
|
|
16
|
-
export function isFunction(value: unknown): value is Function {
|
|
17
|
-
return typeof value === "function"
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Checks if the given value is a BigInt.
|
|
22
|
-
*
|
|
23
|
-
* @param value - The value to check.
|
|
24
|
-
*/
|
|
25
|
-
export const isBigInt = (value: any): value is bigint => {
|
|
26
|
-
return typeof value === "bigint"
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Checks if the given value is a string.
|
|
31
|
-
*
|
|
32
|
-
* @param value - The value to check.
|
|
33
|
-
*/
|
|
34
|
-
export const isString = (value: any): value is string => {
|
|
35
|
-
return typeof value === "string"
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Checks if the given value is acceptable as the key of a property.
|
|
39
|
-
*
|
|
40
|
-
* That is, whether it's a string, a number, or a symbol.
|
|
41
|
-
*
|
|
42
|
-
* @param value - The value to check.
|
|
43
|
-
*/
|
|
44
|
-
export const isPropertyKey = (value: any): value is PropertyKey => {
|
|
45
|
-
return isString(value) || isSymbol(value) || isNumber(value)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Checks if the given value is null or undefined.
|
|
50
|
-
*
|
|
51
|
-
* @param value - The value to check.
|
|
52
|
-
*/
|
|
53
|
-
export const isNullish = (value: any): value is null | undefined => {
|
|
54
|
-
return value == null
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Checks if the given value is an array.
|
|
59
|
-
*
|
|
60
|
-
* @param value - The value to check.
|
|
61
|
-
*/
|
|
62
|
-
export const isArray = Array.isArray
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Checks if the given value is a thenable (i.e., a Promise or similar).
|
|
66
|
-
*
|
|
67
|
-
* @template T - Allows asserting the type of the resolved value. Not part of the check.
|
|
68
|
-
* @param what - The value to check.
|
|
69
|
-
*/
|
|
70
|
-
export const isThenable = <T = unknown>(what: unknown): what is PromiseLike<T> => {
|
|
71
|
-
return isObject(what) && isFunction((what as any).then)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Checks if the given value is a number.
|
|
76
|
-
*
|
|
77
|
-
* @param v - The value to check.
|
|
78
|
-
*/
|
|
79
|
-
export const isNumber = (v: number) => +v === v
|
|
80
|
-
/**
|
|
81
|
-
* Checks if the given value is a number and can be represented exactly in JavaScript.
|
|
82
|
-
*
|
|
83
|
-
* That is, if it's a number in the range (-2⁵³, 2⁵³).
|
|
84
|
-
*
|
|
85
|
-
* @param v - The value to check.
|
|
86
|
-
*/
|
|
87
|
-
export const isInt = Number.isSafeInteger
|
|
88
|
-
/**
|
|
89
|
-
* Checks if the given value is either a {@link isInt safe integer} or Infinity.
|
|
90
|
-
*
|
|
91
|
-
* @param v - The value to check.
|
|
92
|
-
*/
|
|
93
|
-
export const isIntOrInfinity = (v: number) => isInt(v) || v === Infinity
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Checks if the given value is a {@link isInt safe integer} and non-negative.
|
|
97
|
-
*
|
|
98
|
-
* @param v - The value to check.
|
|
99
|
-
* @see {@link isInt}
|
|
100
|
-
*/
|
|
101
|
-
export const isNat = (v: number) => isInt(v) && v >= 0
|
|
102
|
-
/**
|
|
103
|
-
* Checks if the given value is either a non-negative {@link isInt safe integer} or Infinity.
|
|
104
|
-
*
|
|
105
|
-
* @param v - The value to check.
|
|
106
|
-
*/
|
|
107
|
-
export const isNatOrInfinity = (v: number) => isIntOrInfinity(v) && v >= 0
|
|
108
|
-
/**
|
|
109
|
-
* Checks if the given value is a boolean.
|
|
110
|
-
*
|
|
111
|
-
* @param value - The value to check.
|
|
112
|
-
*/
|
|
113
|
-
export const isBool = (value: boolean) => !!value === value
|
|
114
|
-
/**
|
|
115
|
-
* Checks if the given value is not null or undefined.
|
|
116
|
-
*
|
|
117
|
-
* @param value - The value to check.
|
|
118
|
-
*/
|
|
119
|
-
export const isNotNullish = (value: any) => !isNullish(value)
|
|
120
|
-
/**
|
|
121
|
-
* Checks if the given value is an array of two elements.
|
|
122
|
-
*
|
|
123
|
-
* @param value - The value to check.
|
|
124
|
-
*/
|
|
125
|
-
export const isPair = (value: any) => isArray(value) && value.length === 2
|
|
126
|
-
/**
|
|
127
|
-
* Checks if the given value is a {@link isInt safe integer} and positive.
|
|
128
|
-
*
|
|
129
|
-
* @param value - The value to check.
|
|
130
|
-
*/
|
|
131
|
-
export const isPosInt = (value: number) => isInt(value) && value > 0
|
|
132
|
-
/**
|
|
133
|
-
* Checks if the given value is an Error object.
|
|
134
|
-
*
|
|
135
|
-
* @param value - The value to check.
|
|
136
|
-
*/
|
|
137
|
-
export const isError = (value: any) => value instanceof Error
|
|
138
|
-
/**
|
|
139
|
-
* Checks if the given value is a symbol.
|
|
140
|
-
*
|
|
141
|
-
* @param value - The value to check.
|
|
142
|
-
*/
|
|
143
|
-
export const isSymbol = (value: any) => typeof value === "symbol"
|
package/src/is-special.ts
CHANGED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { isFunction, isInt, isObject } from "./is-basic.js"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Checks if the given value is array-like. It must be an object and have an integer `length`
|
|
5
|
-
* property.
|
|
6
|
-
*
|
|
7
|
-
* @template T - Allows asserting the element type. Not part of the check.
|
|
8
|
-
* @param value - The value to check.
|
|
9
|
-
*/
|
|
10
|
-
export function isArrayLike<T>(value: any): value is ArrayLike<T> {
|
|
11
|
-
return isObject(value) && !isFunction(value) && isInt(value.length)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Checks if the given value is an object
|
|
16
|
-
*
|
|
17
|
-
* @param value - The value to check.
|
|
18
|
-
*/
|
|
19
|
-
export function isIterable<T>(value: any): value is Iterable<T> {
|
|
20
|
-
return isObject(value) && isFunction(value[Symbol.iterator])
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Checks if the given value is an iterable or async iterable.
|
|
24
|
-
*
|
|
25
|
-
* @template T - Allows asserting the element type. Not part of the check.
|
|
26
|
-
* @param value - The value to check.
|
|
27
|
-
*/
|
|
28
|
-
export function isAnyIterable<T = unknown>(value: any): value is Iterable<T> {
|
|
29
|
-
return isIterable(value) || isAsyncIterable(value)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Checks if the given value is an async iterable.
|
|
34
|
-
*
|
|
35
|
-
* @template T - Allows asserting the element type. Not part of the check.
|
|
36
|
-
* @param value - The value to check.
|
|
37
|
-
*/
|
|
38
|
-
export function isAsyncIterable<T>(value: any): value is AsyncIterable<T> {
|
|
39
|
-
return isObject(value) && isFunction(value[Symbol.asyncIterator])
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function isNextable<T>(value: any): value is Iterator<T> | AsyncIterator<T> {
|
|
43
|
-
// Checks if value is an iterator
|
|
44
|
-
return isObject(value) && isFunction(value.next)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Checks if the given value is a Doddle object.
|
|
49
|
-
*
|
|
50
|
-
* @param value - The value to check.
|
|
51
|
-
* @returns True if the value is a Doddle object, false otherwise.
|
|
52
|
-
*/
|
|
53
|
-
export function isDoddle<T>(value: any): value is {
|
|
54
|
-
pull(): T
|
|
55
|
-
} {
|
|
56
|
-
return isObject(value) && isFunction(value.pull) && isFunction(value.map)
|
|
57
|
-
}
|