Skip to content

Instantly share code, notes, and snippets.

@nickolasburr
Last active June 14, 2017 01:57
Show Gist options
  • Save nickolasburr/9ebee93c0ac155cc25d54eaf44952a0e to your computer and use it in GitHub Desktop.
Save nickolasburr/9ebee93c0ac155cc25d54eaf44952a0e to your computer and use it in GitHub Desktop.
Static JavaScript microlibrary for basic exception handling, type checking and coercion, and key/value searching. Built for use in autonomous/embedded environments. Compatible from ECMAScript 5 onward.
/**
* Exception methods
*/
var Exception = {};
// Throw TypeError exception with message
Exception.throwTypeError = function (message) {
throw new TypeError(message);
};
// Throw Error exception with message
Exception.throwGenericError = function (message) {
throw new Error(message);
};
/**
* Utility methods
*/
var Utils = Object.create(Exception);
// Coerce `value` to boolean
Utils.toBool = function (value) {
return !!(value);
};
// Coerce `value` to number
Utils.toNumber = function (value) {
return +(value);
};
// Coerce `value` to string
Utils.toString = function (value) {
return ('' + value);
};
// Coerce `value` to array
Utils.toArray = function (value, sep) {
sep = sep || '';
if (!this.isScalar(value)) {
this.throwTypeError('`Utils.toArray` -> `value` must be a scalar, this is a[n] ' + this.getType(value));
}
return this.toString(value).split(sep);
};
// Get primitive type of `arg`
Utils.getType = function (arg) {
return (typeof arg);
};
/**
* Determine if `arg` is null
*
* @note This performs strict checking against `arg`,
* so even if `arg` is a falsey value (e.g. '', 0, false, undefined),
* it will only return true if `arg` contains the null object
*/
Utils.isNull = function (arg) {
return this.toBool(arg === null);
};
// Determine if `arg` is undefined
Utils.isUndefined = function (arg) {
return (this.getType(arg) === 'undefined');
};
// Determine if `arg` is defined (syntactic sugar for negated `Utils.isUndefined`)
Utils.isDefined = function (arg) {
return !this.isUndefined(arg);
};
// Determine if `source` is an instance of `target`
Utils.isInstanceOf = function (source, target) {
return this.toBool(source instanceof target);
};
/**
* Determine if `obj` is of type 'object'
*
* @note This is a **very** loose check on the type 'object', e.g.
* it will return true for an object literal, object instance,
* array literal, array instance, HTMLElement, Node, and so on...
*/
Utils.isObject = function (obj) {
return this.isInstanceOf(obj, Object);
};
/**
* Determine if `obj` is an object constructed from the native
* 'Object' prototype and not a different object constructor
*/
Utils.isObjectNative = function (obj) {
return this.toBool(this.isObject(obj) && Object.getPrototypeOf(obj).constructor.name === 'Object');
};
// Determine if object is empty (has zero properties)
Utils.isObjectEmpty = function (obj) {
if (!this.isObject(obj)) {
this.throwTypeError('`Utils.isObjectEmpty` -> `obj` must be an object, not ' + this.getType(obj));
}
return !this.toBool(Object.keys(obj).length);
};
// Determine if `needle` is in `haystack`
Utils.inArray = function (needle, haystack) {
if (!this.isArray(haystack)) {
this.throwTypeError('`Utils.inArray` -> `haystack` must be an array, not ' + this.getType(haystack));
}
return this.toBool(haystack.indexOf(needle) > -1);
};
// Determine if `arr` is an Array
Utils.isArray = function (arr) {
return this.isInstanceOf(arr, Array);
};
// Determine if `func` is a Function
Utils.isFunc = function (func) {
return this.toBool(this.getType(func) === 'function' && this.isInstanceOf(func, Function));
};
// Determine if `element` is a valid Element object
Utils.isElement = function (element) {
return this.isInstanceOf(element, Element);
};
// Determine if `node` is a valid Node object
Utils.isNode = function (node) {
return this.isInstanceOf(node, Node);
};
// Determine if `arg` is a scalar type
Utils.isScalar = function (arg) {
var scalars = [
'string', 'number', 'boolean'
];
return this.inArray(this.getType(arg), scalars);
};
// Get parent node from Node object
Utils.getParent = function (node) {
if (!this.isNode(node)) {
this.throwTypeError('`Utils.getParent` -> `node` must be a valid Node object!');
}
return node.parentNode;
};
// Get keys from object
Utils.getKeys = function (obj) {
if (!this.isObject(obj)) {
this.throwTypeError('`Utils.getKeys` -> `obj` must be an object, not ' + this.getType(obj));
}
return Object.keys(obj);
};
// Get values from object
Utils.getValues = function (obj) {
if (!this.isObject(obj)) {
this.throwTypeError('`Utils.getValues` -> `obj` must be an object, not ' + this.getType(obj));
}
return Object.values(obj);
};
// Get index of element in array
Utils.getIndexOf = function (needle, haystack) {
// `needle` must be a scalar type in order for us to perform the lookup
if (!this.isScalar(needle)) {
this.throwTypeError('`Utils.getIndexOf` -> `needle` must be a scalar, this is a[n] ' + this.getType(needle));
}
if (!this.isArray(haystack)) {
this.throwTypeError('`Utils.getIndexOf` -> `haystack` must be an array, not ' + this.getType(haystack));
}
return haystack.indexOf(needle);
};
/**
* @description Get last index of `arr`
* @return {number} Last index
*/
Utils.getLastIndex = function (arr) {
if (!this.isArray(arr)) {
this.throwTypeError('`Utils.getLastIndex` -> `arr` must be an array, not ' + this.getType(arr));
}
return this.toNumber(arr.length - 1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment