Comparing version 0.1.0 to 0.1.3
@@ -29,9 +29,9 @@ "use strict"; | ||
test('serialize', () => { | ||
const type = __1.Type.resolve(data); | ||
const type = __1.Type.solve(data); | ||
const result = __1.Type.serialize(type); | ||
const expected = '{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"region","type":{"primitive":"string"}},{"name":"phoneCode","type":{"primitive":"string"}},{"name":"timezones","type":{"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"offset","type":{"primitive":"integer"}},{"name":"pos","type":{"primitive":"obj","obj":{"properties":[{"name":"lat","type":{"primitive":"decimal"}},{"name":"log","type":{"primitive":"decimal"}}]}}}]}}}}}]}}'; | ||
const expected = '{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"region","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"phoneCode","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"timezones","type":{"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"offset","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"pos","type":{"primitive":"obj","obj":{"properties":[{"name":"lat","type":{"primitive":"decimal","nullable":false,"undefinable":false,"async":false}},{"name":"log","type":{"primitive":"decimal","nullable":false,"undefinable":false,"async":false}}]},"nullable":false,"undefinable":false,"async":false}}]},"nullable":false,"undefinable":false,"async":false}},"nullable":false,"undefinable":false,"async":false}}]},"nullable":false,"undefinable":false,"async":false}'; | ||
expect(expected).toStrictEqual(result); | ||
}); | ||
test('stringify', () => { | ||
const type = __1.Type.resolve(data); | ||
const type = __1.Type.solve(data); | ||
const result = __1.Type.stringify(type); | ||
@@ -38,0 +38,0 @@ const expected = '{name:string,region:string,phoneCode:string,timezones:[{name:string,offset:integer,pos:{lat:decimal,log:decimal}}]}'; |
@@ -51,9 +51,9 @@ "use strict"; | ||
test('serialize', () => { | ||
const type = __1.Type.resolve(data); | ||
const type = __1.Type.solve(data); | ||
const result = __1.Type.serialize(type); | ||
const expected = '{"primitive":"obj","obj":{"properties":[{"name":"id","type":{"primitive":"integer"}},{"name":"customerId","type":{"primitive":"string"}},{"name":"employeeId","type":{"primitive":"integer"}},{"name":"orderDate","type":{"primitive":"string"}},{"name":"requiredDate","type":{"primitive":"string"}},{"name":"shippedDate","type":{"primitive":"string"}},{"name":"shipViaId","type":{"primitive":"integer"}},{"name":"freight","type":{"primitive":"decimal"}},{"name":"name","type":{"primitive":"string"}},{"name":"address","type":{"primitive":"string"}},{"name":"city","type":{"primitive":"string"}},{"name":"region","type":{"primitive":"string"}},{"name":"postalCode","type":{"primitive":"string"}},{"name":"country","type":{"primitive":"string"}},{"name":"details","type":{"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"orderId","type":{"primitive":"integer"}},{"name":"productId","type":{"primitive":"integer"}},{"name":"unitPrice","type":{"primitive":"decimal"}},{"name":"quantity","type":{"primitive":"integer"}},{"name":"discount","type":{"primitive":"integer"}}]}}}}}]}}'; | ||
const expected = '{"primitive":"obj","obj":{"properties":[{"name":"id","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"customerId","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"employeeId","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"orderDate","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"requiredDate","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"shippedDate","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"shipViaId","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"freight","type":{"primitive":"decimal","nullable":false,"undefinable":false,"async":false}},{"name":"name","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"address","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"city","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"region","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"postalCode","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"country","type":{"primitive":"string","nullable":false,"undefinable":false,"async":false}},{"name":"details","type":{"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"orderId","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"productId","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"unitPrice","type":{"primitive":"decimal","nullable":false,"undefinable":false,"async":false}},{"name":"quantity","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}},{"name":"discount","type":{"primitive":"integer","nullable":false,"undefinable":false,"async":false}}]},"nullable":false,"undefinable":false,"async":false}},"nullable":false,"undefinable":false,"async":false}}]},"nullable":false,"undefinable":false,"async":false}'; | ||
expect(expected).toStrictEqual(result); | ||
}); | ||
test('stringify', () => { | ||
const type = __1.Type.resolve(data); | ||
const type = __1.Type.solve(data); | ||
const result = __1.Type.stringify(type); | ||
@@ -60,0 +60,0 @@ const expected = '{id:integer,customerId:string,employeeId:integer,orderDate:string,requiredDate:string,shippedDate:string,shipViaId:integer,freight:decimal,name:string,address:string,city:string,region:string,postalCode:string,country:string,details:[{orderId:integer,productId:integer,unitPrice:decimal,quantity:integer,discount:integer}]}'; |
@@ -14,3 +14,4 @@ export declare enum Primitive { | ||
list = "list", | ||
function = "function", | ||
undefined = "undefined" | ||
} |
@@ -18,4 +18,5 @@ "use strict"; | ||
Primitive["list"] = "list"; | ||
Primitive["function"] = "function"; | ||
Primitive["undefined"] = "undefined"; | ||
})(Primitive = exports.Primitive || (exports.Primitive = {})); | ||
})(Primitive || (exports.Primitive = Primitive = {})); | ||
//# sourceMappingURL=primitive.js.map |
@@ -12,2 +12,10 @@ import { Primitive } from './primitive'; | ||
} | ||
export interface FuncType { | ||
params: ParamType[]; | ||
ret: Type; | ||
} | ||
export interface ParamType { | ||
name: string; | ||
type?: Type; | ||
} | ||
export declare class Type { | ||
@@ -17,3 +25,7 @@ primitive: Primitive; | ||
list?: ListType | undefined; | ||
constructor(primitive: Primitive, obj?: ObjType | undefined, list?: ListType | undefined); | ||
func?: FuncType | undefined; | ||
nullable: boolean; | ||
undefinable: boolean; | ||
async: boolean; | ||
constructor(primitive: Primitive, obj?: ObjType | undefined, list?: ListType | undefined, func?: FuncType | undefined); | ||
static get any(): Type; | ||
@@ -31,2 +43,3 @@ static get string(): Type; | ||
static List(items: Type): Type; | ||
static Function(params: ParamType[], ret: Type): Type; | ||
static isPrimitive(type: Type | string): boolean; | ||
@@ -37,8 +50,9 @@ static to(primitive: Primitive | string): Type; | ||
static isObj(type: Type | string): boolean; | ||
static isFunc(type: Type | string): boolean; | ||
static stringify(type?: Type): string; | ||
static parse(schema: string): Type; | ||
static serialize(type?: Type): string | undefined; | ||
static deserialize(type?: string): Type | undefined; | ||
static parse(schema: string): Type; | ||
static resolve(value: any): Type; | ||
private static _resolve; | ||
static solve(value: any): Type; | ||
private static _solve; | ||
} |
@@ -7,7 +7,10 @@ "use strict"; | ||
class Type { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(primitive, obj, list) { | ||
constructor(primitive, obj, list, func) { | ||
this.primitive = primitive; | ||
this.obj = obj; | ||
this.list = list; | ||
this.func = func; | ||
this.nullable = false; | ||
this.undefinable = false; | ||
this.async = false; | ||
} | ||
@@ -51,2 +54,5 @@ static get any() { | ||
} | ||
static Function(params, ret) { | ||
return new Type(primitive_1.Primitive.function, undefined, undefined, { params, ret }); | ||
} | ||
static isPrimitive(type) { | ||
@@ -116,2 +122,14 @@ let value; | ||
} | ||
// Examples: | ||
// sin valor de retorno: (name:string,nro:int) | ||
// sin arguments: (>dateTime) | ||
// simple: (name:string,nro:int>string) | ||
// retorna otra function: (name:string,nro:int>(name:string>int)) | ||
// function que recibe function : ((name:string>string),nro:int>int) | ||
static isFunc(type) { | ||
if (typeof type === 'string') { | ||
return type.startsWith('(') && type.endsWith(')'); | ||
} | ||
return type.primitive === primitive_1.Primitive.function; | ||
} | ||
static stringify(type) { | ||
@@ -136,4 +154,15 @@ if (type === undefined) { | ||
} | ||
if (this.isFunc(type)) { | ||
const funcType = type.func; | ||
const params = []; | ||
for (const paramType of funcType.params) { | ||
params.push(`${paramType.name}:${this.stringify(paramType.type)}`); | ||
} | ||
return `(${params.join(',')}=>${this.stringify(funcType.ret)})`; | ||
} | ||
return 'any'; | ||
} | ||
static parse(schema) { | ||
return new TypeParser(schema).parse(); | ||
} | ||
static serialize(type) { | ||
@@ -151,11 +180,8 @@ if (type === undefined || type === null) { | ||
} | ||
static parse(schema) { | ||
return new TypeParser(schema).parse(); | ||
} | ||
static resolve(value) { | ||
static solve(value) { | ||
const type = new Type(primitive_1.Primitive.undefined); | ||
this._resolve(value, type); | ||
this._solve(value, type); | ||
return type; | ||
} | ||
static _resolve(value, type) { | ||
static _solve(value, type) { | ||
if (value === undefined || value === null) { | ||
@@ -176,5 +202,12 @@ return; | ||
for (const item of value) { | ||
this._resolve(item, type.list.items); | ||
this._solve(item, type.list.items); | ||
} | ||
} | ||
else if (typeof value === 'function') { | ||
const str = value.toString(); | ||
type.primitive = primitive_1.Primitive.function; | ||
const funcType = new TypeParser(str).parse().func; | ||
type.func = funcType; | ||
console.log(str); | ||
} | ||
else if (typeof value === 'object') { | ||
@@ -200,3 +233,3 @@ if (type.primitive === primitive_1.Primitive.undefined) { | ||
} | ||
this._resolve(entry[1], property.type); | ||
this._solve(entry[1], property.type); | ||
} | ||
@@ -284,2 +317,8 @@ } | ||
} | ||
else if (char === '(') { | ||
this.index += 1; | ||
this.forwardSpaces(); | ||
const functionType = this.getFunction(); | ||
return new Type(primitive_1.Primitive.function, undefined, undefined, functionType); | ||
} | ||
else { | ||
@@ -340,2 +379,33 @@ throw new Error('Cannot solve type'); | ||
} | ||
getFunction() { | ||
const params = []; | ||
let ret = Type.void; | ||
while (true) { | ||
const name = this.getValue(); | ||
this.forwardSpaces(); | ||
let type = Type.any; | ||
if (this.current === ':') { | ||
this.index += 1; | ||
type = this.getType(); | ||
} | ||
params.push({ name, type }); | ||
this.forwardSpaces(); | ||
if (this.current === ',') { | ||
this.index += 1; | ||
continue; | ||
} | ||
if (this.current === '>') { | ||
this.index += 2; | ||
ret = this.getType(); | ||
} | ||
if (this.current === ')') { | ||
this.index += 1; | ||
break; | ||
} | ||
else { | ||
throw new Error('Function without end'); | ||
} | ||
} | ||
return { params, ret }; | ||
} | ||
forwardSpaces() { | ||
@@ -342,0 +412,0 @@ while (!this.end && this.buffer[this.index] === ' ') { |
{ | ||
"name": "typ3s", | ||
"version": "0.1.0", | ||
"version": "0.1.3", | ||
"description": "Allow define type on json", | ||
@@ -25,2 +25,5 @@ "scripts": { | ||
"homepage": "https://github.com/FlavioLionelRita/typ3s#readme", | ||
"standard-version": { | ||
"header": "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\n## Versions\n" | ||
}, | ||
"dependencies": {}, | ||
@@ -27,0 +30,0 @@ "main": "index.js", |
# typ3s | ||
json types, type , types | ||
## Primitive | ||
- string | ||
- integer | ||
- decimal | ||
- number | ||
- boolean | ||
- date | ||
- dateTime | ||
- time | ||
- any | ||
- void | ||
## Array / Tuples | ||
[] | ||
## Object | ||
{} | ||
## Function | ||
() | ||
## Modifiers | ||
|Modifier |description | | ||
|:------------------|:-----------------------| | ||
| ! | Nullable | | ||
| ? | Undefinable | | ||
| ~ | Async | | ||
## Examples | ||
|type |description | | ||
|:------------------------------|:----------------------------------------------------------| | ||
| [string] | array of string | | ||
| [string, integer] | tuple with string and integer properties | | ||
| {name:string, nro:integer} | object with string and integer properties | | ||
| [{name:string, today:date}] | array of object | | ||
| (>any) | function without params and return value | | ||
| (list:[integer]>integer) | function with array integer as params and return integer | | ||
| (today:date,nro:?integer>date)| function with undefinable integer param | | ||
| {name:string,nro:?integer} | object of properties string and undefinable integer | | ||
| {name:string,nro:!integer} | object of properties string and nullable integer | | ||
| ([integer]>~integer) | function with return async of integer | | ||
## Methods | ||
| method |description | | ||
|:--------------|:--------------------------------------------------------------------------| | ||
| solve | | | ||
| stringify | | | ||
| parse | | | ||
| serialize | | | ||
| deserialized | | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
58018
665
62
2