StructureJS
0.12.7A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.
(function (factory) { if (typeof module === 'object' && typeof module.exports === 'object') { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === 'function' && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; /** * A Utility class that has several static methods to assist in development. * * @class Util * @module StructureJS * @submodule util * @author Robert S. (www.codeBelt.com) * @static */ var Util = (function () { function Util() { throw new Error('[Util] Do not instantiate the Util class because it is a static class.'); } /** * Generates a unique ID. If a prefix is passed in, the value will be appended to it. * * @method uniqueId * @param [prefix] {string} The string value used for the prefix. * @returns {init|string} Returns the unique identifier. * @public * @static * @example * let property = Util.uniqueId(); * // 1 * * let property = Util.uniqueId('prefixName_'); * // prefixName_1 */ Util.uniqueId = function (prefix) { if (prefix === void 0) { prefix = null; } var id = ++Util._idCounter; if (prefix != null) { return String(prefix + id); } else { return id; } }; /** * Removes a list of properties from an object. * * @method deletePropertyFromObject * @param object {Object} The object you want to remove properties from. * @param value {string|Array.<string>} A property name or an array of property names you want to remove from the object. * @returns {any} Returns the object passed in without the removed the properties. * @public * @static * @example * let obj = { name: 'Robert', gender: 'male', phone: '555-555-5555' } * * Util.deletePropertyFromObject(obj, ['phone', 'gender']); * * // { name: 'Robert' } */ Util.deletePropertyFromObject = function (object, value) { // If properties is not an array then make it an array object. var list = (value instanceof Array) ? value : [value]; // Loop through the object properties. for (var key in object) { // If the key is a property and not function. if (object.hasOwnProperty(key)) { var value_1 = object[key]; // If the property is an Array. if (value_1 instanceof Array) { // Loop through the Array and call the Util.deletePropertyFromObject method on each object in the array. var array = value_1; for (var index in array) { // Recursive function call. Util.deletePropertyFromObject(array[index], list); } } else if (value_1 instanceof Object) { Util.deletePropertyFromObject(value_1, list); } else { // Loop through the list of property name. for (var listIndex in list) { // If the key(property name) equals the property name in the list array. if (key === list[listIndex]) { // Delete the property from the object. delete object[key]; } } } } } return object; }; /** * Renames a property name on an object. * * @method renamePropertyOnObject * @param object {Object} The object you want to rename properties from. * @param oldName {string} * @param newName {string} * @returns {any} Returns the object passed in renamed properties. * @public * @static * @example * let obj = { name: 'Robert', gender: 'male', phone: '555-555-5555' } * * Util.renamePropertyOnObject(obj, 'gender', 'sex'); * * // { name: 'Robert', sex: 'male', phone: '555-555-5555' } */ Util.renamePropertyOnObject = function (object, oldName, newName) { // Check for the old property name to avoid a ReferenceError in strict mode. if (object.hasOwnProperty(oldName)) { object[newName] = object[oldName]; delete object[oldName]; } return object; }; /** * Makes a clone of an object. * * @method clone * @param obj {Object} The object you to clone. * @returns {any} Returns a clone object of the one passed in. * @public * @static * @example * let cloneOfObject = Util.clone(obj); */ Util.clone = function (obj) { //other scripts: http://davidwalsh.name/javascript-clone //http://oranlooney.com/functional-javascript/ //http://oranlooney.com/deep-copy-javascript/ // Handle the 3 simple types, and null or undefined if (null == obj || 'object' != typeof obj) { return obj; } // Handle Date if (obj instanceof Date) { var date = new Date(); date.setTime(obj.getTime()); return date; } // Handle Array if (obj instanceof Array) { var array = []; for (var i = 0, len = obj.length; i < len; i++) { array[i] = Util.clone(obj[i]); } return array; } // Handle Object if (obj instanceof Object) { var copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) { copy[attr] = Util.clone(obj[attr]); } } return copy; } throw new Error("[Util] Unable to copy obj! Its type isn't supported."); }; /** * Converts a string or number to a boolean. * * @method toBoolean * @param strNum {string|number} * @returns {boolean} * @public * @static * @example * Util.toBoolean("TRUE"); * // true * * Util.toBoolean(0); * // false * * Util.toBoolean(undefined); * // false */ Util.toBoolean = function (strNum) { var value = (typeof strNum === 'string') ? strNum.toLowerCase() : strNum; return (value > 0 || value == 'true' || value == 'yes'); }; /** * Returns the name of the function/object passed in. * * @method getName * @param classObject {any} * @returns {string} Returns the name of the function or object. * @static * @example * let someClass = new SomeClass(); * Util.getName(someClass); // 'SomeClass' * * Util.getName(function Test(){}); // 'Test' * Util.getName(function (){}); // 'anonymous' */ Util.getName = function (classObject) { var type = typeof classObject; var value; var funcNameRegex = /function ([^\(]+)/; if (type === 'object') { // Gets the name of the object. var results = classObject.constructor.toString().match(funcNameRegex); value = results[1]; } else { // This else code is mainly for Internet Explore. var isFunction = (type === 'function'); // TODO: figure out how to explain this var name_1 = isFunction && ((classObject.name && ['', classObject.name]) || classObject.toString().match(funcNameRegex)); if (isFunction === false) { value = type; } else if (name_1 && name_1[1]) { value = name_1[1]; } else { value = 'anonymous'; } } return value; }; /** * Creates and returns a new debounced version of the passed function which will postpone its execution until after * wait milliseconds have elapsed since the last time it was invoked. * * @method debounce * @param callback {Function} The function that should be executed. * @param wait {number} Milliseconds to elapsed before invoking the callback. * @param immediate {boolean} Pass true for the immediate parameter to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time. * @param callbackScope {any} The scope of the callback function that should be executed. * @public * @static * @example * Util.debounce(this._onBreakpointChange, 250, false, this); */ Util.debounce = function (callback, wait, immediate, callbackScope) { var timeout; var result; var debounced = function () { var args = arguments; function delayed() { if (immediate == false) { result = callback.apply(callbackScope, args); } timeout = null; } if (timeout) { clearTimeout(timeout); } else if (immediate === true) { result = callback.apply(callbackScope, args); } timeout = setTimeout(delayed, wait); return result; }; debounced.cancel = function () { clearTimeout(timeout); }; return debounced; }; /** * TODO: YUIDoc_comment * * @method applyMixins * @param derivedCtor {any} * @param baseCtors {any} * @public * @static * @example * class Flies { fly() { alert('Is it a bird? Is it a plane?'); } } class Climbs { climb() { alert('My spider-sense is tingling.'); } } class HorseflyWoman implements Climbs, Flies { climb: () => void; fly: () => void; } Util.applyMixins(HorseflyWoman, [Climbs, Flies]); */ Util.applyMixins = function (derivedCtor, baseCtors) { baseCtors.forEach(function (baseCtor) { Object.getOwnPropertyNames(baseCtor.prototype).forEach(function (name) { derivedCtor.prototype[name] = baseCtor.prototype[name]; }); }); }; /** * Returns a new array with duplicates removed. * * @method unique * @param list {Array.<any>} The array you want to use to generate the unique array. * @return {Array<any>} Returns a new array list of unique items. * @protected */ Util.unique = function (list) { var uniqueList = list.reduce(function (previousValue, currentValue) { if (previousValue.indexOf(currentValue) === -1) { previousValue.push(currentValue); } return previousValue; }, []); return uniqueList; }; /** * Keeps track of the count for the uniqueId method. * * @property _idCounter * @type {int} * @private * @static */ Util._idCounter = 0; return Util; }()); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Util; });