Last active
June 10, 2016 11:21
-
-
Save razorthink-com/0454c04264329f74a5ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidType( type, value ) { | |
function getType( item ) { | |
return Object.prototype.toString.call( item ).slice( 8, -1 ); | |
} | |
function checkPrimitiveType( type, value ) { | |
return type == getType( value ); | |
} | |
function checkObjectType( type, value ) { | |
if ( getType( value ) != 'Object' ) return false; | |
return Object.keys( type ) | |
.every( function ( tKey ) { | |
return checkType( type[ tKey ], value[ tKey ] ) | |
} ); | |
} | |
function checkArrayType( type, value ) { | |
if ( getType( value ) != 'Array' ) return false; | |
return value.every( function ( item ) { | |
return checkType( type[ 0 ], item ) | |
} ); | |
} | |
function checkType( type, value ) { | |
switch ( getType( type ) ) { | |
case 'Object': return checkObjectType( type, value ); | |
case 'Array': return checkArrayType( type, value ); | |
default: return checkPrimitiveType( type, value ); | |
} | |
} | |
return checkType( type, value ); | |
} | |
// **************** Unit Tests **************** // | |
var type = { | |
name: 'String', | |
id: 'Number', | |
active: 'Boolean', | |
projects: [{ | |
name: 'String', | |
id: 'Number', | |
titles: [ 'String' ] | |
}] | |
} | |
var trueValue = { | |
name: 'Sandeep', | |
id: 1, | |
active: true, | |
projects: [{ | |
name: 'String', | |
id: 1, | |
titles: [ 'JS', 'Core' ] | |
}] | |
} | |
var falseValue = { | |
name: 'Sandeep', | |
id: 1, | |
active: true | |
} | |
// Complete check | |
console.log('\nisValidType tests: \n'); | |
console.log('True: ', isValidType( type, trueValue ) ); | |
console.log('False: ', isValidType( type, falseValue ) ); | |
console.log('False: ', isValidType( { a: 'String' }, { b: 12 } ) ); | |
// console.log('True: ', isValidType( complexType, complexValue ) ); | |
// checkArrayType | |
console.log('\ncheckArrayType tests: \n'); | |
console.log('True: ', isValidType( ['String'], ['a', 'b', 'c'] ) ); | |
console.log('False: ', isValidType( ['Number'], [1, 'a', 3] ) ); | |
// checkObjectType | |
console.log('\ncheckObjectType tests: \n'); | |
console.log('True: ', isValidType( | |
{ a: 'String', b: 'Number', c: 'Boolean' }, | |
{ a: 'Sandeep', b: 123, c: true } | |
)); | |
console.log('False: ', isValidType( | |
{ a: 'String', b: 'Number', c: 'Boolean' }, | |
{ a: 'Sandeep', b: 123, c: 1 } | |
)); | |
// checkPrimitiveType | |
console.log('\ncheckPrimitiveType tests: \n'); | |
console.log('True: ', isValidType( 'String', 'Sandeep' ) ); | |
console.log('True: ', isValidType( 'Number', 1.23 ) ); | |
console.log('True: ', isValidType( 'Boolean', true ) ); | |
console.log('False: ', isValidType( 'String', 1.12 ) ); | |
console.log('False: ', isValidType( 'Number', 'Sandeep' ) ); | |
console.log('False: ', isValidType( 'Boolean', 0.1 ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ES6 Version: