what-are-you 0.1.7 → 0.1.9

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.
Files changed (55) hide show
  1. package/dist/cjs/ctor-type.d.ts +17 -0
  2. package/dist/cjs/ctor-type.d.ts.map +1 -0
  3. package/dist/cjs/ctor-type.js +3 -0
  4. package/dist/cjs/ctor-type.js.map +1 -0
  5. package/dist/cjs/get-text.d.ts +35 -0
  6. package/dist/cjs/get-text.d.ts.map +1 -1
  7. package/dist/cjs/get-text.js +108 -0
  8. package/dist/cjs/get-text.js.map +1 -1
  9. package/dist/cjs/index.d.ts +4 -194
  10. package/dist/cjs/index.d.ts.map +1 -1
  11. package/dist/cjs/index.js +38 -313
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/is-basic.d.ts +121 -0
  14. package/dist/cjs/is-basic.d.ts.map +1 -1
  15. package/dist/cjs/is-basic.js +154 -0
  16. package/dist/cjs/is-basic.js.map +1 -1
  17. package/dist/cjs/is-special.d.ts +38 -0
  18. package/dist/cjs/is-special.d.ts.map +1 -1
  19. package/dist/cjs/is-special.js +57 -0
  20. package/dist/cjs/is-special.js.map +1 -1
  21. package/dist/cjs/prototypes.d.ts +6 -0
  22. package/dist/cjs/prototypes.d.ts.map +1 -0
  23. package/dist/cjs/prototypes.js +43 -0
  24. package/dist/cjs/prototypes.js.map +1 -0
  25. package/dist/esm/ctor-type.d.ts +17 -0
  26. package/dist/esm/ctor-type.d.ts.map +1 -0
  27. package/dist/esm/ctor-type.js +2 -0
  28. package/dist/esm/ctor-type.js.map +1 -0
  29. package/dist/esm/get-text.d.ts +35 -1
  30. package/dist/esm/get-text.d.ts.map +1 -1
  31. package/dist/esm/get-text.js +101 -1
  32. package/dist/esm/get-text.js.map +1 -1
  33. package/dist/esm/index.d.ts +4 -194
  34. package/dist/esm/index.d.ts.map +1 -1
  35. package/dist/esm/index.js +4 -283
  36. package/dist/esm/index.js.map +1 -1
  37. package/dist/esm/is-basic.d.ts +121 -1
  38. package/dist/esm/is-basic.d.ts.map +1 -1
  39. package/dist/esm/is-basic.js +135 -1
  40. package/dist/esm/is-basic.js.map +1 -1
  41. package/dist/esm/is-special.d.ts +38 -1
  42. package/dist/esm/is-special.d.ts.map +1 -1
  43. package/dist/esm/is-special.js +50 -1
  44. package/dist/esm/is-special.js.map +1 -1
  45. package/dist/esm/prototypes.d.ts +6 -0
  46. package/dist/esm/prototypes.d.ts.map +1 -0
  47. package/dist/esm/prototypes.js +37 -0
  48. package/dist/esm/prototypes.js.map +1 -0
  49. package/package.json +2 -2
  50. package/src/ctor-type.ts +24 -0
  51. package/src/get-text.ts +111 -0
  52. package/src/index.ts +37 -300
  53. package/src/is-basic.ts +143 -0
  54. package/src/is-special.ts +57 -0
  55. package/src/prototypes.ts +48 -0
