-
-
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.
- Loading branch information
Showing
9 changed files
with
96 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
.DS_Store | ||
*.log | ||
.nyc_output/ | ||
coverage/ | ||
node_modules/ | ||
nlcst-test.js | ||
nlcst-test.min.js | ||
yarn.lock |
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,5 +1,2 @@ | ||
coverage/ | ||
nlcst-test.js | ||
nlcst-test.min.js | ||
*.json | ||
*.md |
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,48 +1,73 @@ | ||
'use strict' | ||
|
||
var assert = require('assert') | ||
var zwitch = require('zwitch') | ||
var mapz = require('mapz') | ||
var unist = require('unist-util-assert') | ||
|
||
var nlcst = zwitch('type') | ||
|
||
exports = unist.wrap(nlcst) | ||
module.exports = exports | ||
|
||
exports.parent = unist.wrap(parent) | ||
exports.text = unist.text | ||
exports.void = unist.void | ||
exports.wrap = unist.wrap | ||
exports.all = mapz(exports, {key: 'children', indices: false}) | ||
|
||
// Core interface. | ||
nlcst.unknown = unknown | ||
nlcst.invalid = unknown | ||
|
||
// Per-type handling. | ||
nlcst.handlers = { | ||
RootNode: unist.wrap(RootNode), | ||
ParagraphNode: exports.parent, | ||
SentenceNode: exports.parent, | ||
WordNode: exports.parent, | ||
TextNode: exports.text, | ||
SymbolNode: exports.text, | ||
PunctuationNode: exports.text, | ||
WhiteSpaceNode: exports.text, | ||
SourceNode: exports.text | ||
import nodeAssert from 'assert' | ||
import {zwitch} from 'zwitch' | ||
import {mapz} from 'mapz' | ||
import { | ||
assert as unistAssert, | ||
parent as unistParent, | ||
literal, | ||
_void, | ||
wrap | ||
} from 'unist-util-assert' | ||
|
||
/** | ||
* Assert that `node` is a valid nlcst node. | ||
* If `node` is a parent, all children will be asserted too. | ||
* | ||
* @param {unknown} [node] | ||
* @param {Parent} [parent] | ||
* @returns {asserts node is Node} | ||
*/ | ||
export function assert(node, parent) { | ||
return wrap(nlcst)(node, parent) | ||
} | ||
|
||
/** | ||
* Assert that `node` is a valid nlcst parent. | ||
* | ||
* @param {unknown} [node] | ||
* @param {Parent} [parent] | ||
* @returns {asserts node is Parent} | ||
*/ | ||
export function parent(node, parent) { | ||
return wrap(assertParent)(node, parent) | ||
} | ||
|
||
export {literal, _void, wrap} | ||
|
||
var all = mapz(assert, {key: 'children'}) | ||
|
||
var nlcst = zwitch('type', { | ||
// Core interface. | ||
unknown, | ||
invalid: unknown, | ||
// Per-type handling. | ||
handlers: { | ||
RootNode: wrap(RootNode), | ||
ParagraphNode: parent, | ||
SentenceNode: parent, | ||
WordNode: parent, | ||
TextNode: literal, | ||
SymbolNode: literal, | ||
PunctuationNode: literal, | ||
WhiteSpaceNode: literal, | ||
SourceNode: literal | ||
} | ||
}) | ||
|
||
function unknown(node, ancestor) { | ||
unist(node, ancestor) | ||
unistAssert(node, ancestor) | ||
} | ||
|
||
function parent(node) { | ||
unist.parent(node) | ||
exports.all(node) | ||
function assertParent(node) { | ||
unistParent(node) | ||
all(node) | ||
} | ||
|
||
function RootNode(node, ancestor) { | ||
parent(node) | ||
assert.strictEqual(ancestor, undefined, '`RootNode` should not have a parent') | ||
nodeAssert.strictEqual( | ||
ancestor, | ||
undefined, | ||
'`RootNode` should not have a 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
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,7 +1,5 @@ | ||
'use strict' | ||
|
||
/* eslint-disable import/no-unassigned-import */ | ||
require('./node') | ||
require('./children') | ||
require('./root') | ||
import './node.js' | ||
import './children.js' | ||
import './root.js' | ||
/* eslint-enable import/no-unassigned-import */ |
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