-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
14 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 |
---|---|---|
@@ -1,19 +1,38 @@ | ||
'use strict'; | ||
let { forEach } = require('property-expr'); | ||
let { forEach } = require('property-expr') | ||
, { has } = require('./_'); | ||
|
||
let trim = part => part.substr(0, part.length - 1).substr(1) | ||
|
||
module.exports = function (obj, path) { | ||
forEach(path, (part, isBracket, isArray) => { | ||
if( isArray) | ||
obj = obj._subType | ||
else { | ||
if (obj._subType) // we skipped an array | ||
obj = obj._subType | ||
|
||
obj = obj.fields[isBracket ? trim(part) : part] | ||
} | ||
module.exports = function (obj, path, value, context) { | ||
// if only one "value" arg then use it for both | ||
context = context || value; | ||
|
||
let parent, lastPart; | ||
|
||
forEach(path, (_part, isBracket, isArray) => { | ||
let part = isBracket ? trim(_part) : _part; | ||
|
||
if (isArray || has(obj, '_subType')) { // we skipped an array | ||
obj = obj._resolve(context, parent)._subType; | ||
value = value && value[0] | ||
} | ||
|
||
if (!isArray) { | ||
obj = obj._resolve(context, parent); | ||
|
||
if (!has(obj, 'fields')) | ||
throw new Error( | ||
`The schema does not contain the path: ${path}. ` + | ||
`(failed at: ${lastPart} which is a type: "${obj._type}") ` | ||
) | ||
|
||
obj = obj.fields[part] | ||
parent = value; | ||
value = value && value[part] | ||
lastPart = isBracket ? '[' + _part + ']' : '.' + _part | ||
} | ||
}) | ||
|
||
return obj | ||
return obj._resolve(parent) | ||
} |
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