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.
- package/dist/cjs/ctor-type.d.ts +17 -0
- package/dist/cjs/ctor-type.d.ts.map +1 -0
- package/dist/cjs/ctor-type.js +3 -0
- package/dist/cjs/ctor-type.js.map +1 -0
- package/dist/cjs/get-text.d.ts +35 -0
- package/dist/cjs/get-text.d.ts.map +1 -1
- package/dist/cjs/get-text.js +108 -0
- package/dist/cjs/get-text.js.map +1 -1
- package/dist/cjs/index.d.ts +4 -194
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +38 -313
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/is-basic.d.ts +121 -0
- package/dist/cjs/is-basic.d.ts.map +1 -1
- package/dist/cjs/is-basic.js +154 -0
- package/dist/cjs/is-basic.js.map +1 -1
- package/dist/cjs/is-special.d.ts +38 -0
- package/dist/cjs/is-special.d.ts.map +1 -1
- package/dist/cjs/is-special.js +57 -0
- package/dist/cjs/is-special.js.map +1 -1
- package/dist/cjs/prototypes.d.ts +6 -0
- package/dist/cjs/prototypes.d.ts.map +1 -0
- package/dist/cjs/prototypes.js +43 -0
- package/dist/cjs/prototypes.js.map +1 -0
- package/dist/esm/ctor-type.d.ts +17 -0
- package/dist/esm/ctor-type.d.ts.map +1 -0
- package/dist/esm/ctor-type.js +2 -0
- package/dist/esm/ctor-type.js.map +1 -0
- package/dist/esm/get-text.d.ts +35 -1
- package/dist/esm/get-text.d.ts.map +1 -1
- package/dist/esm/get-text.js +101 -1
- package/dist/esm/get-text.js.map +1 -1
- package/dist/esm/index.d.ts +4 -194
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +4 -283
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/is-basic.d.ts +121 -1
- package/dist/esm/is-basic.d.ts.map +1 -1
- package/dist/esm/is-basic.js +135 -1
- package/dist/esm/is-basic.js.map +1 -1
- package/dist/esm/is-special.d.ts +38 -1
- package/dist/esm/is-special.d.ts.map +1 -1
- package/dist/esm/is-special.js +50 -1
- package/dist/esm/is-special.js.map +1 -1
- package/dist/esm/prototypes.d.ts +6 -0
- package/dist/esm/prototypes.d.ts.map +1 -0
- package/dist/esm/prototypes.js +37 -0
- package/dist/esm/prototypes.js.map +1 -0
- package/package.json +2 -2
- package/src/ctor-type.ts +24 -0
- package/src/get-text.ts +111 -0
- package/src/index.ts +37 -300
- package/src/is-basic.ts +143 -0
- package/src/is-special.ts +57 -0
- package/src/prototypes.ts +48 -0
package/src/is-special.ts
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isFunction, isObject } from "lodash"
|
|
2
|
+
import { AnyCtor } from "./ctor-type.js"
|
|
3
|
+
|
|
4
|
+
const sCtor = "constructor"
|
|
5
|
+
const sPrototype = "prototype"
|
|
6
|
+
|
|
7
|
+
function getCtor<T extends object>(obj: T): AnyCtor<T> {
|
|
8
|
+
return obj[sCtor] as AnyCtor<T>
|
|
9
|
+
}
|
|
10
|
+
function getCtorProto<T extends Function>(obj: T): T {
|
|
11
|
+
return obj[sPrototype] as T
|
|
12
|
+
}
|
|
13
|
+
export const isInstanceOf = <Parent extends object>(
|
|
14
|
+
instance: object,
|
|
15
|
+
what: AnyCtor<Parent>
|
|
16
|
+
): instance is Parent => {
|
|
17
|
+
return instance instanceof what
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Return true when `obj` looks like the prototype object of some constructor. */
|
|
21
|
+
export function isPrototypeObject(obj: object): boolean {
|
|
22
|
+
if (!isObject(obj)) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!Object.hasOwn(obj, sCtor)) {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ctor = getCtor(obj)
|
|
31
|
+
const ctorPtoto = getCtorProto(ctor)
|
|
32
|
+
return isFunction(ctor) && ctorPtoto === obj && isInstanceOf(obj, ctor)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getPrototypeChain<T>(obj: T, depth: number): T[] {
|
|
36
|
+
const chain: T[] = []
|
|
37
|
+
let current: T | null = obj
|
|
38
|
+
|
|
39
|
+
while (current) {
|
|
40
|
+
if (depth <= 0) {
|
|
41
|
+
break
|
|
42
|
+
}
|
|
43
|
+
chain.push(current)
|
|
44
|
+
current = Object.getPrototypeOf(current)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return chain
|
|
48
|
+
}
|