Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

Commit

Permalink
Add advanced/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed May 10, 2017
1 parent be757c0 commit c7eb5f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/advanced/create-curried-function-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {Type} from '../elements/type';
import {TypeAssertion} from '../elements/type-assertion';
import {FunctionType} from '../elements/types/function-type';
import {GenericType} from '../elements/types/generic-type';
import {LiteralType} from '../elements/types/literal-type';
import {ObjectType} from '../elements/types/object-type';
import {TypedType} from '../elements/types/typed-type';
import {left_pad} from '../utils/left-pad';
import {get_default_placeholder_name_generator} from './utils/get-default-placeholder-name-generator';
import {get_default_select_type_generator} from './utils/get-default-select-type-generator';
import {get_default_type_name_generator} from './utils/get-default-type-name-generator';

export interface ICreateCurriedFunctionTypesRequiredParameters {
name: string;
Expand Down Expand Up @@ -53,16 +54,9 @@ export const create_curried_function_types = ({
placeholder = null,
selectable = false,
select_generic_name = 'X',
generate_select_type = (current_type: FunctionType): Type =>
new LiteralType({value: current_type.parameters.parameters
.map((parameter: Parameter): string =>
(parameter.parameters.type === placeholder)
? '0'
: '1')
.join('')}),
generate_type_name = (index: number): string =>
(`${name}_${left_pad(index.toString(2), type.parameters.parameters.length, '0')}`),
generate_placeholder_name = (parameter_name: string): string => `_${parameter_name}`,
generate_select_type = get_default_select_type_generator(placeholder),
generate_type_name = get_default_type_name_generator(name, type),
generate_placeholder_name = get_default_placeholder_name_generator(),
}: ICreateCurriedFunctionTypesParameters): TypeDeclaration[] => {
const curried_type_declarations = [...new Array(2 ** type.parameters.parameters.length)]
.map((_value: any, index: number) => new TypeDeclaration({name: generate_type_name(index)}));
Expand All @@ -76,7 +70,7 @@ export const create_curried_function_types = ({

const return_type = type.parameters.return;
if (return_type instanceof TypeAssertion) {
throw new Error('AssertionType is not supported in create_curried_function_types');
throw new Error('TypeAssertion is not supported in create_curried_function_types');
}

curried_type_declarations.forEach((type_declaration: TypeDeclaration, index: number) => {
Expand Down
3 changes: 3 additions & 0 deletions src/advanced/utils/get-default-placeholder-name-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// tslint:disable-next-line:typedef
export const get_default_placeholder_name_generator = () =>
(parameter_name: string): string => `_${parameter_name}`;
13 changes: 13 additions & 0 deletions src/advanced/utils/get-default-select-type-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Parameter} from '../../elements/parameter';
import {Type} from '../../elements/type';
import {FunctionType} from '../../elements/types/function-type';
import {LiteralType} from '../../elements/types/literal-type';

// tslint:disable-next-line:typedef
export const get_default_select_type_generator = (placeholder: Type | null) =>
(type: FunctionType): Type =>
new LiteralType({
value: type.parameters.parameters
.map((parameter: Parameter): string => (parameter.parameters.type === placeholder) ? '0' : '1')
.join(''),
});
8 changes: 8 additions & 0 deletions src/advanced/utils/get-default-type-name-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {FunctionType} from '../../elements/types/function-type';
import {left_pad} from '../../utils/left-pad';

// tslint:disable-next-line:typedef
export const get_default_type_name_generator = (name: string, type: FunctionType) =>
(index: number): string =>
// tslint:disable-next-line:no-magic-numbers
(`${name}_${left_pad(index.toString(2), type.parameters.parameters.length, '0')}`);

0 comments on commit c7eb5f3

Please sign in to comment.