@@ -0,0 +1,6 @@
1
+ import { AnyCtor } from "./ctor-type.js";
2
+ export declare const isInstanceOf: <Parent extends object>(instance: object, what: AnyCtor<Parent>) => instance is Parent;
3
+ /** Return true when `obj` looks like the prototype object of some constructor. */
4
+ export declare function isPrototypeObject(obj: object): boolean;
5
+ export declare function getPrototypeChain<T>(obj: T, depth: number): T[];
6
+ //# sourceMappingURL=prototypes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prototypes.d.ts","sourceRoot":"","sources":["../../src/prototypes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAWxC,eAAO,MAAM,YAAY,GAAI,MAAM,SAAS,MAAM,EAC9C,UAAU,MAAM,EAChB,MAAM,OAAO,CAAC,MAAM,CAAC,KACtB,QAAQ,IAAI,MAEd,CAAA;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAYtD;AAED,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAE,CAa/D"}
@@ -0,0 +1,37 @@
1
+ import { isFunction, isObject } from "lodash";
2
+ const sCtor = "constructor";
3
+ const sPrototype = "prototype";
4
+ function getCtor(obj) {
5
+ return obj[sCtor];
6
+ }
7
+ function getCtorProto(obj) {
8
+ return obj[sPrototype];
9
+ }
10
+ export const isInstanceOf = (instance, what) => {
11
+ return instance instanceof what;
12
+ };
13
+ /** Return true when `obj` looks like the prototype object of some constructor. */
14
+ export function isPrototypeObject(obj) {
15
+ if (!isObject(obj)) {
16
+ return false;
17
+ }
18
+ if (!Object.hasOwn(obj, sCtor)) {
19
+ return false;
20
+ }
21
+ const ctor = getCtor(obj);
22
+ const ctorPtoto = getCtorProto(ctor);
23
+ return isFunction(ctor) && ctorPtoto === obj && isInstanceOf(obj, ctor);
24
+ }
25
+ export function getPrototypeChain(obj, depth) {
26
+ const chain = [];
27
+ let current = obj;
28
+ while (current) {
29
+ if (depth <= 0) {
30
+ break;
31
+ }
32
+ chain.push(current);
33
+ current = Object.getPrototypeOf(current);
34
+ }
35
+ return chain;
36
+ }
37
+ //# sourceMappingURL=prototypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prototypes.js","sourceRoot":"","sources":["../../src/prototypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAG7C,MAAM,KAAK,GAAG,aAAa,CAAA;AAC3B,MAAM,UAAU,GAAG,WAAW,CAAA;AAE9B,SAAS,OAAO,CAAmB,GAAM;IACrC,OAAO,GAAG,CAAC,KAAK,CAAe,CAAA;AACnC,CAAC;AACD,SAAS,YAAY,CAAqB,GAAM;IAC5C,OAAO,GAAG,CAAC,UAAU,CAAM,CAAA;AAC/B,CAAC;AACD,MAAM,CAAC,MAAM,YAAY,GAAG,CACxB,QAAgB,EAChB,IAAqB,EACH,EAAE;IACpB,OAAO,QAAQ,YAAY,IAAI,CAAA;AACnC,CAAC,CAAA;AAED,kFAAkF;AAClF,MAAM,UAAU,iBAAiB,CAAC,GAAW;IACzC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACjB,OAAO,KAAK,CAAA;IAChB,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAA;IAChB,CAAC;IAED,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;IACzB,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;IACpC,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,SAAS,KAAK,GAAG,IAAI,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;AAC3E,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAI,GAAM,EAAE,KAAa;IACtD,MAAM,KAAK,GAAQ,EAAE,CAAA;IACrB,IAAI,OAAO,GAAa,GAAG,CAAA;IAE3B,OAAO,OAAO,EAAE,CAAC;QACb,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;YACb,MAAK;QACT,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACnB,OAAO,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;IAC5C,CAAC;IAED,OAAO,KAAK,CAAA;AAChB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "what-are-you",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Assortment of type-related checks and information.",
5
5
  "keywords": [
6
6
  "type",
@@ -87,4 +87,4 @@
87
87
  },
88
88
  "packageManager": "yarn@4.9.1",
89
89
  "sourcesRoot": "src"
90
- }
90
+ }
@@ -0,0 +1,24 @@
1
+ type _ProtectedCtor<T extends object> = typeof _ProtectedCtorClass & {
2
+ prototype: T
3
+ }
4
+ type _PrivateCtor<T extends object> = typeof _PrivateCtorClass & {
5
+ prototype: T
6
+ }
7
+ type _PublicCtor<T extends object> = abstract new (...args: any[]) => T
8
+ export type AnyCtor<T extends object> = _ProtectedCtor<T> | _PublicCtor<T> | _PrivateCtor<T>
9
+
10
+ export type InstanceTypeOf<T extends AnyCtor<any>> =
11
+ T extends _PrivateCtor<infer U>
12
+ ? U
13
+ : T extends _ProtectedCtor<infer U>
14
+ ? U
15
+ : T extends _PublicCtor<infer U>
16
+ ? U
17
+ : never
18
+ declare abstract class _ProtectedCtorClass {
19
+ protected constructor(...args: any[])
20
+ }
21
+
22
+ declare abstract class _PrivateCtorClass {
23
+ private constructor(...args: any[])
24
+ }
package/src/get-text.ts CHANGED
@@ -0,0 +1,111 @@
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,300 +1,37 @@
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
- }
1
+ export {
2
+ getFunctionName,
3
+ getNiceClassName,
4
+ getNiceTypeOf,
5
+ getNiceValueName,
6
+ getSymbolDescription
7
+ } from "./get-text.js"
8
+ export {
9
+ isArray,
10
+ isBigInt,
11
+ isBool,
12
+ isError,
13
+ isFunction,
14
+ isInt,
15
+ isIntOrInfinity,
16
+ isNat,
17
+ isNatOrInfinity,
18
+ isNotNullish,
19
+ isNullish,
20
+ isNumber,
21
+ isObject,
22
+ isPair,
23
+ isPosInt,
24
+ isPropertyKey,
25
+ isString,
26
+ isSymbol,
27
+ isThenable
28
+ } from "./is-basic.js"
29
+ export {
30
+ isAnyIterable,
31
+ isArrayLike,
32
+ isAsyncIterable,
33
+ isDoddle,
34
+ isIterable,
35
+ isNextable
36
+ } from "./is-special.js"
37
+ export { getPrototypeChain, isInstanceOf, isPrototypeObject } from "./prototypes.js"
package/src/is-basic.ts CHANGED
@@ -0,0 +1,143 @@
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"