Skip to content

Commit b0ee531

Browse files
committed
fix: refactor function get to accept only one argument
1 parent 806eea1 commit b0ee531

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/functions.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import { compile } from './compile'
33
import { isArray, isString } from './is'
44
import { compileArgs } from './compileArgs'
55

6-
export const get = (...props: JSONPath | [JSONPath]) =>
7-
isArray(props[0]) ? _get(props[0]) : _get(props as JSONPath)
8-
9-
const _get = (path: JSONPath) =>
10-
path.length === 1
11-
? (data: unknown) => data?.[path[0]]
12-
: (data: unknown) => {
6+
export const get = (path: string | number | JSONPath) =>
7+
isArray(path)
8+
? (data: unknown) => {
139
let value = data
1410

1511
for (const prop of path) {
@@ -18,6 +14,7 @@ const _get = (path: JSONPath) =>
1814

1915
return value
2016
}
17+
: (data: unknown) => data?.[path]
2118

2219
export const string = (text: string) => () => text
2320

@@ -44,7 +41,7 @@ export const sort = <T>(path: JSONPath | JSONProperty = [], direction?: 'asc' |
4441
return (data: T[]) => data.slice().sort(compare)
4542
}
4643

47-
export const pick = (...paths: (JSONPath | JSONProperty)[]) => {
44+
export const pick = (...paths: JSONPath[]) => {
4845
const getters: Getter[] = paths.map((path) =>
4946
isString(path) ? [path, get([path])] : [path[path.length - 1], get(path)]
5047
)

src/jsonquery.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ const scoresData = [
3636

3737
describe('jsonquery', () => {
3838
describe('prop', () => {
39-
test('should get a path with brackets', () => {
39+
test('should get a path with a single property as string', () => {
4040
expect(jsonquery({ name: 'Joe' }, ['get', 'name'])).toEqual('Joe')
4141
})
4242

43-
test('should get a nested path as array', () => {
44-
expect(jsonquery({ user: { name: 'Joe' } }, ['get', ['user', 'name']])).toEqual('Joe')
43+
test('should get a path with a single property as array', () => {
44+
expect(jsonquery({ name: 'Joe' }, ['get', ['name']])).toEqual('Joe')
4545
})
4646

47-
test('should get a nested path as arguments', () => {
48-
expect(jsonquery({ user: { name: 'Joe' } }, ['get', 'user', 'name'])).toEqual('Joe')
47+
test('should get a nested path as array', () => {
48+
expect(jsonquery({ user: { name: 'Joe' } }, ['get', ['user', 'name']])).toEqual('Joe')
4949
})
5050

5151
test('should return undefined in case of a non existing path', () => {
@@ -461,7 +461,7 @@ describe('jsonquery', () => {
461461
})
462462

463463
test('should sort nested data', () => {
464-
expect(jsonquery(nestedData, ['sort', ['get', 'address', 'city']])).toEqual([
464+
expect(jsonquery(nestedData, ['sort', ['get', ['address', 'city']]])).toEqual([
465465
{ name: 'Emily', age: 19, address: { city: 'Atlanta' } },
466466
{ name: 'Kevin', age: 19, address: { city: 'Atlanta' } },
467467
{ name: 'Michelle', age: 27, address: { city: 'Los Angeles' } },
@@ -596,7 +596,7 @@ describe('jsonquery', () => {
596596
})
597597

598598
test('should get nested data from an array with objects', () => {
599-
expect(jsonquery(nestedData, ['map', ['get', 'address', 'city']])).toEqual([
599+
expect(jsonquery(nestedData, ['map', ['get', ['address', 'city']]])).toEqual([
600600
'New York',
601601
'Atlanta',
602602
'New York',
@@ -670,8 +670,8 @@ describe('jsonquery', () => {
670670
expect(jsonquery(data, ['not', 2])).toEqual(false)
671671
expect(jsonquery({ a: false }, ['not', ['get', 'a']])).toEqual(true)
672672
expect(jsonquery({ a: true }, ['not', ['get', 'a']])).toEqual(false)
673-
expect(jsonquery({ nested: { a: false } }, ['not', ['get', 'nested', 'a']])).toEqual(true)
674-
expect(jsonquery({ nested: { a: true } }, ['not', ['get', 'nested', 'a']])).toEqual(false)
673+
expect(jsonquery({ nested: { a: false } }, ['not', ['get', ['nested', 'a']]])).toEqual(true)
674+
expect(jsonquery({ nested: { a: true } }, ['not', ['get', ['nested', 'a']]])).toEqual(false)
675675

676676
expect(jsonquery(data, ['filter', ['not', ['eq', ['get', 'city'], 'New York']]])).toEqual([
677677
{ name: 'Emily', age: 19, city: 'Atlanta' },

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export type JSONProperty = string
2-
export type JSONPath = JSONProperty[]
3-
41
export type JSONQueryPipe = JSONQuery[]
52
export type JSONQueryFunction = [name: string, ...args: JSONQuery[]]
63
export type JSONQueryObject = { [key: string]: JSONQuery }
74
export type JSONQueryPrimitive = string | number | boolean | null
85
export type JSONQuery = JSONQueryFunction | JSONQueryPipe | JSONQueryObject | JSONQueryPrimitive
96

7+
export type JSONPath = string[]
8+
export type JSONProperty = ['get', path: JSONPath]
9+
1010
export interface JSONQueryOptions {
1111
functions?: FunctionsMap
1212
}

0 commit comments

Comments
 (0)