Skip to content

Commit

Permalink
fix(stringToParts): handle empty string and trailing dot the same way…
Browse files Browse the repository at this point in the history
… that `split()` does for backwards compat

Re: Automattic/mongoose#9681
  • Loading branch information
vkarpov15 committed Dec 10, 2020
1 parent 484c22c commit c507d2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/stringToParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ module.exports = function stringToParts(str) {
curPropertyName += str[i];
}
}
if (curPropertyName.length > 0) {
result.push(curPropertyName);
}
result.push(curPropertyName);

return result;
};
8 changes: 8 additions & 0 deletions test/stringToParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '']);
});
});

0 comments on commit c507d2c

Please sign in to comment.