diff --git a/test/test.js b/test/test.js index 3104f2b3c..581a1da70 100644 --- a/test/test.js +++ b/test/test.js @@ -38,6 +38,7 @@ body = root.document && root.document.body, create = Object.create, freeze = Object.freeze, + hasOwnProperty = objectProto.hasOwnProperty, JSON = root.JSON, noop = function() {}, params = root.arguments, @@ -1561,6 +1562,11 @@ deepEqual(actual, expected); }); + + test('should work as an iteratee for `_.map`', 1, function() { + var actual = _.map([[1, 2], [3, 4]], _.chunk); + deepEqual(actual, [[[1], [2]], [[3], [4]]]); + }); }()); /*--------------------------------------------------------------------------*/ @@ -1751,7 +1757,7 @@ } }); - test('`_.' + methodName + '` should perform a ' + (isDeep ? 'deep' : 'shallow') + ' clone when used as a callback for `_.map`', 3, function() { + test('`_.' + methodName + '` should perform a ' + (isDeep ? 'deep' : 'shallow') + ' clone when used as an iteratee for `_.map`', 3, function() { var expected = [{ 'a': [0] }, { 'b': [1] }], actual = _.map(expected, func); @@ -2145,6 +2151,18 @@ deepEqual(actual, expected); }); + + test('should work as an iteratee for `_.map`', 1, function() { + var array = [{ 'a': 1 }, { 'a': 1 }, { 'a': 1 }], + expected = _.map(array, _.constant(true)), + objects = _.map(array, _.create); + + var actual = _.map(objects, function(object) { + return object.a === 1 && !_.keys(object).length; + }); + + deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -2238,7 +2256,7 @@ test('should support binding built-in methods', 2, function() { var object = { 'a': 1 }, - callback = _.callback(objectProto.hasOwnProperty, object); + callback = _.callback(hasOwnProperty, object); strictEqual(callback('a'), true); @@ -2301,6 +2319,19 @@ supportBizarro.funcNames = funcNames; }); + test('should work as an iteratee for `_.map`', 1, function() { + var fn = function() { return this instanceof Number; }, + array = [fn, fn, fn], + expected = _.map(array, _.constant(false)), + callbacks = _.map(array, _.callback); + + var actual = _.map(callbacks, function(callback) { + return callback(); + }); + + deepEqual(actual, expected); + }); + test('should be aliased', 1, function() { strictEqual(_.iteratee, _.callback); }); @@ -2525,6 +2556,25 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('curry methods'); + + _.each(['curry', 'curryRight'], function(methodName) { + var func = _[methodName]; + + test('`_.' + methodName + '` should work as an iteratee for `_.map`', 1, function() { + var array = [_.identity, _.identity, _.identity], + curries = _.map(array, func); + + var actual = _.map(curries, function(curried, index) { + return curried(index); + }); + + deepEqual(actual, [0, 1, 2]); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.debounce'); (function() { @@ -3039,7 +3089,7 @@ }); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.drop); @@ -3093,7 +3143,7 @@ }); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.dropRight); @@ -3600,7 +3650,7 @@ strictEqual(_.first([]), undefined); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.first); @@ -3656,7 +3706,7 @@ }); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.take); @@ -3710,7 +3760,7 @@ }); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.takeRight); @@ -3884,7 +3934,7 @@ deepEqual(_.flattenDeep(array), expected); }); - test('should work when used as a callback for `_.map`', 2, function() { + test('should work as an iteratee for `_.map`', 2, function() { var array = [[[['a']]], [[['b']]]]; deepEqual(_.map(array, _.flatten), [[['a']], [['b']]]); @@ -4420,7 +4470,7 @@ deepEqual(actual, expected); }); - test('`_.' + methodName + '` should work with `_.reduce`', 1, function() { + test('`_.' + methodName + '` should work as an iteratee for `_.reduce`', 1, function() { var array = [{ 'b': 2 }, { 'c': 3 }]; deepEqual(_.reduce(array, func, { 'a': 1}), { 'a': 1, 'b': 2, 'c': 3 }); }); @@ -4906,7 +4956,7 @@ deepEqual(_.initial([]), []); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.initial); @@ -5015,6 +5065,14 @@ ok(_.isEqual(_.invert(object, true), { 'hasOwnProperty': ['a'], 'constructor': ['b'] })); }); + test('should work as an iteratee for `_.map`', 1, function() { + var inverted = { '1': 'c', '2': 'b' }, + object = { 'a': 1, 'b': 2, 'c': 1 }, + actual = _.map([object, object, object], _.invert); + + deepEqual(actual, [inverted, inverted, inverted]); + }); + test('should return a wrapped value when chaining', 2, function() { if (!isNpm) { var object = { 'a': 1, 'b': 2 }, @@ -5891,7 +5949,7 @@ strictEqual(actual, false); }); - test('should work when used as a callback for `_.every`', 1, function() { + test('should work as an iteratee for `_.every`', 1, function() { var actual = _.every([1, 1, 1], _.partial(_.isEqual, 1)); ok(actual); }); @@ -6891,7 +6949,7 @@ strictEqual(_.last([]), undefined); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.last); @@ -7725,7 +7783,7 @@ strictEqual(func(array), isMax ? 499999 : 0); }); - test('`_.' + methodName + '` should work when used as a callback for `_.map`', 2, function() { + test('`_.' + methodName + '` should work as an iteratee for `_.map`', 2, function() { var array = [[2, 3, 1], [5, 6, 4], [8, 9, 7]], actual = _.map(array, func); @@ -8192,8 +8250,9 @@ QUnit.module('pad methods'); - _.each(['pad', 'padLeft', 'padRight'], function(methodName, index) { - var func = _[methodName]; + _.each(['pad', 'padLeft', 'padRight'], function(methodName) { + var func = _[methodName], + isPadLeft = methodName == 'padLeft'; test('`_.' + methodName + '` should not pad is string is >= `length`', 2, function() { strictEqual(func('abc', 2), 'abc'); @@ -8208,7 +8267,7 @@ test('`_.' + methodName + '` should coerce `length` to a number', 2, function() { _.each(['', '4'], function(length) { - var actual = length ? (index == 1 ? ' abc' : 'abc ') : 'abc'; + var actual = length ? (isPadLeft ? ' abc' : 'abc ') : 'abc'; strictEqual(func('abc', length), actual); }); }); @@ -8317,7 +8376,7 @@ strictEqual(_.parseInt('0x20', object), 32); }); - test('should work when used as a callback for `_.map`', 2, function() { + test('should work as an iteratee for `_.map`', 2, function() { var actual = _.map(['1', '10', '08'], _.parseInt); deepEqual(actual, [1, 10, 8]); @@ -8992,12 +9051,12 @@ ok(actual % 1 && actual >= 2 && actual <= 4); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [1, 2, 3], - actual = _.map(array, _.random), - expected = _.map(array, _.constant(true)); + expected = _.map(array, _.constant(true)), + randoms = _.map(array, _.random); - actual = _.map(actual, function(result, index) { + var actual = _.map(randoms, function(result, index) { return result >= 0 && result <= array[index] && (result % 1) == 0; }); @@ -9051,7 +9110,7 @@ deepEqual(actual, [[0], [0], [0], [], []]); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var actual = _.map([1, 2, 3], _.range); deepEqual(actual, [[0], [0, 1], [0, 1, 2]]); }); @@ -9478,7 +9537,7 @@ deepEqual(_.rest([]), []); }); - test('should work when used as a callback for `_.map`', 1, function() { + test('should work as an iteratee for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.rest); @@ -9600,12 +9659,12 @@ ok(actual.length == 2 && actual[0] !== actual[1] && _.contains(array, actual[0]) && _.contains(array, actual[1])); }); - test('should work when used as a callback for `_.map`', 2, function() { + test('should work as an iteratee for `_.map`', 2, function() { _.each([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], ['abc', 'def', 'ghi']], function(values) { var a = values[0], b = values[1], c = values[2], - actual = _.map([a, b, c], _.sample); + actual = _.map(values, _.sample); ok(_.contains(a, actual[0]) && _.contains(b, actual[1]) && _.contains(c, actual[2])); }); @@ -9835,6 +9894,14 @@ var actual = [_.slice(array, '0', 1), _.slice(array, 0, '1'), _.slice(array, '1'), _.slice(array, NaN, 1), _.slice(array, 1, NaN)]; deepEqual(actual, [[1], [1], [2, 3], [1], []]); }); + + test('should work as an iteratee for `_.map`', 2, function() { + var array = [[1], [2, 3]], + actual = _.map(array, _.slice); + + deepEqual(actual, array); + notStrictEqual(actual, array) + }); }()); /*--------------------------------------------------------------------------*/ @@ -9994,6 +10061,13 @@ deepEqual(actual, [objects[0], objects[2], objects[1], objects[3]]); }); + + test('should work as an iteratee for `_.map`', 1, function() { + var array = [[2, 1, 3], [3, 2, 1]], + actual = _.map(array, _.sortBy); + + deepEqual(actual, [[1, 2, 3], [1, 2, 3]]); + }); }()); /*--------------------------------------------------------------------------*/ @@ -10298,11 +10372,14 @@ (function() { test('should escape values in "escape" delimiters', 1, function() { - var escaped = '
&<>"'\/
', - unescaped = '&<>"\'\/'; + var strings = ['<%- value %>
', '<%-value%>
', '<%-\nvalue\n%>
'], + expected = _.map(strings, _.constant('&<>"'`\/
')); - var compiled = _.template('<%- value %>
'); - strictEqual(compiled({ 'value': unescaped }), escaped); + var actual = _.map(strings, function(string) { + return _.template(string)({ 'value': '&<>"\'`\/' }); + }); + + deepEqual(actual, expected); }); test('should evaluate JavaScript in "evaluate" delimiters', 1, function() { @@ -10318,8 +10395,14 @@ }); test('should interpolate data object properties', 1, function() { - var compiled = _.template('<%= a %>BC'); - strictEqual(compiled({ 'a': 'A' }), 'ABC'); + var strings = ['<%= a %>BC', '<%=a%>BC', '<%=\na\n%>BC'], + expected = _.map(strings, _.constant('ABC')); + + var actual = _.map(strings, function(string) { + return _.template(string)({ 'a': 'A' }); + }); + + deepEqual(actual, expected); }); test('should support escaped values in "interpolation" delimiters', 1, function() { @@ -10563,7 +10646,7 @@ strictEqual(compiled({ 'a': {} }), ''); }); - test('should parse delimiters with newlines', 1, function() { + test('should parse delimiters without newlines', 1, function() { var expected = '<<\nprint("" + (value ? "yes" : "no") + "
")\n>>', compiled = _.template(expected, { 'evaluate': /<<(.+?)>>/g }), data = { 'value': true }; @@ -10628,6 +10711,17 @@ } ok(/__p/.test(source)); }); + + test('should work as an iteratee for `_.map`', 1, function() { + var array = ['<%= a %>', '<%- b %>', '<% print(c) %>'], + compiles = _.map(array, _.template); + + var actual = _.map(compiles, function(compiled) { + return compiled({ 'a': 'one', 'b': '`two`', 'c': 'three' }); + }); + + deepEqual(actual, ['one', '`two`', 'three']); + }); }()); /*--------------------------------------------------------------------------*/ @@ -10682,6 +10776,13 @@ strictEqual(_.trunc(Object(string), 4), 'h...'); strictEqual(_.trunc({ 'toString': _.constant(string) }, 5), 'hi...'); }); + + test('should work as an iteratee for `_.map`', 1, function() { + var actual = _.map([string, string, string], _.trunc), + truncated = 'hi-diddly-ho there, neighbo...'; + + deepEqual(actual, [truncated, truncated, truncated]); + }); }()); /*--------------------------------------------------------------------------*/ @@ -11318,6 +11419,14 @@ strictEqual(func(string, ''), string); }); + test('should work as an iteratee for `_.map`', 1, function() { + var string = whitespace + 'a b c' + whitespace, + trimmed = (index == 2 ? whitespace : '') + 'a b c' + (index == 1 ? whitespace : ''), + actual = _.map([string, string, string], func); + + deepEqual(actual, [trimmed, trimmed, trimmed]); + }); + test('`_.' + methodName + '` should return an unwrapped value when chaining', 1, function() { if (!isNpm) { var string = whitespace + 'a b c' + whitespace, @@ -11460,7 +11569,7 @@ deepEqual(actual, arrays.slice(0, 3)); }); - test('should perform an unsorted uniq operation when used as a callback for `_.map`', 1, function() { + test('should perform an unsorted uniq when used as an iteratee for `_.map`', 1, function() { var array = [[2, 1, 2], [1, 2, 1]], actual = _.map(array, _.uniq); @@ -11651,10 +11760,8 @@ deepEqual(actual, expected); }); - test('should work when used as a callback for `_.map`', 1, function() { - var array = ['a', 'b', 'c'], - actual = _.map(array, _.words); - + test('should work as an iteratee for `_.map`', 1, function() { + var actual = _.map(['a', 'b', 'c'], _.words); deepEqual(actual, [['a'], ['b'], ['c']]); });