Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. /*jslint
  2. browser
  3. */
  4.  
  5. /*global
  6. window
  7. */
  8.  
  9. // a utility library similar to underscore.js
  10. (function () {
  11.  
  12. "use strict";
  13.  
  14. // client only for now
  15. var global = window;
  16.  
  17. // store public methods here
  18. var Pub = {};
  19.  
  20. // create quick references
  21. var nativeSlice = Array.prototype.slice;
  22. var nativeToString = Object.prototype.toString;
  23.  
  24. // set your global variable with glob
  25. Pub.globalManager = (function () {
  26. var glob = "$A";
  27. var previous = global[glob];
  28. var pub = {};
  29.  
  30. // package name is set here
  31. // this is the first component
  32. Pub.pack = {
  33. utility: true
  34. };
  35.  
  36. // set the global property
  37. pub.release = function () {
  38. global[glob] = Pub.extendSafe(global[glob] || {}, Pub);
  39. };
  40.  
  41. // return the global property back to its original owner
  42. pub.noConflict = function () {
  43. var temp = global[glob];
  44. global[glob] = previous;
  45. return temp;
  46. };
  47. return pub;
  48. }());
  49.  
  50.  
  51. // section - type checking, looping
  52.  
  53. // returns type in a capitalized string form
  54. // typeof fails for array ( reports as object - true but inconsistent and misleading )
  55. // typoef fails for null ( reports as object - it is type Null )
  56. // typeof fails for regular expressions ( reported as function - it is type Object or Regular Expression)
  57. Pub.getType = function (obj) {
  58. return nativeToString.call(obj).slice(8, -1);
  59. };
  60.  
  61. // Number, String, Boolean, Null, Undefined, Object and its children
  62. Pub.isType = function (type, obj) {
  63. return Pub.getType(obj) === type;
  64. };
  65.  
  66. // Number, String, Boolean, Null, Undefined
  67. // Note that null and undefined will throw an error if you try to add a property
  68. Pub.isNative = function (check) {
  69. var t = Pub.getType(check);
  70. if (t === "Number" || t === "String" || t === "Boolean" || t === "Null" || t === "Undefined") {
  71. return true;
  72. }
  73. return false;
  74. };
  75.  
  76. // if it is not a native it is an object
  77. Pub.isObj = function (check) {
  78. return !Pub.isNative(check);
  79. };
  80.  
  81. // on a truthy match returns true and breaks from loop
  82. // otherwise returns false, similar to Array.some()
  83. // underscore equivalent "cornerstone" fails b.c. of storage duality
  84. Pub.forEach = function (obj, func, con) {
  85. if (Pub.isType("Function", func)) {
  86. Object.keys(obj).forEach(function (key) {
  87. if (func.call(con, obj[key], key, obj)) {
  88. return true;
  89. }
  90. });
  91. }
  92. return false;
  93. };
  94.  
  95. // syntactical sugar
  96. // 5 primitive types include Null, Undefined, Number, String, and Boolean
  97. Pub.forEach(["Boolean", "Number", "String", "Null", "Undefined"], function (type) {
  98. Pub["is" + type] = function (obj) {
  99. return Pub.isType(type, obj);
  100. };
  101. });
  102.  
  103. // add object sub-types here as needed
  104. // split for conceptual purposes - handles remaining Object and sub types
  105. Pub.forEach(["Object", "Array", "Function", "RegExp"], function (type) {
  106. Pub["is" + type] = function (obj) {
  107. return Pub.isType(type, obj);
  108. };
  109. });
  110.  
  111. // section - string mannipulation, cloning, extending
  112.  
  113. // useful for looping through className
  114. Pub.someString = function (str, func, con) {
  115. if (!Pub.isString(str)) {
  116. return false;
  117. }
  118. return Pub.forEach(str.split(/s+/), func, con);
  119. };
  120. Pub.forSomeString = Pub.someString;
  121.  
  122. // morphs ( applies function ) to the values of an object
  123. Pub.morph = function (obj, func, con) {
  124. if (Pub.isType("Function", func)) {
  125. return false;
  126. }
  127. Pub.forEach(obj, function (val, key) {
  128. obj[key] = func.call(con, val);
  129. });
  130. return obj;
  131. };
  132.  
  133. // useful for setting default values
  134. Pub.lacks = function (obj) {
  135. Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
  136. Pub.forEach(object, function (val, key) {
  137. if (obj[key] === undefined) {
  138. obj[key] = val;
  139. }
  140. });
  141. });
  142. return obj;
  143. };
  144.  
  145. // shallow ( does not recurse in ) and ( no prototype chaining ) flat clone
  146. Pub.cloneFlat = function (obj) {
  147. return Pub.isArray(obj)
  148. ? obj.slice()
  149. : Pub.extendFlat({}, obj);
  150. };
  151.  
  152. // close
  153. Pub.cloneDeep = function (obj) {
  154. Pub.forEach(nativeSlice.call(arguments, 1), function (val) {
  155. Pub.forEach(val, function (val_inner, key) {
  156. obj[key] = JSON.parse(JSON.stringify(val_inner));
  157. });
  158. });
  159. return obj;
  160. };
  161.  
  162. // relies on Pub.forEach
  163. // does not extend up the prototype chain like underscore ( flat )
  164. Pub.extendFlat = function (obj) {
  165. if (Pub.isNative(obj)) {
  166. return obj;
  167. }
  168. Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
  169. Pub.forEach(object, function (val, key) {
  170. obj[key] = val;
  171. });
  172. });
  173. return obj;
  174. };
  175.  
  176. // relies on Pub.forEach
  177. // extends non-prototype properties with out any over writing.
  178. Pub.extendSafe = function (obj) {
  179. Pub.forEach(nativeSlice.call(arguments, 1), function (object) {
  180. Pub.forEach(object, function (val, key) {
  181. if (obj.hasOwnProperty(key)) {
  182. throw new Error("naming collision: " + key);
  183. }
  184. obj[key] = val;
  185. });
  186. });
  187. return obj;
  188. };
  189.  
  190. // for removing the last underscore and suffix
  191. Pub.removeU = function (str) {
  192. var res = str.lastIndexOf("_");
  193. if (res !== -1) {
  194. return str.slice(0, res);
  195. }
  196. return str;
  197. };
  198.  
  199. Pub.oneKey = function (obj) {
  200. var arr = Object.keys(obj);
  201. if (arr[0]) {
  202. return arr[0];
  203. }
  204. return false;
  205. };
  206.  
  207. Pub.randomKey = function (obj) {
  208. var arr = Object.keys(obj);
  209. var rand = arr[Math.floor(Math.random() * arr.length)];
  210. return rand;
  211. };
  212.  
  213. Pub.nextKey = function (obj, mark) {
  214. var arr = Object.keys(obj);
  215. var last = arr.length - 1;
  216. arr.forEach(function (index) {
  217. if (arr[index] === mark && (index === last)) {
  218. return arr[0];
  219. }
  220. if (arr[index] === mark) {
  221. return arr[index + 1];
  222. }
  223. });
  224. return false;
  225. };
  226.  
  227. Pub.nextIndex = function (arr, mark) {
  228. var last = arr.length - 1;
  229. arr.forEach(function (index) {
  230. if (index === mark && index === last) {
  231. return 0;
  232. }
  233. if (index === mark) {
  234. return index + 1;
  235. }
  236. });
  237. return false;
  238. };
  239.  
  240. Pub.stringifyKeys = function (obj) {
  241. var arr = Object.keys(obj);
  242. var string = "";
  243. arr.forEach(function (index) {
  244. string += arr[index];
  245. });
  246. return string;
  247. };
  248.  
  249. // all done relase code to the global object
  250. Pub.globalManager.release();
  251.  
  252. }());
  253.  
  254. // registry and event modules
  255. (function () {
  256.  
  257. "use strict";
  258.  
  259. var global = window;
  260.  
  261. // reference public, private and imported methods
  262. var Pub = {};
  263. var Priv = {};
  264.  
  265. // set and validate the global variable
  266. var Imp = (function manageGlobal() {
  267.  
  268. // Priv.g holds the single global variable
  269. Priv.g = "$A";
  270.  
  271. if (global[Priv.g] && global[Priv.g].pack && global[Priv.g].pack.utility) {
  272. global[Priv.g].pack.comms = true;
  273. } else {
  274. throw new Error("comms requires utility package");
  275. }
  276. return global[Priv.g];
  277. }());
  278.  
  279. Pub.Reg = (function () {
  280. var publik = {};
  281. var register = {};
  282. publik.get = function (key) {
  283. return register[key];
  284. };
  285. publik.set = function (test, value) {
  286. if (typeof test === "string") {
  287. register[test] = value;
  288. } else if (Imp.isType("Object", test)) {
  289. publik.setMany(test);
  290. } else {
  291. throw new Error("arguments not supported");
  292. }
  293. };
  294. publik.setMany = function () {
  295. Imp.forEach(arguments, function (obj) {
  296. Imp.forEach(obj, function (val_inner, key) {
  297. register[key] = val_inner;
  298. });
  299. });
  300. };
  301. return publik;
  302. }());
  303.  
  304. Pub.Event = (function () {
  305. var publik = {};
  306. var events = {};
  307.  
  308. // add an event name and callback
  309. publik.add = function (name, callback) {
  310. events[name] = events[name] || [];
  311. events[name].push(callback);
  312. };
  313.  
  314. // remove an event name/callback or all callbacks for that name
  315. publik.remove = function (name, callback) {
  316. if (name && callback) {
  317. delete events[name][callback];
  318. } else if (name) {
  319. delete events[name];
  320. }
  321. };
  322.  
  323. // trigger an event on all callbacks for that name
  324. publik.trigger = function (name) {
  325. if (events[name]) {
  326. Imp.forEach(events[name], function (val) {
  327. val();
  328. });
  329. }
  330. };
  331. publik.release = function () {
  332. return events;
  333. };
  334. return publik;
  335. }());
  336.  
  337. global[Priv.g] = Imp.extendSafe(global[Priv.g], Pub);
  338.  
  339. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement