@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,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* isPlainObject checks if a value is a plain object, that is,
|
|
3
|
+
* an object created by the Object constructor or with a null prototype.
|
|
4
|
+
*
|
|
5
|
+
* @param v value to check.
|
|
6
|
+
* @returns {v is Record<PropertyKey, unknown>} returns true if the value is a plain object or otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* isPlainObject({}) // true
|
|
11
|
+
* isPlainObject(Object.create(null)) // true
|
|
12
|
+
* isPlainObject([]) // false
|
|
13
|
+
* isPlainObject(null) // false
|
|
14
|
+
* isPlainObject('hello') // false
|
|
15
|
+
* isPlainObject(42) // false
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
const isPlainObject = (v: unknown): v is Record<PropertyKey, unknown> => {
|
|
19
|
+
if (typeof v !== 'object' || v === null) {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const proto = Object.getPrototypeOf(v) as unknown
|
|
24
|
+
|
|
25
|
+
return proto === Object.prototype || proto === null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { isPlainObject }
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isPrimitive } from './is-primitive.js'
|
|
4
|
+
|
|
5
|
+
describe('is primitive', () => {
|
|
6
|
+
it('should return true for primitives', () => {
|
|
7
|
+
expect(isPrimitive(42)).toBe(true)
|
|
8
|
+
expect(isPrimitive('hello')).toBe(true)
|
|
9
|
+
expect(isPrimitive(true)).toBe(true)
|
|
10
|
+
expect(isPrimitive(null)).toBe(true)
|
|
11
|
+
expect(isPrimitive(undefined)).toBe(true)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return false for non-primitives', () => {
|
|
15
|
+
expect(isPrimitive({})).toBe(false)
|
|
16
|
+
expect(isPrimitive([])).toBe(false)
|
|
17
|
+
expect(
|
|
18
|
+
isPrimitive(() => {
|
|
19
|
+
void 0
|
|
20
|
+
}),
|
|
21
|
+
).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Primitive type represents the primitive values.
|
|
3
|
+
*/
|
|
4
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined
|
|
5
|
+
|
|
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
|
+
const isPrimitive = (v: unknown): v is Primitive => v === null || (typeof v !== 'object' && typeof v !== 'function')
|
|
25
|
+
|
|
26
|
+
export { isPrimitive }
|
package/src/is-string.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* isString checks if the provided value is a string.
|
|
3
3
|
*
|
|
4
|
-
* @param v value to check
|
|
5
|
-
* @returns
|
|
4
|
+
* @param v value to check.
|
|
5
|
+
* @returns {v is string} returns true if v is a string or otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* isString('hello') // true
|
|
10
|
+
* isString(123) // false
|
|
11
|
+
* isString({}) // false
|
|
12
|
+
* ```
|
|
6
13
|
*/
|
|
7
14
|
const isString = (v: unknown): v is string => typeof v === 'string'
|
|
8
15
|
|