From c507d2c8e6cc20c1b67f2645e868f6d4dd521247 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 10 Dec 2020 14:53:03 -0500 Subject: [PATCH] fix(stringToParts): handle empty string and trailing dot the same way that `split()` does for backwards compat Re: Automattic/mongoose#9681 --- lib/stringToParts.js | 4 +--- test/stringToParts.js | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/stringToParts.js b/lib/stringToParts.js index 5994209..a34daa1 100644 --- a/lib/stringToParts.js +++ b/lib/stringToParts.js @@ -25,9 +25,7 @@ module.exports = function stringToParts(str) { curPropertyName += str[i]; } } - if (curPropertyName.length > 0) { - result.push(curPropertyName); - } + result.push(curPropertyName); return result; }; \ No newline at end of file diff --git a/test/stringToParts.js b/test/stringToParts.js index 623aafb..3758d10 100644 --- a/test/stringToParts.js +++ b/test/stringToParts.js @@ -17,4 +17,12 @@ describe('stringToParts', function() { it('throws for invalid numbers in square brackets', function() { assert.throws(() => stringToParts('foo[1mystring]'), /1mystring/); }); + + it('handles empty string', function() { + assert.deepEqual(stringToParts(''), ['']); + }); + + it('handles trailing dot', function() { + assert.deepEqual(stringToParts('a.b.'), ['a', 'b', '']); + }); }); \ No newline at end of file