Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active April 19, 2019 14:24
Show Gist options
  • Save matthewstokeley/6823f80f61c586359b88f58de5fa56ee to your computer and use it in GitHub Desktop.
Save matthewstokeley/6823f80f61c586359b88f58de5fa56ee to your computer and use it in GitHub Desktop.
whatis - returns type
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
@matthewstokeley
Copy link
Author

drafting versions of api's with consideration for performance costs and literacy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment