@jblib/is 0.0.5 → 0.0.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/index.d.ts +13 -1
- package/dist/index.js +13 -1
- package/dist/is-empty-array.d.ts +19 -0
- package/dist/is-empty-array.d.ts.map +1 -0
- package/dist/is-empty-array.js +20 -0
- package/dist/is-empty-array.js.map +1 -0
- package/dist/is-falsy.d.ts +30 -0
- package/dist/is-falsy.d.ts.map +1 -0
- package/dist/is-falsy.js +30 -0
- package/dist/is-falsy.js.map +1 -0
- package/dist/is-map.d.ts +20 -0
- package/dist/is-map.d.ts.map +1 -0
- package/dist/is-map.js +21 -0
- package/dist/is-map.js.map +1 -0
- package/dist/is-negative-number.d.ts +20 -0
- package/dist/is-negative-number.d.ts.map +1 -0
- package/dist/is-negative-number.js +23 -0
- package/dist/is-negative-number.js.map +1 -0
- package/dist/is-null.d.ts +20 -0
- package/dist/is-null.d.ts.map +1 -0
- package/dist/is-null.js +21 -0
- package/dist/is-null.js.map +1 -0
- package/dist/is-positive-number.d.ts +20 -0
- package/dist/is-positive-number.d.ts.map +1 -0
- package/dist/is-positive-number.js +23 -0
- package/dist/is-positive-number.js.map +1 -0
- package/dist/is-promise.d.ts +20 -0
- package/dist/is-promise.d.ts.map +1 -0
- package/dist/is-promise.js +21 -0
- package/dist/is-promise.js.map +1 -0
- package/dist/is-reg-exp.d.ts +23 -0
- package/dist/is-reg-exp.d.ts.map +1 -0
- package/dist/is-reg-exp.js +24 -0
- package/dist/is-reg-exp.js.map +1 -0
- package/dist/is-set.d.ts +20 -0
- package/dist/is-set.d.ts.map +1 -0
- package/dist/is-set.js +21 -0
- package/dist/is-set.js.map +1 -0
- package/dist/is-symbol.d.ts +20 -0
- package/dist/is-symbol.d.ts.map +1 -0
- package/dist/is-symbol.js +21 -0
- package/dist/is-symbol.js.map +1 -0
- package/dist/is-truthy.d.ts +31 -0
- package/dist/is-truthy.d.ts.map +1 -0
- package/dist/is-truthy.js +30 -0
- package/dist/is-truthy.js.map +1 -0
- package/dist/is-undefined.d.ts +20 -0
- package/dist/is-undefined.d.ts.map +1 -0
- package/dist/is-undefined.js +21 -0
- package/dist/is-undefined.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +12 -0
- package/src/is-empty-array.spec.ts +21 -0
- package/src/is-empty-array.ts +17 -0
- package/src/is-falsy.spec.ts +22 -0
- package/src/is-falsy.ts +31 -0
- package/src/is-map.spec.ts +17 -0
- package/src/is-map.ts +18 -0
- package/src/is-negative-number.spec.ts +15 -0
- package/src/is-negative-number.ts +20 -0
- package/src/is-null.spec.ts +17 -0
- package/src/is-null.ts +18 -0
- package/src/is-positive-number.spec.ts +15 -0
- package/src/is-positive-number.ts +20 -0
- package/src/is-promise.spec.ts +27 -0
- package/src/is-promise.ts +22 -0
- package/src/is-reg-exp.spec.ts +19 -0
- package/src/is-reg-exp.ts +21 -0
- package/src/is-set.spec.ts +17 -0
- package/src/is-set.ts +18 -0
- package/src/is-symbol.spec.ts +18 -0
- package/src/is-symbol.ts +18 -0
- package/src/is-truthy.spec.ts +22 -0
- package/src/is-truthy.ts +29 -0
- package/src/is-undefined.spec.ts +29 -0
- package/src/is-undefined.ts +18 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/is-truthy.ts
|
|
2
|
+
/**
|
|
3
|
+
* isTruthy checks if a value is truthy. A value is considered truthy if it is not falsy.
|
|
4
|
+
* Falsy values are false, 0, 0n, '', null, and undefined.
|
|
5
|
+
* Useful for type narrowing in conditional statements.
|
|
6
|
+
* See isFalsy for the opposite of isTruthy.
|
|
7
|
+
*
|
|
8
|
+
* @param v value to check.
|
|
9
|
+
* @returns {v is Exclude<T, Falsy>} returns true if v is truthy or otherwise false.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* isTruthy(true) // true
|
|
14
|
+
* isTruthy(1) // true
|
|
15
|
+
* isTruthy('hello') // true
|
|
16
|
+
* isTruthy([]) // true
|
|
17
|
+
* isTruthy({}) // true
|
|
18
|
+
* isTruthy(false) // false
|
|
19
|
+
* isTruthy(0) // false
|
|
20
|
+
* isTruthy(0n) // false
|
|
21
|
+
* isTruthy('') // false
|
|
22
|
+
* isTruthy(null) // false
|
|
23
|
+
* isTruthy(undefined) // false
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const isTruthy = (v) => !!v;
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { isTruthy };
|
|
30
|
+
//# sourceMappingURL=is-truthy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-truthy.js","names":[],"sources":["../src/is-truthy.ts"],"sourcesContent":["import { type Falsy } from './is-falsy.js'\n\n/**\n * isTruthy checks if a value is truthy. A value is considered truthy if it is not falsy.\n * Falsy values are false, 0, 0n, '', null, and undefined.\n * Useful for type narrowing in conditional statements.\n * See isFalsy for the opposite of isTruthy.\n *\n * @param v value to check.\n * @returns {v is Exclude<T, Falsy>} returns true if v is truthy or otherwise false.\n *\n * @example\n * ```ts\n * isTruthy(true) // true\n * isTruthy(1) // true\n * isTruthy('hello') // true\n * isTruthy([]) // true\n * isTruthy({}) // true\n * isTruthy(false) // false\n * isTruthy(0) // false\n * isTruthy(0n) // false\n * isTruthy('') // false\n * isTruthy(null) // false\n * isTruthy(undefined) // false\n * ```\n */\nconst isTruthy = <T>(v: T): v is Exclude<T, Falsy> => !!v\n\nexport { isTruthy }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAM,YAAe,MAAiC,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/is-undefined.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* isUndefined checks if a value is undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is undefined} returns true if v is undefined or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isUndefined(undefined) // true
|
|
11
|
+
* isUndefined('hello') // false
|
|
12
|
+
* isUndefined(123) // false
|
|
13
|
+
* isUndefined({}) // false
|
|
14
|
+
* isUndefined([]) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare const isUndefined: (v: unknown) => v is undefined;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { isUndefined };
|
|
20
|
+
//# sourceMappingURL=is-undefined.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-undefined.d.ts","names":[],"sources":["../src/is-undefined.ts"],"mappings":";;;;;;;;;;;;;;;;cAeM,WAAA,GAAe,CAAA,cAAa,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/is-undefined.ts
|
|
2
|
+
/**
|
|
3
|
+
* isUndefined checks if a value is undefined.
|
|
4
|
+
*
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is undefined} returns true if v is undefined or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isUndefined(undefined) // true
|
|
11
|
+
* isUndefined('hello') // false
|
|
12
|
+
* isUndefined(123) // false
|
|
13
|
+
* isUndefined({}) // false
|
|
14
|
+
* isUndefined([]) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const isUndefined = (v) => v === void 0;
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { isUndefined };
|
|
21
|
+
//# sourceMappingURL=is-undefined.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-undefined.js","names":[],"sources":["../src/is-undefined.ts"],"sourcesContent":["/**\n * isUndefined checks if a value is undefined.\n *\n * @param v value to check.\n * @returns {v is undefined} returns true if v is undefined or otherwise false.\n *\n * @example\n * ```ts\n * isUndefined(undefined) // true\n * isUndefined('hello') // false\n * isUndefined(123) // false\n * isUndefined({}) // false\n * isUndefined([]) // false\n * ```\n */\nconst isUndefined = (v: unknown): v is undefined => v === undefined\n\nexport { isUndefined }\n"],"mappings":";;;;;;;;;;;;;;;;AAeA,MAAM,eAAe,MAA+B,MAAM"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,13 +2,25 @@ export { isArray } from './is-array.js'
|
|
|
2
2
|
export { isBoolean } from './is-boolean.js'
|
|
3
3
|
export { isDate } from './is-date.js'
|
|
4
4
|
export { isDefined } from './is-defined.js'
|
|
5
|
+
export { isEmptyArray } from './is-empty-array.js'
|
|
5
6
|
export { isEmptyObject } from './is-empty-object.js'
|
|
6
7
|
export { isError } from './is-error.js'
|
|
8
|
+
export { isFalsy } from './is-falsy.js'
|
|
7
9
|
export { isFunction } from './is-function.js'
|
|
10
|
+
export { isMap } from './is-map.js'
|
|
11
|
+
export { isNegativeNumber } from './is-negative-number.js'
|
|
8
12
|
export { isNil } from './is-nil.js'
|
|
9
13
|
export { isNonEmptyArray } from './is-non-empty-array.js'
|
|
14
|
+
export { isNull } from './is-null.js'
|
|
10
15
|
export { isNumber } from './is-number.js'
|
|
11
16
|
export { isObject } from './is-object.js'
|
|
12
17
|
export { isPlainObject } from './is-plain-object.js'
|
|
18
|
+
export { isPositiveNumber } from './is-positive-number.js'
|
|
13
19
|
export { isPrimitive } from './is-primitive.js'
|
|
20
|
+
export { isPromise } from './is-promise.js'
|
|
21
|
+
export { isRegExp } from './is-reg-exp.js'
|
|
22
|
+
export { isSet } from './is-set.js'
|
|
14
23
|
export { isString } from './is-string.js'
|
|
24
|
+
export { isSymbol } from './is-symbol.js'
|
|
25
|
+
export { isTruthy } from './is-truthy.js'
|
|
26
|
+
export { isUndefined } from './is-undefined.js'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isEmptyArray } from './is-empty-array.js'
|
|
4
|
+
|
|
5
|
+
describe('is empty array', () => {
|
|
6
|
+
it('should return true for empty arrays', () => {
|
|
7
|
+
expect(isEmptyArray([])).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-empty arrays', () => {
|
|
11
|
+
expect(isEmptyArray([1, 2, 3])).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return false for non-arrays', () => {
|
|
15
|
+
expect(isEmptyArray('hello')).toBe(false)
|
|
16
|
+
expect(isEmptyArray({})).toBe(false)
|
|
17
|
+
expect(isEmptyArray(123)).toBe(false)
|
|
18
|
+
expect(isEmptyArray(null)).toBe(false)
|
|
19
|
+
expect(isEmptyArray(undefined)).toBe(false)
|
|
20
|
+
})
|
|
21
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isEmptyArray checks if the value is an empty array.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is unknown[]} returns true if v is an empty array or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isEmptyArray([]) // true
|
|
10
|
+
* isEmptyArray([1, 2, 3]) // false
|
|
11
|
+
* isEmptyArray('hello') // false
|
|
12
|
+
* isEmptyArray({}) // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const isEmptyArray = (v: unknown): v is unknown[] => Array.isArray(v) && v.length === 0
|
|
16
|
+
|
|
17
|
+
export { isEmptyArray }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isFalsy } from './is-falsy.js'
|
|
4
|
+
|
|
5
|
+
describe('is falsy', () => {
|
|
6
|
+
it('should return true for falsy values', () => {
|
|
7
|
+
expect(isFalsy(false)).toBe(true)
|
|
8
|
+
expect(isFalsy(0)).toBe(true)
|
|
9
|
+
expect(isFalsy(0n)).toBe(true)
|
|
10
|
+
expect(isFalsy('')).toBe(true)
|
|
11
|
+
expect(isFalsy(null)).toBe(true)
|
|
12
|
+
expect(isFalsy(undefined)).toBe(true)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('should return false for non-falsy values', () => {
|
|
16
|
+
expect(isFalsy(true)).toBe(false)
|
|
17
|
+
expect(isFalsy(1)).toBe(false)
|
|
18
|
+
expect(isFalsy('hello')).toBe(false)
|
|
19
|
+
expect(isFalsy([])).toBe(false)
|
|
20
|
+
expect(isFalsy({})).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
})
|
package/src/is-falsy.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type Falsy = false | 0 | 0n | '' | null | undefined
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* isFalsy checks if a value is falsy.
|
|
5
|
+
* A value is considered falsy if it is false, 0, 0n, '', null, or undefined.
|
|
6
|
+
* Useful for type narrowing in conditional statements.
|
|
7
|
+
* See isTruthy for the opposite of isFalsy.
|
|
8
|
+
*
|
|
9
|
+
* @param v value to check.
|
|
10
|
+
* @returns {v is Extract<T, Falsy>} returns true if v is falsy or otherwise false.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* isFalsy(false) // true
|
|
15
|
+
* isFalsy(0) // true
|
|
16
|
+
* isFalsy(0n) // true
|
|
17
|
+
* isFalsy('') // true
|
|
18
|
+
* isFalsy(null) // true
|
|
19
|
+
* isFalsy(undefined) // true
|
|
20
|
+
* isFalsy(true) // false
|
|
21
|
+
* isFalsy(1) // false
|
|
22
|
+
* isFalsy('hello') // false
|
|
23
|
+
* isFalsy([]) // false
|
|
24
|
+
* isFalsy({}) // false
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const isFalsy = <T>(v: T): v is Extract<T, Falsy> => !v
|
|
28
|
+
|
|
29
|
+
export type { Falsy }
|
|
30
|
+
|
|
31
|
+
export { isFalsy }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isMap } from './is-map.js'
|
|
4
|
+
|
|
5
|
+
describe('is map', () => {
|
|
6
|
+
it('should return true for maps', () => {
|
|
7
|
+
expect(isMap(new Map())).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-maps', () => {
|
|
11
|
+
expect(isMap(123)).toBe(false)
|
|
12
|
+
expect(isMap({})).toBe(false)
|
|
13
|
+
expect(isMap([])).toBe(false)
|
|
14
|
+
expect(isMap(null)).toBe(false)
|
|
15
|
+
expect(isMap(undefined)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/is-map.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isMap checks if a value is a Map.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is Map<unknown, unknown>} returns true if v is a Map or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isMap(new Map()) // true
|
|
10
|
+
* isMap(new Set()) // false
|
|
11
|
+
* isMap([]) // false
|
|
12
|
+
* isMap({}) // false
|
|
13
|
+
* isMap('hello') // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isMap = (v: unknown): v is Map<unknown, unknown> => v instanceof Map
|
|
17
|
+
|
|
18
|
+
export { isMap }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isNegativeNumber } from './is-negative-number.js'
|
|
4
|
+
|
|
5
|
+
describe('is negative number', () => {
|
|
6
|
+
it('should return true for negative numbers', () => {
|
|
7
|
+
expect(isNegativeNumber(-5)).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-negative numbers', () => {
|
|
11
|
+
expect(isNegativeNumber(5)).toBe(false)
|
|
12
|
+
expect(isNegativeNumber(0)).toBe(false)
|
|
13
|
+
expect(isNegativeNumber('hello')).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isNegativeNumber checks if a value is a negative number.
|
|
3
|
+
* Note that zero is not considered a negative number.
|
|
4
|
+
*
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is number} returns true if v is a negative number or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isNegativeNumber(-5) // true
|
|
11
|
+
* isNegativeNumber(5) // false
|
|
12
|
+
* isNegativeNumber(0) // false
|
|
13
|
+
* isNegativeNumber('hello') // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isNegativeNumber = (v: unknown): v is number => {
|
|
17
|
+
return typeof v === 'number' && v < 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { isNegativeNumber }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isNull } from './is-null.js'
|
|
4
|
+
|
|
5
|
+
describe('is null', () => {
|
|
6
|
+
it('should return true for null values', () => {
|
|
7
|
+
expect(isNull(null)).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-null values', () => {
|
|
11
|
+
expect(isNull([])).toBe(false)
|
|
12
|
+
expect(isNull('hello')).toBe(false)
|
|
13
|
+
expect(isNull({})).toBe(false)
|
|
14
|
+
expect(isNull(123)).toBe(false)
|
|
15
|
+
expect(isNull(undefined)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/is-null.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isNull checks if a value is null.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is null} returns true if v is null or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isNull(null) // true
|
|
10
|
+
* isNull('hello') // false
|
|
11
|
+
* isNull(123) // false
|
|
12
|
+
* isNull({}) // false
|
|
13
|
+
* isNull([]) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isNull = (v: unknown): v is null => v === null
|
|
17
|
+
|
|
18
|
+
export { isNull }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isPositiveNumber } from './is-positive-number.js'
|
|
4
|
+
|
|
5
|
+
describe('is positive number', () => {
|
|
6
|
+
it('should return true for positive numbers', () => {
|
|
7
|
+
expect(isPositiveNumber(5)).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-positive numbers', () => {
|
|
11
|
+
expect(isPositiveNumber(-5)).toBe(false)
|
|
12
|
+
expect(isPositiveNumber(0)).toBe(false)
|
|
13
|
+
expect(isPositiveNumber('hello')).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isPositiveNumber checks if a value is a positive number.
|
|
3
|
+
* Note that zero is not considered a positive number.
|
|
4
|
+
*
|
|
5
|
+
* @param value value to check.
|
|
6
|
+
* @returns {value is number} returns true if value is a positive number or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isPositiveNumber(5) // true
|
|
11
|
+
* isPositiveNumber(-5) // false
|
|
12
|
+
* isPositiveNumber(0) // false
|
|
13
|
+
* isPositiveNumber('hello') // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isPositiveNumber = (value: unknown): value is number => {
|
|
17
|
+
return typeof value === 'number' && value > 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { isPositiveNumber }
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isPromise } from './is-promise.js'
|
|
4
|
+
|
|
5
|
+
describe('is promise', () => {
|
|
6
|
+
it('should return true for promises', () => {
|
|
7
|
+
expect(
|
|
8
|
+
isPromise({
|
|
9
|
+
then: () => {
|
|
10
|
+
void 0
|
|
11
|
+
},
|
|
12
|
+
catch: () => {
|
|
13
|
+
void 0
|
|
14
|
+
},
|
|
15
|
+
}),
|
|
16
|
+
).toBe(true)
|
|
17
|
+
expect(isPromise(Promise.resolve())).toBe(true)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should return false for non-promises', () => {
|
|
21
|
+
expect(isPromise(123)).toBe(false)
|
|
22
|
+
expect(isPromise({})).toBe(false)
|
|
23
|
+
expect(isPromise([])).toBe(false)
|
|
24
|
+
expect(isPromise(null)).toBe(false)
|
|
25
|
+
expect(isPromise(undefined)).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isPromise checks if a value is a Promise.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is Promise<unknown>} returns true if v is a Promise or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isPromise(Promise.resolve()) // true
|
|
10
|
+
* isPromise(new Set()) // false
|
|
11
|
+
* isPromise([]) // false
|
|
12
|
+
* isPromise({}) // false
|
|
13
|
+
* isPromise('hello') // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isPromise = (v: unknown): v is Promise<unknown> =>
|
|
17
|
+
typeof v === 'object' &&
|
|
18
|
+
v !== null &&
|
|
19
|
+
typeof (v as Promise<unknown>).then === 'function' &&
|
|
20
|
+
typeof (v as Promise<unknown>).catch === 'function'
|
|
21
|
+
|
|
22
|
+
export { isPromise }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isRegExp } from './is-reg-exp.js'
|
|
4
|
+
|
|
5
|
+
describe('is reg exp', () => {
|
|
6
|
+
it('should return true for regexp values', () => {
|
|
7
|
+
expect(isRegExp(/abc/)).toBe(true)
|
|
8
|
+
expect(isRegExp(new RegExp('abc'))).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-regexp values', () => {
|
|
12
|
+
expect(isRegExp('abc')).toBe(false)
|
|
13
|
+
expect(isRegExp(123)).toBe(false)
|
|
14
|
+
expect(isRegExp({})).toBe(false)
|
|
15
|
+
expect(isRegExp([])).toBe(false)
|
|
16
|
+
expect(isRegExp(null)).toBe(false)
|
|
17
|
+
expect(isRegExp(undefined)).toBe(false)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isRegExp checks if a value is a regular expression.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is RegExp} returns true if v is a RegExp or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isRegExp(/abc/) // true
|
|
10
|
+
* isRegExp(new RegExp('abc')) // true
|
|
11
|
+
* isRegExp('abc') // false
|
|
12
|
+
* isRegExp(123) // false
|
|
13
|
+
* isRegExp({}) // false
|
|
14
|
+
* isRegExp([]) // false
|
|
15
|
+
* isRegExp(null) // false
|
|
16
|
+
* isRegExp(undefined) // false
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
const isRegExp = (v: unknown): v is RegExp => v instanceof RegExp
|
|
20
|
+
|
|
21
|
+
export { isRegExp }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isSet } from './is-set.js'
|
|
4
|
+
|
|
5
|
+
describe('is set', () => {
|
|
6
|
+
it('should return true for sets', () => {
|
|
7
|
+
expect(isSet(new Set())).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-sets', () => {
|
|
11
|
+
expect(isSet(123)).toBe(false)
|
|
12
|
+
expect(isSet({})).toBe(false)
|
|
13
|
+
expect(isSet([])).toBe(false)
|
|
14
|
+
expect(isSet(null)).toBe(false)
|
|
15
|
+
expect(isSet(undefined)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/is-set.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isSet checks if a value is a Set.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is Set<unknown>} returns true if v is a Set or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isSet(new Set()) // true
|
|
10
|
+
* isSet(new Map()) // false
|
|
11
|
+
* isSet([]) // false
|
|
12
|
+
* isSet({}) // false
|
|
13
|
+
* isSet('hello') // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isSet = (v: unknown): v is Set<unknown> => v instanceof Set
|
|
17
|
+
|
|
18
|
+
export { isSet }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isSymbol } from './is-symbol.js'
|
|
4
|
+
|
|
5
|
+
describe('is symbol', () => {
|
|
6
|
+
it('should return true for symbols', () => {
|
|
7
|
+
expect(isSymbol(Symbol('foo'))).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for non-symbols', () => {
|
|
11
|
+
expect(isSymbol(123)).toBe(false)
|
|
12
|
+
expect(isSymbol('hello')).toBe(false)
|
|
13
|
+
expect(isSymbol({})).toBe(false)
|
|
14
|
+
expect(isSymbol([])).toBe(false)
|
|
15
|
+
expect(isSymbol(null)).toBe(false)
|
|
16
|
+
expect(isSymbol(undefined)).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
})
|
package/src/is-symbol.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isSymbol checks if a value is a symbol.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is symbol} returns true if v is a symbol or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isSymbol(Symbol('foo')) // true
|
|
10
|
+
* isSymbol('hello') // false
|
|
11
|
+
* isSymbol(123) // false
|
|
12
|
+
* isSymbol({}) // false
|
|
13
|
+
* isSymbol([]) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isSymbol = (v: unknown): v is symbol => typeof v === 'symbol'
|
|
17
|
+
|
|
18
|
+
export { isSymbol }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isTruthy } from './is-truthy.js'
|
|
4
|
+
|
|
5
|
+
describe('is truthy', () => {
|
|
6
|
+
it('should return true for truthy values', () => {
|
|
7
|
+
expect(isTruthy(true)).toBe(true)
|
|
8
|
+
expect(isTruthy(1)).toBe(true)
|
|
9
|
+
expect(isTruthy('hello')).toBe(true)
|
|
10
|
+
expect(isTruthy([])).toBe(true)
|
|
11
|
+
expect(isTruthy({})).toBe(true)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return false for non-truthy values', () => {
|
|
15
|
+
expect(isTruthy(false)).toBe(false)
|
|
16
|
+
expect(isTruthy(0)).toBe(false)
|
|
17
|
+
expect(isTruthy(0n)).toBe(false)
|
|
18
|
+
expect(isTruthy('')).toBe(false)
|
|
19
|
+
expect(isTruthy(null)).toBe(false)
|
|
20
|
+
expect(isTruthy(undefined)).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
})
|
package/src/is-truthy.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Falsy } from './is-falsy.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* isTruthy checks if a value is truthy. A value is considered truthy if it is not falsy.
|
|
5
|
+
* Falsy values are false, 0, 0n, '', null, and undefined.
|
|
6
|
+
* Useful for type narrowing in conditional statements.
|
|
7
|
+
* See isFalsy for the opposite of isTruthy.
|
|
8
|
+
*
|
|
9
|
+
* @param v value to check.
|
|
10
|
+
* @returns {v is Exclude<T, Falsy>} returns true if v is truthy or otherwise false.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* isTruthy(true) // true
|
|
15
|
+
* isTruthy(1) // true
|
|
16
|
+
* isTruthy('hello') // true
|
|
17
|
+
* isTruthy([]) // true
|
|
18
|
+
* isTruthy({}) // true
|
|
19
|
+
* isTruthy(false) // false
|
|
20
|
+
* isTruthy(0) // false
|
|
21
|
+
* isTruthy(0n) // false
|
|
22
|
+
* isTruthy('') // false
|
|
23
|
+
* isTruthy(null) // false
|
|
24
|
+
* isTruthy(undefined) // false
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const isTruthy = <T>(v: T): v is Exclude<T, Falsy> => !!v
|
|
28
|
+
|
|
29
|
+
export { isTruthy }
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isUndefined } from './is-undefined.js'
|
|
4
|
+
|
|
5
|
+
describe('is undefined', () => {
|
|
6
|
+
it('should return true for undefined', () => {
|
|
7
|
+
expect(isUndefined(undefined)).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return true for a type that could be undefined and is undefined', () => {
|
|
11
|
+
const v: string | undefined = undefined
|
|
12
|
+
|
|
13
|
+
expect(isUndefined(v)).toBe(true)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should return false for non-undefined values', () => {
|
|
17
|
+
expect(isUndefined(null)).toBe(false)
|
|
18
|
+
expect(isUndefined('hello')).toBe(false)
|
|
19
|
+
expect(isUndefined(123)).toBe(false)
|
|
20
|
+
expect(isUndefined({})).toBe(false)
|
|
21
|
+
expect(isUndefined([])).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should return false for a type that could be undefined and is not undefined', () => {
|
|
25
|
+
const v: string | undefined = 'hello'
|
|
26
|
+
|
|
27
|
+
expect(isUndefined(v)).toBe(false)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isUndefined checks if a value is undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is undefined} returns true if v is undefined or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isUndefined(undefined) // true
|
|
10
|
+
* isUndefined('hello') // false
|
|
11
|
+
* isUndefined(123) // false
|
|
12
|
+
* isUndefined({}) // false
|
|
13
|
+
* isUndefined([]) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isUndefined = (v: unknown): v is undefined => v === undefined
|
|
17
|
+
|
|
18
|
+
export { isUndefined }
|