-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stringtopath): add util to convert string to a property path array
- Loading branch information
1 parent
8d19068
commit c4d0dd4
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters