Comparing version 0.0.2 to 0.1.0
@@ -31,3 +31,3 @@ "use strict"; | ||
const result = __1.Type.serialize(type); | ||
const expected = '{"kind":"obj","obj":{"properties":[{"name":"name","type":{"kind":"string"}},{"name":"region","type":{"kind":"string"}},{"name":"phoneCode","type":{"kind":"string"}},{"name":"timezones","type":{"kind":"list","list":{"items":{"kind":"obj","obj":{"properties":[{"name":"name","type":{"kind":"string"}},{"name":"offset","type":{"kind":"integer"}},{"name":"pos","type":{"kind":"obj","obj":{"properties":[{"name":"lat","type":{"kind":"decimal"}},{"name":"log","type":{"kind":"decimal"}}]}}}]}}}}}]}}'; | ||
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"}}]}}}]}}}}}]}}'; | ||
expect(expected).toStrictEqual(result); | ||
@@ -34,0 +34,0 @@ }); |
@@ -53,3 +53,3 @@ "use strict"; | ||
const result = __1.Type.serialize(type); | ||
const expected = '{"kind":"obj","obj":{"properties":[{"name":"id","type":{"kind":"integer"}},{"name":"customerId","type":{"kind":"string"}},{"name":"employeeId","type":{"kind":"integer"}},{"name":"orderDate","type":{"kind":"string"}},{"name":"requiredDate","type":{"kind":"string"}},{"name":"shippedDate","type":{"kind":"string"}},{"name":"shipViaId","type":{"kind":"integer"}},{"name":"freight","type":{"kind":"decimal"}},{"name":"name","type":{"kind":"string"}},{"name":"address","type":{"kind":"string"}},{"name":"city","type":{"kind":"string"}},{"name":"region","type":{"kind":"string"}},{"name":"postalCode","type":{"kind":"string"}},{"name":"country","type":{"kind":"string"}},{"name":"details","type":{"kind":"list","list":{"items":{"kind":"obj","obj":{"properties":[{"name":"orderId","type":{"kind":"integer"}},{"name":"productId","type":{"kind":"integer"}},{"name":"unitPrice","type":{"kind":"decimal"}},{"name":"quantity","type":{"kind":"integer"}},{"name":"discount","type":{"kind":"integer"}}]}}}}}]}}'; | ||
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"}}]}}}}}]}}'; | ||
expect(expected).toStrictEqual(result); | ||
@@ -56,0 +56,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
export * from './kind'; | ||
export * from './primitive'; | ||
export * from './type'; |
@@ -17,4 +17,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./kind"), exports); | ||
__exportStar(require("./primitive"), exports); | ||
__exportStar(require("./type"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { Kind } from './kind'; | ||
import { Primitive } from './primitive'; | ||
export interface PropertyType { | ||
@@ -13,6 +13,6 @@ name: string; | ||
export declare class Type { | ||
kind: Kind; | ||
primitive: Primitive; | ||
obj?: ObjType | undefined; | ||
list?: ListType | undefined; | ||
constructor(kind: Kind, obj?: ObjType | undefined, list?: ListType | undefined); | ||
constructor(primitive: Primitive, obj?: ObjType | undefined, list?: ListType | undefined); | ||
static get any(): Type; | ||
@@ -31,3 +31,3 @@ static get string(): Type; | ||
static isPrimitive(type: Type | string): boolean; | ||
static to(kind: Kind | string): Type; | ||
static to(primitive: Primitive | string): Type; | ||
static get(value: any): Type; | ||
@@ -34,0 +34,0 @@ static isList(type: Type | string): boolean; |
@@ -5,7 +5,7 @@ "use strict"; | ||
/* eslint-disable no-use-before-define */ | ||
const kind_1 = require("./kind"); | ||
const primitive_1 = require("./primitive"); | ||
class Type { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(kind, obj, list) { | ||
this.kind = kind; | ||
constructor(primitive, obj, list) { | ||
this.primitive = primitive; | ||
this.obj = obj; | ||
@@ -15,37 +15,37 @@ this.list = list; | ||
static get any() { | ||
return new Type(kind_1.Kind.any); | ||
return new Type(primitive_1.Primitive.any); | ||
} | ||
static get string() { | ||
return new Type(kind_1.Kind.string); | ||
return new Type(primitive_1.Primitive.string); | ||
} | ||
static get integer() { | ||
return new Type(kind_1.Kind.integer); | ||
return new Type(primitive_1.Primitive.integer); | ||
} | ||
static get decimal() { | ||
return new Type(kind_1.Kind.decimal); | ||
return new Type(primitive_1.Primitive.decimal); | ||
} | ||
static get number() { | ||
return new Type(kind_1.Kind.number); | ||
return new Type(primitive_1.Primitive.number); | ||
} | ||
static get boolean() { | ||
return new Type(kind_1.Kind.boolean); | ||
return new Type(primitive_1.Primitive.boolean); | ||
} | ||
static get date() { | ||
return new Type(kind_1.Kind.date); | ||
return new Type(primitive_1.Primitive.date); | ||
} | ||
static get dateTime() { | ||
return new Type(kind_1.Kind.dateTime); | ||
return new Type(primitive_1.Primitive.dateTime); | ||
} | ||
static get time() { | ||
return new Type(kind_1.Kind.time); | ||
return new Type(primitive_1.Primitive.time); | ||
} | ||
static get void() { | ||
return new Type(kind_1.Kind.void); | ||
return new Type(primitive_1.Primitive.void); | ||
} | ||
// eslint-disable-next-line no-use-before-define, | ||
static Obj(properties = []) { | ||
return new Type(kind_1.Kind.obj, { properties }); | ||
return new Type(primitive_1.Primitive.obj, { properties }); | ||
} | ||
static List(items) { | ||
return new Type(kind_1.Kind.list, undefined, { items }); | ||
return new Type(primitive_1.Primitive.list, undefined, { items }); | ||
} | ||
@@ -57,4 +57,4 @@ static isPrimitive(type) { | ||
} | ||
else if (type !== undefined && type.kind !== undefined) { | ||
value = type.kind.toString(); | ||
else if (type !== undefined && type.primitive !== undefined) { | ||
value = type.primitive.toString(); | ||
} | ||
@@ -66,8 +66,8 @@ else { | ||
} | ||
static to(kind) { | ||
if (typeof kind === 'string') { | ||
const kindKey = kind; | ||
return new Type(kind_1.Kind[kindKey]); | ||
static to(primitive) { | ||
if (typeof primitive === 'string') { | ||
const primitiveKey = primitive; | ||
return new Type(primitive_1.Primitive[primitiveKey]); | ||
} | ||
return new Type(kind); | ||
return new Type(primitive); | ||
} | ||
@@ -110,3 +110,3 @@ static get(value) { | ||
} | ||
return type.kind === kind_1.Kind.list; | ||
return type.primitive === primitive_1.Primitive.list; | ||
} | ||
@@ -117,7 +117,4 @@ static isObj(type) { | ||
} | ||
return type.kind === kind_1.Kind.obj; | ||
return type.primitive === primitive_1.Primitive.obj; | ||
} | ||
// public static parse (value:string): Type { | ||
// return new TypeParser(value).parse() | ||
// } | ||
static stringify(type) { | ||
@@ -128,3 +125,3 @@ if (type === undefined) { | ||
if (this.isPrimitive(type)) { | ||
return type.kind.toString(); | ||
return type.primitive.toString(); | ||
} | ||
@@ -161,3 +158,3 @@ if (this.isObj(type)) { | ||
static resolve(value) { | ||
const type = new Type(kind_1.Kind.undefined); | ||
const type = new Type(primitive_1.Primitive.undefined); | ||
this._resolve(value, type); | ||
@@ -171,11 +168,11 @@ return type; | ||
if (Array.isArray(value)) { | ||
if (type.kind === kind_1.Kind.undefined) { | ||
type.kind = kind_1.Kind.list; | ||
type.list = { items: new Type(kind_1.Kind.undefined) }; | ||
if (type.primitive === primitive_1.Primitive.undefined) { | ||
type.primitive = primitive_1.Primitive.list; | ||
type.list = { items: new Type(primitive_1.Primitive.undefined) }; | ||
} | ||
else if (type.kind !== kind_1.Kind.list && type.kind !== kind_1.Kind.any) { | ||
type.kind = kind_1.Kind.any; | ||
else if (type.primitive !== primitive_1.Primitive.list && type.primitive !== primitive_1.Primitive.any) { | ||
type.primitive = primitive_1.Primitive.any; | ||
} | ||
if (type.list === undefined || type.list.items === undefined) { | ||
type.list = { items: new Type(kind_1.Kind.undefined) }; | ||
type.list = { items: new Type(primitive_1.Primitive.undefined) }; | ||
} | ||
@@ -187,8 +184,8 @@ for (const item of value) { | ||
else if (typeof value === 'object') { | ||
if (type.kind === kind_1.Kind.undefined) { | ||
type.kind = kind_1.Kind.obj; | ||
if (type.primitive === primitive_1.Primitive.undefined) { | ||
type.primitive = primitive_1.Primitive.obj; | ||
type.obj = { properties: [] }; | ||
} | ||
else if (type.kind !== kind_1.Kind.obj && type.kind !== kind_1.Kind.any) { | ||
type.kind = kind_1.Kind.any; | ||
else if (type.primitive !== primitive_1.Primitive.obj && type.primitive !== primitive_1.Primitive.any) { | ||
type.primitive = primitive_1.Primitive.any; | ||
} | ||
@@ -201,7 +198,7 @@ if (type.obj === undefined || type.obj.properties === undefined) { | ||
if (property === undefined) { | ||
property = { name: entry[0], type: new Type(kind_1.Kind.undefined) }; | ||
property = { name: entry[0], type: new Type(primitive_1.Primitive.undefined) }; | ||
type.obj.properties.push(property); | ||
} | ||
else if (property.type === undefined) { | ||
property.type = new Type(kind_1.Kind.undefined); | ||
property.type = new Type(primitive_1.Primitive.undefined); | ||
} | ||
@@ -212,31 +209,31 @@ this._resolve(entry[1], property.type); | ||
else if (typeof value === 'string') { | ||
if (type.kind === kind_1.Kind.undefined) { | ||
type.kind = kind_1.Kind.string; | ||
if (type.primitive === primitive_1.Primitive.undefined) { | ||
type.primitive = primitive_1.Primitive.string; | ||
} | ||
else if (type.kind !== kind_1.Kind.string && type.kind !== kind_1.Kind.any) { | ||
type.kind = kind_1.Kind.any; | ||
else if (type.primitive !== primitive_1.Primitive.string && type.primitive !== primitive_1.Primitive.any) { | ||
type.primitive = primitive_1.Primitive.any; | ||
} | ||
} | ||
else if (typeof value === 'number') { | ||
if (type.kind === kind_1.Kind.undefined) { | ||
if (type.primitive === primitive_1.Primitive.undefined) { | ||
if (Number.isInteger(value)) { | ||
type.kind = kind_1.Kind.integer; | ||
type.primitive = primitive_1.Primitive.integer; | ||
} | ||
else { | ||
type.kind = kind_1.Kind.decimal; | ||
type.primitive = primitive_1.Primitive.decimal; | ||
} | ||
} | ||
else if (type.kind === kind_1.Kind.integer && !Number.isInteger(value)) { | ||
type.kind = kind_1.Kind.decimal; | ||
else if (type.primitive === primitive_1.Primitive.integer && !Number.isInteger(value)) { | ||
type.primitive = primitive_1.Primitive.decimal; | ||
} | ||
else if (type.kind !== kind_1.Kind.integer && type.kind !== kind_1.Kind.decimal && type.kind !== kind_1.Kind.any) { | ||
type.kind = kind_1.Kind.any; | ||
else if (type.primitive !== primitive_1.Primitive.integer && type.primitive !== primitive_1.Primitive.decimal && type.primitive !== primitive_1.Primitive.any) { | ||
type.primitive = primitive_1.Primitive.any; | ||
} | ||
} | ||
else if (typeof value === 'boolean') { | ||
if (type.kind === kind_1.Kind.undefined) { | ||
type.kind = kind_1.Kind.boolean; | ||
if (type.primitive === primitive_1.Primitive.undefined) { | ||
type.primitive = primitive_1.Primitive.boolean; | ||
} | ||
else if (type.kind !== kind_1.Kind.boolean && type.kind !== kind_1.Kind.any) { | ||
type.kind = kind_1.Kind.any; | ||
else if (type.primitive !== primitive_1.Primitive.boolean && type.primitive !== primitive_1.Primitive.any) { | ||
type.primitive = primitive_1.Primitive.any; | ||
} | ||
@@ -273,3 +270,3 @@ } | ||
const value = this.getValue(); | ||
if (Type.isPrimitive(value) || value === kind_1.Kind.any) { | ||
if (Type.isPrimitive(value) || value === primitive_1.Primitive.any) { | ||
return Type.to(value); | ||
@@ -285,3 +282,3 @@ } | ||
const objectType = this.getObject(); | ||
return new Type(kind_1.Kind.obj, objectType); | ||
return new Type(primitive_1.Primitive.obj, objectType); | ||
} | ||
@@ -292,3 +289,3 @@ else if (char === '[') { | ||
const listType = this.getList(); | ||
return new Type(kind_1.Kind.list, undefined, listType); | ||
return new Type(primitive_1.Primitive.list, undefined, listType); | ||
} | ||
@@ -295,0 +292,0 @@ else { |
{ | ||
"name": "typ3s", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Allow define type on json", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
48503
573
1