-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model): add support to nested keys for relations (#127)
* feat(utils): add utilities `getProp` and `setProp` * feat(model): add support to nested keys for relations * test(model): test support of nested keys for relations * test(utils): test utilities * chore: update readme
- Loading branch information
1 parent
1bc6248
commit 095c1c3
Showing
11 changed files
with
204 additions
and
15 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
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,44 @@ | ||
/** | ||
* Get property defined by dot notation in string. | ||
* Based on https://github.com/dy/dotprop (MIT) | ||
* | ||
* @param {Object} holder Target object where to look property up | ||
* @param {string} propName Dot notation, like 'this.a.b.c' | ||
* @return {*} A property value | ||
*/ | ||
export function getProp (holder, propName) { | ||
if (!propName || !holder) { | ||
return holder | ||
} | ||
|
||
if (propName in holder) { | ||
return holder[propName] | ||
} | ||
|
||
const propParts = Array.isArray(propName) ? propName : (propName + '').split('.') | ||
|
||
let result = holder | ||
while (propParts.length && result) { | ||
result = result[propParts.shift()] | ||
} | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* Set property defined by dot notation in string. | ||
* Based on https://github.com/lukeed/dset (MIT) | ||
* | ||
* @param {Object} holder Target object where to look property up | ||
* @param {string} propName Dot notation, like 'this.a.b.c' | ||
* @param {*} value The value to be set | ||
*/ | ||
export function setProp (holder, propName, value) { | ||
const propParts = Array.isArray(propName) ? propName : (propName + '').split('.') | ||
let i = 0, l = propParts.length, t = holder, x | ||
|
||
for (; i < l; ++i) { | ||
x = t[propParts[i]] | ||
t = t[propParts[i]] = (i === l - 1 ? value : (x != null ? x : (!!~propParts[i + 1].indexOf('.') || !(+propParts[i + 1] > -1)) ? {} : [])) | ||
} | ||
} |
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
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 @@ | ||
import BaseModel from './BaseModel' | ||
|
||
export default class Tag extends BaseModel { | ||
// | ||
} |
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,33 @@ | ||
import { getProp, setProp } from '../src/utils' | ||
|
||
describe('Utilities', () => { | ||
test('Get property defined by dot notation in string.', () => { | ||
const holder = { | ||
a: { | ||
b: { | ||
c: 1 | ||
} | ||
} | ||
} | ||
|
||
const result = getProp(holder, 'a.b.c') | ||
|
||
expect(result).toBe(1) | ||
}) | ||
|
||
test('Set property defined by dot notation in string.', () => { | ||
const holder = { | ||
a: { | ||
b: { | ||
c: 1 | ||
} | ||
} | ||
} | ||
|
||
setProp(holder, 'a.b.c', 2) | ||
|
||
const result = getProp(holder, 'a.b.c') | ||
|
||
expect(result).toBe(2) | ||
}) | ||
}) |