Last active
July 10, 2017 14:10
-
-
Save vegarringdal/c6374ead493ad7f19b4ddc0eae83b356 to your computer and use it in GitHub Desktop.
arrow function
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 if "x" have attribute "attr" with option to return it | |
| const haveAttr = (x: any, attr: string, returnVal?: boolean): boolean | any => !x ? false : !x.hasOwnProperty(attr) ? false : returnVal ? x[attr] : true; | |
| const isArray = (x: any): boolean => (!!x) && (x.constructor === Array); | |
| const isObject = (x: any): boolean => (!!x) && (x.constructor === Object); | |
| const isDate = (x: any): boolean => (!!x) && (x.constructor === Date); | |
| const assert = (result, mustbe: any, name: string, check: any) => | |
| result === mustbe ? | |
| console.log(`Success: ${name}, check ${check} is ${mustbe}`) : | |
| console.log(`-----Error-----: ${name}, check ${check} is ${mustbe}`); | |
| console.log('\n'); | |
| assert(haveAttr(null, 'name'), false, 'haveAttr(null, "name")', null); | |
| assert(haveAttr(undefined, 'name'), false, 'haveAtt', undefined); | |
| assert(haveAttr({}, 'name'), false, 'haveAtt', JSON.stringify({})); | |
| assert(haveAttr({wrong: true}, 'name'), false, 'haveAtt', JSON.stringify({wrong: true})); | |
| assert(haveAttr({Name: true}, 'name'), false, 'haveAtt', JSON.stringify({Name: true})); | |
| assert(haveAttr({name: undefined}, 'name'), true, 'haveAtt', JSON.stringify({name: undefined}) + ' att have undefined value...so only showing {}'); | |
| assert(haveAttr({name: false}, 'name'), true, 'haveAtt', JSON.stringify({name: false})); | |
| assert(haveAttr({name: null}, 'name'), true, 'haveAtt', JSON.stringify({name: null})); | |
| assert(haveAttr(new Date(), 'name'), false, 'haveAtt', JSON.stringify(new Date())); | |
| assert(haveAttr([], 'name'), false, 'haveAtt', JSON.stringify([])); | |
| assert(haveAttr(NaN, 'name'), false, 'haveAtt', NaN); | |
| assert(haveAttr(1, 'name'), false, 'haveAtt', 1); | |
| assert(haveAttr(1.1, 'name'), false, 'haveAtt', 1.1); | |
| console.log('\n'); | |
| assert(isArray(null), false, 'isArray', null); | |
| assert(isArray(undefined), false, 'isArray', undefined); | |
| assert(isArray({}), false, 'isArray', JSON.stringify({})); | |
| assert(isArray({wrong: true}), false, 'isArray', JSON.stringify({wrong: true})); | |
| assert(isArray({Name: true}), false, 'isArray', JSON.stringify({Name: true})); | |
| assert(isArray({name: undefined}), false, 'isArray', JSON.stringify({name: undefined}) + ' att have undefined value...so only showing {}'); | |
| assert(isArray({name: false}), false, 'isArray', JSON.stringify({name: false})); | |
| assert(isArray({name: null}), false, 'isArray', JSON.stringify({name: null})); | |
| assert(isArray(new Date()), false, 'isArray', JSON.stringify(new Date())); | |
| assert(isArray([]), true, 'isArray', JSON.stringify([])); | |
| assert(isArray(NaN), false, 'isArray', NaN); | |
| assert(isArray(1), false, 'isArray', 1); | |
| assert(isArray(1.1), false, 'isArray', 1.1); | |
| console.log('\n'); | |
| assert(isObject(null), false, 'isObject', null); | |
| assert(isObject(undefined), false, 'isObject', undefined); | |
| assert(isObject({}), true, 'isObject', JSON.stringify({})); | |
| assert(isObject({wrong: true}), true, 'isObject', JSON.stringify({wrong: true})); | |
| assert(isObject({Name: true}), true, 'isObject', JSON.stringify({Name: true})); | |
| assert(isObject({name: undefined}), true, 'isObject', JSON.stringify({name: undefined}) + ' att have undefined value...so only showing {}'); | |
| assert(isObject({name: false}), true, 'isObject', JSON.stringify({name: false})); | |
| assert(isObject({name: null}), true, 'isObject', JSON.stringify({name: null})); | |
| assert(isObject(new Date()), false, 'isObject', JSON.stringify(new Date())); | |
| assert(isObject([]), false, 'isObject', JSON.stringify([])); | |
| assert(isObject(NaN), false, 'isObject', NaN); | |
| assert(isObject(1), false, 'isObject', 1); | |
| assert(isObject(1.1), false, 'isObject', 1.1); | |
| console.log('\n'); | |
| assert(isDate(null), false, 'isDate', null); | |
| assert(isDate(undefined), false, 'isDate', undefined); | |
| assert(isDate({}), false, 'isDate', JSON.stringify({})); | |
| assert(isDate({wrong: true}), false, 'isDate', JSON.stringify({wrong: true})); | |
| assert(isDate({Name: true}), false, 'isDate', JSON.stringify({Name: true})); | |
| assert(isDate({name: undefined}), false, 'isDate', JSON.stringify({name: undefined}) + ' att have undefined value...so only showing {}'); | |
| assert(isDate({name: false}), false, 'isDate', JSON.stringify({name: false})); | |
| assert(isDate({name: null}), false, 'isDate', JSON.stringify({name: null})); | |
| assert(isDate(new Date()), true, 'isDate', JSON.stringify(new Date())); | |
| assert(isDate([]), false, 'isDate', JSON.stringify([])); | |
| assert(isDate(NaN), false, 'isDate', NaN); | |
| assert(isDate(1), false, 'isDate', 1); | |
| assert(isDate(1.1), false, 'isDate', 1.1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment