1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 | 1
1
1
1
1
1
1
1
1
1
1
1
1
3
3
3
3
3
6
2
1
1
1
1
1
1
1
1
19
19
1
2
2
2
2
2
2
2
2
1
1
2
1
1
1
1
1
7
3
7
1
1
1
1
1
| (function (factory) {
Eif (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); Iif (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) {
Eif (prefix === void 0) { prefix = null; }
var id = ++Util._idCounter;
Iif (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.
Eif (object.hasOwnProperty(key)) {
var value_1 = object[key];
// If the property is an Array.
Iif (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 Iif (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.
Eif (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 ([^\(]+)/;
Iif (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));
Iif (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;
});
|