Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jslint
- browser
- */
- /*global
- window
- */
- // a utility library similar to underscore.js
- (function () {
- "use strict";
- // client only for now
- var global = window;
- // store public methods here
- var Pub = {};
- // create quick references
- var nativeSlice = Array.prototype.slice;
- var nativeToString = Object.prototype.toString;
- // set your global variable with glob
- Pub.globalManager = (function () {
- var glob = "$A";
- var previous = global[glob];
- var pub = {};
- // package name is set here
- // this is the first component
- Pub.pack = {
- utility: true
- };
- // set the global property
- pub.release = function () {
- global[glob] = Pub.extendSafe(global[glob] || {}, Pub);
- };
- // return the global property back to its original owner
- pub.noConflict = function () {
- var temp = global[glob];
- global[glob] = previous;
- return temp;
- };
- return pub;
- }());
- // section - type checking, looping
- // returns type in a capitalized string form
- // typeof fails for array ( reports as object - true but inconsistent and misleading )
- // typoef fails for null ( reports as object - it is type Null )
- // typeof fails for regular expressions ( reported as function - it is type Object or Regular Expression)
- Pub.getType = function (obj) {
- return nativeToString.call(obj).slice(8, -1);
- };
- // Number, String, Boolean, Null, Undefined, Object and its children
- Pub.isType = function (type, obj) {
- return Pub.getType(obj) === type;
- };
- // Number, String, Boolean, Null, Undefined
- // Note that null and undefined will throw an error if you try to add a property
- Pub.isNative = function (check) {
- var t = Pub.getType(check);
- if (t === "Number" || t === "String" || t === "Boolean" || t === "Null" || t === "Undefined") {
- return true;
- }
- return false;
- };
- // if it is not a native it is an object
- Pub.isObj = function (check) {
- return !Pub.isNative(check);
- };
- // on a truthy match returns true and breaks from loop
- // otherwise returns false, similar to Array.some()
- // underscore equivalent "cornerstone" fails b.c. of storage duality
- Pub.forEach = function (obj, func, con) {
- if (Pub.isType("Function", func)) {
- Object.keys(obj).forEach(function (key) {
- if (func.call(con, obj[key], key, obj)) {
- return true;
- }
- });
- }
- return false;
- };
- // syntactical sugar
- // 5 primitive types include Null, Undefined, Number, String, and Boolean
- Pub.forEach(["Boolean", "Number", "String", "Null", "Undefined"], function (type) {
- Pub["is" + type] = function (obj) {
- return Pub.isType(type, obj);
- };
- });
- // add object sub-types here as needed
- // split for conceptual purposes - handles remaining Object and sub types
- Pub.forEach(["Object", "Array", "Function", "RegExp"], function (type) {
- Pub["is" + type] = function (obj) {
- return Pub.isType(type, obj);
- };
- });
- // section - string mannipulation, cloning, extending
- // useful for looping through className
- Pub.someString = function (str, func, con) {
- if (!Pub.isString(str)) {
- return false;
- }
- return Pub.forEach(str.split(/s+/), func, con);
- };
- Pub.forSomeString = Pub.someString;
- // morphs ( applies function ) to the values of an object
- Pub.morph = function (obj, func, con) {
- if (Pub.isType("Function", func)) {
- return false;
- }
- Pub.forEach(obj, function (val, key) {
- obj[key] = func.call(con, val);
- });
- return obj;
- };
- // useful for setting default values
- Pub.lacks = function (obj) {
- Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
- Pub.forEach(object, function (val, key) {
- if (obj[key] === undefined) {
- obj[key] = val;
- }
- });
- });
- return obj;
- };
- // shallow ( does not recurse in ) and ( no prototype chaining ) flat clone
- Pub.cloneFlat = function (obj) {
- return Pub.isArray(obj)
- ? obj.slice()
- : Pub.extendFlat({}, obj);
- };
- // close
- Pub.cloneDeep = function (obj) {
- Pub.forEach(nativeSlice.call(arguments, 1), function (val) {
- Pub.forEach(val, function (val_inner, key) {
- obj[key] = JSON.parse(JSON.stringify(val_inner));
- });
- });
- return obj;
- };
- // relies on Pub.forEach
- // does not extend up the prototype chain like underscore ( flat )
- Pub.extendFlat = function (obj) {
- if (Pub.isNative(obj)) {
- return obj;
- }
- Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
- Pub.forEach(object, function (val, key) {
- obj[key] = val;
- });
- });
- return obj;
- };
- // relies on Pub.forEach
- // extends non-prototype properties with out any over writing.
- Pub.extendSafe = function (obj) {
- Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
- Pub.forEach(object, function (val, key) {
- if (obj.hasOwnProperty(key)) {
- throw new Error("naming collision: " + key);
- }
- obj[key] = val;
- });
- });
- return obj;
- };
- // for removing the last underscore and suffix
- Pub.removeU = function (str) {
- var res = str.lastIndexOf("_");
- if (res !== -1) {
- return str.slice(0, res);
- }
- return str;
- };
- Pub.oneKey = function (obj) {
- var arr = Object.keys(obj);
- if (arr[0]) {
- return arr[0];
- }
- return false;
- };
- Pub.randomKey = function (obj) {
- var arr = Object.keys(obj);
- var rand = arr[Math.floor(Math.random() * arr.length)];
- return rand;
- };
- Pub.nextKey = function (obj, mark) {
- var arr = Object.keys(obj);
- var last = arr.length - 1;
- arr.forEach(function (index) {
- if (arr[index] === mark && (index === last)) {
- return arr[0];
- }
- if (arr[index] === mark) {
- return arr[index + 1];
- }
- });
- return false;
- };
- Pub.nextIndex = function (arr, mark) {
- var last = arr.length - 1;
- arr.forEach(function (index) {
- if (index === mark && index === last) {
- return 0;
- }
- if (index === mark) {
- return index + 1;
- }
- });
- return false;
- };
- Pub.stringifyKeys = function (obj) {
- var arr = Object.keys(obj);
- var string = "";
- arr.forEach(function (index) {
- string += arr[index];
- });
- return string;
- };
- // all done relase code to the global object
- Pub.globalManager.release();
- }());
- // registry and event modules
- (function () {
- "use strict";
- var global = window;
- // reference public, private and imported methods
- var Pub = {};
- var Priv = {};
- // set and validate the global variable
- var Imp = (function manageGlobal() {
- // Priv.g holds the single global variable
- Priv.g = "$A";
- if (global[Priv.g] && global[Priv.g].pack && global[Priv.g].pack.utility) {
- global[Priv.g].pack.comms = true;
- } else {
- throw new Error("comms requires utility package");
- }
- return global[Priv.g];
- }());
- Pub.Reg = (function () {
- var publik = {};
- var register = {};
- publik.get = function (key) {
- return register[key];
- };
- publik.set = function (test, value) {
- if (typeof test === "string") {
- register[test] = value;
- } else if (Imp.isType("Object", test)) {
- publik.setMany(test);
- } else {
- throw new Error("arguments not supported");
- }
- };
- publik.setMany = function () {
- Imp.forEach(arguments, function (obj) {
- Imp.forEach(obj, function (val_inner, key) {
- register[key] = val_inner;
- });
- });
- };
- return publik;
- }());
- Pub.Event = (function () {
- var publik = {};
- var events = {};
- // add an event name and callback
- publik.add = function (name, callback) {
- events[name] = events[name] || [];
- events[name].push(callback);
- };
- // remove an event name/callback or all callbacks for that name
- publik.remove = function (name, callback) {
- if (name && callback) {
- delete events[name][callback];
- } else if (name) {
- delete events[name];
- }
- };
- // trigger an event on all callbacks for that name
- publik.trigger = function (name) {
- if (events[name]) {
- Imp.forEach(events[name], function (val) {
- val();
- });
- }
- };
- publik.release = function () {
- return events;
- };
- return publik;
- }());
- global[Priv.g] = Imp.extendSafe(global[Priv.g], Pub);
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement