Skip to content

Commit

Permalink
Add support for numbers and symbols to entriesFromList #492
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jul 4, 2024
1 parent 5869f95 commit 2edbd22
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
5 changes: 5 additions & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add support for async schemas to `entriesFromList` util
- Add support for numbers and symbols to `entriesFromList` util (issue #492)

## v0.35.0 (June 25, 2024)

- Increase argument limit of `pipe` and `pipeAsync` method (issue #643)
Expand Down
23 changes: 17 additions & 6 deletions library/src/utils/entriesFromList/entriesFromList.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { describe, expectTypeOf, test } from 'vitest';
import { string } from '../../schemas/index.ts';
import { arrayAsync, string } from '../../schemas/index.ts';
import { entriesFromList } from './entriesFromList.ts';

describe('entriesFromList', () => {
test('should return object entries', () => {
const schema = string();
expectTypeOf(entriesFromList(['foo', 'bar', 'baz'], schema)).toEqualTypeOf<
Record<'foo' | 'bar' | 'baz', typeof schema>
>();
describe('should return object entries', () => {
const symbol = Symbol();

test('for sync schemas', () => {
const schema = string();
expectTypeOf(entriesFromList(['foo', 123, symbol], schema)).toEqualTypeOf<
Record<'foo' | 123 | typeof symbol, typeof schema>
>();
});

test('for async schemas', () => {
const schema = arrayAsync(string());
expectTypeOf(entriesFromList(['foo', 123, symbol], schema)).toEqualTypeOf<
Record<'foo' | 123 | typeof symbol, typeof schema>
>();
});
});
});
27 changes: 20 additions & 7 deletions library/src/utils/entriesFromList/entriesFromList.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { describe, expect, test } from 'vitest';
import { string } from '../../schemas/index.ts';
import { arrayAsync, string } from '../../schemas/index.ts';
import { entriesFromList } from './entriesFromList.ts';

describe('entriesFromList', () => {
test('should return object entries', () => {
const schema = string();
expect(entriesFromList(['foo', 'bar', 'baz'], schema)).toEqual({
foo: schema,
bar: schema,
baz: schema,
describe('should return object entries', () => {
const symbol = Symbol();

test('for sync schemas', () => {
const schema = string();
expect(entriesFromList(['foo', 123, symbol], schema)).toEqual({
foo: schema,
[123]: schema,
[symbol]: schema,
});
});

test('for async schemas', () => {
const schema = arrayAsync(string());
expect(entriesFromList(['foo', 123, symbol], schema)).toEqual({
foo: schema,
[123]: schema,
[symbol]: schema,
});
});
});
});
12 changes: 9 additions & 3 deletions library/src/utils/entriesFromList/entriesFromList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { BaseIssue, BaseSchema } from '../../types/index.ts';
import type {
BaseIssue,
BaseSchema,
BaseSchemaAsync,
} from '../../types/index.ts';

/**
* Creates a object entries definition from a list of keys and a schema.
Expand All @@ -9,8 +13,10 @@ import type { BaseIssue, BaseSchema } from '../../types/index.ts';
* @returns The object entries.
*/
export function entriesFromList<
const TList extends readonly string[],
const TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>,
const TList extends readonly (string | number | symbol)[],
const TSchema extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(list: TList, schema: TSchema): Record<TList[number], TSchema> {
// @ts-expect-error
const entries: Record<TList[number], TSchema> = {};
Expand Down

0 comments on commit 2edbd22

Please sign in to comment.