Skip to content

Commit 4677492

Browse files
committed
Initial commit
0 parents  commit 4677492

18 files changed

+1073
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
unist-util-assert.js
7+
unist-util-assert.min.js

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: node_js
2+
node_js:
3+
- '0.11'
4+
- '0.12'
5+
- '4.0'
6+
- '5.0'
7+
- '6.0'
8+
after_success: bash <(curl -s https://codecov.io/bash)
9+
deploy:
10+
- provider: npm
11+
email: tituswormer@gmail.com
12+
api_key:
13+
secure: YnwZlIth5sFwvits1uGnF5T7/NTkGtYaO/DI4dVkCzirrZ/LAcKObu+1LHuV6x8JwrsNEkxOZojrXy1M6/uLG5JI2i7uCTqkZeDj5GCvnHYS9fnHIXW7tNFPCrhXPHz9AVM2wH1YHL4io+WEDwcmj8+NSgW+NpT2w1VZebNionKeyVxKNHxcefvkKIcQkyfhhMTBtZizgQDFXBYns46KK2Z7K3T3RpKYUVQHCaKbsBe9gnORx/TYJCEisEFuRSAfX2q2BbLIhx3b24lPn2YnTP7CJtmHQBMBah0Q2qfADoVGDwBdYVat+zZixvV/1cwMb+sBAWZJaaLNCsvj6yCyDRThlrX85qduvD0X8k74H3KXXp9DygBtzg+33Ntk4J8dqwvgoa65hpCZxFHZvLmA7yFvZTgbQV3kQ0T0fXE6K16xd0j2OMMOJChIkbTKuq2g51WHmPQCdU2Iyk+OTB1/8cTVq0nRu7ASGNiJlAulqZPLxIo1bDZXM5kdCmrXCWOLJPQCV+S0tKRbO8Re2jB6LOWcD5pduR7DvhABd+tx2eFw8ZrCkhwcji3oadv0INLgJuT0BLYPrtVs+I+Xq7gxz/hc2PxbY1EC/4XqUIAuFxRNWFF2PwA10qvQDKLx79NafSJ7tVIVCchMjsTZza8b57Oaecw2+Q58nGnz98wZds4=
14+
on:
15+
tags: true
16+
node: '5.0'
17+
- provider: releases
18+
api_key:
19+
secure: qHdUT4ioWdJk8Vsk1QcWRU88miY/VEOHUSaRMYGs66/KwjKJZTXwwEpaU0O10AZvlX+xGwvg2omzrc0MY6YMu6Zol/PiGcI0yDD06LAOIW2J+X2877vw5YvQBZMW2iSL7bF8utOt5SpxtYEIV58tNHZwbFCS0zTDpqj62chKlFHOi+c95fqV33upRUx3vjVCbz0VdGIjsgyg++4Us2qZawfilhfsvNqG1UoJTdZqG5qvEJJ10IxnJqRTylrzzn+h2wP3RODmuWTYOhSQj8maE+jdlTb5bt0vd2Omo/Rd8dsAOWY87qsaL8x2hTcQO3dM9p07QjyhVu62YLfEqMP0TclnHb0L4t9mu5FVkp8aNs7ocL2tqh8B0RBTZSx1m0IVfOe6HSDNsGuMxZai2Zk9dZLxQyWO9Y66ohH3wv34MPtFvrdBBCB60Kcc4DjRNjSirFqU716zF0PzmkKLA55z2Txbqcqp3qBnMUojuDtuSkfquy5+/30q6ssfQV6czy/Sp/4zZMyb2X3Oq2smk/cb593rVQ/KtJIkjNByw+WZk90z6tXMQvxkhftNVjZxmBXZC70igV+zaWNAYwXQMGPkp+dcu5IkeX3GTfry50Z4BKfHmkJzgT3l4d6FpeO0yynzmHH6jSHxXsbnoo32CDWyhYTpdZPq/0LL1afWd13h9rk=
20+
file:
21+
- "unist-util-assert.js"
22+
- "unist-util-assert.min.js"
23+
on:
24+
tags: true
25+
node: '6.0'

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

history.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--remark setext-->
2+
3+
<!--lint disable no-multiple-toplevel-headings -->
4+
5+
0.0.0 / 2016-07-19
6+
==================

index.js

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2016 Titus Wormer
4+
* @license MIT
5+
* @module unist-util-assert
6+
* @fileoverview Assert `unist` nodes.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
/* eslint-disable no-useless-concat */
13+
14+
/* Dependencies. */
15+
var assert = require('assert');
16+
var array = require('x-is-array');
17+
var object = require('x-is-object');
18+
19+
var inspect;
20+
21+
try {
22+
inspect = require('ut' + 'il').inspect;
23+
} catch (err) { /* Empty. */ }
24+
25+
/* Expose. */
26+
module.exports = exports = wrap(unist);
27+
28+
exports.parent = wrap(parent);
29+
exports.text = wrap(text);
30+
exports.void = wrap(empty);
31+
exports.wrap = wrap;
32+
33+
/* Identifier to check if a value is seen. */
34+
var ID = '__unist__';
35+
36+
/* List of specced properties. */
37+
var defined = ['type', 'value', 'children', 'position'];
38+
39+
/**
40+
* Wrapper around `Node` which adds the current node
41+
* (and parent, if available), to the message.
42+
*
43+
* @param {Node} node - Node to checl.
44+
* @param {Node?} [parent] - Parent of `node`.
45+
* @throws {Error} - Prettified error.
46+
*/
47+
function wrap(fn) {
48+
return wrapped;
49+
50+
function wrapped(node, parent) {
51+
try {
52+
fn(node, parent);
53+
} catch (err) {
54+
if (!err[ID]) {
55+
err[ID] = true;
56+
57+
err.message += ': `' + view(node) + '`';
58+
59+
if (parent) {
60+
err.message += ' in `' + view(parent) + '`';
61+
}
62+
}
63+
64+
throw err;
65+
}
66+
}
67+
}
68+
69+
/**
70+
* Assert.
71+
*
72+
* @param {Node} node - Value to assert.
73+
* @throws {Error} - If the given value doesn’t adhere to
74+
* unist.
75+
*/
76+
function unist(node) {
77+
var type;
78+
var children;
79+
var value;
80+
var key;
81+
var index;
82+
var length;
83+
84+
assert.ok(object(node), 'node should be an object');
85+
86+
type = node.type;
87+
children = node.children;
88+
value = node.value;
89+
90+
assert.ok('type' in node, 'node should have a type');
91+
assert.equal(typeof type, 'string', '`type` should be a string');
92+
assert.notEqual(type, '', '`type` should not be empty');
93+
94+
if (value != null) {
95+
assert.equal(typeof value, 'string', '`value` should be a string');
96+
}
97+
98+
location(node.position);
99+
100+
for (key in node) {
101+
if (defined.indexOf(key) === -1) {
102+
vanilla(key, node[key]);
103+
}
104+
}
105+
106+
if (children != null) {
107+
assert.ok(array(children), '`children` should be an array');
108+
index = -1;
109+
length = children.length;
110+
111+
while (++index < length) {
112+
exports(children[index], node);
113+
}
114+
}
115+
}
116+
117+
/**
118+
* Assert `value` (which lives at `key`) can be stringified
119+
* and re-parsed to the same (deep) value.
120+
*
121+
* @param {*} value - Value to assert.
122+
* @param {string} key - Property at which `value` lives.
123+
* @throws {Error} - If the given value doesn’t adhere to
124+
* `Parent`.
125+
*/
126+
function vanilla(key, value) {
127+
try {
128+
assert.deepEqual(value, JSON.parse(JSON.stringify(value)));
129+
} catch (err) {
130+
assert.fail('', '', 'non-specced property `' + key + '` should be JSON');
131+
}
132+
}
133+
134+
/**
135+
* Stringify a value to inspect it. Tries `JSON.stringify()`,
136+
* and if that fails uses `String()` instead. If `stringify()`
137+
* works, ``
138+
*
139+
* @param {*} value - Value to assert.
140+
* @param {string} key - Property at which `value` lives.
141+
* @throws {Error} - If the given value doesn’t adhere to
142+
* `Parent`.
143+
*/
144+
function view(value) {
145+
try {
146+
/* eslint-disable no-else-return */
147+
/* istanbul ignore else - Browser. */
148+
if (inspect) {
149+
return inspect(value, {colors: false});
150+
} else {
151+
return JSON.stringify(value);
152+
}
153+
} catch (err) {
154+
/* istanbul ignore next - Cyclical. */
155+
return String(value);
156+
}
157+
}
158+
159+
/**
160+
* Assert `node` is a parent node.
161+
*
162+
* @param {Node} node - Value to assert.
163+
* @throws {Error} - If the given value doesn’t adhere to
164+
* `Parent`.
165+
*/
166+
function parent(node) {
167+
unist(node);
168+
169+
assert.equal('value' in node, false, 'parent should not have `value`');
170+
assert.ok('children' in node, 'parent should have `children`');
171+
}
172+
173+
/**
174+
* Assert `node` is a text node.
175+
*
176+
* @param {Node} node - Value to assert.
177+
* @throws {Error} - If the given value doesn’t adhere to
178+
* `Text`.
179+
*/
180+
function text(node) {
181+
unist(node);
182+
183+
assert.equal('children' in node, false, 'text should not have `children`');
184+
assert.ok('value' in node, 'text should have `value`');
185+
}
186+
187+
/**
188+
* Assert `node` is a Unist node, but neither parent nor
189+
* text.
190+
*
191+
* @param {Node} node - Value to assert.
192+
* @throws {Error} - If the given value doesn’t adhere to
193+
* Unist, is a `Parent`, or a `Text`.
194+
*/
195+
function empty(node) {
196+
unist(node);
197+
198+
assert.equal('value' in node, false, 'void should not have `value`');
199+
assert.equal('children' in node, false, 'void should not have `children`');
200+
}
201+
202+
/**
203+
* Assert `location` is a Unist Location.
204+
*
205+
* @param {Location} location - Location to assert.
206+
* @throws {Error} - If the given value doesn’t adhere to
207+
* Unist Location.
208+
*/
209+
function location(location) {
210+
if (location != null) {
211+
assert.ok(object(location), '`position` should be an object');
212+
213+
position(location.start, 'position.start');
214+
position(location.end, 'position.end');
215+
}
216+
}
217+
218+
/**
219+
* Assert `location` is a Unist Location.
220+
*
221+
* @param {Location} location - Location to assert.
222+
* @throws {Error} - If the given value doesn’t adhere to
223+
* Unist Location.
224+
*/
225+
function position(position, name) {
226+
if (position != null) {
227+
assert.ok(object(position), '`' + name + '` should be an object');
228+
229+
if (position.line != null) {
230+
assert.ok('line' in position, '`' + name + '` should have numeric `line`');
231+
assert.ok(position.line >= 1, '`' + name + '.line` should be gte `1`');
232+
}
233+
234+
if (position.column != null) {
235+
assert.ok('column' in position, '`' + name + '` should have numeric `column`');
236+
assert.ok(position.column >= 1, '`' + name + '.column` should be gte `1`');
237+
}
238+
}
239+
}

package.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "unist-util-assert",
3+
"version": "0.0.0",
4+
"description": "Assert Unist nodes",
5+
"license": "MIT",
6+
"keywords": [
7+
"assert",
8+
"unist",
9+
"util",
10+
"utility"
11+
],
12+
"files": [
13+
"index.js"
14+
],
15+
"repository": "https://github.com/wooorm/unist-util-assert",
16+
"bugs": "https://github.com/wooorm/unist-util-assert/issues",
17+
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
18+
"contributors": [
19+
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
20+
],
21+
"dependencies": {
22+
"x-is-array": "^0.1.0",
23+
"x-is-object": "^0.1.0"
24+
},
25+
"devDependencies": {
26+
"browserify": "^13.0.1",
27+
"esmangle": "^1.0.1",
28+
"nyc": "^7.0.0",
29+
"remark-cli": "^1.0.0",
30+
"remark-comment-config": "^4.0.0",
31+
"remark-github": "^5.0.0",
32+
"remark-lint": "^4.0.0",
33+
"remark-validate-links": "^4.0.0",
34+
"tape": "^4.0.0",
35+
"xo": "^0.16.0"
36+
},
37+
"scripts": {
38+
"build-md": "remark . --quiet --frail",
39+
"build-bundle": "browserify index.js -s unistUtilAssert > unist-util-assert.js",
40+
"build-mangle": "esmangle < unist-util-assert.js > unist-util-assert.min.js",
41+
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
42+
"lint": "xo",
43+
"test-api": "node test",
44+
"test-coverage": "nyc --reporter lcov tape test",
45+
"test": "npm run build && npm run lint && npm run test-coverage"
46+
},
47+
"xo": {
48+
"space": true,
49+
"rules": {
50+
"no-eq-null": "off",
51+
"eqeqeq": "off"
52+
},
53+
"ignores": [
54+
"unist-util-assert.js",
55+
"unist-util-assert.min.js"
56+
]
57+
},
58+
"nyc": {
59+
"check-coverage": true,
60+
"lines": 100,
61+
"functions": 100,
62+
"branches": 100
63+
},
64+
"remarkConfig": {
65+
"output": true,
66+
"plugins": [
67+
"comment-config",
68+
"github",
69+
"lint",
70+
"validate-links"
71+
],
72+
"settings": {
73+
"bullet": "*"
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)