]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/underscore-plus/lib/underscore-plus.js
32eb482085abb7c2242f123575a6dc17c8f5766b
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / underscore-plus / lib / underscore-plus.js
1 (function() {
2 var isEqual, isPlainObject, macModifierKeyMap, nonMacModifierKeyMap, plus, shiftKeyMap, splitKeyPath, _,
3 __slice = [].slice;
4
5 _ = require('underscore');
6
7 macModifierKeyMap = {
8 cmd: '\u2318',
9 ctrl: '\u2303',
10 alt: '\u2325',
11 option: '\u2325',
12 shift: '\u21e7',
13 enter: '\u23ce',
14 left: '\u2190',
15 right: '\u2192',
16 up: '\u2191',
17 down: '\u2193'
18 };
19
20 nonMacModifierKeyMap = {
21 cmd: 'Cmd',
22 ctrl: 'Ctrl',
23 alt: 'Alt',
24 option: 'Alt',
25 shift: 'Shift',
26 enter: 'Enter',
27 left: 'Left',
28 right: 'Right',
29 up: 'Up',
30 down: 'Down'
31 };
32
33 shiftKeyMap = {
34 '~': '`',
35 '_': '-',
36 '+': '=',
37 '|': '\\',
38 '{': '[',
39 '}': ']',
40 ':': ';',
41 '"': '\'',
42 '<': ',',
43 '>': '.',
44 '?': '/'
45 };
46
47 splitKeyPath = function(keyPath) {
48 var char, i, keyPathArray, startIndex, _i, _len;
49 startIndex = 0;
50 keyPathArray = [];
51 if (keyPath == null) {
52 return keyPathArray;
53 }
54 for (i = _i = 0, _len = keyPath.length; _i < _len; i = ++_i) {
55 char = keyPath[i];
56 if (char === '.' && (i === 0 || keyPath[i - 1] !== '\\')) {
57 keyPathArray.push(keyPath.substring(startIndex, i));
58 startIndex = i + 1;
59 }
60 }
61 keyPathArray.push(keyPath.substr(startIndex, keyPath.length));
62 return keyPathArray;
63 };
64
65 isPlainObject = function(value) {
66 return _.isObject(value) && !_.isArray(value);
67 };
68
69 plus = {
70 adviseBefore: function(object, methodName, advice) {
71 var original;
72 original = object[methodName];
73 return object[methodName] = function() {
74 var args;
75 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
76 if (advice.apply(this, args) !== false) {
77 return original.apply(this, args);
78 }
79 };
80 },
81 camelize: function(string) {
82 if (string) {
83 return string.replace(/[_-]+(\w)/g, function(m) {
84 return m[1].toUpperCase();
85 });
86 } else {
87 return '';
88 }
89 },
90 capitalize: function(word) {
91 if (!word) {
92 return '';
93 }
94 if (word.toLowerCase() === 'github') {
95 return 'GitHub';
96 } else {
97 return word[0].toUpperCase() + word.slice(1);
98 }
99 },
100 compactObject: function(object) {
101 var key, newObject, value;
102 newObject = {};
103 for (key in object) {
104 value = object[key];
105 if (value != null) {
106 newObject[key] = value;
107 }
108 }
109 return newObject;
110 },
111 dasherize: function(string) {
112 if (!string) {
113 return '';
114 }
115 string = string[0].toLowerCase() + string.slice(1);
116 return string.replace(/([A-Z])|(_)/g, function(m, letter) {
117 if (letter) {
118 return "-" + letter.toLowerCase();
119 } else {
120 return "-";
121 }
122 });
123 },
124 deepClone: function(object) {
125 if (_.isArray(object)) {
126 return object.map(function(value) {
127 return plus.deepClone(value);
128 });
129 } else if (_.isObject(object) && !_.isFunction(object)) {
130 return plus.mapObject(object, (function(_this) {
131 return function(key, value) {
132 return [key, plus.deepClone(value)];
133 };
134 })(this));
135 } else {
136 return object;
137 }
138 },
139 deepExtend: function(target) {
140 var i, key, object, result, _i, _len, _ref;
141 result = target;
142 i = 0;
143 while (++i < arguments.length) {
144 object = arguments[i];
145 if (isPlainObject(result) && isPlainObject(object)) {
146 _ref = Object.keys(object);
147 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
148 key = _ref[_i];
149 result[key] = plus.deepExtend(result[key], object[key]);
150 }
151 } else {
152 result = plus.deepClone(object);
153 }
154 }
155 return result;
156 },
157 deepContains: function(array, target) {
158 var object, _i, _len;
159 if (array == null) {
160 return false;
161 }
162 for (_i = 0, _len = array.length; _i < _len; _i++) {
163 object = array[_i];
164 if (_.isEqual(object, target)) {
165 return true;
166 }
167 }
168 return false;
169 },
170 endsWith: function(string, suffix) {
171 if (suffix == null) {
172 suffix = '';
173 }
174 if (string) {
175 return string.indexOf(suffix, string.length - suffix.length) !== -1;
176 } else {
177 return false;
178 }
179 },
180 escapeAttribute: function(string) {
181 if (string) {
182 return string.replace(/"/g, '&quot;').replace(/\n/g, '').replace(/\\/g, '-');
183 } else {
184 return '';
185 }
186 },
187 escapeRegExp: function(string) {
188 if (string) {
189 return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
190 } else {
191 return '';
192 }
193 },
194 humanizeEventName: function(eventName, eventDoc) {
195 var event, namespace, namespaceDoc, _ref;
196 _ref = eventName.split(':'), namespace = _ref[0], event = _ref[1];
197 if (event == null) {
198 return plus.undasherize(namespace);
199 }
200 namespaceDoc = plus.undasherize(namespace);
201 if (eventDoc == null) {
202 eventDoc = plus.undasherize(event);
203 }
204 return "" + namespaceDoc + ": " + eventDoc;
205 },
206 humanizeKey: function(key, platform) {
207 var modifierKeyMap;
208 if (platform == null) {
209 platform = process.platform;
210 }
211 if (!key) {
212 return key;
213 }
214 modifierKeyMap = platform === 'darwin' ? macModifierKeyMap : nonMacModifierKeyMap;
215 if (modifierKeyMap[key]) {
216 return modifierKeyMap[key];
217 } else if (key.length === 1 && (shiftKeyMap[key] != null)) {
218 return [modifierKeyMap.shift, shiftKeyMap[key]];
219 } else if (key.length === 1 && key === key.toUpperCase() && key.toUpperCase() !== key.toLowerCase()) {
220 return [modifierKeyMap.shift, key.toUpperCase()];
221 } else if (key.length === 1 || /f[0-9]{1,2}/.test(key)) {
222 return key.toUpperCase();
223 } else {
224 if (platform === 'darwin') {
225 return key;
226 } else {
227 return plus.capitalize(key);
228 }
229 }
230 },
231 humanizeKeystroke: function(keystroke, platform) {
232 var humanizedKeystrokes, index, key, keys, keystrokes, splitKeystroke, _i, _j, _len, _len1;
233 if (platform == null) {
234 platform = process.platform;
235 }
236 if (!keystroke) {
237 return keystroke;
238 }
239 keystrokes = keystroke.split(' ');
240 humanizedKeystrokes = [];
241 for (_i = 0, _len = keystrokes.length; _i < _len; _i++) {
242 keystroke = keystrokes[_i];
243 keys = [];
244 splitKeystroke = keystroke.split('-');
245 for (index = _j = 0, _len1 = splitKeystroke.length; _j < _len1; index = ++_j) {
246 key = splitKeystroke[index];
247 if (key === '' && splitKeystroke[index - 1] === '') {
248 key = '-';
249 }
250 if (key) {
251 keys.push(plus.humanizeKey(key, platform));
252 }
253 }
254 keys = _.uniq(_.flatten(keys));
255 if (platform === 'darwin') {
256 keys = keys.join('');
257 } else {
258 keys = keys.join('+');
259 }
260 humanizedKeystrokes.push(keys);
261 }
262 return humanizedKeystrokes.join(' ');
263 },
264 isSubset: function(potentialSubset, potentialSuperset) {
265 return _.every(potentialSubset, function(element) {
266 return _.include(potentialSuperset, element);
267 });
268 },
269 losslessInvert: function(hash) {
270 var inverted, key, value;
271 inverted = {};
272 for (key in hash) {
273 value = hash[key];
274 if (inverted[value] == null) {
275 inverted[value] = [];
276 }
277 inverted[value].push(key);
278 }
279 return inverted;
280 },
281 mapObject: function(object, iterator) {
282 var key, newObject, value, _i, _len, _ref, _ref1;
283 newObject = {};
284 _ref = Object.keys(object);
285 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
286 key = _ref[_i];
287 _ref1 = iterator(key, object[key]), key = _ref1[0], value = _ref1[1];
288 newObject[key] = value;
289 }
290 return newObject;
291 },
292 multiplyString: function(string, n) {
293 var finalString, i;
294 finalString = "";
295 i = 0;
296 while (i < n) {
297 finalString += string;
298 i++;
299 }
300 return finalString;
301 },
302 pluralize: function(count, singular, plural) {
303 if (count == null) {
304 count = 0;
305 }
306 if (plural == null) {
307 plural = singular + 's';
308 }
309 if (count === 1) {
310 return "" + count + " " + singular;
311 } else {
312 return "" + count + " " + plural;
313 }
314 },
315 remove: function(array, element) {
316 var index;
317 index = array.indexOf(element);
318 if (index >= 0) {
319 array.splice(index, 1);
320 }
321 return array;
322 },
323 setValueForKeyPath: function(object, keyPath, value) {
324 var key, keys;
325 keys = splitKeyPath(keyPath);
326 while (keys.length > 1) {
327 key = keys.shift();
328 if (object[key] == null) {
329 object[key] = {};
330 }
331 object = object[key];
332 }
333 if (value != null) {
334 return object[keys.shift()] = value;
335 } else {
336 return delete object[keys.shift()];
337 }
338 },
339 hasKeyPath: function(object, keyPath) {
340 var key, keys, _i, _len;
341 keys = splitKeyPath(keyPath);
342 for (_i = 0, _len = keys.length; _i < _len; _i++) {
343 key = keys[_i];
344 if (!object.hasOwnProperty(key)) {
345 return false;
346 }
347 object = object[key];
348 }
349 return true;
350 },
351 spliceWithArray: function(originalArray, start, length, insertedArray, chunkSize) {
352 var chunkStart, _i, _ref, _results;
353 if (chunkSize == null) {
354 chunkSize = 100000;
355 }
356 if (insertedArray.length < chunkSize) {
357 return originalArray.splice.apply(originalArray, [start, length].concat(__slice.call(insertedArray)));
358 } else {
359 originalArray.splice(start, length);
360 _results = [];
361 for (chunkStart = _i = 0, _ref = insertedArray.length; chunkSize > 0 ? _i <= _ref : _i >= _ref; chunkStart = _i += chunkSize) {
362 _results.push(originalArray.splice.apply(originalArray, [start + chunkStart, 0].concat(__slice.call(insertedArray.slice(chunkStart, chunkStart + chunkSize)))));
363 }
364 return _results;
365 }
366 },
367 sum: function(array) {
368 var elt, sum, _i, _len;
369 sum = 0;
370 for (_i = 0, _len = array.length; _i < _len; _i++) {
371 elt = array[_i];
372 sum += elt;
373 }
374 return sum;
375 },
376 uncamelcase: function(string) {
377 var result;
378 if (!string) {
379 return '';
380 }
381 result = string.replace(/([A-Z])|_+/g, function(match, letter) {
382 if (letter == null) {
383 letter = '';
384 }
385 return " " + letter;
386 });
387 return plus.capitalize(result.trim());
388 },
389 undasherize: function(string) {
390 if (string) {
391 return string.split('-').map(plus.capitalize).join(' ');
392 } else {
393 return '';
394 }
395 },
396 underscore: function(string) {
397 if (!string) {
398 return '';
399 }
400 string = string[0].toLowerCase() + string.slice(1);
401 return string.replace(/([A-Z])|-+/g, function(match, letter) {
402 if (letter == null) {
403 letter = '';
404 }
405 return "_" + (letter.toLowerCase());
406 });
407 },
408 valueForKeyPath: function(object, keyPath) {
409 var key, keys, _i, _len;
410 keys = splitKeyPath(keyPath);
411 for (_i = 0, _len = keys.length; _i < _len; _i++) {
412 key = keys[_i];
413 object = object[key];
414 if (object == null) {
415 return;
416 }
417 }
418 return object;
419 },
420 isEqual: function(a, b, aStack, bStack) {
421 if (_.isArray(aStack) && _.isArray(bStack)) {
422 return isEqual(a, b, aStack, bStack);
423 } else {
424 return isEqual(a, b);
425 }
426 },
427 isEqualForProperties: function() {
428 var a, b, properties, property, _i, _len;
429 a = arguments[0], b = arguments[1], properties = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
430 for (_i = 0, _len = properties.length; _i < _len; _i++) {
431 property = properties[_i];
432 if (!_.isEqual(a[property], b[property])) {
433 return false;
434 }
435 }
436 return true;
437 }
438 };
439
440 isEqual = function(a, b, aStack, bStack) {
441 var aCtor, aCtorValid, aElement, aKeyCount, aValue, bCtor, bCtorValid, bKeyCount, bValue, equal, i, key, stackIndex, _i, _len;
442 if (aStack == null) {
443 aStack = [];
444 }
445 if (bStack == null) {
446 bStack = [];
447 }
448 if (a === b) {
449 return _.isEqual(a, b);
450 }
451 if (_.isFunction(a) || _.isFunction(b)) {
452 return _.isEqual(a, b);
453 }
454 stackIndex = aStack.length;
455 while (stackIndex--) {
456 if (aStack[stackIndex] === a) {
457 return bStack[stackIndex] === b;
458 }
459 }
460 aStack.push(a);
461 bStack.push(b);
462 equal = false;
463 if (_.isFunction(a != null ? a.isEqual : void 0)) {
464 equal = a.isEqual(b, aStack, bStack);
465 } else if (_.isFunction(b != null ? b.isEqual : void 0)) {
466 equal = b.isEqual(a, bStack, aStack);
467 } else if (_.isArray(a) && _.isArray(b) && a.length === b.length) {
468 equal = true;
469 for (i = _i = 0, _len = a.length; _i < _len; i = ++_i) {
470 aElement = a[i];
471 if (!isEqual(aElement, b[i], aStack, bStack)) {
472 equal = false;
473 break;
474 }
475 }
476 } else if (_.isRegExp(a) && _.isRegExp(b)) {
477 equal = _.isEqual(a, b);
478 } else if (_.isElement(a) && _.isElement(b)) {
479 equal = a === b;
480 } else if (_.isObject(a) && _.isObject(b)) {
481 aCtor = a.constructor;
482 bCtor = b.constructor;
483 aCtorValid = _.isFunction(aCtor) && aCtor instanceof aCtor;
484 bCtorValid = _.isFunction(bCtor) && bCtor instanceof bCtor;
485 if (aCtor !== bCtor && !(aCtorValid && bCtorValid)) {
486 equal = false;
487 } else {
488 aKeyCount = 0;
489 equal = true;
490 for (key in a) {
491 aValue = a[key];
492 if (!_.has(a, key)) {
493 continue;
494 }
495 aKeyCount++;
496 if (!(_.has(b, key) && isEqual(aValue, b[key], aStack, bStack))) {
497 equal = false;
498 break;
499 }
500 }
501 if (equal) {
502 bKeyCount = 0;
503 for (key in b) {
504 bValue = b[key];
505 if (_.has(b, key)) {
506 bKeyCount++;
507 }
508 }
509 equal = aKeyCount === bKeyCount;
510 }
511 }
512 } else {
513 equal = _.isEqual(a, b);
514 }
515 aStack.pop();
516 bStack.pop();
517 return equal;
518 };
519
520 module.exports = _.extend({}, _, plus);
521
522 }).call(this);