Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"url": "https://github.com/webdeveric/utils/issues"
},
"homepage": "https://github.com/webdeveric/utils/#readme",
"packageManager": "pnpm@10.5.0+sha512.11106a5916c7406fe4b8cb8e3067974b8728f47308a4f5ac5e850304afa6f57e2847d7950dfe78877d8d36bfb401d381c4215db3a4c3547ffa63c14333a6fa51",
"packageManager": "pnpm@10.6.1+sha512.40ee09af407fa9fbb5fbfb8e1cb40fbb74c0af0c3e10e9224d7b53c7658528615b2c92450e74cfad91e3a2dcafe3ce4050d80bda71d757756d2ce2b66213e9a3",
"scripts": {
"clean": "rimraf ./dist/",
"prebuild": "pnpm clean",
Expand All @@ -85,28 +85,28 @@
},
"prettier": "@webdeveric/prettier-config",
"devDependencies": {
"@commitlint/config-conventional": "^19.7.1",
"@commitlint/types": "^19.5.0",
"@types/node": "^22.13.8",
"@vitest/coverage-v8": "^3.0.7",
"@commitlint/config-conventional": "^19.8.0",
"@commitlint/types": "^19.8.0",
"@types/node": "^22.13.10",
"@vitest/coverage-v8": "^3.0.8",
"@webdeveric/eslint-config-ts": "^0.11.0",
"@webdeveric/prettier-config": "^0.3.0",
"commitlint": "^19.7.1",
"commitlint": "^19.8.0",
"commitlint-plugin-cspell": "^0.1.1",
"conventional-changelog-conventionalcommits": "^8.0.0",
"cspell": "^8.17.5",
"eslint": "^8.57.1",
"eslint-config-prettier": "^10.0.2",
"eslint-config-prettier": "^10.1.1",
"eslint-import-resolver-typescript": "^3.8.3",
"eslint-plugin-import": "^2.31.0",
"husky": "^9.1.7",
"jsdom": "^26.0.0",
"lint-staged": "^15.4.3",
"prettier": "^3.5.2",
"prettier": "^3.5.3",
"rimraf": "^6.0.1",
"semantic-release": "^24.2.3",
"typescript": "^5.8.2",
"validate-package-exports": "^0.8.0",
"vitest": "^3.0.7"
"vitest": "^3.0.8"
}
}
709 changes: 355 additions & 354 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion src/predicate-factory/createIsEnumPredicate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import type { TypePredicateFn } from '../types/functions.js';
import type { UnknownRecord } from '../types/records.js';

/**
* @internal
*/
const getEnumValues = <T extends UnknownRecord>(enumObject: T): Set<unknown> => {
const entries = Object.entries(enumObject);

const ignoreKeys = entries.reduce((keys, [, value]) => {
if (typeof value === 'number') {
keys.add(String(value));
}

return keys;
}, new Set<string>());

return entries.reduce((values, [key, value]) => {
if (!ignoreKeys.has(key)) {
values.add(value);
}

return values;
}, new Set<unknown>());
};

/**
* Create a type predicate function that checks if the input is a member of the TypeScript `enum`
*/
export const createIsEnumPredicate = <T extends UnknownRecord>(enumObject: T): TypePredicateFn<T[keyof T]> => {
const values = new Set(Object.values(enumObject));
const values = getEnumValues(enumObject);

return (input: unknown): input is T[keyof T] => values.has(input);
};
1 change: 1 addition & 0 deletions src/predicate-factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './createObjectShapePredicate.js';
export * from './createStringLengthRangePredicate.js';
export * from './createStringMatchingPredicate.js';
export * from './everyItem.js';
export * from './is.js';
export * from './maybeArray.js';
export * from './maybeNull.js';
export * from './maybeUndefined.js';
7 changes: 7 additions & 0 deletions src/predicate-factory/is.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { TypePredicateFn } from '../types/functions.js';
import type { Pretty, Writable } from '../types/utils.js';

export const is =
<const T extends unknown[]>(...options: T): TypePredicateFn<Pretty<Writable<T[number]>>> =>
(input: unknown): input is T[number] =>
options.some((option) => Object.is(input, option));
62 changes: 62 additions & 0 deletions test/predicate-factory/createIsEnumPredicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect } from 'vitest';

import { createIsEnumPredicate } from '../../src/predicate-factory/createIsEnumPredicate.js';

describe('createIsEnumPredicate()', () => {
describe('enum with string values', () => {
enum StringEnum {
A = 'a',
B = 'b',
C = 'c',
}

const fn = createIsEnumPredicate(StringEnum);

it.each(Object.values(StringEnum))('Returns true for valid input: %s', (value) => {
expect(fn(value)).toBeTruthy();
});

it.each(Object.keys(StringEnum))('Returns false for invalid input: %s', (value) => {
expect(fn(value)).toBeFalsy();
});
});

describe('enum with number values', () => {
enum Demo {
Demo = 'Demo',
Example = 'Example',
One = 1,
Two = 2,
Pi = Math.PI,
NegativeInfinity = Number.NEGATIVE_INFINITY,
PositiveInfinity = Number.POSITIVE_INFINITY,
NotANumber = Number.NaN,
}

const fn = createIsEnumPredicate(Demo);

it('Returns a type predicate function', () => {
expect(fn).toBeInstanceOf(Function);
});

it.each([
Demo.Demo,
Demo.Example,
Demo.One,
Demo.Two,
Demo.Pi,
Demo.NegativeInfinity,
Demo.PositiveInfinity,
Demo.NotANumber,
])('Returns true for valid input: %s', (value) => {
expect(fn(value)).toBeTruthy();
});

it.each([1, 2, Math.PI, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NaN].map(String))(
'Returns false for invalid input: "%s"',
(value) => {
expect(fn(value)).toBeFalsy();
},
);
});
});
31 changes: 31 additions & 0 deletions test/predicate-factory/is.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest';

import { is } from '../../src/predicate-factory/is.js';

describe('is()', () => {
it('Returns a type predicate function', () => {
const fn = is('test', 123);

expect(fn('test')).toBeTruthy();

expect(fn(123)).toBeTruthy();

expect(fn('bad input')).toBeFalsy();
});

it('Empty args gets a predicate function that always returns false', () => {
expect(is()('test')).toBeFalsy();
});

it('Uses Object.is()', () => {
const now = new Date();

const fn = is('test', now);

expect(fn('abc123')).toBeFalsy();

expect(fn(new Date())).toBeFalsy();

expect(fn(now)).toBeTruthy();
});
});
18 changes: 0 additions & 18 deletions test/type-predicate-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@ import { describe, it, expect, vi } from 'vitest';

import { isBoolean } from '../src/predicate/isBoolean.js';
import {
createIsEnumPredicate,
createStringMatchingPredicate,
everyItem,
maybeArray,
maybeNull,
maybeUndefined,
} from '../src/predicate-factory/index.js';

describe('createIsEnumPredicate()', () => {
enum Demo {
Demo = 'Demo',
}

it('Returns a type predicate function', () => {
expect(createIsEnumPredicate(Demo)).toBeInstanceOf(Function);
});

it('Predicate function checks if input is a member of the enum', () => {
const fn = createIsEnumPredicate(Demo);

expect(fn(Demo.Demo)).toBeTruthy();
expect(fn('Fail')).toBeFalsy();
});
});

describe('createStringMatchingPredicate()', () => {
const pattern = /.+/;

Expand Down
Loading