Skip to content

Commit

Permalink
Handle date type
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Alchimavicius committed Mar 11, 2020
1 parent a568350 commit 8324464
Show file tree
Hide file tree
Showing 7 changed files with 528 additions and 576 deletions.
24 changes: 15 additions & 9 deletions src/get-type-structure.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as hash from "hash.js";

import { TypeDescription, TypeStructure } from "./model";
import { isHash, parseKeyMetaData, getTypeDescriptionGroup, findTypeById, isArray, isObject, onlyUnique } from "./util";
import { TypeGroup, NameEntry, NameStructure, InterfaceDescription } from "./model";
import { isHash, getTypeDescriptionGroup, findTypeById, isArray, isObject, onlyUnique, isDate } from "./util";
import { TypeGroup } from "./model";

function createTypeDescription(typeObj: any | string[], isUnion: boolean): TypeDescription {
if (isArray(typeObj)) {
Expand Down Expand Up @@ -68,20 +68,20 @@ function objectsHaveSameEntries(obj1: any, obj2: any): boolean {
return sameLength && sameTypes;
}

function hasSamePrimitiveElements(a: any[], b: any[]) {
return a.every(el => b.indexOf(el) !== -1);
}

function getSimpleTypeName(value: any): string {
if (value === null) {
return "null";
} else if (value instanceof Date) {
return "Date";
} else {
return typeof value;
}
}

function getTypeGroup(value: any): TypeGroup {
if (isArray(value)) {
if (isDate(value)) {
return TypeGroup.Date;
} else if (isArray(value)) {
return TypeGroup.Array;
} else if (isObject(value)) {
return TypeGroup.Object;
Expand Down Expand Up @@ -254,10 +254,16 @@ export function getTypeStructure(
};

case TypeGroup.Primitive:
const simpleType = getSimpleTypeName(targetObj);
return {
rootTypeId: getSimpleTypeName(targetObj),
types
};

case TypeGroup.Date:
const dateType = getSimpleTypeName(targetObj);

return {
rootTypeId: simpleType,
rootTypeId: dateType,
types
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export enum TypeGroup {
Primitive,
Array,
Object
Object,
Date
}

export interface TypeDescription {
Expand Down
9 changes: 5 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function isObject(x) {
return Object.prototype.toString.call(x) === "[object Object]" && x !== null;
}

export function isDate(x) {
return x instanceof Date;
}

export function parseKeyMetaData(key: string): KeyMetaData {
const isOptional = key.endsWith("--?");

Expand All @@ -48,9 +52,6 @@ export function getTypeDescriptionGroup(desc: TypeDescription): TypeGroup {
}
}

export function findTypeById(
id: string,
types: TypeDescription[]
): TypeDescription {
export function findTypeById(id: string, types: TypeDescription[]): TypeDescription {
return types.find(_ => _.id === id);
}
Loading

0 comments on commit 8324464

Please sign in to comment.