chore: workspace improvements (#282)

This commit is contained in:
rahim
2026-01-04 01:39:26 +11:00
committed by GitHub
parent d74e4e6701
commit 2901593085
77 changed files with 2085 additions and 573 deletions
+1
View File
@@ -0,0 +1 @@
export * from './predicate';
+48
View File
@@ -0,0 +1,48 @@
export function isString(value: unknown): value is string {
return typeof value === 'string';
}
export function isNumber(value: unknown): value is number {
return typeof value === 'number';
}
export function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
}
export function isFunction(value: unknown): value is (...args: any[]) => any {
return typeof value === 'function';
}
export function isNull(value: unknown): value is null {
return value === null;
}
export function isUndefined(value: unknown): value is undefined {
return typeof value === 'undefined';
}
export function isNil(value: unknown): value is null | undefined {
return value == null;
}
export function isPromise(value: unknown): value is Promise<any> {
return value instanceof Promise;
}
/**
* Check if a value is an object, excluding null.
*/
export function isObject(value: unknown): value is object {
return value !== null && typeof value === 'object';
}
/**
* Check if a value is an AbortError.
*/
export function isAbortError(value: unknown): value is Error {
return (
value instanceof Error
&& value.name === 'AbortError'
);
}
@@ -0,0 +1,203 @@
import { describe, expect, it } from 'vitest';
import {
isAbortError,
isBoolean,
isFunction,
isNil,
isNull,
isNumber,
isObject,
isPromise,
isString,
isUndefined,
} from '../predicate';
describe('predicate', () => {
describe('isString', () => {
it('returns true for strings', () => {
expect(isString('')).toBe(true);
expect(isString('hello')).toBe(true);
expect(isString('123')).toBe(true);
});
it('returns false for non-strings', () => {
expect(isString(123)).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString({})).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});
describe('isNumber', () => {
it('returns true for numbers', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-1)).toBe(true);
expect(isNumber(3.14)).toBe(true);
expect(isNumber(Number.NaN)).toBe(true);
expect(isNumber(Infinity)).toBe(true);
});
it('returns false for non-numbers', () => {
expect(isNumber('123')).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isNumber(undefined)).toBe(false);
expect(isNumber({})).toBe(false);
expect(isNumber([])).toBe(false);
expect(isNumber(true)).toBe(false);
});
});
describe('isBoolean', () => {
it('returns true for booleans', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);
});
it('returns false for non-booleans', () => {
expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean(undefined)).toBe(false);
expect(isBoolean({})).toBe(false);
});
});
describe('isFunction', () => {
it('returns true for functions', () => {
expect(isFunction(() => {})).toBe(true);
expect(isFunction(() => {})).toBe(true);
expect(isFunction(async () => {})).toBe(true);
expect(isFunction(class {})).toBe(true);
expect(isFunction(Array.isArray)).toBe(true);
});
it('returns false for non-functions', () => {
expect(isFunction({})).toBe(false);
expect(isFunction([])).toBe(false);
expect(isFunction('function')).toBe(false);
expect(isFunction(null)).toBe(false);
expect(isFunction(undefined)).toBe(false);
});
});
describe('isNull', () => {
it('returns true for null', () => {
expect(isNull(null)).toBe(true);
});
it('returns false for non-null', () => {
expect(isNull(undefined)).toBe(false);
expect(isNull(0)).toBe(false);
expect(isNull('')).toBe(false);
expect(isNull(false)).toBe(false);
expect(isNull({})).toBe(false);
});
});
describe('isUndefined', () => {
it('returns true for undefined', () => {
expect(isUndefined(undefined)).toBe(true);
expect(isUndefined(void 0)).toBe(true);
});
it('returns false for non-undefined', () => {
expect(isUndefined(null)).toBe(false);
expect(isUndefined(0)).toBe(false);
expect(isUndefined('')).toBe(false);
expect(isUndefined(false)).toBe(false);
expect(isUndefined({})).toBe(false);
});
});
describe('isNil', () => {
it('returns true for null and undefined', () => {
expect(isNil(null)).toBe(true);
expect(isNil(undefined)).toBe(true);
});
it('returns false for non-nil values', () => {
expect(isNil(0)).toBe(false);
expect(isNil('')).toBe(false);
expect(isNil(false)).toBe(false);
expect(isNil({})).toBe(false);
expect(isNil([])).toBe(false);
expect(isNil(Number.NaN)).toBe(false);
});
});
describe('isPromise', () => {
it('returns true for promises', () => {
expect(isPromise(Promise.resolve())).toBe(true);
// eslint-disable-next-line prefer-promise-reject-errors
expect(isPromise(Promise.reject().catch(() => {}))).toBe(true);
expect(isPromise(new Promise(() => {}))).toBe(true);
});
it('returns false for non-promises', () => {
expect(isPromise({})).toBe(false);
expect(isPromise({ then: () => {} })).toBe(false); // thenable but not Promise
expect(isPromise(null)).toBe(false);
expect(isPromise(undefined)).toBe(false);
expect(isPromise(() => {})).toBe(false);
});
});
describe('isObject', () => {
it('returns true for objects', () => {
expect(isObject({})).toBe(true);
expect(isObject([])).toBe(true);
expect(isObject(new Date())).toBe(true);
expect(isObject(/regex/)).toBe(true);
expect(isObject(new Map())).toBe(true);
expect(isObject(new Set())).toBe(true);
});
it('returns false for null', () => {
expect(isObject(null)).toBe(false);
});
it('returns false for primitives', () => {
expect(isObject(undefined)).toBe(false);
expect(isObject('string')).toBe(false);
expect(isObject(123)).toBe(false);
expect(isObject(true)).toBe(false);
// eslint-disable-next-line symbol-description
expect(isObject(Symbol())).toBe(false);
});
it('returns false for functions', () => {
expect(isObject(() => {})).toBe(false);
});
});
describe('isAbortError', () => {
it('returns true for AbortError', () => {
const error = new DOMException('Aborted', 'AbortError');
expect(isAbortError(error)).toBe(true);
});
it('returns true for custom AbortError', () => {
const error = new Error('Aborted');
error.name = 'AbortError';
expect(isAbortError(error)).toBe(true);
});
it('returns false for other errors', () => {
expect(isAbortError(new Error('test'))).toBe(false);
expect(isAbortError(new TypeError('test'))).toBe(false);
expect(isAbortError(new RangeError('test'))).toBe(false);
});
it('returns false for non-errors', () => {
expect(isAbortError(null)).toBe(false);
expect(isAbortError(undefined)).toBe(false);
expect(isAbortError('AbortError')).toBe(false);
expect(isAbortError({ name: 'AbortError' })).toBe(false);
});
});
});