Skip to content

Commit

Permalink
feat: function, class, enum parser and parseAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jun 10, 2024
1 parent 0758701 commit 717b489
Show file tree
Hide file tree
Showing 25 changed files with 613 additions and 360 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pnpm install tsukiko
- ArrayParser
- TupleParser
- ObjectParser
- EnumParser
- LiteralParser
- IntersectionParser
- UnionParser
Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Todo list

- [x] Function Parser
- [x] Instance Parser (instanceof)
- [x] Instance Parser (instance of)
- [x] Class (Constructor) Parser (extends)
- [x] Promise Parser
- [x] Tools: async parse
- [ ] export json schema
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsukiko",
"version": "1.2.6",
"version": "1.3.0",
"description": "Dynamic Types Cheker At Runtime Which Develop",
"main": "dist/index.js",
"scripts": {
Expand Down
82 changes: 51 additions & 31 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,96 @@
import Parser from './parser';
import {
AnyParser,
ArrayParser,
BooleanParser,
NeverParser,
NullParser,
NumberParser,
ObjectParser,
StringParser,
TupleParser,
UndefinedParser,
UnknownParser,
AnyParser,
ArrayParser,
BooleanParser,
NeverParser,
NullParser,
NumberParser,
ObjectParser,
StringParser,
TupleParser,
UndefinedParser,
UnknownParser,
CustomParser,
IntersectionParser,
LiteralParser,
UnionParser,
EnumParser,
ClassParser,
FunctionParser
} from './parsers';
import { IonParserConfig, ObjectParserConfig, ParserFunction, TupleParserConfig } from './types';
import { CustomParser, IntersectionParser, LiteralParser, UnionParser } from './parsers/advance';
import { Constructor, IonParserConfig, ObjectParserConfig, ParserFunction, TupleParserConfig } from './types';

export function numberFactory() {
return new NumberParser();
return new NumberParser();
}

export function stringFactory() {
return new StringParser();
return new StringParser();
}

export function booleanFactory() {
return new BooleanParser();
return new BooleanParser();
}

export function undefinedFactory() {
return new UndefinedParser();
return new UndefinedParser();
}

export function nullFactory() {
return new NullParser();
return new NullParser();
}

export function anyFactory() {
return new AnyParser();
return new AnyParser();
}

export function unknownFactory() {
return new UnknownParser();
return new UnknownParser();
}

export function neverFactory() {
return new NeverParser();
return new NeverParser();
}

export function arrayFactory<T extends Parser<unknown>>(types: T) {
return new ArrayParser<T>(types);
return new ArrayParser<T>(types);
}

export function tupleFactory<T extends TupleParserConfig>(types: T) {
return new TupleParser<T>(types);
return new TupleParser<T>(types);
}

export function objectFactory<T extends ObjectParserConfig>(types: T) {
return new ObjectParser<T>(types);
const emptyObject = {};

export function objectFactory<T extends ObjectParserConfig = typeof emptyObject>(types?: T) {
return new ObjectParser<T>(types ?? ({} as T));
}

export function literalFactory<T extends string | number>(values: T) {
return new LiteralParser<T>(values);
return new LiteralParser<T>(values);
}

export function intersectionFactory<T extends IonParserConfig>(...values: [...T]) {
return new IntersectionParser<T>(...values);
}

export function intersectionFactory<T extends IonParserConfig>(values: T) {
return new IntersectionParser<T>(values);
export function unionFactory<T extends IonParserConfig>(...values: [...T]) {
return new UnionParser<T>(...values);
}

export function unionFactory<T extends IonParserConfig>(values: T) {
return new UnionParser<T>(values);
export function enumFactory<T extends Parser<string | number>[]>(...values: T) {
return new EnumParser<T>(...values);
}

export function customFactory<T>(handle: ParserFunction<boolean>) {
return new CustomParser<T>(handle);
return new CustomParser<T>(handle);
}

export function functionFactory<T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown>() {
return new FunctionParser<T>();
}

export function classFactory<T extends Constructor = Constructor>() {
return new ClassParser<T>();
}
28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
* @Blog: https://hotaru.icu
* @Date: 2023-11-24 18:43:20
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2023-12-10 15:38:50
* @LastEditTime: 2024-06-10 18:41:03
*/
import {
anyFactory,
arrayFactory,
booleanFactory,
classFactory,
customFactory,
enumFactory,
functionFactory,
intersectionFactory,
literalFactory,
neverFactory,
Expand All @@ -32,10 +35,11 @@ import type {
ParserFunction
} from './types';
import { DEFAULT_LANG } from './utils/lang';
import { Constructor, IonParserConfig } from './types';
import { Parser } from './parser';

export * from './factory';
export * from './types';
export * from './parsers/advance';
export * from './parsers';
export * from './parser';
export * from './utils/error';
Expand All @@ -56,7 +60,10 @@ export namespace Tsu {
export const Literal = literalFactory;
export const Intersection = intersectionFactory;
export const Union = unionFactory;
export const Enum = enumFactory;
export const Custom = customFactory;
export const Function = functionFactory;
export const Class = classFactory;
export type infer<T> = ParserInfer<T>;
export type inferObject<T extends ObjectParserConfig> = ObjectParserInfer<T>;
export type inferTuple<T extends TupleParserConfig> = TupleParserInfer<T>;
Expand Down Expand Up @@ -100,14 +107,23 @@ export function tsuFactory(lang: langType = DEFAULT_LANG): typeof Tsu {
Literal(value) {
return Tsu.Literal(value).lang(lang);
},
Intersection(values) {
return Tsu.Intersection(values).lang(lang);
Intersection<T extends IonParserConfig>(...values: T) {
return Tsu.Intersection<T>(...values).lang(lang);
},
Union(value) {
return Tsu.Union(value).lang(lang);
Union<T extends IonParserConfig>(...value: T) {
return Tsu.Union<T>(...value).lang(lang);
},
Enum<T extends Parser<string | number>[]>(...values: T) {
return Tsu.Enum(...values).lang(lang);
},
Custom<T>(handle: ParserFunction<boolean>) {
return Tsu.Custom<T>(handle).lang(lang);
},
Function<T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown>() {
return functionFactory<T>().lang(lang);
},
Class<T extends Constructor = Constructor>() {
return classFactory<T>();
}
};
}
Expand Down
127 changes: 71 additions & 56 deletions src/lang/en_US.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
export default {
/* string */
not_string: 'Target is not a string',
not_a_email: 'Target string "%input%" is not an email',
not_a_domain: 'Target string "%input%" is not a domain',
not_a_url: 'Target string "%input%" is not a URL',
illegal_match_string: 'Target string "%input%" does not match the pattern %value%',
illegal_starts_with: 'Target string "%input%" does not start with %value%',
illegal_ends_with: 'Target string "%input%" does not end with %value%',
/* number */
not_number: 'Target is not a number',
not_integer_number: 'Target number "%input%" is not an integer',
not_odd_number: 'Target number "%input%" is not an odd number',
not_even_number: 'Target number "%input%" is not an even number',
not_natural_number: 'Target number "%input%" is not a natural number ( >= 0 )',
not_positive_number: 'Target number "%input%" is not a positive number ( > 0 )',
not_negative_number: 'Target number "%input%" is not a negative number ( < 0 )',
not_percentage: 'Target number "%input%" is not a percentage ( >= 0, <= 1 )',
too_bigger: 'Target number "%input%" is too big, should be < %value%',
too_bigger_has: 'Target number "%input%" is too big, should be <= %value%',
too_smaller: 'Target number "%input%" is too small, should be > %value%',
too_smaller_has: 'Target number "%input%" is too small, should be >= %value%',
is_a_NaN: 'Target number is a NaN',
/* boolean */
not_boolean: 'Target is not a boolean',
not_true: 'Target is not true',
not_false: 'Target is not false',
/* empty */
not_null: 'Target is not null',
not_undefined: 'Target is not undefined',
/* extends */
not_never: 'Target is not never',
/* stacks - array */
not_an_array: 'Target is not an array',
array_error: 'Target array error at %length%: %value%',
/* stacks - tuple */
not_a_tuple: 'Target is not a tuple',
illegal_tuple_length: 'Target tuple length should be %value%, not %input%',
tuple_error: 'Target tuple error at %length%: %value%',
/* stacks - object */
not_an_object: 'Target is not an object',
object_is_null: 'Target object is null',
object_is_an_array: 'Target object is an array',
object_keys_too_many: 'Strict mode: too many keys for target object, should be %value% or less, not %input%',
object_error: 'Target object error at %key%: %value%',
object_key_error: 'Target object key type error',
/* advance - intersection */
intersection_error_first: 'Intersection type first error: %value%',
intersection_error_second: 'Intersection type second error: %value%',
/* advance - union */
union_error: 'Union type error: %value1%, %value2%',
/* advance - literal */
literal_only: 'Literal types allow only strings and numbers',
literal_number_error: 'Target number cannot assign to %value%',
literal_string_error: 'Target string cannot assign to %value%',
/* advance - custom */
custom_error: 'Cannot pass custom validation: %value%',
/* string */
not_string: 'Target is not a string',
not_a_email: 'Target string "%input%" is not an email',
not_a_domain: 'Target string "%input%" is not a domain',
not_a_url: 'Target string "%input%" is not a URL',
illegal_match_string: 'Target string "%input%" does not match the pattern %value%',
illegal_starts_with: 'Target string "%input%" does not start with %value%',
illegal_ends_with: 'Target string "%input%" does not end with %value%',
/* number */
not_number: 'Target is not a number',
not_integer_number: 'Target number "%input%" is not an integer',
not_odd_number: 'Target number "%input%" is not an odd number',
not_even_number: 'Target number "%input%" is not an even number',
not_natural_number: 'Target number "%input%" is not a natural number ( >= 0 )',
not_positive_number: 'Target number "%input%" is not a positive number ( > 0 )',
not_negative_number: 'Target number "%input%" is not a negative number ( < 0 )',
not_percentage: 'Target number "%input%" is not a percentage ( >= 0, <= 1 )',
too_bigger: 'Target number "%input%" is too big, should be < %value%',
too_bigger_has: 'Target number "%input%" is too big, should be <= %value%',
too_smaller: 'Target number "%input%" is too small, should be > %value%',
too_smaller_has: 'Target number "%input%" is too small, should be >= %value%',
is_a_NaN: 'Target number is a NaN',
/* boolean */
not_boolean: 'Target is not a boolean',
not_true: 'Target is not true',
not_false: 'Target is not false',
/* empty */
not_null: 'Target is not null',
not_undefined: 'Target is not undefined',
/* extends */
not_never: 'Target is not never',
/* stacks - array */
not_an_array: 'Target is not an array',
array_error: 'Target array error at %length%: %value%',
/* stacks - tuple */
not_a_tuple: 'Target is not a tuple',
illegal_tuple_length: 'Target tuple length should be %value%, not %input%',
tuple_error: 'Target tuple error at %length%: %value%',
/* stacks - object */
not_an_object: 'Target is not an object',
object_is_null: 'Target object is null',
object_is_an_array: 'Target object is an array',
object_not_instance_of_constructor: 'Target object is not an instance of constructor',
object_keys_too_many: 'Strict mode: too many keys for target object, should be %value% or less, not %input%',
object_error: 'Target object error at %key%: %value%',
object_key_error: 'Target object key type error',
/* advance - intersection */
intersection_error_first: 'Intersection type first error: %value%',
intersection_error_second: 'Intersection type second error: %value%',
/* advance - union */
union_error: 'Union type error: %value1%, %value2%',
/* advance - literal */
literal_only: 'Literal types allow only strings and numbers',
literal_number_error: 'Target number cannot assign to %value%',
literal_string_error: 'Target string cannot assign to %value%',
/* advance - custom */
custom_error: 'Cannot pass custom validation: %value%',
/* standard - function */
not_a_function: 'Target is not a function',
not_a_constructor: 'Target is not a constructor',
not_an_async_function: 'Target is not an async function',
not_a_generator_function: 'Target is not a generator function',
not_an_async_generator_function: 'Target is not an async generator function',
not_an_arrow_function: 'Target is not an arrow function',
function_args_count_mismatch: 'Target function call arguments count mismatch, expected %expected%, got %actual%',
function_name_mismatch: 'Target function name mismatch: %value%',
/* standard - class */
not_a_class: 'Target is not a class',
class_args_count_mismatch: 'Target class constructor arguments count mismatch, expected %expected%, got %actual%',
class_name_mismatch: 'Target class name mismatch: %value%',
class_prototype_error: 'Target class does not inherit specified Constructor'
};
Loading

0 comments on commit 717b489

Please sign in to comment.