Skip to content

Commit 42b1643

Browse files
committed
Commit improvement to splitPath that helps with empty keys and accessing "/"
1 parent 8808be0 commit 42b1643

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

general.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ describe('Various Test Cases', () => {
153153
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: 'hello\\.world' }, { 'hello.world': 2 }, 2)
154154
})
155155

156+
it('is able to access empty keys', async () => {
157+
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '.' }, { '': 2 }, 2)
158+
})
159+
160+
it('is able to access dot keys', async () => {
161+
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\.' }, { '.': 2 }, 2)
162+
})
163+
164+
it('is able to access "/" keys from above', async () => {
165+
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[1], { '+': [{ var: '' }, { var: '../../..\\/' }] }] }, { '': { '': { '/': 3 } } }, [4])
166+
})
167+
156168
it('is able to handle path escaping with multiple escapes', async () => {
157169
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\foo' }, { '\\foo': 2 }, 2)
158170
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\\\foo' }, { '\\foo': 2 }, 2)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-logic-engine",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "Construct complex rules with JSON & process them.",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

utilities/splitPath.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,31 @@ export function splitPathMemoized (str) {
3535
* @param {string} separator
3636
* @returns {string[]}
3737
*/
38-
export function splitPath (str, separator = '.', escape = '\\') {
38+
export function splitPath (str, separator = '.', escape = '\\', up = '/') {
3939
const parts = []
4040
let current = ''
4141

4242
for (let i = 0; i < str.length; i++) {
4343
const char = str[i]
4444
if (char === escape) {
45-
if (str[i + 1] === separator) {
46-
current += separator
45+
if (str[i + 1] === separator || str[i + 1] === up) {
46+
current += str[i + 1]
4747
i++
4848
} else if (str[i + 1] === escape) {
4949
current += escape
5050
i++
51+
// The following else might be something tweaked in a spec.
5152
} else current += escape
5253
} else if (char === separator) {
5354
parts.push(current)
5455
current = ''
5556
} else current += char
5657
}
57-
parts.push(current)
58+
59+
// The if prevents me from pushing more sections than characters
60+
// This is so that "." will [''] and not ['','']
61+
// But .h will be ['','.h']
62+
// .. becomes ['',''], ..h becomes ['', '', 'h']
63+
if (parts.length !== str.length) parts.push(current)
5864
return parts
5965
}

0 commit comments

Comments
 (0)