-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
f30c043
commit d3d24fa
Showing
5 changed files
with
195 additions
and
103 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,70 @@ | ||
import { | ||
getChildKey, | ||
sanitizeResults | ||
} from './utils' | ||
|
||
// Recursively bump node. | ||
export function bumpChildren (node, rules, options, bumpFn, resolve, reject) { | ||
const childKey = getChildKey(node, rules, options) | ||
// Outlet for leaf node. | ||
if (!node) { | ||
resolve(null) | ||
return | ||
} | ||
|
||
if (!Array.isArray(node[childKey])) { | ||
resolve(node) | ||
return | ||
} | ||
|
||
const children = node[childKey] | ||
const childPromises = children.map(bumpFn) | ||
const bumpAll = Promise.all(childPromises) | ||
bumpAll.then(results => { | ||
resolve({ ...node, [childKey]: sanitizeResults(results) }) | ||
}).catch(reject) | ||
} | ||
|
||
// Recursively bump node. Since ignored nodes doesn't have their `rules`, | ||
// we don't pass in `rules` here, other args remains the same. | ||
export function bumpIgnoredNode (node, options, bumpFn, resolve, reject) { | ||
const childKey = getChildKey(node, [], options) | ||
// Resolve null if the ignored node is leaf. | ||
if (!node || !node[childKey]) { | ||
resolve(null) | ||
return | ||
} | ||
// Resolve array of results. | ||
const children = node[childKey] | ||
const childPromises = children.map(bumpFn) | ||
const bumpAll = Promise.all(childPromises) | ||
bumpAll.then(results => { | ||
resolve(sanitizeResults(results)) | ||
}).catch(reject) | ||
} | ||
|
||
// Recursively bump root node. | ||
export function bumpRoot (node, rules, options, bumpFn, resolve, reject) { | ||
const { serializer, defaultValue } = options | ||
const childKey = getChildKey(node, rules, options) | ||
if (!node) { | ||
resolve(defaultValue) | ||
return | ||
} | ||
|
||
if (!Array.isArray(node[childKey])) { | ||
resolve(serializer(node) || defaultValue) | ||
return | ||
} | ||
|
||
const children = node[childKey] | ||
const childPromises = children.map(bumpFn) | ||
const bumpChildren = Promise.all(childPromises) | ||
bumpChildren.then(results => { | ||
const output = serializer({ | ||
...node, | ||
[childKey]: sanitizeResults(results) | ||
}) | ||
resolve(output || defaultValue) | ||
}).catch(reject) | ||
} |
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 @@ | ||
import { getRule } from '../rules' | ||
|
||
// Validate node with possible struct provided in rules. | ||
function validateNode (node, struct) { | ||
if (!struct) return node | ||
try { return struct(node) } catch (e) { throw e } | ||
} | ||
|
||
// Result items can be array, object or null. | ||
// Flatten results to array of objects. | ||
export function sanitizeResults (maybeResults) { | ||
const results = maybeResults | ||
.map(result => Array.isArray(result) ? result : [result]) | ||
.reduce((a, b) => [...a, ...b], []) | ||
.filter(result => !!result) | ||
return results | ||
} | ||
|
||
// Get child key by rules and options | ||
export function getChildKey (node, rules, options) { | ||
const rule = getRule(node, rules) | ||
if (rule && rule.childKey) return rule.childKey | ||
else return options.childKey | ||
} | ||
|
||
// Result can be array, object or null. Unify its shape to result struct. | ||
export function resolveResult (node, result, struct, childKey) { | ||
if (!result) { | ||
return { action: 'stop', newNode: null } | ||
} else if (Array.isArray(result)) { | ||
return { | ||
action: 'next', | ||
newNode: { | ||
...validateNode(node, struct), | ||
[childKey]: sanitizeResults(result) | ||
} | ||
} | ||
} else { | ||
// Provide default action. | ||
const { action = 'next', node } = result | ||
const newNode = validateNode(node, struct) | ||
return { action, newNode } | ||
} | ||
} |
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