Last active
January 29, 2020 14:25
-
-
Save martinandersen3d/ac57f54a2893bb391963030322305e57 to your computer and use it in GitHub Desktop.
javascript type checking mini library
This file contains hidden or 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
/* | |
Check Type, is used in the beginning of a method. | |
Here we will check that the Method Parameters is of the correct type. | |
*/ | |
class Check { | |
constructor(obj, errorCode) { | |
this.obj = obj; | |
this.errorCode = errorCode; | |
this.succes = false | |
// Guess the object type, as string | |
this.objectType = ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() | |
} | |
stringOrNull(){ | |
if (this.objectType === 'string' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('stringOrNull()\nVariable is not String or Null, but is: ') | |
} | |
return this | |
} | |
string(){ | |
if ( this.objectType === 'string' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('string()\nVariable is not String, but is: ') | |
} | |
return this | |
} | |
object(){ | |
if ( (!!this.obj) && (this.obj.constructor === Object) && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('object()\nVariable is not Object, but is: ') | |
} | |
return this | |
} | |
objectOrNull(){ | |
if ( (!!this.obj) && (this.obj.constructor === Object) || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('objectOrNull()\nVariable is not Object or Null, but is: ') | |
} | |
return this | |
} | |
array(){ | |
if ( this.objectType === 'array' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('array()\nVariable is not Array, but is: ') | |
} | |
return this | |
} | |
arrayOrNull(){ | |
if ( this.objectType === 'array' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('arrayOrNull()\nVariable is not Array or Null, but is: ') | |
} | |
return this | |
} | |
number(){ | |
if ( this.objectType === 'number' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('array()\nVariable is not Number, but is: ') | |
} | |
return this | |
} | |
numberOrNull(){ | |
if ( this.objectType === 'number' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('numberOrNull()\nVariable is not Number or Null, but is: ') | |
} | |
return this | |
} | |
boolean(){ | |
if ( this.objectType === 'boolean' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('boolean()\nVariable is not Boolean, but is: ') | |
} | |
return this | |
} | |
booleanOrNull(){ | |
if ( this.objectType === 'boolean' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('booleanOrNull()\nVariable is not Number or Null, but is: ') | |
} | |
return this | |
} | |
date(){ | |
if ( this.objectType === 'date' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('date()\nVariable is not Date, but is: ') | |
} | |
return this | |
} | |
dateOrNull(){ | |
if ( this.objectType === 'date' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('dateOrNull()\nVariable is not Number or Null, but is: ') | |
} | |
return this | |
} | |
json(){ | |
if ( this.objectType === 'json' && this.obj !== null) | |
this.succes = true | |
else{ | |
this.thowError('json()\nVariable is not JSON, but is: ') | |
} | |
return this | |
} | |
jsonOrNull(){ | |
if ( this.objectType === 'json' || this.obj === null) | |
this.succes = true | |
else{ | |
this.thowError('jsonOrNull()\nVariable is not JSON or Null, but is: ') | |
} | |
return this | |
} | |
thowError(strError){ | |
let guessObjectType = (Object.prototype.toString.call(this.obj)).toString().split(' ')[1].slice(0, -1); | |
let e = new Error(); | |
let stacktraceAsArray = String(e.stack).split('\n') | |
let line = stacktraceAsArray[3].slice(7).replace('(', '').replace(')', '') | |
let method = line.split(' ')[0] | |
// split to array, and take last element in array | |
let filename = line.split('/').pop() | |
let filenameShort = filename.split(':')[0] | |
let lineNumber = filename.split(':')[1] | |
let fileMethodLineNumber = 'Filename: ' + filenameShort + '\nMethod: ' + method + '()' + '\nLine: ' + lineNumber; | |
let errorString = '\n\n'+ strError + guessObjectType + '\n' +fileMethodLineNumber + '\nErrorCode(optional): ' + this.errorCode + '\n\nStacktrace:' | |
throw new Error(errorString); | |
} | |
} | |
/** | |
* Type() will check if the Method-Parameter-Variable is of the correct type. | |
* | |
* How to use, examples: | |
* | |
* type(['a', 'b']).array() | |
* type(null).arrayOrNull() | |
* type(true).boolean() | |
* type(42).number() | |
* type({a: 3}).object() | |
* type('Hello World').stringOrNull() | |
* | |
* @param {*} object: any variable | |
* @param {number} [errorCode=0] Optional: You can add a errorcode, but is not required. | |
* @returns | |
*/ | |
function type(object, errorCode = 0) { | |
let result = new Check(object, errorCode) | |
return result; | |
} | |
function myMethod(joe){ | |
// Check: Throw Exeption if wrong type | |
type( joe ).array() | |
type( joe ).arrayOrNull() | |
type( joe ).boolean() | |
type( joe ).number() | |
type( joe ).object() | |
type( joe ).stringOrNull() | |
// -------------------------------- | |
// Method Content... | |
} | |
myMethod( ['a', 'b'] ) | |
myMethod( null ) | |
myMethod( true ) | |
myMethod( 42 ) | |
myMethod( {'a': 3} ) | |
myMethod( 'Hello World' ) | |
// TODO: Lav syntaxen om til: | |
type.int(age, heigth) | |
type.nullable.int() eller type.null.int() eller type.nullOr.int(age, heigth) | |
type.check(age > 18, heigth > 120, heigth < 180 ) | |
// Todo: Sikre at metode værdier returner destrictable værdier | |
function person(){ | |
return {name: 'james', age: 33} | |
} | |
Let {name, age} = person() | |
//> name: 'james' | |
//> age: 33 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment