Skip to content
This repository has been archived by the owner on Dec 25, 2018. It is now read-only.

Commit

Permalink
Added TypeValidator class.
Browse files Browse the repository at this point in the history
  • Loading branch information
electricessence committed May 27, 2016
1 parent f00aee3 commit 6a1b9af
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 11 deletions.
5 changes: 3 additions & 2 deletions dist/commonjs/System/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Primitive } from "./Primitive";
import { IArray } from "./Collections/Array/IArray";
export declare class TypeInfo {
private target;
protected target: any;
type: string;
isBoolean: boolean;
isNumber: boolean;
Expand All @@ -14,12 +14,13 @@ export declare class TypeInfo {
isString: boolean;
isTrueNaN: boolean;
isObject: boolean;
isArray: boolean;
isFunction: boolean;
isUndefined: boolean;
isNull: boolean;
isNullOrUndefined: boolean;
isPrimitive: boolean;
constructor(target: any);
constructor(target: any, onBeforeFreeze?: () => void);
member(name: string): TypeInfo;
static getFor(target: any): TypeInfo;
}
Expand Down
4 changes: 3 additions & 1 deletion dist/commonjs/System/Types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/commonjs/System/Types.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions source/System/Threading/deferImmediate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/System/Threading/deferImmediate.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions source/System/TypeValidator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions source/System/TypeValidator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions source/System/TypeValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT
*/

/**
* A descriptor is simply a JSON tree that either has an actual value or a type that identifies what the expect type should be at that leaf in the tree.
*
* var descriptor = {
* a : Object,
* b : String,
* c : {
* d : true ,
* e : Array,
* f : []
* },
* g : "literal"
* }
*/

import {TypeInfo} from "./Types";
import {areEqual} from "./Collections/Array/Compare";
export class TypeValidator extends TypeInfo {

private _value:any;
constructor(value:any) {
super(value,()=>this._value = value);

}

contains<TDescriptor>(descriptor:any):this is TDescriptor {

let value = this._value;

if(value===descriptor)
return true;

switch (descriptor) {
case Function:
return this.isFunction;
case Object:
return this.isObject;
case Array:
return this.isArray;
case String:
return this.isString;
case Number:
return this.isNumber;
case Boolean:
return this.isBoolean;
}

if(this.type != typeof descriptor)
return false;

// TODO: Fix later, ignore arrays for now...
if(this.isArray && Array.isArray(descriptor)) {
return true;
}

if(this.isObject) {
let targetKeys = Object.keys(value);
let dKeys = Object.keys(descriptor);

// Quick check...
if(dKeys.length>targetKeys.length)
return false;

// Quick check #2...
for(let key of dKeys) {
if(targetKeys.indexOf(key)==-1)
return false;
}

// Final pass with recursive...
// Quick check #2...
for(let key of dKeys) {
let v = value[key], d = descriptor[key];
if(areEqual(v,d)) continue;
let memberType = new TypeValidator(value[key]);
if(!memberType.contains(descriptor[key]))
return false;
}
}

return true;
}

}

export default TypeValidator;
5 changes: 4 additions & 1 deletion source/System/Types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6a1b9af

Please sign in to comment.