From e776e679afeeef477c87ee6b565eca416cb57d87 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 8 Apr 2016 01:03:43 -0700 Subject: [PATCH] Ensure `_.split` works with emojis. --- lodash.js | 13 ++++++++++++- test/test.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index ab41fba39..08693b1a7 100644 --- a/lodash.js +++ b/lodash.js @@ -13560,7 +13560,18 @@ * // => ['a', 'b'] */ function split(string, separator, limit) { - return toString(string).split(separator, limit); + string = toString(string); + if (string && ( + typeof separator == 'string' || + (separator != null && !isRegExp(separator)) + )) { + separator += ''; + if (separator == '' && reHasComplexSymbol.test(string)) { + var strSymbols = stringToArray(string); + return limit === undefined ? strSymbols : strSymbols.slice(0, limit < 0 ? 0 : limit); + } + } + return string.split(separator, limit); } /** diff --git a/test/test.js b/test/test.js index 89495a5dc..27817fc41 100644 --- a/test/test.js +++ b/test/test.js @@ -20092,7 +20092,7 @@ QUnit.module('lodash.split'); (function() { - QUnit.test('should support string split', function(assert) { + QUnit.test('should split a string by `separator`', function(assert) { assert.expect(3); var string = 'abcde'; @@ -20101,6 +20101,19 @@ assert.deepEqual(_.split(string, '', 2), ['a', 'b']); }); + QUnit.test('should return an array containing an empty string for empty values', function(assert) { + assert.expect(1); + + var values = [, null, undefined, ''], + expected = lodashStable.map(values, lodashStable.constant([''])); + + var actual = lodashStable.map(values, function(value, index) { + return index ? _.split(value) : _.split(); + }); + + assert.deepEqual(actual, expected); + }); + QUnit.test('should allow mixed string and array prototype methods', function(assert) { assert.expect(1); @@ -23238,7 +23251,7 @@ thumbsUp = '\ud83d\udc4d'; QUnit.test('should account for astral symbols', function(assert) { - assert.expect(26); + assert.expect(33); var allHearts = _.repeat(hearts, 10), chars = hearts + comboGlyph, @@ -23259,7 +23272,17 @@ assert.strictEqual(_.padEnd(string, 16, chars), string + chars + hearts); assert.strictEqual(_.size(string), 13); - assert.deepEqual(_.toArray(string), ['A', ' ', leafs, ',', ' ', comboGlyph, ',', ' ', 'a', 'n', 'd', ' ', rocket]); + assert.deepEqual(_.split(string, ' '), ['A', leafs + ',', comboGlyph + ',', 'and', rocket]); + assert.deepEqual(_.split(string, ' ', 3), ['A', leafs + ',', comboGlyph + ',']); + assert.deepEqual(_.split(string, undefined), [string]); + assert.deepEqual(_.split(string, undefined, 0), []); + assert.deepEqual(_.split(string, '', -1), []); + + var expected = ['A', ' ', leafs, ',', ' ', comboGlyph, ',', ' ', 'a', 'n', 'd', ' ', rocket]; + + assert.deepEqual(_.split(string, ''), expected); + assert.deepEqual(_.split(string, '', 6), expected.slice(0, 6)); + assert.deepEqual(_.toArray(string), expected); assert.strictEqual(_.trim(trimString, chars), string); assert.strictEqual(_.trimStart(trimString, chars), string + trimChars);