Skip to content

Commit 5cea4dc

Browse files
committed
Initial commit
0 parents  commit 5cea4dc

File tree

8 files changed

+214
-0
lines changed

8 files changed

+214
-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-size.js
7+
unist-util-size.min.js

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: node_js
2+
node_js:
3+
- '4.0'
4+
- '6.0'
5+
after_script: bash <(curl -s https://codecov.io/bash)
6+
deploy:
7+
- provider: npm
8+
email: tituswormer@gmail.com
9+
api_key:
10+
secure: 4e1QR4Yx1K6SwZ8iA26PC0JpFdZv7BpNoOE1dIeon6L/e+kH+FXMPsId0jAEB7zLhSRySMjlZE1Ha6oWk6bVPpAVdVaU3t5IC+OOZqIrEtkP/scsv7XD6PmG5OKswjc3KjKWKS/cLX0XmeDAmtxLtiiHYbYGCbMV4ZN5HttPiWCpgUjPrh1Cb0nnQJu05Bwbw8KR/8iazKJKJUcVuu51mKFYplsBIXmLPiBP/h69ql+GhKZ103/+04IZdrNh3C/JfxU9HXksWUqvFL4N76BsxCLw9w5am2JG6jude2OQwcoM4bcYLu+94s7LqRHjpSAlrUSpoE3fJgAf92eIRRtO4atgPlTAFY77BS5QLlokYNs5yDdgY3xkEpBV6tkaJEcMCAbOn2wwmDpk8q12i607+rssYEyese58pRaQymKmQnP/Afi+xgwn5uthOC4vZrsAux7KOVGlrJ9nxoJn4ON9pZwgLgap1LCeJXWjyA/ZswgReCBRQbvZ2zKvEkp9BzL1uukFqkR2btnr7+d+MjY5gJOMrvAe8++p7hz8OUz50D6E5Ss3yYNo93Yn9C3Zf5v7kLvpAptl/e/mtJq/2d/xnVrd2l8iDa412KNt6sNsVGgelPqFV2km/+Nqm3Jwnya83KEfWdbiNb1qh3vw7HpqneL28zWZOng3IIOlTT5DP6g=
11+
on:
12+
tags: true
13+
node: '4.0'
14+
- provider: releases
15+
api_key:
16+
secure: 2Qov064uOLZcZC3gDTFZIhMEZ3I0+v+JkIsKYqQoM7QFwErkmumi9GcYcrufGFIMJCF7AowA6nvQQkiekrLQNbe4bDR4U1EVH0yVv6PMhQL65fxdP2LXseST61lpFg6khvxsJGyBScx+XnQC3I36EkXpjaUZ/EVJEIX6Pr4hWVXZi1kRODGt1UzqLmqEWGLSIxc562wPYuO2SQhfJ2DTcsTK4Lhzu6AsOcQiRb/lSLng4CNEFE6AKhXr92OInzlFbl+j/hZdeDP4hJ55jHrys5KPvNNYgrOR638Jy4aeLuS98Md+poxfKFo1uf4GzG9gVU+RyR5KRXqG/crIpfV5ELAPG8st02sIS1DFh8o2AquGBQnSizqPvlqeme2hApFaDL5cv6pZay6CfSj3LZv/pKDaf0Kqnmye9HPSc9Mr0VEYYMlGTsKh+Vs+a3e5XILWHNH2avqHvgZH2s6taFoq3TYE0Sw9OiZqijkPNSkje10jyD5Yox8Q33Aju6XmGWUupNtH8cCL32VvbJ/7mbGh9AD5+g1MBl1XfTjfDNcARWfwwrtDNuc7HX1DvR1DROmgSBLQbbpLEqKmMeGAWVdS7K5wM7eG4j7+KWRyMjZq6ytrJmbipvU9v5ir6VzPINRc7j78Wg6LPNNdd6FzFyhDV4gnCq/LDSvREby4rUCLC+Q=
17+
file:
18+
- "unist-util-size.js"
19+
- "unist-util-size.min.js"
20+
on:
21+
tags: true
22+
node: '7.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) 2017 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.

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+
module.exports = size;
4+
5+
function size(node) {
6+
var children = node && node.children;
7+
var length = (children && children.length) || 0;
8+
var count = length;
9+
var index = -1;
10+
11+
while (++index < length) {
12+
count += size(children[index]);
13+
}
14+
15+
return count;
16+
}

package.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "unist-util-size",
3+
"version": "0.0.0",
4+
"description": "Calculate the number of nodes in a unist tree",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"node",
9+
"size",
10+
"count"
11+
],
12+
"repository": "https://github.com/syntax-tree/unist-util-size",
13+
"bugs": "https://github.com/syntax-tree/unist-util-size/issues",
14+
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
15+
"contributors": [
16+
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
17+
],
18+
"files": [
19+
"index.js"
20+
],
21+
"dependencies": {},
22+
"devDependencies": {
23+
"browserify": "^13.0.1",
24+
"esmangle": "^1.0.1",
25+
"hastscript": "^3.0.1",
26+
"nyc": "^10.0.0",
27+
"remark": "^6.0.1",
28+
"remark-cli": "^2.0.0",
29+
"remark-preset-wooorm": "^1.0.0",
30+
"tape": "^4.0.0",
31+
"xo": "^0.17.1"
32+
},
33+
"scripts": {
34+
"build-md": "remark . -qfo",
35+
"build-bundle": "browserify index.js --bare -s unistUtilSize > unist-util-size.js",
36+
"build-mangle": "esmangle < unist-util-size.js > unist-util-size.min.js",
37+
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
38+
"lint": "xo",
39+
"test-api": "node test",
40+
"test-coverage": "nyc --reporter lcov tape test.js",
41+
"test": "npm run build && npm run lint && npm run test-coverage"
42+
},
43+
"nyc": {
44+
"check-coverage": true,
45+
"lines": 100,
46+
"functions": 100,
47+
"branches": 100
48+
},
49+
"xo": {
50+
"space": true,
51+
"rules": {
52+
"max-params": "off"
53+
},
54+
"ignores": [
55+
"unist-util-size.js"
56+
]
57+
},
58+
"remarkConfig": {
59+
"presets": "wooorm"
60+
}
61+
}

