Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make literals work with null #698

Merged
merged 3 commits into from
Jul 11, 2023
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
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ export function literals(): never;
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function literals<T extends Atomic.Type>(): T[];
export function literals<T extends Atomic.Type | null>(): T[];

/**
* @internal
Expand Down
6 changes: 4 additions & 2 deletions src/programmers/LiteralsProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ export namespace LiteralsProgrammer {
throw new Error(ErrorMessages.ONLY);
},
})(new MetadataCollection())(type);
const values: Set<Atomic.Type> = new Set([
const values: Set<Atomic.Type | null> = new Set([
...ArrayUtil.flat<Atomic.Type>(meta.constants.map((c) => c.values)),
...(meta.atomics.filter((type) => type === "boolean").length
? [true, false]
: []),
...meta.nullable ? [null] : []
]);
return ts.factory.createAsExpression(
ts.factory.createArrayLiteralExpression(
[...values].map((v) =>
typeof v === "boolean"
v === null ? ts.factory.createNull()
: typeof v === "boolean"
? v
? ts.factory.createTrue()
: ts.factory.createFalse()
Expand Down
13 changes: 13 additions & 0 deletions test/features/literals/test_literals_WithNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typia from '../../../src';
import { _test_literals } from '../../internal/_test_literals';

const areArraysEqual = <T>(a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]);

export const test_literals_WithNull = _test_literals(
'WithNull',
() => 'A' as 'A' | 'B' | null,
(input) => {
const generatedArray = typia.literals<typeof input>();
return areArraysEqual(generatedArray, ['A', 'B', null]);
}
);
10 changes: 10 additions & 0 deletions test/features/literals/test_literals_WithStrings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typia from '../../../src';
import { _test_literals } from '../../internal/_test_literals';

const areArraysEqual = <T>(a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]);

export const test_literals_WithStrings = _test_literals(
'WithStrings',
() => 'A' as 'A' | 'B',
(input) => areArraysEqual(typia.literals<typeof input>(), ['A', 'B'])
);
5 changes: 5 additions & 0 deletions test/internal/_test_literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function _test_literals<T>(name: string, generator: () => T, validator: (input: T) => boolean): () => void {
return () => {
if (validator(generator()) === false) throw new Error(`Bug on typia.literals() for ${ name }: array is different than expected`);
};
}