Skip to content

Commit

Permalink
feat(stringtopath): add util to convert string to a property path array
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed May 2, 2020
1 parent 8d19068 commit c4d0dd4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/isSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getTag } from './getTag';
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
* @example
*
* isSymbol(Symbol.iterator)
* isSymbol(Symbol())
* // => true
*
* isSymbol('abc')
Expand Down
17 changes: 17 additions & 0 deletions src/stringToPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { stringToPath } from './stringToPath';

describe('stringToPath', () => {
it("should match anything that isn't a dot or bracket", () => {
expect(stringToPath('hi')).toEqual(['hi']);
expect(stringToPath('attribute?')).toEqual(['attribute?']);
});
it('should match property names within brackets', () => {
expect(stringToPath('first[second].third')).toEqual([
'first',
'second',
'third'
]);
expect(stringToPath('first[].third')).toEqual(['first', '', 'third']);
expect(stringToPath("first['second\x02]")).toEqual(['first', 'second']);
});
});
54 changes: 54 additions & 0 deletions src/stringToPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { memoizeCapped } from './memoizeCapped';

const charCodeOfDot = '.'.charCodeAt(0);
const reEscapeChar = /\\(\\)?/g;
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' +
'|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'][^[]*)' +
'|' +
// Or match strings (supports escaping characters).
'(["\'])((?:(?!\\x02)[^\\\\]|\\\\.)*?)\\x02' +
')\\]' +
'|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))',
'g'
);

/**
* Converts `string` to a property path array.
*
* @private
* @param {string} string The string to convert.
* @returns {Array} Returns the property path array.
*/
export const stringToPath = memoizeCapped((string: string) => {
const result = [];
if (string.charCodeAt(0) === charCodeOfDot) {
result.push('');
}
string.replace(
rePropName,
(
match: string,
expression: string,
quote: string,
subString: string
): string => {
let key = match;
if (quote) {
key = subString.replace(reEscapeChar, '$1');
} else if (expression) {
key = expression.trim();
}
result.push(key);
return '';
}
);
return result;
});
7 changes: 7 additions & 0 deletions src/toKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const INFINITY = 1 / 0;
* @private
* @param {*} value The value to inspect.
* @returns {string|symbol} Returns the key.
* @example
*
* toKey(Symbol.iterator)
* // => true
*
* toKey('abc')
* // => false
*/
export function toKey(value: any): string | symbol {
if (typeof value === 'string' || isSymbol(value)) {
Expand Down

0 comments on commit c4d0dd4

Please sign in to comment.