-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from adamstoffel/parse-json-paths
- Loading branch information
Showing
5 changed files
with
75 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-core": minor | ||
--- | ||
|
||
Adds support for parsing JSON arrow expressions in order clauses |
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,25 @@ | ||
import { get } from '../../src/lib/get'; | ||
|
||
describe('get', () => { | ||
it.each([ | ||
[{ a: 1 }, 'a', undefined, 1], // simple case | ||
[{ a: 1 }, 'b', 2, 2], // default case | ||
[{ a: 1 }, '', undefined, undefined], // empty case | ||
[{ a: { b: 1 } }, 'a.b', undefined, 1], // dot syntax | ||
[{ a: { b: 1 } }, 'a,b', undefined, 1], // comma syntax | ||
[{ a: { b: 1 } }, 'a[b]', undefined, 1], // bracket syntax | ||
[{ a: { b: { c: { d: 1 } } } }, 'a.b,c.[d]', undefined, 1], // combination syntax | ||
[{ a: { b: 1 } }, 'a->b', undefined, 1], // json value syntax | ||
[{ a: { b: 1 } }, 'a->>b', undefined, '1'], // json string syntax | ||
[{ a: [1, 2] }, 'a->0', undefined, 1], // json array value syntax | ||
[{ a: [1, 2] }, 'a->>0', undefined, '1'], // json array string syntax | ||
[{ a: { b: { c: 1 } } }, 'a->b->c', undefined, 1], // nested json syntax | ||
[{ a: { b: { c: 1 } } }, 'a->b->>c', undefined, '1'], | ||
[{ a: { b: [1, 2] } }, 'a.b->0', undefined, 1], | ||
[{ a: { b: [1, 2] } }, 'a.b->>0', undefined, '1'], | ||
[{ a: { b: 1 } }, 'a->0', undefined, undefined], // not an array | ||
[{ a: [1, 2] }, 'a->2', undefined, undefined], // missing array value | ||
])('get(%j, "%s", %s) should be %s', (obj, path, defaultValue, expected) => { | ||
expect(get(obj, path, defaultValue)).toEqual(expected); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
export const get = (obj: any, path: string, defaultValue: any = undefined) => { | ||
const travel = (regexp: RegExp) => | ||
String.prototype.split | ||
.call(path, regexp) | ||
.filter(Boolean) | ||
.reduce( | ||
(res, key) => (res !== null && res !== undefined ? res[key] : res), | ||
obj, | ||
); | ||
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); | ||
const split = path.split(/((?:\.|,|\[|\]|->>|->)+)/g); | ||
let result: any = obj; | ||
for (let i = -1; i < split.length; i += 2) { | ||
const separator = split[i]; | ||
let key: string | number = split[i + 1]; | ||
if (!key) { | ||
continue; | ||
} | ||
if (separator?.endsWith('->') || separator?.endsWith('->>')) { | ||
if (/^\d+$/.test(key)) { | ||
key = parseInt(key, 10); | ||
} | ||
} | ||
if (separator?.endsWith('->>')) { | ||
result = `${result ? result[key] : result}`; | ||
} else { | ||
result = result ? result[key] : result; | ||
} | ||
} | ||
return result === undefined || result === obj ? defaultValue : result; | ||
}; |