diff --git a/README.md b/README.md index 0cc84fc..0df51d5 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ import {TsTypeGenerator} from "@pghalliday/ts-type-generator"; new TsTypeGenerator() // TODO: add types here - .generate(resolve(__dirname, "../lib")); + .generate(resolve(__dirname, "../index.ts")); ``` You can then run this using `ts-node`: @@ -36,7 +36,7 @@ You can then run this using `ts-node`: ts-node ./types/src/index.ts ``` -As it stands this will not create any types or type guards as none have been defined. However, it will create a `./types/lib` directory and copy in some library files for use in generated type files. +As it stands this will not create any types or type guards as none have been defined. However, it will create a `./types/index.ts` module and copy in some utility functions for use in generated type guards. ### Adding types @@ -62,7 +62,7 @@ new TsTypeGenerator() .property("userId", stringType) .property("message", stringType) ) - .generate(resolve(__dirname, "../lib")); + .generate(resolve(__dirname, "../index.ts")); ``` This will generate 2 types equivalent to this: @@ -91,30 +91,21 @@ export function isMessage(value: unknown): value is Message { } ``` -These will be generated in their own files under the output `lib` directory. As such you can import them like this: +You can import them like this: ```typescript -import {User, isUser} from './types/lib/User' -import {Message, isMessage} from './types/lib/User' +import {User, isUser, Message, isMessage} from "./types"; ``` -So what's happening here. Well the main thing to know is that any number of types can be added and each type and the types they depend on will result in a generated type file. +So what's happening here. Well the main thing to know is that any number of types can be added and each type, and the types they depend on, will be added to the generated types module. -Types all have the same base `Type` class, so they can be used wherever a type is required. +Types all have the same base `Type` class, so they can be re-used wherever a type is required. -Some primitive types are provided as constants. Here we are using the `stringType` as an alias for `string`. We have to use an instance of `Type` so this has been created as a singleton. Using the `stringType` will result in a `String` type file being created with the following content: - -```typescript -export type StringTypeTs = string; - -export function isStringType(value: unknown): value is StringTypeTs { - ... -} -``` +Some primitive types are provided as constants. Here we are using the `stringType` as an alias for `string`. We have to use an instance of `Type` so this has been created as a singleton for convenience. ### Anonymous types -When defining types and sub types, you may not always care what they're called. As such, type names are always optional. As it happens, when the type file is generated, a name will also be generated but this is just an implementation detail. +When defining types and sub-types, you may not always care what they are called. As such, type names are always optional. As it happens, when the type file is generated, a name will also be generated but this is an internal implementation detail. Anonymous types will not be exported from the types module. For example to create a more complex structure where we only care about the top level type name: @@ -132,7 +123,7 @@ new TsTypeGenerator() .property("lastName", stringType) ) ) - .generate(resolve(__dirname, "../lib")); + .generate(resolve(__dirname, "../index.ts")); ``` Which will create a named type and type guard equivalent to: @@ -153,22 +144,20 @@ export function isUser(value: unknown): value is User { ### Type constants -The following `Type` constants are provided as primitive types: +The following `Type` constants are provided as convenience primitive types: - `stringType` - the `string` primitive - `numberType` - the `number` primitive - `booleanType` - the `boolean` primitive -The following are provided as convenience `Type` instances: +The following `Type` constants are provided as convenience primitive collection instances: -- `stringArrayType` - for arrays of `string` primitives -- `stringMapType` - for maps of `string` primitives -- `numberArrayType` - for arrays of `number` primitives -- `numberMapType` - for maps of `number` primitives -- `booleanArrayType` - for arrays of `boolean` primitives -- `booleanMapType` - for maps of `boolean` primitives - -NB. Only maps keyed by `string` are supported. +- `stringListType` - for lists of `string` primitives +- `stringDictionaryType` - for dictionaries of `string` primitives +- `numberListType` - for lists of `number` primitives +- `numberDictionaryType` - for dictionaries of `number` primitives +- `booleanListType` - for lists of `boolean` primitives +- `booleanDictionaryType` - for dictionaries of `boolean` primitives ### Type classes @@ -194,24 +183,22 @@ new UnionType(NAME?) ... ``` -#### `ArrayType` +#### `ListType` -To define an array type. +To define a list type. ```typescript -new ArrayType(TYPE, NAME?) +new ListType(TYPE, NAME?) ``` -#### `MapType` +#### `DictionaryType` -To define a map type. +To define a dictionary type. ```typescript -new MapType(TYPE, NAME?) +new DictionaryType(TYPE, NAME?) ``` -NB. Only `string` keys are supported for maps. - #### `StringLiteralType` To define a `string` literal type. diff --git a/files/isDictionaryOf.ts b/files/isDictionaryOf.ts new file mode 100644 index 0000000..dbbe226 --- /dev/null +++ b/files/isDictionaryOf.ts @@ -0,0 +1,6 @@ +function isDictionaryOf(obj: X, typeGuard: (value: unknown) => value is Y): obj is X & { [key: string]: Y } { + for (const key in obj) { + if (!typeGuard(obj[key])) return false; + } + return true; +} diff --git a/files/isMapOf.ts b/files/isMapOf.ts deleted file mode 100644 index 9e00b27..0000000 --- a/files/isMapOf.ts +++ /dev/null @@ -1,6 +0,0 @@ -function isMapOf(obj: X, typeGuard: (value: unknown) => value is Y): obj is X & { [key: string]: Y } { - for (const key in obj) { - if (!typeGuard(obj[key])) return false; - } - return true; -} diff --git a/src/TsTypeGenerator.ts b/src/TsTypeGenerator.ts index 30fa7ff..5b428d1 100644 --- a/src/TsTypeGenerator.ts +++ b/src/TsTypeGenerator.ts @@ -10,7 +10,7 @@ import isUndefined from "lodash/isUndefined" import {readFileSync} from "fs"; const HAS_OWN_PROPERTY_DEFINITION = readFileSync(join(FILES_DIR, 'hasOwnProperty.ts')).toString() -const IS_MAP_OF_DEFINITION = readFileSync(join(FILES_DIR, 'isMapOf.ts')).toString() +const IS_DICTIONARY_OF_DEFINITION = readFileSync(join(FILES_DIR, 'isDictionaryOf.ts')).toString() const EXPORT_PREFIX = 'export ' function collectTypes(types: Type[], typeMap: Record): Record { @@ -56,7 +56,7 @@ export class TsTypeGenerator { await file.write(type.getTypeGuardDefinition()) } await file.write(HAS_OWN_PROPERTY_DEFINITION) - await file.write(IS_MAP_OF_DEFINITION) + await file.write(IS_DICTIONARY_OF_DEFINITION) await file.close() } } diff --git a/src/constants/booleanArrayType.ts b/src/constants/booleanArrayType.ts deleted file mode 100644 index 412cf0b..0000000 --- a/src/constants/booleanArrayType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {ArrayType} from "../types"; -import {booleanType} from "./booleanType"; - -export const booleanArrayType = new ArrayType(booleanType) \ No newline at end of file diff --git a/src/constants/booleanDictionaryType.ts b/src/constants/booleanDictionaryType.ts new file mode 100644 index 0000000..b966cd9 --- /dev/null +++ b/src/constants/booleanDictionaryType.ts @@ -0,0 +1,4 @@ +import {DictionaryType} from "../types"; +import {booleanType} from "./booleanType"; + +export const booleanDictionaryType = new DictionaryType(booleanType) \ No newline at end of file diff --git a/src/constants/booleanListType.ts b/src/constants/booleanListType.ts new file mode 100644 index 0000000..23113e3 --- /dev/null +++ b/src/constants/booleanListType.ts @@ -0,0 +1,4 @@ +import {ListType} from "../types"; +import {booleanType} from "./booleanType"; + +export const booleanListType = new ListType(booleanType) \ No newline at end of file diff --git a/src/constants/booleanMapType.ts b/src/constants/booleanMapType.ts deleted file mode 100644 index 351fd2c..0000000 --- a/src/constants/booleanMapType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {MapType} from "../types"; -import {booleanType} from "./booleanType"; - -export const booleanMapType = new MapType(booleanType) \ No newline at end of file diff --git a/src/constants/index.ts b/src/constants/index.ts index 8dc9eb9..71d742f 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,9 +1,9 @@ export * from './stringType' export * from './numberType' export * from './booleanType' -export * from './stringArrayType' -export * from './numberArrayType' -export * from './booleanArrayType' -export * from './stringMapType' -export * from './numberMapType' -export * from './booleanMapType' +export * from './stringListType' +export * from './numberListType' +export * from './booleanListType' +export * from './stringDictionaryType' +export * from './numberDictionaryType' +export * from './booleanDictionaryType' diff --git a/src/constants/numberArrayType.ts b/src/constants/numberArrayType.ts deleted file mode 100644 index aab3e6b..0000000 --- a/src/constants/numberArrayType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {ArrayType} from "../types"; -import {numberType} from "./numberType"; - -export const numberArrayType = new ArrayType(numberType) \ No newline at end of file diff --git a/src/constants/numberDictionaryType.ts b/src/constants/numberDictionaryType.ts new file mode 100644 index 0000000..d72fe70 --- /dev/null +++ b/src/constants/numberDictionaryType.ts @@ -0,0 +1,4 @@ +import {DictionaryType} from "../types"; +import {numberType} from "./numberType"; + +export const numberDictionaryType = new DictionaryType(numberType) \ No newline at end of file diff --git a/src/constants/numberListType.ts b/src/constants/numberListType.ts new file mode 100644 index 0000000..437e7b6 --- /dev/null +++ b/src/constants/numberListType.ts @@ -0,0 +1,4 @@ +import {ListType} from "../types"; +import {numberType} from "./numberType"; + +export const numberListType = new ListType(numberType) \ No newline at end of file diff --git a/src/constants/numberMapType.ts b/src/constants/numberMapType.ts deleted file mode 100644 index 30e1af9..0000000 --- a/src/constants/numberMapType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {MapType} from "../types"; -import {numberType} from "./numberType"; - -export const numberMapType = new MapType(numberType) \ No newline at end of file diff --git a/src/constants/stringArrayType.ts b/src/constants/stringArrayType.ts deleted file mode 100644 index 29f547d..0000000 --- a/src/constants/stringArrayType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {ArrayType} from "../types"; -import {stringType} from "./stringType"; - -export const stringArrayType = new ArrayType(stringType) \ No newline at end of file diff --git a/src/constants/stringDictionaryType.ts b/src/constants/stringDictionaryType.ts new file mode 100644 index 0000000..5649fd4 --- /dev/null +++ b/src/constants/stringDictionaryType.ts @@ -0,0 +1,4 @@ +import {DictionaryType} from "../types"; +import {stringType} from "./stringType"; + +export const stringDictionaryType = new DictionaryType(stringType) \ No newline at end of file diff --git a/src/constants/stringListType.ts b/src/constants/stringListType.ts new file mode 100644 index 0000000..ed0d87f --- /dev/null +++ b/src/constants/stringListType.ts @@ -0,0 +1,4 @@ +import {ListType} from "../types"; +import {stringType} from "./stringType"; + +export const stringListType = new ListType(stringType) \ No newline at end of file diff --git a/src/constants/stringMapType.ts b/src/constants/stringMapType.ts deleted file mode 100644 index 0b8add4..0000000 --- a/src/constants/stringMapType.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {MapType} from "../types"; -import {stringType} from "./stringType"; - -export const stringMapType = new MapType(stringType) \ No newline at end of file diff --git a/src/types/MapType.ts b/src/types/DictionaryType.ts similarity index 83% rename from src/types/MapType.ts rename to src/types/DictionaryType.ts index a616f1e..3487c8f 100644 --- a/src/types/MapType.ts +++ b/src/types/DictionaryType.ts @@ -5,15 +5,15 @@ import {join} from "path"; import {TEMPLATES_DIR} from "../util/constants"; import Mustache from "mustache"; -const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'MapType.ts.mustache')).toString() -const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'MapType.guard.ts.mustache')).toString() +const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'DictionaryType.ts.mustache')).toString() +const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'DictionaryType.guard.ts.mustache')).toString() -export class MapType implements Type { +export class DictionaryType implements Type { exportParams: ExportParams type: Type constructor(type: Type, name?: string) { - this.exportParams = getExportParams('Map', name) + this.exportParams = getExportParams('Dictionary', name) this.type = type } diff --git a/src/types/ArrayType.ts b/src/types/ListType.ts similarity index 85% rename from src/types/ArrayType.ts rename to src/types/ListType.ts index 92b2041..8e72116 100644 --- a/src/types/ArrayType.ts +++ b/src/types/ListType.ts @@ -5,15 +5,15 @@ import {join} from "path"; import {TEMPLATES_DIR} from "../util/constants"; import Mustache from "mustache"; -const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ArrayType.ts.mustache')).toString() -const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ArrayType.guard.ts.mustache')).toString() +const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ListType.ts.mustache')).toString() +const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ListType.guard.ts.mustache')).toString() -export class ArrayType implements Type { +export class ListType implements Type { exportParams: ExportParams type: Type constructor(type: Type, name?: string) { - this.exportParams = getExportParams('Array', name) + this.exportParams = getExportParams('List', name) this.type = type } diff --git a/src/types/index.ts b/src/types/index.ts index 8ef0768..d6f930d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,5 +3,5 @@ export * from './NumberLiteralType' export * from './BooleanLiteralType' export * from './UnionType' export * from './InterfaceType' -export * from './ArrayType' -export * from './MapType' +export * from './ListType' +export * from './DictionaryType' diff --git a/templates/MapType.guard.ts.mustache b/templates/DictionaryType.guard.ts.mustache similarity index 76% rename from templates/MapType.guard.ts.mustache rename to templates/DictionaryType.guard.ts.mustache index 2ad253d..6ea3887 100644 --- a/templates/MapType.guard.ts.mustache +++ b/templates/DictionaryType.guard.ts.mustache @@ -1,5 +1,5 @@ function is{{{name}}}(value: unknown): value is {{{name}}} { if (!(typeof value === "object")) return false; if (value === null) return false; - return isMapOf(value, is{{{type}}}); + return isDictionaryOf(value, is{{{type}}}); } diff --git a/templates/MapType.ts.mustache b/templates/DictionaryType.ts.mustache similarity index 100% rename from templates/MapType.ts.mustache rename to templates/DictionaryType.ts.mustache diff --git a/templates/ArrayType.guard.ts.mustache b/templates/ListType.guard.ts.mustache similarity index 100% rename from templates/ArrayType.guard.ts.mustache rename to templates/ListType.guard.ts.mustache diff --git a/templates/ArrayType.ts.mustache b/templates/ListType.ts.mustache similarity index 100% rename from templates/ArrayType.ts.mustache rename to templates/ListType.ts.mustache diff --git a/test/src/TsTypeGenerator.test.ts b/test/src/TsTypeGenerator.test.ts index d397a22..79685c5 100644 --- a/test/src/TsTypeGenerator.test.ts +++ b/test/src/TsTypeGenerator.test.ts @@ -13,7 +13,7 @@ const TYPES_FILE_NAME = 'types.ts' const OUTPUT_FILE = join(OUTPUT_DIRECTORY, TYPES_FILE_NAME) const HAS_OWN_PROPERTY_DEFINITION = readFileSync(join(FILES_DIR, 'hasOwnProperty.ts')).toString() -const IS_MAP_OF_DEFINITION = readFileSync(join(FILES_DIR, 'isMapOf.ts')).toString() +const IS_DICTIONARY_OF_DEFINITION = readFileSync(join(FILES_DIR, 'isDictionaryOf.ts')).toString() const EXPORT_PREFIX = 'export ' const TYPE_1 = new TestType('Type1', true) @@ -48,7 +48,7 @@ const EXPECTED_OUTPUT_FILE_CONTENT = TYPE_6.getTypeDefinition() + TYPE_6.getTypeGuardDefinition() + HAS_OWN_PROPERTY_DEFINITION + - IS_MAP_OF_DEFINITION + IS_DICTIONARY_OF_DEFINITION describe('TsTypeGenerator', () => { let instance: TsTypeGenerator diff --git a/test/src/constants/booleanArrayType.test.ts b/test/src/constants/booleanArrayType.test.ts deleted file mode 100644 index 50ede7b..0000000 --- a/test/src/constants/booleanArrayType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {booleanArrayType, ArrayType, booleanType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Array_[0-9]+$') - -describe('booleanArrayType', () => { - it('should be an ArrayType', () => { - booleanArrayType.should.be.an.instanceOf(ArrayType); - }) - - it('should have the correct name', () => { - booleanArrayType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - booleanArrayType.isExported().should.be.false - }) - - it('should be an array of booleans', () => { - booleanArrayType.type.should.equal(booleanType); - }) -}) diff --git a/test/src/constants/booleanDictionaryType.test.ts b/test/src/constants/booleanDictionaryType.test.ts new file mode 100644 index 0000000..f7a768d --- /dev/null +++ b/test/src/constants/booleanDictionaryType.test.ts @@ -0,0 +1,21 @@ +import {booleanDictionaryType, DictionaryType, booleanType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Dictionary_[0-9]+$') + +describe('booleanDictionaryType', () => { + it('should be a DictionaryType', () => { + booleanDictionaryType.should.be.an.instanceOf(DictionaryType); + }) + + it('should have the correct name', () => { + booleanDictionaryType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + booleanDictionaryType.isExported().should.be.false + }) + + it('should be an array of booleans', () => { + booleanDictionaryType.type.should.equal(booleanType); + }) +}) diff --git a/test/src/constants/booleanListType.test.ts b/test/src/constants/booleanListType.test.ts new file mode 100644 index 0000000..ad7fd03 --- /dev/null +++ b/test/src/constants/booleanListType.test.ts @@ -0,0 +1,21 @@ +import {booleanListType, ListType, booleanType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_List_[0-9]+$') + +describe('booleanListType', () => { + it('should be a ListType', () => { + booleanListType.should.be.an.instanceOf(ListType); + }) + + it('should have the correct name', () => { + booleanListType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + booleanListType.isExported().should.be.false + }) + + it('should be an array of booleans', () => { + booleanListType.type.should.equal(booleanType); + }) +}) diff --git a/test/src/constants/booleanMapType.test.ts b/test/src/constants/booleanMapType.test.ts deleted file mode 100644 index 2e32e21..0000000 --- a/test/src/constants/booleanMapType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {booleanMapType, MapType, booleanType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Map_[0-9]+$') - -describe('booleanMapType', () => { - it('should be a MapType', () => { - booleanMapType.should.be.an.instanceOf(MapType); - }) - - it('should have the correct name', () => { - booleanMapType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - booleanMapType.isExported().should.be.false - }) - - it('should be an array of booleans', () => { - booleanMapType.type.should.equal(booleanType); - }) -}) diff --git a/test/src/constants/numberArrayType.test.ts b/test/src/constants/numberArrayType.test.ts deleted file mode 100644 index aa1ab1d..0000000 --- a/test/src/constants/numberArrayType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {numberArrayType, ArrayType, numberType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Array_[0-9]+$') - -describe('numberArrayType', () => { - it('should be an ArrayType', () => { - numberArrayType.should.be.an.instanceOf(ArrayType); - }) - - it('should have the correct name', () => { - numberArrayType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - numberArrayType.isExported().should.be.false - }) - - it('should be an array of numbers', () => { - numberArrayType.type.should.equal(numberType); - }) -}) diff --git a/test/src/constants/numberDictionaryType.test.ts b/test/src/constants/numberDictionaryType.test.ts new file mode 100644 index 0000000..4a70738 --- /dev/null +++ b/test/src/constants/numberDictionaryType.test.ts @@ -0,0 +1,21 @@ +import {numberDictionaryType, DictionaryType, numberType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Dictionary_[0-9]+$') + +describe('numberDictionaryType', () => { + it('should be a DictionaryType', () => { + numberDictionaryType.should.be.an.instanceOf(DictionaryType); + }) + + it('should have the correct name', () => { + numberDictionaryType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + numberDictionaryType.isExported().should.be.false + }) + + it('should be an array of numbers', () => { + numberDictionaryType.type.should.equal(numberType); + }) +}) diff --git a/test/src/constants/numberListType.test.ts b/test/src/constants/numberListType.test.ts new file mode 100644 index 0000000..700666d --- /dev/null +++ b/test/src/constants/numberListType.test.ts @@ -0,0 +1,21 @@ +import {numberListType, ListType, numberType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_List_[0-9]+$') + +describe('numberListType', () => { + it('should be a ListType', () => { + numberListType.should.be.an.instanceOf(ListType); + }) + + it('should have the correct name', () => { + numberListType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + numberListType.isExported().should.be.false + }) + + it('should be an array of numbers', () => { + numberListType.type.should.equal(numberType); + }) +}) diff --git a/test/src/constants/numberMapType.test.ts b/test/src/constants/numberMapType.test.ts deleted file mode 100644 index 3a3b71a..0000000 --- a/test/src/constants/numberMapType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {numberMapType, MapType, numberType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Map_[0-9]+$') - -describe('numberMapType', () => { - it('should be a MapType', () => { - numberMapType.should.be.an.instanceOf(MapType); - }) - - it('should have the correct name', () => { - numberMapType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - numberMapType.isExported().should.be.false - }) - - it('should be an array of numbers', () => { - numberMapType.type.should.equal(numberType); - }) -}) diff --git a/test/src/constants/stringArrayType.test.ts b/test/src/constants/stringArrayType.test.ts deleted file mode 100644 index 0accb30..0000000 --- a/test/src/constants/stringArrayType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {stringArrayType, ArrayType, stringType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Array_[0-9]+$') - -describe('stringArrayType', () => { - it('should be an ArrayType', () => { - stringArrayType.should.be.an.instanceOf(ArrayType); - }) - - it('should have the correct name', () => { - stringArrayType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - stringArrayType.isExported().should.be.false - }) - - it('should be an array of strings', () => { - stringArrayType.type.should.equal(stringType); - }) -}) diff --git a/test/src/constants/stringDictionaryType.test.ts b/test/src/constants/stringDictionaryType.test.ts new file mode 100644 index 0000000..914007b --- /dev/null +++ b/test/src/constants/stringDictionaryType.test.ts @@ -0,0 +1,21 @@ +import {stringDictionaryType, DictionaryType, stringType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Dictionary_[0-9]+$') + +describe('stringDictionaryType', () => { + it('should be a DictionaryType', () => { + stringDictionaryType.should.be.an.instanceOf(DictionaryType) + }) + + it('should have the correct name', () => { + stringDictionaryType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + stringDictionaryType.isExported().should.be.false + }) + + it('should be an array of strings', () => { + stringDictionaryType.type.should.equal(stringType); + }) +}) diff --git a/test/src/constants/stringListType.test.ts b/test/src/constants/stringListType.test.ts new file mode 100644 index 0000000..39b3d3f --- /dev/null +++ b/test/src/constants/stringListType.test.ts @@ -0,0 +1,21 @@ +import {stringListType, ListType, stringType} from "../../../src" + +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_List_[0-9]+$') + +describe('stringListType', () => { + it('should be a ListType', () => { + stringListType.should.be.an.instanceOf(ListType); + }) + + it('should have the correct name', () => { + stringListType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) + }) + + it('should not be exported', () => { + stringListType.isExported().should.be.false + }) + + it('should be an array of strings', () => { + stringListType.type.should.equal(stringType); + }) +}) diff --git a/test/src/constants/stringMapType.test.ts b/test/src/constants/stringMapType.test.ts deleted file mode 100644 index c7cd1d6..0000000 --- a/test/src/constants/stringMapType.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {stringMapType, MapType, stringType} from "../../../src" - -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Map_[0-9]+$') - -describe('stringMapType', () => { - it('should be an ArrayType', () => { - stringMapType.should.be.an.instanceOf(MapType) - }) - - it('should have the correct name', () => { - stringMapType.getName().should.match(GENERATED_TYPE_NAME_REGEXP) - }) - - it('should not be exported', () => { - stringMapType.isExported().should.be.false - }) - - it('should be an array of strings', () => { - stringMapType.type.should.equal(stringType); - }) -}) diff --git a/test/src/types/MapType.test.ts b/test/src/types/DictionaryType.test.ts similarity index 82% rename from test/src/types/MapType.test.ts rename to test/src/types/DictionaryType.test.ts index 3ada5d6..8e0d764 100644 --- a/test/src/types/MapType.test.ts +++ b/test/src/types/DictionaryType.test.ts @@ -1,24 +1,24 @@ -import {MapType} from "../../../src" +import {DictionaryType} from "../../../src" import {join} from 'path' import {readFileSync} from "fs"; import {TestType} from "../../TestType"; import {TEMPLATES_DIR} from "../../../src/util/constants"; import Mustache = require("mustache"); -const TYPE_NAME = 'MyMapType' +const TYPE_NAME = 'MyDictionaryType' const MAP_TYPE_NAME = 'Type' const TYPE = new TestType(MAP_TYPE_NAME, true) -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Map_[0-9]+$') +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Dictionary_[0-9]+$') -const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'MapType.ts.mustache')).toString() -const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'MapType.guard.ts.mustache')).toString() +const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'DictionaryType.ts.mustache')).toString() +const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'DictionaryType.guard.ts.mustache')).toString() -describe('MapType', () => { - let instance: MapType +describe('DictionaryType', () => { + let instance: DictionaryType describe('with name', () => { beforeEach(async () => { - instance = new MapType(TYPE, TYPE_NAME) + instance = new DictionaryType(TYPE, TYPE_NAME) }) it('should have the correct name', () => { @@ -50,7 +50,7 @@ describe('MapType', () => { describe('when anonymous', () => { beforeEach(async () => { - instance = new MapType(TYPE) + instance = new DictionaryType(TYPE) }) it('should have a generated name', () => { diff --git a/test/src/types/ArrayType.test.ts b/test/src/types/ListType.test.ts similarity index 85% rename from test/src/types/ArrayType.test.ts rename to test/src/types/ListType.test.ts index 5507508..7da3de9 100644 --- a/test/src/types/ArrayType.test.ts +++ b/test/src/types/ListType.test.ts @@ -1,4 +1,4 @@ -import {ArrayType} from "../../../src" +import {ListType} from "../../../src" import {join} from 'path' import {readFileSync} from "fs"; import {TestType} from "../../TestType"; @@ -9,17 +9,17 @@ const TYPE_NAME = 'MyArrayType' const ARRAY_TYPE_NAME = 'Type' const TYPE = new TestType(ARRAY_TYPE_NAME, true) -const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_Array_[0-9]+$') +const GENERATED_TYPE_NAME_REGEXP = new RegExp('^__TTG_Anonymous_List_[0-9]+$') -const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ArrayType.ts.mustache')).toString() -const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ArrayType.guard.ts.mustache')).toString() +const TYPE_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ListType.ts.mustache')).toString() +const TYPE_GUARD_DEFINITION_TEMPLATE = readFileSync(join(TEMPLATES_DIR, 'ListType.guard.ts.mustache')).toString() -describe('ArrayType', () => { - let instance: ArrayType +describe('ListType', () => { + let instance: ListType describe('with name', () => { beforeEach(async () => { - instance = new ArrayType(TYPE, TYPE_NAME) + instance = new ListType(TYPE, TYPE_NAME) }) it('should have the correct name', () => { @@ -51,7 +51,7 @@ describe('ArrayType', () => { describe('when anonymous', () => { beforeEach(async () => { - instance = new ArrayType(TYPE) + instance = new ListType(TYPE) }) it('should have a generated name', () => {