]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/underscore-plus/lib/underscore-plus.js
32eb482085abb7c2242f123575a6dc17c8f5766b
2 var isEqual
, isPlainObject
, macModifierKeyMap
, nonMacModifierKeyMap
, plus
, shiftKeyMap
, splitKeyPath
, _
,
5 _
= require('underscore');
20 nonMacModifierKeyMap
= {
47 splitKeyPath = function(keyPath
) {
48 var char, i
, keyPathArray
, startIndex
, _i
, _len
;
51 if (keyPath
== null) {
54 for (i
= _i
= 0, _len
= keyPath
.length
; _i
< _len
; i
= ++_i
) {
56 if (char === '.' && (i
=== 0 || keyPath
[i
- 1] !== '\\')) {
57 keyPathArray
.push(keyPath
.substring(startIndex
, i
));
61 keyPathArray
.push(keyPath
.substr(startIndex
, keyPath
.length
));
65 isPlainObject = function(value
) {
66 return _
.isObject(value
) && !_
.isArray(value
);
70 adviseBefore: function(object
, methodName
, advice
) {
72 original
= object
[methodName
];
73 return object
[methodName
] = function() {
75 args
= 1 <= arguments
.length
? __slice
.call(arguments
, 0) : [];
76 if (advice
.apply(this, args
) !== false) {
77 return original
.apply(this, args
);
81 camelize: function(string
) {
83 return string
.replace(/[_-]+(\w)/g, function(m
) {
84 return m
[1].toUpperCase();
90 capitalize: function(word
) {
94 if (word
.toLowerCase() === 'github') {
97 return word
[0].toUpperCase() + word
.slice(1);
100 compactObject: function(object
) {
101 var key
, newObject
, value
;
103 for (key
in object
) {
106 newObject
[key
] = value
;
111 dasherize: function(string
) {
115 string
= string
[0].toLowerCase() + string
.slice(1);
116 return string
.replace(/([A-Z])|(_)/g, function(m
, letter
) {
118 return "-" + letter
.toLowerCase();
124 deepClone: function(object
) {
125 if (_
.isArray(object
)) {
126 return object
.map(function(value
) {
127 return plus
.deepClone(value
);
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
)];
139 deepExtend: function(target
) {
140 var i
, key
, object
, result
, _i
, _len
, _ref
;
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
++) {
149 result
[key
] = plus
.deepExtend(result
[key
], object
[key
]);
152 result
= plus
.deepClone(object
);
157 deepContains: function(array
, target
) {
158 var object
, _i
, _len
;
162 for (_i
= 0, _len
= array
.length
; _i
< _len
; _i
++) {
164 if (_
.isEqual(object
, target
)) {
170 endsWith: function(string
, suffix
) {
171 if (suffix
== null) {
175 return string
.indexOf(suffix
, string
.length
- suffix
.length
) !== -1;
180 escapeAttribute: function(string
) {
182 return string
.replace(/"/g, '"').replace(/\n/g, '').replace(/\\/g, '-');
187 escapeRegExp: function(string) {
189 return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
194 humanizeEventName: function(eventName, eventDoc) {
195 var event, namespace, namespaceDoc, _ref;
196 _ref = eventName.split(':'), namespace = _ref[0], event = _ref[1];
198 return plus.undasherize(namespace);
200 namespaceDoc = plus.undasherize(namespace);
201 if (eventDoc == null) {
202 eventDoc = plus.undasherize(event);
204 return "" + namespaceDoc + ": " + eventDoc;
206 humanizeKey: function(key, platform) {
208 if (platform == null) {
209 platform = process.platform;
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();
224 if (platform === 'darwin') {
227 return plus.capitalize(key);
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;
239 keystrokes = keystroke.split(' ');
240 humanizedKeystrokes = [];
241 for (_i = 0, _len = keystrokes.length; _i < _len; _i++) {
242 keystroke = keystrokes[_i];
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] === '') {
251 keys.push(plus.humanizeKey(key, platform));
254 keys = _.uniq(_.flatten(keys));
255 if (platform === 'darwin') {
256 keys = keys.join('');
258 keys = keys.join('+');
260 humanizedKeystrokes.push(keys);
262 return humanizedKeystrokes.join(' ');
264 isSubset: function(potentialSubset, potentialSuperset) {
265 return _.every(potentialSubset, function(element) {
266 return _.include(potentialSuperset, element);
269 losslessInvert: function(hash) {
270 var inverted, key, value;
274 if (inverted[value] == null) {
275 inverted[value] = [];
277 inverted[value].push(key);
281 mapObject: function(object, iterator) {
282 var key, newObject, value, _i, _len, _ref, _ref1;
284 _ref = Object.keys(object);
285 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
287 _ref1 = iterator(key, object[key]), key = _ref1[0], value = _ref1[1];
288 newObject[key] = value;
292 multiplyString: function(string, n) {
297 finalString += string;
302 pluralize: function(count, singular, plural) {
306 if (plural == null) {
307 plural = singular + 's';
310 return "" + count + " " + singular;
312 return "" + count + " " + plural;
315 remove: function(array, element) {
317 index = array.indexOf(element);
319 array.splice(index, 1);
323 setValueForKeyPath: function(object, keyPath, value) {
325 keys = splitKeyPath(keyPath);
326 while (keys.length > 1) {
328 if (object[key] == null) {
331 object = object[key];
334 return object[keys.shift()] = value;
336 return delete object[keys.shift()];
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++) {
344 if (!object.hasOwnProperty(key)) {
347 object = object[key];
351 spliceWithArray: function(originalArray, start, length, insertedArray, chunkSize) {
352 var chunkStart, _i, _ref, _results;
353 if (chunkSize == null) {
356 if (insertedArray.length < chunkSize) {
357 return originalArray.splice.apply(originalArray, [start, length].concat(__slice.call(insertedArray)));
359 originalArray.splice(start, length);
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)))));
367 sum: function(array) {
368 var elt, sum, _i, _len;
370 for (_i = 0, _len = array.length; _i < _len; _i++) {
376 uncamelcase: function(string) {
381 result = string.replace(/([A-Z])|_+/g, function(match, letter) {
382 if (letter == null) {
387 return plus.capitalize(result.trim());
389 undasherize: function(string) {
391 return string.split('-').map(plus.capitalize).join(' ');
396 underscore: function(string) {
400 string = string[0].toLowerCase() + string.slice(1);
401 return string.replace(/([A-Z])|-+/g, function(match, letter) {
402 if (letter == null) {
405 return "_
" + (letter.toLowerCase());
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++) {
413 object = object[key];
414 if (object == null) {
420 isEqual: function(a, b, aStack, bStack) {
421 if (_.isArray(aStack) && _.isArray(bStack)) {
422 return isEqual(a, b, aStack, bStack);
424 return isEqual(a, b);
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])) {
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) {
445 if (bStack == null) {
449 return _.isEqual(a, b);
451 if (_.isFunction(a) || _.isFunction(b)) {
452 return _.isEqual(a, b);
454 stackIndex = aStack.length;
455 while (stackIndex--) {
456 if (aStack[stackIndex] === a) {
457 return bStack[stackIndex] === b;
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) {
469 for (i = _i = 0, _len = a.length; _i < _len; i = ++_i) {
471 if (!isEqual(aElement, b[i], aStack, bStack)) {
476 } else if (_.isRegExp(a) && _.isRegExp(b)) {
477 equal = _.isEqual(a, b);
478 } else if (_.isElement(a) && _.isElement(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)) {
492 if (!_.has(a, key)) {
496 if (!(_.has(b, key) && isEqual(aValue, b[key], aStack, bStack))) {
509 equal = aKeyCount === bKeyCount;
513 equal = _.isEqual(a, b);
520 module.exports = _.extend({}, _, plus);