From a18be78bd3087661047ae425acb9c5e80e473809 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 25 Aug 2013 16:49:42 -0700 Subject: [PATCH] Allow `defineProperty` to be used in the lodash.compat build too. Former-commit-id: c610fd24ca8cfefab4c355099a034a8b2542aa77 --- build.js | 13 ++++++------- lodash.js | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/build.js b/build.js index b49a54d7f..1be5c8550 100644 --- a/build.js +++ b/build.js @@ -543,6 +543,7 @@ 'cloneableClasses', 'contextProps', 'ctorByClass', + 'defineProperty', 'freeGlobal', 'nonEnumProps', 'shadowedProps', @@ -1557,7 +1558,7 @@ // match a variable declaration in a declaration list indentB + '(\\w+) *=.+?[,;]\\n', // match a variable that is not part of a declaration list - '(' + indentA + ')var (\\w+) *(?:|= *(?:.+?(?:&&\\n[^;]+)?|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' + '(' + indentA + ')var (\\w+) *(?:|= *(?:.+?(?:&&\\n[^;]+)?|(?:[(\\w]+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' ], function(result, reSource) { source = source.replace(RegExp('^' + reSource, 'gm'), function(match, indent, varName) { if (typeof varName == 'number') { @@ -1620,7 +1621,7 @@ }, null); return result && ( - /@type +Function\b/.test(result[0]) || + /@type +function\b/i.test(result[0]) || /(?:function(?:\s+\w+)?\b|create[A-Z][a-z]+|template)\(/.test(result[1])) ? (leadingComments ? result[0] : '') + result[1] : ''; @@ -1667,7 +1668,7 @@ // match a variable declaration in a declaration list indentB + varName + ' *=.+?[,;]\\n', // match a variable that is not part of a declaration list - '(' + indentA + ')var ' + varName + ' *(?:|= *(?:.+?(?:&&\\n[^;]+)?|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' + '(' + indentA + ')var ' + varName + ' *(?:|= *(?:.+?(?:&&\\n[^;]+)?|(?:[(\\w]+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' ]; // match complex variable assignments @@ -2395,7 +2396,7 @@ // remove a variable that is not part of a declaration list return source.replace(RegExp( multilineComment + - '( *)var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' + '( *)var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:[(\\w]+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' ), ''); } ], function(func) { @@ -3244,10 +3245,8 @@ }); } } - if (!isModern || isMobile) { - source = removeEsOptimization(source); - } if (isLegacy || isMobile || isUnderscore) { + source = removeEsOptimization(source); if (isMobile || (!isLodash('assign') && !isLodash('defaults') && !isLodash('forIn') && !isLodash('forOwn'))) { source = removeKeysOptimization(source); } diff --git a/lodash.js b/lodash.js index 558e0b33a..e7fd68a6f 100644 --- a/lodash.js +++ b/lodash.js @@ -492,7 +492,6 @@ /** Native method shortcuts */ var ceil = Math.ceil, clearTimeout = context.clearTimeout, - defineProperty = reNative.test(defineProperty = Object.defineProperty) && defineProperty, floor = Math.floor, fnToString = Function.prototype.toString, getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf, @@ -506,6 +505,15 @@ toString = objectProto.toString, unshift = arrayRef.unshift; + var defineProperty = (function() { + try { + var o = {}, + func = reNative.test(func = Object.defineProperty) && func, + result = func(o, o, o) && func; + } catch(e) { } + return result; + }()); + /* Native method shortcuts for methods with the same name as other `lodash` methods */ var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind, nativeCreate = reNative.test(nativeCreate = Object.create) && nativeCreate, @@ -2039,7 +2047,7 @@ * iteration. If a property name or object is provided it will be used to * create a "_.pluck" or "_.where" style callback, respectively. * @param {*} [thisArg] The `this` binding of `callback`. - * @returns {*} Returns the key of the found element, else `undefined`. + * @returns {string|undefined} Returns the key of the found element, else `undefined`. * @example * * _.findKey({ 'a': 1, 'b': 2, 'c': 3, 'd': 4 }, function(num) { @@ -2071,7 +2079,7 @@ * iteration. If a property name or object is provided it will be used to * create a "_.pluck" or "_.where" style callback, respectively. * @param {*} [thisArg] The `this` binding of `callback`. - * @returns {*} Returns the key of the found element, else `undefined`. + * @returns {string|undefined} Returns the key of the found element, else `undefined`. * @example * * _.findLastKey({ 'a': 1, 'b': 2, 'c': 3, 'd': 4 }, function(num) { @@ -4183,7 +4191,7 @@ * per iteration. If a property name or object is provided it will be used * to create a "_.pluck" or "_.where" style callback, respectively. * @param {*} [thisArg] The `this` binding of `callback`. - * @returns {*} Returns the index of the found element, else `-1`. + * @returns {number} Returns the index of the found element, else `-1`. * @example * * _.findIndex(['apple', 'banana', 'beet'], function(food) { @@ -4216,7 +4224,7 @@ * per iteration. If a property name or object is provided it will be used * to create a "_.pluck" or "_.where" style callback, respectively. * @param {*} [thisArg] The `this` binding of `callback`. - * @returns {*} Returns the index of the found element, else `-1`. + * @returns {number} Returns the index of the found element, else `-1`. * @example * * _.findLastIndex(['apple', 'banana', 'beet'], function(food) {