typedconverter
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0-beta.5
@@ -23,2 +23,6 @@ import { Opt, val } from "./decorators"; | ||
path?: string; | ||
/** | ||
* Guess single value as array element if expected type is array. Default false | ||
*/ | ||
guessArrayElement?: boolean; | ||
} | ||
@@ -25,0 +29,0 @@ /** |
@@ -21,3 +21,7 @@ "use strict"; | ||
function convert(value, opt) { | ||
return visitor_1.pipeline(value, transformer_1.transform(opt.type), opt.path || "", opt.visitors || [], opt.decorators || []); | ||
return visitor_1.pipeline(value, transformer_1.transform(opt.type), { | ||
path: opt.path || "", extension: opt.visitors || [], | ||
decorators: opt.decorators || [], | ||
guessArrayElement: opt.guessArrayElement || false | ||
}); | ||
} | ||
@@ -38,4 +42,7 @@ exports.convert = convert; | ||
} | ||
return convert(value, { type: opt.type, decorators: opt.decorators, path: opt.path, visitors }); | ||
return convert(value, { | ||
decorators: opt.decorators, guessArrayElement: opt.guessArrayElement, | ||
path: opt.path, type: opt.type, visitors | ||
}); | ||
} | ||
exports.validate = validate; |
@@ -16,2 +16,9 @@ import { VisitorExtension } from "./invocation"; | ||
} | ||
interface VisitorOption { | ||
path: string; | ||
extension: VisitorExtension[]; | ||
decorators: any[]; | ||
guessArrayElement: boolean; | ||
parent?: ParentInfo; | ||
} | ||
declare namespace Result { | ||
@@ -21,3 +28,3 @@ function create(value: any): Result; | ||
} | ||
declare function pipeline(value: any, ast: SuperNode, path: string, extension: VisitorExtension[], decorators: any[], parent?: ParentInfo): Result; | ||
declare function pipeline(value: any, ast: SuperNode, opt: VisitorOption): Result; | ||
export { pipeline, ResultMessages, Result, ParentInfo }; |
@@ -36,16 +36,23 @@ "use strict"; | ||
// --------------------------------------------------------------------- // | ||
function primitiveVisitor(value, ast, path, extension, decorators, parent) { | ||
function primitiveVisitor(value, ast, opt) { | ||
const result = ast.converter(value); | ||
if (result === undefined) | ||
return Result.error(path, unableToConvert(value, ast.type.name)); | ||
return Result.error(opt.path, unableToConvert(value, ast.type.name)); | ||
else | ||
return Result.create(result); | ||
} | ||
function arrayVisitor(value, ast, path, extension, decorators, parent) { | ||
const newValues = Array.isArray(value) ? value : [value]; | ||
function arrayVisitor(value, ast, opt) { | ||
const newValues = opt.guessArrayElement && !Array.isArray(value) ? [value] : value; | ||
if (!Array.isArray(newValues)) | ||
return Result.error(opt.path, unableToConvert(value, `Array<${ast.type.name}>`)); | ||
const result = []; | ||
const errors = []; | ||
for (let i = 0; i < newValues.length; i++) { | ||
const val = value[i]; | ||
const elValue = pipeline(val, ast.element, getPath(path, i.toString()), extension, decorators, parent); | ||
const val = newValues[i]; | ||
const option = { | ||
path: getPath(opt.path, i.toString()), extension: opt.extension, | ||
decorators: opt.decorators, parent: opt.parent, | ||
guessArrayElement: opt.guessArrayElement | ||
}; | ||
const elValue = pipeline(val, ast.element, option); | ||
result.push(elValue.value); | ||
@@ -57,5 +64,5 @@ if (elValue.issues) | ||
} | ||
function objectVisitor(value, ast, path, extension, decorators, parent) { | ||
function objectVisitor(value, ast, opt) { | ||
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") | ||
return Result.error(path, unableToConvert(value, ast.type.name)); | ||
return Result.error(opt.path, unableToConvert(value, ast.type.name)); | ||
const instance = Object.create(ast.type.prototype); | ||
@@ -66,3 +73,8 @@ const meta = tinspector_1.default(ast.type); | ||
const node = ast.properties.get(property.name); | ||
const propValue = pipeline(value[property.name], node, getPath(path, property.name), extension, property.decorators, { type: ast.type, decorators: decorators }); | ||
const option = { | ||
path: getPath(opt.path, property.name), extension: opt.extension, | ||
decorators: property.decorators, parent: { type: ast.type, decorators: opt.decorators }, | ||
guessArrayElement: opt.guessArrayElement | ||
}; | ||
const propValue = pipeline(value[property.name], node, option); | ||
if (propValue.issues) | ||
@@ -76,11 +88,11 @@ errors.push(...propValue.issues); | ||
} | ||
function visitor(value, ast, path, extension, decorators, parent) { | ||
function visitor(value, ast, opt) { | ||
if (value === undefined || value === null || value.constructor === ast.type || ast.type === Object) | ||
return { value }; | ||
return visitorMap[ast.kind](value, ast, path, extension, decorators, parent); | ||
return visitorMap[ast.kind](value, ast, opt); | ||
} | ||
function pipeline(value, ast, path, extension, decorators, parent) { | ||
const visitors = invocation_1.pipe(value, path, ast, decorators, extension, () => visitor(value, ast, path, extension, decorators, parent), parent); | ||
function pipeline(value, ast, opt) { | ||
const visitors = invocation_1.pipe(value, opt.path, ast, opt.decorators, opt.extension, () => visitor(value, ast, opt), opt.parent); | ||
return visitors.proceed(); | ||
} | ||
exports.pipeline = pipeline; |
{ | ||
"name": "typedconverter", | ||
"version": "1.0.0-beta.4", | ||
"version": "1.0.0-beta.5", | ||
"description": "Convert object into classes match with TypeScript type annotation", | ||
@@ -15,3 +15,3 @@ "main": "lib/index.js", | ||
"gen": "ts-node src/generator.ts", | ||
"clean": "rm -rf src/*.js && rm -rf test/*.js", | ||
"clean": "rm -rf src/*.js && rm -rf test/*.js && rm -rf lib", | ||
"benchmark": "ts-node test/benchmark.ts" | ||
@@ -22,5 +22,5 @@ }, | ||
"dependencies": { | ||
"@types/validator": "^10.11.0", | ||
"tinspector": "^2.2.1", | ||
"tslib": "^1.9.3", | ||
"@types/validator": "^10.11.1", | ||
"tinspector": "^2.2.2", | ||
"tslib": "^1.10.0", | ||
"validator": "^11.0.0" | ||
@@ -30,11 +30,11 @@ }, | ||
"@types/ejs": "^2.6.3", | ||
"@types/jest": "^24.0.11", | ||
"@types/joi": "^14.3.2", | ||
"coveralls": "^3.0.3", | ||
"ejs": "^2.6.1", | ||
"jest": "^24.5.0", | ||
"@types/jest": "^24.0.15", | ||
"@types/joi": "^14.3.3", | ||
"coveralls": "^3.0.4", | ||
"ejs": "^2.6.2", | ||
"jest": "^24.8.0", | ||
"joi": "^14.3.1", | ||
"ts-jest": "^24.0.0", | ||
"ts-node": "^8.0.3", | ||
"typescript": "^3.3.4000" | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.5.2" | ||
}, | ||
@@ -41,0 +41,0 @@ "bugs": { |
@@ -15,3 +15,3 @@ # TypedConverter | ||
const numb = await convert("YES", { type: Boolean }) //return true | ||
const numb = await convert("2019-1-1", { type: Date }) //return date 1/1/2019 | ||
const numb = await convert("2019-2-2", { type: Date }) //return date 1/1/2019 | ||
``` | ||
@@ -53,3 +53,3 @@ | ||
deceased: "ON", | ||
birthday: "2018-1-1" }, | ||
birthday: "2018-2-2" }, | ||
{ type: AnimalClass }) | ||
@@ -97,3 +97,3 @@ ``` | ||
```typescript | ||
const b = await convert("1", { type: [Number] }) //ok [1] | ||
const b = await convert("1", { type: [Number], guessArrayElement: true }) //ok [1] | ||
``` | ||
@@ -100,0 +100,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38864
22
791
Updated@types/validator@^10.11.1
Updatedtinspector@^2.2.2
Updatedtslib@^1.10.0