readme.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# unist-util-size [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
2+
3+
Calculate the number of nodes in a [**Unist**][unist] tree.
4+
5+
## Installation
6+
7+
[npm][]:
8+
9+
```bash
10+
npm install unist-util-size
11+
```
12+
13+
## Usage
14+
15+
```js
16+
var h = require('hastscript');
17+
var size = require('unist-util-size');
18+
19+
var tree = h('div', [
20+
h('p', ['Some ', h('strong', 'importance'), ' and ', h('em', 'emphasis'), '.']),
21+
h('pre', h('code', 'bar()'))
22+
]);
23+
24+
console.log(size(tree)); // 11
25+
console.log(size(tree.children[0])); // 7
26+
console.log(size(tree.children[0].children[0])); // 0
27+
```
28+
29+
## API
30+
31+
### `size(tree)`
32+
33+
Calculate the number of nodes in [`node`][node].
34+
The result is excluding the given node itself.
35+
36+
## License
37+
38+
[MIT][license] © [Titus Wormer][author]
39+
40+
<!-- Definitions -->
41+
42+
[travis-badge]: https://img.shields.io/travis/syntax-tree/unist-util-size.svg
43+
44+
[travis]: https://travis-ci.org/syntax-tree/unist-util-size
45+
46+
[codecov-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-size.svg
47+
48+
[codecov]: https://codecov.io/github/syntax-tree/unist-util-size
49+
50+
[npm]: https://docs.npmjs.com/cli/install
51+
52+
[license]: LICENSE
53+
54+
[author]: http://wooorm.com
55+
56+
[unist]: https://github.com/syntax-tree/unist
57+
58+
[node]: https://github.com/syntax-tree/unist#node

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var h = require('hastscript');
5+
var size = require('./');
6+
7+
test('unist-util-size', function (t) {
8+
var tree = h('div', [
9+
h('p', ['Some ', h('strong', 'importance'), ' and ', h('em', 'emphasis'), '.']),
10+
h('pre', h('code', 'bar()'))
11+
]);
12+
13+
t.equal(size(tree), 11, 'complete tree');
14+
t.equal(size(tree.children[0]), 7, 'parent node with mixed children');
15+
t.equal(size(tree.children[0].children[1]), 1, 'parent of text');
16+
t.equal(size(tree.children[0].children[0]), 0, 'text node');
17+
18+
t.end();
19+
});

0 commit comments

Comments
 (0)