ts-auto-guard
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -213,3 +213,11 @@ "use strict"; | ||
.filter(function (x) { return x && x.getName() !== '__type'; })[0]) === null || _c === void 0 ? void 0 : _c.getName(); | ||
if (name) { | ||
var isPrimitive = [ | ||
'undefined', | ||
'null', | ||
'boolean', | ||
'bigint', | ||
'string', | ||
'number', | ||
].includes(t.getText()); | ||
if (name && !isPrimitive) { | ||
return 'is' + name; | ||
@@ -308,3 +316,3 @@ } | ||
if (!ts_morph_1.Node.isInterfaceDeclaration(declaration)) { | ||
throw new TypeError('Extected declaration to be an interface delcaration!'); | ||
throw new TypeError('Extected declaration to be an interface declaration!'); | ||
} | ||
@@ -458,3 +466,6 @@ declaration.getBaseTypes().forEach(function (baseType) { | ||
var propertyName = property.name; | ||
var isIdentifier = propertyName[0] !== '"' && propertyName[0] !== "'" && !hasSpaces; | ||
var isIdentifier = propertyName[0] !== '"' && | ||
propertyName[0] !== "'" && | ||
!hasSpaces && | ||
isNaN(parseInt(propertyName)); | ||
var strippedName = propertyName.replace(/"/g, ''); | ||
@@ -461,0 +472,0 @@ var varName = isIdentifier |
{ | ||
"name": "ts-auto-guard", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -89,3 +89,3 @@ # ts-auto-guard | ||
``` | ||
$ ts-auto-guard --export-all | ||
$ ts-auto-guard --export-all 'src/domain/*.ts' | ||
``` | ||
@@ -92,0 +92,0 @@ |
@@ -1034,2 +1034,72 @@ import test from 'tape' | ||
testProcessProject( | ||
'generated type guards for numeric enums in optional records', | ||
{ | ||
'test.ts': ` | ||
export enum Types{ | ||
TheGood = 1, | ||
TheBad, | ||
TheTypeSafe | ||
} | ||
export interface TestItem { | ||
room: Partial<Record<Types, string>>>; | ||
}`, | ||
}, | ||
{ | ||
'test.ts': null, | ||
'test.guard.ts': ` | ||
import { Types, TestItem } from "./test"; | ||
export function isTypes(obj: any, _argumentName?: string): obj is Types { | ||
return ( | ||
(obj === Types.TheGood || | ||
obj === Types.TheBad || | ||
obj === Types.TheTypeSafe) | ||
) | ||
} | ||
export function isTestItem(obj: any, _argumentName?: string): obj is TestItem { | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(obj.room !== null && | ||
typeof obj.room === "object" || | ||
typeof obj.room === "function") && | ||
(typeof obj.room["1"] === "undefined" || | ||
typeof obj.room["1"] === "string") && | ||
(typeof obj.room["2"] === "undefined" || | ||
typeof obj.room["2"] === "string") && | ||
(typeof obj.room["3"] === "undefined" || | ||
typeof obj.room["3"] === "string") | ||
) | ||
}`, | ||
}, | ||
{ options: { exportAll: true } } | ||
) | ||
testProcessProject( | ||
'no type guards for primitive alias types', | ||
{ | ||
'test.ts': ` | ||
export type Days = number | ||
export type UUID = string | ||
export enum Types { TheGood } | ||
export type Blank = undefined | ||
export type AlwaysNull = null`, | ||
}, | ||
{ | ||
'test.ts': null, | ||
'test.guard.ts': ` | ||
import { Types } from "./test"; | ||
export function isTypes(obj: any, _argumentName?: string): obj is Types { | ||
return ( | ||
obj === Types.TheGood | ||
) | ||
}`, | ||
}, | ||
{ options: { exportAll: true } } | ||
) | ||
testProcessProject( | ||
'generated type guards for arrays of any', | ||
@@ -1036,0 +1106,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
132151
2650