@jblib/is 0.0.2 → 0.0.4
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-array.d.ts +21 -0
- package/dist/is-array.d.ts.map +1 -0
- package/dist/is-array.js +22 -0
- package/dist/is-array.js.map +1 -0
- package/dist/is-boolean.d.ts +21 -0
- package/dist/is-boolean.d.ts.map +1 -0
- package/dist/is-boolean.js +22 -0
- package/dist/is-boolean.js.map +1 -0
- package/dist/is-date.d.ts +21 -0
- package/dist/is-date.d.ts.map +1 -0
- package/dist/is-date.js +22 -0
- package/dist/is-date.js.map +1 -0
- package/dist/is-defined.d.ts +19 -0
- package/dist/is-defined.d.ts.map +1 -0
- package/dist/is-defined.js +20 -0
- package/dist/is-defined.js.map +1 -0
- package/dist/is-empty-object.d.ts +23 -0
- package/dist/is-empty-object.d.ts.map +1 -0
- package/dist/is-empty-object.js +26 -0
- package/dist/is-empty-object.js.map +1 -0
- package/dist/is-error.d.ts +20 -0
- package/dist/is-error.d.ts.map +1 -0
- package/dist/is-error.js +21 -0
- package/dist/is-error.js.map +1 -0
- package/dist/is-function.d.ts +20 -0
- package/dist/is-function.d.ts.map +1 -0
- package/dist/is-function.js +21 -0
- package/dist/is-function.js.map +1 -0
- package/dist/is-nil.d.ts +21 -0
- package/dist/is-nil.d.ts.map +1 -0
- package/dist/is-nil.js +22 -0
- package/dist/is-nil.js.map +1 -0
- package/dist/is-non-empty-array.d.ts +19 -0
- package/dist/is-non-empty-array.d.ts.map +1 -0
- package/dist/is-non-empty-array.js +20 -0
- package/dist/is-non-empty-array.js.map +1 -0
- package/dist/is-number.d.ts +10 -3
- package/dist/is-number.d.ts.map +1 -1
- package/dist/is-number.js +10 -3
- package/dist/is-number.js.map +1 -1
- package/dist/is-object.d.ts +20 -0
- package/dist/is-object.d.ts.map +1 -0
- package/dist/is-object.js +21 -0
- package/dist/is-object.js.map +1 -0
- package/dist/is-plain-object.d.ts +22 -0
- package/dist/is-plain-object.d.ts.map +1 -0
- package/dist/is-plain-object.js +27 -0
- package/dist/is-plain-object.js.map +1 -0
- package/dist/is-primitive.d.ts +27 -0
- package/dist/is-primitive.d.ts.map +1 -0
- package/dist/is-primitive.js +24 -0
- package/dist/is-primitive.js.map +1 -0
- package/dist/is-string.d.ts +10 -3
- package/dist/is-string.d.ts.map +1 -1
- package/dist/is-string.js +10 -3
- package/dist/is-string.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +14 -4
- package/src/is-array.spec.ts +18 -0
- package/src/is-array.ts +19 -0
- package/src/is-boolean.spec.ts +18 -0
- package/src/is-boolean.ts +19 -0
- package/src/is-date.spec.ts +21 -0
- package/src/is-date.ts +19 -0
- package/src/is-defined.spec.ts +15 -0
- package/src/is-defined.ts +17 -0
- package/src/is-empty-object.spec.ts +25 -0
- package/src/is-empty-object.ts +24 -0
- package/src/is-error.spec.ts +19 -0
- package/src/is-error.ts +18 -0
- package/src/is-function.spec.ts +35 -0
- package/src/is-function.ts +18 -0
- package/src/is-nil.spec.ts +17 -0
- package/src/is-nil.ts +19 -0
- package/src/is-non-empty-array.spec.ts +21 -0
- package/src/is-non-empty-array.ts +17 -0
- package/src/is-number.ts +10 -3
- package/src/is-object.spec.ts +17 -0
- package/src/is-object.ts +18 -0
- package/src/is-plain-object.spec.ts +18 -0
- package/src/is-plain-object.ts +28 -0
- package/src/is-primitive.spec.ts +23 -0
- package/src/is-primitive.ts +26 -0
- package/src/is-string.ts +10 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-plain-object.d.ts","names":[],"sources":["../src/is-plain-object.ts"],"mappings":";;;;;;;;;;;;;;;;;;cAiBM,aAAA,GAAiB,CAAA,cAAa,CAAA,IAAK,MAAA,CAAO,WAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/is-plain-object.ts
|
|
2
|
+
/**
|
|
3
|
+
* isPlainObject checks if a value is a plain object, that is,
|
|
4
|
+
* an object created by the Object constructor or with a null prototype.
|
|
5
|
+
*
|
|
6
|
+
* @param v value to check.
|
|
7
|
+
* @returns {v is Record<PropertyKey, unknown>} returns true if the value is a plain object or otherwise false.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* isPlainObject({}) // true
|
|
12
|
+
* isPlainObject(Object.create(null)) // true
|
|
13
|
+
* isPlainObject([]) // false
|
|
14
|
+
* isPlainObject(null) // false
|
|
15
|
+
* isPlainObject('hello') // false
|
|
16
|
+
* isPlainObject(42) // false
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
const isPlainObject = (v) => {
|
|
20
|
+
if (typeof v !== "object" || v === null) return false;
|
|
21
|
+
const proto = Object.getPrototypeOf(v);
|
|
22
|
+
return proto === Object.prototype || proto === null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { isPlainObject };
|
|
27
|
+
//# sourceMappingURL=is-plain-object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-plain-object.js","names":[],"sources":["../src/is-plain-object.ts"],"sourcesContent":["/**\n * isPlainObject checks if a value is a plain object, that is,\n * an object created by the Object constructor or with a null prototype.\n *\n * @param v value to check.\n * @returns {v is Record<PropertyKey, unknown>} returns true if the value is a plain object or otherwise false.\n *\n * @example\n * ```ts\n * isPlainObject({}) // true\n * isPlainObject(Object.create(null)) // true\n * isPlainObject([]) // false\n * isPlainObject(null) // false\n * isPlainObject('hello') // false\n * isPlainObject(42) // false\n * ```\n */\nconst isPlainObject = (v: unknown): v is Record<PropertyKey, unknown> => {\n if (typeof v !== 'object' || v === null) {\n return false\n }\n\n const proto = Object.getPrototypeOf(v) as unknown\n\n return proto === Object.prototype || proto === null\n}\n\nexport { isPlainObject }\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,MAAM,iBAAiB,MAAkD;AACvE,KAAI,OAAO,MAAM,YAAY,MAAM,KACjC,QAAO;CAGT,MAAM,QAAQ,OAAO,eAAe,EAAE;AAEtC,QAAO,UAAU,OAAO,aAAa,UAAU"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/is-primitive.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Primitive type represents the primitive values.
|
|
4
|
+
*/
|
|
5
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* isPrimitive checks if a value is a primitive.
|
|
8
|
+
*
|
|
9
|
+
* @param v value to check.
|
|
10
|
+
* @returns {v is Primitive} returns true if the value is a primitive or otherwise false.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* isPrimitive(42) // true
|
|
15
|
+
* isPrimitive('hello') // true
|
|
16
|
+
* isPrimitive(true) // true
|
|
17
|
+
* isPrimitive(null) // true
|
|
18
|
+
* isPrimitive(undefined) // true
|
|
19
|
+
* isPrimitive({}) // false
|
|
20
|
+
* isPrimitive([]) // false
|
|
21
|
+
* isPrimitive(() => {}) // false
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare const isPrimitive: (v: unknown) => v is Primitive;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { isPrimitive };
|
|
27
|
+
//# sourceMappingURL=is-primitive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-primitive.d.ts","names":[],"sources":["../src/is-primitive.ts"],"mappings":";;;;KAGK,SAAA;;;AAAS;;;;;;;;;;;;;;;;cAoBR,WAAA,GAAe,CAAA,cAAa,CAAA,IAAK,SAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/is-primitive.ts
|
|
2
|
+
/**
|
|
3
|
+
* isPrimitive checks if a value is a primitive.
|
|
4
|
+
*
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is Primitive} returns true if the value is a primitive or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isPrimitive(42) // true
|
|
11
|
+
* isPrimitive('hello') // true
|
|
12
|
+
* isPrimitive(true) // true
|
|
13
|
+
* isPrimitive(null) // true
|
|
14
|
+
* isPrimitive(undefined) // true
|
|
15
|
+
* isPrimitive({}) // false
|
|
16
|
+
* isPrimitive([]) // false
|
|
17
|
+
* isPrimitive(() => {}) // false
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
const isPrimitive = (v) => v === null || typeof v !== "object" && typeof v !== "function";
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { isPrimitive };
|
|
24
|
+
//# sourceMappingURL=is-primitive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-primitive.js","names":[],"sources":["../src/is-primitive.ts"],"sourcesContent":["/**\n * Primitive type represents the primitive values.\n */\ntype Primitive = string | number | boolean | bigint | symbol | null | undefined\n\n/**\n * isPrimitive checks if a value is a primitive.\n *\n * @param v value to check.\n * @returns {v is Primitive} returns true if the value is a primitive or otherwise false.\n *\n * @example\n * ```ts\n * isPrimitive(42) // true\n * isPrimitive('hello') // true\n * isPrimitive(true) // true\n * isPrimitive(null) // true\n * isPrimitive(undefined) // true\n * isPrimitive({}) // false\n * isPrimitive([]) // false\n * isPrimitive(() => {}) // false\n * ```\n */\nconst isPrimitive = (v: unknown): v is Primitive => v === null || (typeof v !== 'object' && typeof v !== 'function')\n\nexport { isPrimitive }\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAuBA,MAAM,eAAe,MAA+B,MAAM,QAAS,OAAO,MAAM,YAAY,OAAO,MAAM"}
|
package/dist/is-string.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
//#region src/is-string.d.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* isString checks if the provided value is a string.
|
|
4
4
|
*
|
|
5
|
-
* @param v value to check
|
|
6
|
-
* @returns
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is string} returns true if v is a string or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isString('hello') // true
|
|
11
|
+
* isString(123) // false
|
|
12
|
+
* isString({}) // false
|
|
13
|
+
* ```
|
|
7
14
|
*/
|
|
8
15
|
declare const isString: (v: unknown) => v is string;
|
|
9
16
|
//#endregion
|
package/dist/is-string.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-string.d.ts","names":[],"sources":["../src/is-string.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"is-string.d.ts","names":[],"sources":["../src/is-string.ts"],"mappings":";;;;;;;;;;;;;;cAaM,QAAA,GAAY,CAAA,cAAa,CAAA"}
|
package/dist/is-string.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
//#region src/is-string.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* isString checks if the provided value is a string.
|
|
4
4
|
*
|
|
5
|
-
* @param v value to check
|
|
6
|
-
* @returns
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is string} returns true if v is a string or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isString('hello') // true
|
|
11
|
+
* isString(123) // false
|
|
12
|
+
* isString({}) // false
|
|
13
|
+
* ```
|
|
7
14
|
*/
|
|
8
15
|
const isString = (v) => typeof v === "string";
|
|
9
16
|
|
package/dist/is-string.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-string.js","names":[],"sources":["../src/is-string.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"file":"is-string.js","names":[],"sources":["../src/is-string.ts"],"sourcesContent":["/**\n * isString checks if the provided value is a string.\n *\n * @param v value to check.\n * @returns {v is string} returns true if v is a string or otherwise false.\n *\n * @example\n * ```ts\n * isString('hello') // true\n * isString(123) // false\n * isString({}) // false\n * ```\n */\nconst isString = (v: unknown): v is string => typeof v === 'string'\n\nexport { isString }\n"],"mappings":";;;;;;;;;;;;;;AAaA,MAAM,YAAY,MAA4B,OAAO,MAAM"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jblib/is",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "is, condition checks",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devDependencies": {
|
|
7
|
-
"vitest": "^4.1.5",
|
|
8
|
-
"typescript": "^6.0.3",
|
|
9
7
|
"prettier": "^3.8.3",
|
|
10
8
|
"tsdown": "^0.21.10",
|
|
11
|
-
"
|
|
9
|
+
"typescript": "^6.0.3",
|
|
10
|
+
"vitest": "^4.1.5",
|
|
11
|
+
"@jblib/eslint": "0.0.4"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"is"
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export {
|
|
1
|
+
export { isArray } from './is-array.js'
|
|
2
|
+
export { isBoolean } from './is-boolean.js'
|
|
3
|
+
export { isDate } from './is-date.js'
|
|
4
|
+
export { isDefined } from './is-defined.js'
|
|
5
|
+
export { isEmptyObject } from './is-empty-object.js'
|
|
6
|
+
export { isError } from './is-error.js'
|
|
7
|
+
export { isFunction } from './is-function.js'
|
|
8
|
+
export { isNil } from './is-nil.js'
|
|
9
|
+
export { isNonEmptyArray } from './is-non-empty-array.js'
|
|
10
|
+
export { isNumber } from './is-number.js'
|
|
11
|
+
export { isObject } from './is-object.js'
|
|
12
|
+
export { isPlainObject } from './is-plain-object.js'
|
|
13
|
+
export { isPrimitive } from './is-primitive.js'
|
|
14
|
+
export { isString } from './is-string.js'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isArray } from './is-array.js'
|
|
4
|
+
|
|
5
|
+
describe('is array', () => {
|
|
6
|
+
it('should return true for arrays', () => {
|
|
7
|
+
expect(isArray([])).toBe(true)
|
|
8
|
+
expect(isArray([1, 2, 3])).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-arrays', () => {
|
|
12
|
+
expect(isArray('hello')).toBe(false)
|
|
13
|
+
expect(isArray({})).toBe(false)
|
|
14
|
+
expect(isArray(123)).toBe(false)
|
|
15
|
+
expect(isArray(null)).toBe(false)
|
|
16
|
+
expect(isArray(undefined)).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
})
|
package/src/is-array.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isArray checks if the provided value is an array.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is unknown[]} returns true if the value is an array or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isArray([]) // true
|
|
10
|
+
* isArray([1, 2, 3]) // true
|
|
11
|
+
* isArray('hello') // false
|
|
12
|
+
* isArray({}) // false
|
|
13
|
+
* isArray(123) // false
|
|
14
|
+
* isArray(undefined) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const isArray = (v: unknown): v is unknown[] => Array.isArray(v)
|
|
18
|
+
|
|
19
|
+
export { isArray }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isBoolean } from './is-boolean.js'
|
|
4
|
+
|
|
5
|
+
describe('is boolean', () => {
|
|
6
|
+
it('should return true for booleans', () => {
|
|
7
|
+
expect(isBoolean(true)).toBe(true)
|
|
8
|
+
expect(isBoolean(false)).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-booleans', () => {
|
|
12
|
+
expect(isBoolean('hello')).toBe(false)
|
|
13
|
+
expect(isBoolean({})).toBe(false)
|
|
14
|
+
expect(isBoolean(123)).toBe(false)
|
|
15
|
+
expect(isBoolean(null)).toBe(false)
|
|
16
|
+
expect(isBoolean(undefined)).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isBoolean checks if a value is a boolean.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is boolean} returns true if the value is a boolean or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isBoolean(true) // true
|
|
10
|
+
* isBoolean(false) // true
|
|
11
|
+
* isBoolean('hello') // false
|
|
12
|
+
* isBoolean(42) // false
|
|
13
|
+
* isBoolean(null) // false
|
|
14
|
+
* isBoolean(undefined) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const isBoolean = (v: unknown): v is boolean => typeof v === 'boolean'
|
|
18
|
+
|
|
19
|
+
export { isBoolean }
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isDate } from './is-date.js'
|
|
4
|
+
|
|
5
|
+
describe('is date', () => {
|
|
6
|
+
it('should return true for valid dates', () => {
|
|
7
|
+
expect(isDate(new Date())).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for invalid dates', () => {
|
|
11
|
+
expect(isDate(new Date('invalid date'))).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return false for non-date values', () => {
|
|
15
|
+
expect(isDate('2022-01-01')).toBe(false)
|
|
16
|
+
expect(isDate(1640995200000)).toBe(false)
|
|
17
|
+
expect(isDate({})).toBe(false)
|
|
18
|
+
expect(isDate(null)).toBe(false)
|
|
19
|
+
expect(isDate(undefined)).toBe(false)
|
|
20
|
+
})
|
|
21
|
+
})
|
package/src/is-date.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isDate checks if the value is an instance of Date and is a valid date.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v instanceof Date} returns true if the value is an instance of Date and is a valid date or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isDate(new Date()) // true
|
|
10
|
+
* isDate(new Date('invalid date')) // false
|
|
11
|
+
* isDate('2022-01-01') // false
|
|
12
|
+
* isDate(1640995200000) // false
|
|
13
|
+
* isDate(null) // false
|
|
14
|
+
* isDate(undefined) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const isDate = (v: unknown): v is Date => v instanceof Date && !isNaN(v.getTime())
|
|
18
|
+
|
|
19
|
+
export { isDate }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isDefined } from './is-defined.js'
|
|
4
|
+
|
|
5
|
+
describe('is defined', () => {
|
|
6
|
+
it('should return true for defined values', () => {
|
|
7
|
+
expect(isDefined(42)).toBe(true)
|
|
8
|
+
expect(isDefined('hello')).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for null or undefined', () => {
|
|
12
|
+
expect(isDefined(null)).toBe(false)
|
|
13
|
+
expect(isDefined(undefined)).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isDefined checks if a value is not null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is T} returns true if the value is defined (not null or undefined) or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isDefined(42) // true
|
|
10
|
+
* isDefined('hello') // true
|
|
11
|
+
* isDefined(null) // false
|
|
12
|
+
* isDefined(undefined) // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const isDefined = <T>(v: T | null | undefined): v is T => v !== null && v !== undefined
|
|
16
|
+
|
|
17
|
+
export { isDefined }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isEmptyObject } from './is-empty-object.js'
|
|
4
|
+
|
|
5
|
+
describe('is empty object', () => {
|
|
6
|
+
it('should return true for empty objects', () => {
|
|
7
|
+
expect(isEmptyObject({})).toBe(true)
|
|
8
|
+
expect(isEmptyObject(Object.create(null))).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-empty objects', () => {
|
|
12
|
+
expect(
|
|
13
|
+
isEmptyObject({
|
|
14
|
+
a: 1,
|
|
15
|
+
}),
|
|
16
|
+
).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should return false for non-object values', () => {
|
|
20
|
+
expect(isEmptyObject([])).toBe(false)
|
|
21
|
+
expect(isEmptyObject(null)).toBe(false)
|
|
22
|
+
expect(isEmptyObject('hello')).toBe(false)
|
|
23
|
+
expect(isEmptyObject(42)).toBe(false)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isPlainObject } from './is-plain-object.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* isEmptyObject checks if a value is an empty object, that is,
|
|
5
|
+
* a plain object with no own properties.
|
|
6
|
+
*
|
|
7
|
+
* @param v value to check.
|
|
8
|
+
* @returns {v is Record<PropertyKey, never>} returns true if the value is an empty object or otherwise false.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* isEmptyObject({}) // true
|
|
13
|
+
* isEmptyObject(Object.create(null)) // true
|
|
14
|
+
* isEmptyObject({ a: 1 }) // false
|
|
15
|
+
* isEmptyObject([]) // false
|
|
16
|
+
* isEmptyObject(null) // false
|
|
17
|
+
* isEmptyObject('hello') // false
|
|
18
|
+
* isEmptyObject(42) // false
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
const isEmptyObject = (v: unknown): v is Record<PropertyKey, never> =>
|
|
22
|
+
isPlainObject(v) && Reflect.ownKeys(v).length === 0
|
|
23
|
+
|
|
24
|
+
export { isEmptyObject }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isError } from './is-error.js'
|
|
4
|
+
|
|
5
|
+
describe('is error', () => {
|
|
6
|
+
it('should return true for errors', () => {
|
|
7
|
+
expect(isError(new Error())).toBe(true)
|
|
8
|
+
expect(isError(new Error('error message'))).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-errors', () => {
|
|
12
|
+
expect(isError('hello')).toBe(false)
|
|
13
|
+
expect(isError({})).toBe(false)
|
|
14
|
+
expect(isError(123)).toBe(false)
|
|
15
|
+
expect(isError(null)).toBe(false)
|
|
16
|
+
expect(isError(undefined)).toBe(false)
|
|
17
|
+
expect(isError([])).toBe(false)
|
|
18
|
+
})
|
|
19
|
+
})
|
package/src/is-error.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isError checks if the value is an instance of Error.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is Error} returns true if the value is an instance of Error or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isError(new Error()) // true
|
|
10
|
+
* isError('hello') // false
|
|
11
|
+
* isError({}) // false
|
|
12
|
+
* isError(123) // false
|
|
13
|
+
* isError(null) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isError = (v: unknown): v is Error => v instanceof Error
|
|
17
|
+
|
|
18
|
+
export { isError }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isFunction } from './is-function.js'
|
|
4
|
+
|
|
5
|
+
describe('is function', () => {
|
|
6
|
+
it('should return true for functions', () => {
|
|
7
|
+
expect(
|
|
8
|
+
isFunction(() => {
|
|
9
|
+
void 0
|
|
10
|
+
}),
|
|
11
|
+
).toBe(true)
|
|
12
|
+
|
|
13
|
+
expect(
|
|
14
|
+
isFunction(function () {
|
|
15
|
+
void 0
|
|
16
|
+
}),
|
|
17
|
+
).toBe(true)
|
|
18
|
+
|
|
19
|
+
expect(
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
21
|
+
isFunction(async function () {
|
|
22
|
+
void 0
|
|
23
|
+
}),
|
|
24
|
+
).toBe(true)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should return false for non-functions', () => {
|
|
28
|
+
expect(isFunction('hello')).toBe(false)
|
|
29
|
+
expect(isFunction({})).toBe(false)
|
|
30
|
+
expect(isFunction(123)).toBe(false)
|
|
31
|
+
expect(isFunction(null)).toBe(false)
|
|
32
|
+
expect(isFunction(undefined)).toBe(false)
|
|
33
|
+
expect(isFunction([])).toBe(false)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isFunction checks if the provided value is a function.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is (...args: unknown[]) => unknown} returns true if v is a function or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isFunction(() => {}) // true
|
|
10
|
+
* isFunction(function() {}) // true
|
|
11
|
+
* isFunction(async function() {}) // true
|
|
12
|
+
* isFunction('hello') // false
|
|
13
|
+
* isFunction({}) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isFunction = (v: unknown): v is (...args: unknown[]) => unknown => typeof v === 'function'
|
|
17
|
+
|
|
18
|
+
export { isFunction }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isNil } from './is-nil.js'
|
|
4
|
+
|
|
5
|
+
describe('is nil', () => {
|
|
6
|
+
it('should return true for null and undefined', () => {
|
|
7
|
+
expect(isNil(null)).toBe(true)
|
|
8
|
+
expect(isNil(undefined)).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-nil values', () => {
|
|
12
|
+
expect(isNil(42)).toBe(false)
|
|
13
|
+
expect(isNil('hello')).toBe(false)
|
|
14
|
+
expect(isNil({})).toBe(false)
|
|
15
|
+
expect(isNil([])).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/is-nil.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isNil checks if a value is null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is null | undefined} returns true if the value is null or undefined or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isNil(null) // true
|
|
10
|
+
* isNil(undefined) // true
|
|
11
|
+
* isNil(42) // false
|
|
12
|
+
* isNil('hello') // false
|
|
13
|
+
* isNil({}) // false
|
|
14
|
+
* isNil([]) // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const isNil = (v: unknown): v is null | undefined => v === null || v === undefined
|
|
18
|
+
|
|
19
|
+
export { isNil }
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isNonEmptyArray } from './is-non-empty-array.js'
|
|
4
|
+
|
|
5
|
+
describe('is non empty array', () => {
|
|
6
|
+
it('should return true for non-empty arrays', () => {
|
|
7
|
+
expect(isNonEmptyArray([1, 2, 3])).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should return false for empty arrays', () => {
|
|
11
|
+
expect(isNonEmptyArray([])).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return false for non-arrays', () => {
|
|
15
|
+
expect(isNonEmptyArray('hello')).toBe(false)
|
|
16
|
+
expect(isNonEmptyArray({})).toBe(false)
|
|
17
|
+
expect(isNonEmptyArray(123)).toBe(false)
|
|
18
|
+
expect(isNonEmptyArray(null)).toBe(false)
|
|
19
|
+
expect(isNonEmptyArray(undefined)).toBe(false)
|
|
20
|
+
})
|
|
21
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isNonEmptyArray checks if the provided value is an array and contains at least one element.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is unknown[]} returns true if v is a non-empty array or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isNonEmptyArray([1, 2, 3]) // true
|
|
10
|
+
* isNonEmptyArray([]) // false
|
|
11
|
+
* isNonEmptyArray('hello') // false
|
|
12
|
+
* isNonEmptyArray({}) // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const isNonEmptyArray = (v: unknown): v is unknown[] => Array.isArray(v) && v.length > 0
|
|
16
|
+
|
|
17
|
+
export { isNonEmptyArray }
|
package/src/is-number.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* isNumber checks if the provided value is a number.
|
|
3
3
|
*
|
|
4
|
-
* @param v value to check
|
|
5
|
-
* @returns
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is number} returns true if v is a number or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isNumber(123) // true
|
|
10
|
+
* isNumber('hello') // false
|
|
11
|
+
* isNumber({}) // false
|
|
12
|
+
* ```
|
|
6
13
|
*/
|
|
7
14
|
const isNumber = (v: unknown): v is number => typeof v === 'number'
|
|
8
15
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isObject } from './is-object.js'
|
|
4
|
+
|
|
5
|
+
describe('is object', () => {
|
|
6
|
+
it('should return true for objects', () => {
|
|
7
|
+
expect(isObject({})).toBe(true)
|
|
8
|
+
expect(isObject([])).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-objects', () => {
|
|
12
|
+
expect(isObject('hello')).toBe(false)
|
|
13
|
+
expect(isObject(123)).toBe(false)
|
|
14
|
+
expect(isObject(null)).toBe(false)
|
|
15
|
+
expect(isObject(undefined)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
package/src/is-object.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isObject checks if the value is an object.
|
|
3
|
+
*
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is object} returns true if the value is an object or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isObject({}) // true
|
|
10
|
+
* isObject([]) // true
|
|
11
|
+
* isObject(null) // false
|
|
12
|
+
* isObject('hello') // false
|
|
13
|
+
* isObject(42) // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const isObject = (v: unknown): v is object => typeof v === 'object' && v !== null
|
|
17
|
+
|
|
18
|
+
export { isObject }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isPlainObject } from './is-plain-object.js'
|
|
4
|
+
|
|
5
|
+
describe('is plain object', () => {
|
|
6
|
+
it('should return true for plain objects', () => {
|
|
7
|
+
expect(isPlainObject({})).toBe(true)
|
|
8
|
+
expect(isPlainObject(Object.create(null))).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return false for non-plain objects', () => {
|
|
12
|
+
expect(isPlainObject([])).toBe(false)
|
|
13
|
+
expect(isPlainObject(null)).toBe(false)
|
|
14
|
+
expect(isPlainObject(undefined)).toBe(false)
|
|
15
|
+
expect(isPlainObject('hello')).toBe(false)
|
|
16
|
+
expect(isPlainObject(42)).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
})
|