Skip to content

Commit

Permalink
add beforeMatch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind committed Jan 10, 2018
1 parent 8ff167d commit dea3081
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/reference.md
Expand Up @@ -10,6 +10,7 @@
* [`defaultValue`](#defaultvalue)
* [`ignoreUnknown`](#ignoreunknown)
* [`childKey`](#childkey)
* [`beforeMatch`](#beforematch)
* [`onUnmatch`](#onunmatch)
* [`serializer`](#serializer)
* [`deserializer`](#deserializer)
Expand Down Expand Up @@ -77,6 +78,7 @@ const Options = struct({
defaultValue: 'any',
ignoreUnknown: 'boolean',
childKey: 'string',
beforeMatch: 'function',
onUnmatch: 'function',
serializer: 'function',
deserializer: 'function'
Expand All @@ -94,6 +96,9 @@ Decide if current node should be ignored when no rule is matched, `false` by def
#### `childKey`
Key string referencing children of a node. `'children'` by default.

#### `beforeMatch`
Called before matching rules for current node. This handler does not affect subsequent traversing. `(node) => {}` by default.

#### `onUnmatch`
Called when no rule is found for current node and `ignoreKnown === false`. This handler does not affect subsequent traversing. `(node) => {}` by default.

Expand Down
12 changes: 8 additions & 4 deletions src/bumpover/index.js
Expand Up @@ -18,6 +18,7 @@ export class Bumpover {
defaultValue: null,
ignoreUnknown: false,
childKey: 'children',
beforeMatch: node => {},
onUnmatch: node => {},
serializer: a => a,
deserializer: a => a,
Expand All @@ -33,10 +34,11 @@ export class Bumpover {
return
}
const { rules, options, bumpNode } = this
const { ignoreUnknown, onUnmatch, beforeMatch } = options
beforeMatch(node)
const rule = getRule(node, rules)
// Keep or discard unknown node according to `ignoreUnknown` option.
if (!rule) {
const { ignoreUnknown, onUnmatch } = options
if (ignoreUnknown) {
bumpIgnoredNode(node, options, bumpNode, resolve, reject)
return
Expand Down Expand Up @@ -68,20 +70,22 @@ export class Bumpover {
try { Rules(rules) } catch (e) { reject(e) }
try { Options(options) } catch (e) { reject(e) }

const { defaultValue, deserializer, ignoreUnknown } = options
const {
defaultValue, deserializer, ignoreUnknown, beforeMatch, onUnmatch
} = options
// Update root node with rules.
const rootNode = deserializer(input)
if (!rootNode) {
resolve(defaultValue)
return
}

beforeMatch(rootNode)
const rule = getRule(rootNode, rules)
// Resolve default value if root node is unknown.
if (!rule) {
if (ignoreUnknown) resolve(defaultValue)
else {
this.options.onUnmatch(rootNode)
onUnmatch(rootNode)
bumpRoot(rootNode, [], options, bumpNode, resolve, reject)
}
} else {
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Expand Up @@ -4,6 +4,7 @@ export const Options = struct({
defaultValue: 'any',
ignoreUnknown: 'boolean',
childKey: 'string',
beforeMatch: 'function',
onUnmatch: 'function',
serializer: 'function',
deserializer: 'function'
Expand Down

0 comments on commit dea3081

Please sign in to comment.