Skip to content

Commit 896a903

Browse files
committed
Init
0 parents  commit 896a903

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "0.12"
4+
- iojs

LICENSE

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

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[![npm](https://nodei.co/npm/unist-util-builder.png)](https://npmjs.com/package/unist-util-builder)
2+
3+
# unist-util-builder
4+
5+
[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david]
6+
7+
Helper for creating [unist] trees with [hyperscript]-like syntax.
8+
9+
[unist]: https://github.com/wooorm/unist
10+
[hyperscript]: https://github.com/dominictarr/hyperscript
11+
12+
[travis]: https://travis-ci.org/eush77/unist-util-builder
13+
[travis-badge]: https://travis-ci.org/eush77/unist-util-builder.svg?branch=master
14+
[david]: https://david-dm.org/eush77/unist-util-builder
15+
[david-badge]: https://david-dm.org/eush77/unist-util-builder.png
16+
17+
## Example
18+
19+
```js
20+
var u = require('unist-util-builder');
21+
22+
u('root', [
23+
u('subtree', { id: 1 }),
24+
u('subtree', { id: 2 }, [
25+
u('node', [
26+
u('leaf', 'leaf-1'),
27+
u('leaf', 'leaf-2')
28+
]),
29+
u('leaf', { id: 3 }, 'leaf-3')
30+
])
31+
])
32+
```
33+
34+
results in the following tree:
35+
36+
```json
37+
{
38+
"type": "root",
39+
"children": [
40+
{
41+
"type": "subtree",
42+
"id": 1
43+
},
44+
{
45+
"type": "subtree",
46+
"id": 2,
47+
"children": [
48+
{
49+
"type": "node",
50+
"children": [
51+
{
52+
"type": "leaf",
53+
"value": "leaf-1"
54+
},
55+
{
56+
"type": "leaf",
57+
"value": "leaf-2"
58+
}
59+
]
60+
},
61+
{
62+
"type": "leaf",
63+
"id": 3,
64+
"value": "leaf-3"
65+
}
66+
]
67+
}
68+
]
69+
}
70+
```
71+
72+
## API
73+
74+
#### `u(type, [props], [value])`
75+
76+
`type`: `String`<br>
77+
`props`: `Object`<br>
78+
`value`: `String`<br>
79+
80+
Creates a node from `props` and optional `value`.
81+
82+
#### `u(type, [props], children)`
83+
84+
`type`: `String`<br>
85+
`props`: `Object`<br>
86+
`children`: `Array`<br>
87+
88+
Creates a node from `props` and given child nodes.
89+
90+
## Install
91+
92+
```
93+
npm install unist-util-builder
94+
```
95+
96+
## License
97+
98+
MIT

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var assign = require('object.assign');
4+
5+
6+
module.exports = function u (type, props, value) {
7+
if (value == null && (typeof props != 'object' || Array.isArray(props))) {
8+
value = props;
9+
props = {};
10+
}
11+
12+
return assign({}, props, { type: String(type) },
13+
value && (Array.isArray(value)
14+
? { children: value }
15+
: { value: String(value) }));
16+
};

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "unist-util-builder",
3+
"version": "0.0.0",
4+
"description": "Helper for creating unist trees",
5+
"author": "Eugene Sharygin <[email protected]>",
6+
"license": "MIT",
7+
"scripts": {
8+
"test": "tape test/*.js"
9+
},
10+
"files": [
11+
"index.js"
12+
],
13+
"homepage": "https://github.com/eush77/unist-util-builder",
14+
"repository": "eush77/unist-util-builder",
15+
"bugs": {
16+
"url": "https://github.com/eush77/unist-util-builder/issues"
17+
},
18+
"keywords": [
19+
"ast",
20+
"build",
21+
"builder",
22+
"create",
23+
"dsl",
24+
"hyperscript",
25+
"sugar",
26+
"syntax",
27+
"tree",
28+
"unist"
29+
],
30+
"dependencies": {
31+
"object.assign": "^4.0.1"
32+
},
33+
"devDependencies": {
34+
"tape": "^4.2.0"
35+
}
36+
}

test/test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
var u = require('..');
4+
5+
var test = require('tape');
6+
7+
8+
test(function (t) {
9+
var ast = u('root', [
10+
u('subtree', { id: 1 }),
11+
u('subtree', { id: 2 }, [
12+
u('node', [
13+
u('leaf', 'leaf-1'),
14+
u('leaf', 'leaf-2')
15+
]),
16+
u('leaf', { id: 3 }, 'leaf-3')
17+
])
18+
]);
19+
20+
t.deepEqual(ast, {
21+
type: 'root',
22+
children: [{
23+
type: 'subtree',
24+
id: 1
25+
}, {
26+
type: 'subtree',
27+
id: 2,
28+
children: [{
29+
type: 'node',
30+
children: [{
31+
type: 'leaf',
32+
value: 'leaf-1'
33+
}, {
34+
type: 'leaf',
35+
value: 'leaf-2'
36+
}]
37+
}, {
38+
type: 'leaf',
39+
id: 3,
40+
value: 'leaf-3'
41+
}]
42+
}]
43+
});
44+
45+
t.end();
46+
});

0 commit comments

Comments
 (0)