mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
feat(store): add reactive state primitives (#311)
This commit is contained in:
@@ -37,12 +37,18 @@ export function isObject(value: unknown): value is object {
|
||||
return value !== null && typeof value === 'object';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a value is a plain object (not a class instance like Date, Map, etc).
|
||||
*/
|
||||
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
if (!isObject(value)) return false;
|
||||
const proto = Object.getPrototypeOf(value);
|
||||
return proto === null || proto === Object.prototype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a value is an AbortError.
|
||||
*/
|
||||
export function isAbortError(value: unknown): value is Error {
|
||||
return (
|
||||
value instanceof Error
|
||||
&& value.name === 'AbortError'
|
||||
);
|
||||
return value instanceof Error && value.name === 'AbortError';
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
isNull,
|
||||
isNumber,
|
||||
isObject,
|
||||
isPlainObject,
|
||||
isPromise,
|
||||
isString,
|
||||
isUndefined,
|
||||
@@ -175,6 +176,44 @@ describe('predicate', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('isPlainObject', () => {
|
||||
it('returns true for plain objects', () => {
|
||||
expect(isPlainObject({})).toBe(true);
|
||||
expect(isPlainObject({ a: 1 })).toBe(true);
|
||||
expect(isPlainObject(Object.create(null))).toBe(true);
|
||||
expect(isPlainObject(new Object())).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for arrays', () => {
|
||||
expect(isPlainObject([])).toBe(false);
|
||||
expect(isPlainObject([1, 2, 3])).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for class instances', () => {
|
||||
class Foo {}
|
||||
expect(isPlainObject(new Foo())).toBe(false);
|
||||
expect(isPlainObject(new Date())).toBe(false);
|
||||
expect(isPlainObject(new Map())).toBe(false);
|
||||
expect(isPlainObject(new Set())).toBe(false);
|
||||
expect(isPlainObject(/regex/)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for primitives', () => {
|
||||
expect(isPlainObject(null)).toBe(false);
|
||||
expect(isPlainObject(undefined)).toBe(false);
|
||||
expect(isPlainObject('string')).toBe(false);
|
||||
expect(isPlainObject(123)).toBe(false);
|
||||
expect(isPlainObject(true)).toBe(false);
|
||||
// eslint-disable-next-line symbol-description
|
||||
expect(isPlainObject(Symbol())).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for functions', () => {
|
||||
expect(isPlainObject(() => {})).toBe(false);
|
||||
expect(isPlainObject(() => {})).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isAbortError', () => {
|
||||
it('returns true for AbortError', () => {
|
||||
const error = new DOMException('Aborted', 'AbortError');
|
||||
|
||||
Reference in New Issue
Block a user