Skip to content

Commit fd35b5e

Browse files
committed
Refactor code-style
1 parent 2bf5f2c commit fd35b5e

File tree

5 files changed

+135
-122
lines changed

5 files changed

+135
-122
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
unist-util-is.js
3+
unist-util-is.min.js

index.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,119 @@
1-
'use strict';
1+
'use strict'
22

33
/* eslint-disable max-params */
44

55
/* Expose. */
6-
module.exports = is;
6+
module.exports = is
77

88
/* Assert if `test` passes for `node`.
99
* When a `parent` node is known the `index` of node */
1010
function is(test, node, index, parent, context) {
11-
var hasParent = parent !== null && parent !== undefined;
12-
var hasIndex = index !== null && index !== undefined;
13-
var check = convert(test);
11+
var hasParent = parent !== null && parent !== undefined
12+
var hasIndex = index !== null && index !== undefined
13+
var check = convert(test)
1414

1515
if (
1616
hasIndex &&
1717
(typeof index !== 'number' || index < 0 || index === Infinity)
1818
) {
19-
throw new Error('Expected positive finite index or child node');
19+
throw new Error('Expected positive finite index or child node')
2020
}
2121

2222
if (hasParent && (!is(null, parent) || !parent.children)) {
23-
throw new Error('Expected parent node');
23+
throw new Error('Expected parent node')
2424
}
2525

2626
if (!node || !node.type || typeof node.type !== 'string') {
27-
return false;
27+
return false
2828
}
2929

3030
if (hasParent !== hasIndex) {
31-
throw new Error('Expected both parent and index');
31+
throw new Error('Expected both parent and index')
3232
}
3333

34-
return Boolean(check.call(context, node, index, parent));
34+
return Boolean(check.call(context, node, index, parent))
3535
}
3636

3737
function convert(test) {
3838
if (typeof test === 'string') {
39-
return typeFactory(test);
39+
return typeFactory(test)
4040
}
4141

4242
if (test === null || test === undefined) {
43-
return ok;
43+
return ok
4444
}
4545

4646
if (typeof test === 'object') {
47-
return ('length' in test ? anyFactory : matchesFactory)(test);
47+
return ('length' in test ? anyFactory : matchesFactory)(test)
4848
}
4949

5050
if (typeof test === 'function') {
51-
return test;
51+
return test
5252
}
5353

54-
throw new Error('Expected function, string, or object as test');
54+
throw new Error('Expected function, string, or object as test')
5555
}
5656

5757
function convertAll(tests) {
58-
var results = [];
59-
var length = tests.length;
60-
var index = -1;
58+
var results = []
59+
var length = tests.length
60+
var index = -1
6161

6262
while (++index < length) {
63-
results[index] = convert(tests[index]);
63+
results[index] = convert(tests[index])
6464
}
6565

66-
return results;
66+
return results
6767
}
6868

6969
/* Utility assert each property in `test` is represented
7070
* in `node`, and each values are strictly equal. */
7171
function matchesFactory(test) {
72-
return matches;
72+
return matches
7373

7474
function matches(node) {
75-
var key;
75+
var key
7676

7777
for (key in test) {
7878
if (node[key] !== test[key]) {
79-
return false;
79+
return false
8080
}
8181
}
8282

83-
return true;
83+
return true
8484
}
8585
}
8686

8787
function anyFactory(tests) {
88-
var checks = convertAll(tests);
89-
var length = checks.length;
88+
var checks = convertAll(tests)
89+
var length = checks.length
9090

91-
return matches;
91+
return matches
9292

9393
function matches() {
94-
var index = -1;
94+
var index = -1
9595

9696
while (++index < length) {
9797
if (checks[index].apply(this, arguments)) {
98-
return true;
98+
return true
9999
}
100100
}
101101

102-
return false;
102+
return false
103103
}
104104
}
105105

106106
/* Utility to convert a string into a function which checks
107107
* a given node’s type for said string. */
108108
function typeFactory(test) {
109-
return type;
109+
return type
110110

111111
function type(node) {
112-
return Boolean(node && node.type === test);
112+
return Boolean(node && node.type === test)
113113
}
114114
}
115115

116116
/* Utility to return true. */
117117
function ok() {
118-
return true;
118+
return true
119119
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,31 @@
2727
"browserify": "^16.0.0",
2828
"esmangle": "^1.0.1",
2929
"nyc": "^11.0.0",
30+
"prettier": "^1.12.1",
3031
"remark-cli": "^5.0.0",
3132
"remark-preset-wooorm": "^4.0.0",
3233
"tape": "^4.0.0",
3334
"xo": "^0.20.0"
3435
},
3536
"scripts": {
36-
"build-md": "remark . -qfo",
37+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3738
"build-bundle": "browserify index.js --bare -s unistUtilIs > unist-util-is.js",
3839
"build-mangle": "esmangle < unist-util-is.js > unist-util-is.min.js",
39-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
40-
"lint": "xo",
40+
"build": "npm run build-bundle && npm run build-mangle",
4141
"test-api": "node test",
4242
"test-coverage": "nyc --reporter lcov tape test.js",
43-
"test": "npm run build && npm run lint && npm run test-coverage"
43+
"test": "npm run format && npm run build && npm run test-coverage"
44+
},
45+
"prettier": {
46+
"tabWidth": 2,
47+
"useTabs": false,
48+
"singleQuote": true,
49+
"bracketSpacing": false,
50+
"semi": false,
51+
"trailingComma": "none"
4452
},
4553
"xo": {
46-
"space": true,
54+
"prettier": true,
4755
"esnext": false,
4856
"rules": {
4957
"guard-for-in": "off",

readme.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@ npm install unist-util-is
1313
## Usage
1414

1515
```js
16-
var is = require('unist-util-is');
16+
var is = require('unist-util-is')
1717

18-
var node = {type: 'strong'};
19-
var parent = {type: 'paragraph', children: [node]};
18+
var node = {type: 'strong'}
19+
var parent = {type: 'paragraph', children: [node]}
2020

21-
function test(node, n) { return n === 5 }
21+
function test(node, n) {
22+
return n === 5
23+
}
2224

23-
is(); // false
24-
is(null, {children: []}); // false
25-
is(null, node); // true
26-
is('strong', node); // true
27-
is('emphasis', node); // false
25+
is() // => false
26+
is(null, {children: []}) // => false
27+
is(null, node) // => true
28+
is('strong', node) // => true
29+
is('emphasis', node) // => false
2830

29-
is(node, node) // true
30-
is({type: 'paragraph'}, parent) // true
31-
is({type: 'strong'}, parent) // false
31+
is(node, node) // => true
32+
is({type: 'paragraph'}, parent) // => true
33+
is({type: 'strong'}, parent) // => false
3234

33-
is(test, node); // false
34-
is(test, node, 4, parent); // false
35-
is(test, node, 5, parent); // true
35+
is(test, node) // => false
36+
is(test, node, 4, parent) // => false
37+
is(test, node, 5, parent) // => true
3638
```
3739

3840
## API

0 commit comments

Comments
 (0)