diff --git a/lodash.js b/lodash.js index 7f7416f73..330022508 100644 --- a/lodash.js +++ b/lodash.js @@ -1422,6 +1422,7 @@ var nativeCeil = Math.ceil, nativeFloor = Math.floor, nativeIsFinite = context.isFinite, + nativeJoin = arrayProto.join, nativeKeys = Object.keys, nativeMax = Math.max, nativeMin = Math.min, @@ -5746,6 +5747,24 @@ : []; }); + /** + * Converts all elements in `array` into a string separated by `separator`. + * + * @static + * @memberOf _ + * @category Array + * @param {Array} array The array to convert. + * @param {string} [separator=','] The element separator. + * @returns {string} Returns the joined string. + * @example + * + * _.join(['a', 'b', 'c'], '~'); + * // => 'a~b~c' + */ + function join(array, separator) { + return array ? nativeJoin.call(array, separator) : ''; + } + /** * Gets the last element of `array`. * @@ -13791,6 +13810,7 @@ lodash.isString = isString; lodash.isTypedArray = isTypedArray; lodash.isUndefined = isUndefined; + lodash.join = join; lodash.kebabCase = kebabCase; lodash.last = last; lodash.lastIndexOf = lastIndexOf; @@ -14025,10 +14045,10 @@ }); // Add `Array` and `String` methods to `lodash.prototype`. - arrayEach(['join', 'pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) { + arrayEach(['pop', 'push', 'replace', 'shift', 'sort', 'splice', 'split', 'unshift'], function(methodName) { var func = (/^(?:replace|split)$/.test(methodName) ? stringProto : arrayProto)[methodName], chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru', - retUnwrapped = /^(?:join|pop|replace|shift)$/.test(methodName); + retUnwrapped = /^(?:pop|replace|shift)$/.test(methodName); lodash.prototype[methodName] = function() { var args = arguments; diff --git a/test/test.js b/test/test.js index 60f29f596..6f56ca65d 100644 --- a/test/test.js +++ b/test/test.js @@ -10657,6 +10657,44 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.join'); + + (function() { + var array = ['a', 'b', 'c']; + + QUnit.test('should return join all array elements into a string', function(assert) { + assert.expect(1); + + assert.strictEqual(_.join(array, '~'), 'a~b~c'); + }); + + QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) { + assert.expect(2); + + if (!isNpm) { + var wrapped = _(array); + assert.strictEqual(wrapped.join('~'), 'a~b~c'); + assert.strictEqual(wrapped.value(), array); + } + else { + skipTest(assert, 2); + } + }); + + QUnit.test('should return a wrapped value when explicitly chaining', function(assert) { + assert.expect(1); + + if (!isNpm) { + assert.ok(_(array).chain().join('~') instanceof _); + } + else { + skipTest(assert); + } + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.keyBy'); (function() { @@ -21533,38 +21571,6 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('lodash(...).join'); - - (function() { - var array = [1, 2, 3]; - - QUnit.test('should return join all array elements into a string', function(assert) { - assert.expect(2); - - if (!isNpm) { - var wrapped = _(array); - assert.strictEqual(wrapped.join('.'), '1.2.3'); - assert.strictEqual(wrapped.value(), array); - } - else { - skipTest(assert, 2); - } - }); - - QUnit.test('should return a wrapped value when explicitly chaining', function(assert) { - assert.expect(1); - - if (!isNpm) { - assert.ok(_(array).chain().join('.') instanceof _); - } - else { - skipTest(assert); - } - }); - }()); - - /*--------------------------------------------------------------------------*/ - QUnit.module('lodash(...).next'); lodashStable.each([true, false], function(implict) { @@ -22528,7 +22534,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(278); + assert.expect(279); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));