Last active
April 19, 2019 14:24
-
-
Save matthewstokeley/6823f80f61c586359b88f58de5fa56ee to your computer and use it in GitHub Desktop.
whatis - returns type
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
var given = function(object, property) { | |
var does = (property) => given(object, property); | |
var exist = () => { | |
if (!is(object).object) return false | |
!object.hasOwnProperty(property) | |
? false | |
: true | |
} | |
return { | |
does: does, | |
exist: exist | |
} | |
}) | |
/** | |
* ht to bash for beginners for the introduction | |
to 'whereis' and 'which'-style *nix naming conventions, to the 'hapi' library | |
that is used in a popular TDD lib and to vue.js for their ternary operator syntax | |
* @return {Object} an object of public methods for determining type | |
*/ | |
var is = (function(property) { | |
var property = property; | |
var isObject = () => is(property).array() | |
? false | |
: isType(property, 'object') | |
var isArray = () => !Array.isArray(property) | |
? false | |
: true | |
var isString = () => isType(property, 'string') | |
var isNumber = () => isType(property, 'number') | |
var isBoolean = () => isType(property, 'boolean') | |
//var isInteger = (property) => property === Math.floor(property) | |
// ? true | |
// : false | |
/** | |
* returns a boolean if strings match. | |
*/ | |
var isType = (type) => typeof property !== type | |
? false | |
: true; | |
var isDomProperty = function(type) {}; | |
var isDomElement = function() {}; | |
var isElementList = function() {}; | |
var dom = () => !is(property).object() | |
? false | |
: given(property).does('tagname').exist() | |
var element = () => !is(property).object() | |
? false | |
: given(property).does('tagName').exist() | |
var node = () => ! is(property).object() | |
? false | |
: given(property).does('nodeName').exist() | |
var type = (type) => !is[type] | |
? undefined | |
: is(property)[type]() | |
return { | |
object: isObject, | |
array: isArray, | |
string: (property) => isType(property, 'string'), | |
number: (property) => isType(property, 'number'), | |
boolean: (property) => isType(property, 'boolean'), | |
typeof: isType, | |
type: type, | |
dom: dom, | |
element: element, | |
node: node | |
}; | |
})(); | |
var whatis = (prop) => { | |
switch prop { | |
case is(prop).boolean(): | |
return 'boolean' | |
break; | |
case is(prop).string(): | |
return 'string' | |
break; | |
//... | |
} | |
} | |
// Literate syntax | |
// ------------------------------ | |
is(false).boolean(); | |
// returns true | |
given(object).does('property').exist() | |
// returns true | |
whatis(false); | |
// returns 'boolean;; | |
// Original syntax | |
// ------------------------------- | |
is.boolean(false); | |
// return true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
drafting versions of api's with consideration for performance costs and literacy.