-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nested access on dynamic getter + enhance object pÃath access
- Loading branch information
Showing
8 changed files
with
165 additions
and
65 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
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,8 @@ | ||
/* | ||
* Copyright (c) 2023. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
export * from './module'; |
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,46 @@ | ||
/* | ||
* Copyright (c) 2023-2023. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import type { Context } from '../type'; | ||
import { getObjectPathProperty, isObject } from '../utils'; | ||
|
||
type Output = { success: true, data: any } | { success: false}; | ||
export function evaluatePathForDynamicGetters( | ||
data: Record<string, any>, | ||
key: string, | ||
context: Context, | ||
) : Output { | ||
const dotIndex = key.indexOf('.'); | ||
const currentKey = dotIndex === -1 ? | ||
key : | ||
key.substring(0, dotIndex); | ||
|
||
if (typeof data[currentKey] === 'function') { | ||
const value = data[currentKey](context); | ||
|
||
if (dotIndex === -1) { | ||
return { | ||
success: true, | ||
data: value, | ||
}; | ||
} | ||
|
||
return { | ||
success: true, | ||
data: getObjectPathProperty(value, key.substring(currentKey.length + 1)), | ||
}; | ||
} | ||
|
||
if (isObject(data[currentKey])) { | ||
const nextKey = key.substring(currentKey.length + 1); | ||
if (nextKey.length > 0) { | ||
return evaluatePathForDynamicGetters(data[currentKey], nextKey, context); | ||
} | ||
} | ||
|
||
return { success: false }; | ||
} |
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
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,27 @@ | ||
import { hasObjectPathProperty, setObjectPathProperty } from '../../src'; | ||
|
||
describe('src/utils/object-path', () => { | ||
it('should set object path property', () => { | ||
let data : Record<string, any> = {}; | ||
setObjectPathProperty(data, 'foo', 'bar'); | ||
expect(data).toEqual({ foo: 'bar' }); | ||
|
||
data = {}; | ||
setObjectPathProperty(data, 'foo.bar', 'baz'); | ||
expect(data).toEqual({ foo: { bar: 'baz' } }); | ||
}); | ||
|
||
it('should check object path property', () => { | ||
const data = { | ||
foo: { | ||
bar: { | ||
baz: 'boz', | ||
}, | ||
}, | ||
}; | ||
|
||
expect(hasObjectPathProperty(data, 'foo.bar')).toBeTruthy(); | ||
expect(hasObjectPathProperty(data, 'foo.bar.baz')).toBeTruthy(); | ||
expect(hasObjectPathProperty(data, 'foo.boz')).toBeFalsy(); | ||
}); | ||
}); |