Skip to content

Commit f6395f0

Browse files
committed
Refactor code-style
1 parent 84b02c5 commit f6395f0

File tree

5 files changed

+81
-143
lines changed

5 files changed

+81
-143
lines changed

.editorconfig

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ root = true
22

33
[*]
44
indent_style = space
5-
indent_size = 4
5+
indent_size = 2
66
end_of_line = lf
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.{json,html,svg,css,remarkrc,eslintrc}]
12-
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

index.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2015 Titus Wormer
4-
* @license MIT
5-
* @module mdast:util:to-string
6-
* @fileoverview Utility to get the plain text content of a node.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env node */
12-
13-
/**
14-
* Get the value of `node`. Checks, `value`,
15-
* `alt`, and `title`, in that order.
16-
*
17-
* @param {Node} node - Node to get the internal value of.
18-
* @return {string} - Textual representation.
19-
*/
20-
function valueOf(node) {
21-
return node &&
22-
(node.value ? node.value :
23-
(node.alt ? node.alt : node.title)) || '';
24-
}
3+
/* Expose. */
4+
module.exports = toString;
255

26-
/**
27-
* Returns the text content of a node. If the node itself
6+
/* Get the text content of a node. If the node itself
287
* does not expose plain-text fields, `toString` will
29-
* recursivly try its children.
30-
*
31-
* @param {Node} node - Node to transform to a string.
32-
* @return {string} - Textual representation.
33-
*/
8+
* recursivly try its children. */
349
function toString(node) {
35-
return valueOf(node) ||
36-
(node.children && node.children.map(toString).join('')) ||
37-
'';
10+
return valueOf(node) ||
11+
(node.children && node.children.map(toString).join('')) ||
12+
'';
3813
}
3914

40-
/*
41-
* Expose.
42-
*/
43-
44-
module.exports = toString;
15+
/* Get the value of `node`. Checks, `value`,
16+
* `alt`, and `title`, in that order. */
17+
function valueOf(node) {
18+
if (!node) {
19+
return '';
20+
}
21+
return node.value ? node.value : (node.alt ? node.alt : node.title) || '';
22+
}

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,31 @@
2727
"dependencies": {},
2828
"devDependencies": {
2929
"browserify": "^13.0.0",
30-
"eslint": "^2.0.0",
3130
"esmangle": "^1.0.0",
3231
"istanbul": "^0.4.0",
33-
"jscs": "^3.0.0",
34-
"jscs-jsdoc": "^2.0.0",
3532
"remark-cli": "^1.0.0",
3633
"remark-comment-config": "^4.0.0",
3734
"remark-github": "^5.0.0",
3835
"remark-lint": "^4.0.0",
3936
"remark-slug": "^4.0.0",
4037
"remark-validate-links": "^4.0.0",
41-
"tape": "^4.4.0"
38+
"tape": "^4.4.0",
39+
"xo": "^0.17.1"
4240
},
4341
"scripts": {
4442
"build-md": "remark . --quiet --frail",
4543
"build-bundle": "browserify index.js --no-builtins -s mdastUtilToString > mdast-util-to-string.js",
4644
"build-mangle": "esmangle mdast-util-to-string.js > mdast-util-to-string.min.js",
4745
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
48-
"lint-api": "eslint .",
49-
"lint-style": "jscs --reporter inline .",
50-
"lint": "npm run lint-api && npm run lint-style",
46+
"lint": "xo",
5147
"test-api": "node test.js",
5248
"test-coverage": "istanbul cover test.js",
5349
"test": "npm run build && npm run lint && npm run test-coverage"
50+
},
51+
"xo": {
52+
"space": true,
53+
"ignore": [
54+
"mdast-util-to-string.js"
55+
]
5456
}
5557
}

readme.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,13 @@ globals module, [uncompressed and compressed][releases].
1717

1818
## Usage
1919

20-
Dependencies:
21-
2220
```js
2321
var remark = require('remark');
2422
var toString = require('mdast-util-to-string');
25-
```
2623

27-
Parse:
24+
var tree = remark().parse('Some *emphasis*, **importance**, and `code`.');
2825

29-
```js
30-
var ast = remark().parse('Some *emphasis*, **strongness**, and `code`.');
31-
```
32-
33-
Stringify:
34-
35-
```js
36-
toString(ast);
37-
// 'Some emphasis, strongness, and code.'
26+
console.log(toString(tree)); //=> 'Some emphasis, importance, and code.'
3827
```
3928

4029
## API

test.js

Lines changed: 54 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,69 @@
1-
/**
2-
* @author Titus Wormer
3-
* @copyright 2015 Titus Wormer
4-
* @license MIT
5-
* @module mdast:util:to-string
6-
* @fileoverview Test suite for `mdast-util-to-string`.
7-
*/
8-
91
'use strict';
102

11-
/* eslint-env node */
12-
13-
/*
14-
* Dependencies.
15-
*/
16-
173
var test = require('tape');
184
var toString = require('./index.js');
195

20-
/*
21-
* Tests.
22-
*/
23-
246
test('mdast-util-to-string', function (t) {
25-
t.throws(
26-
function () {
27-
toString();
28-
},
29-
'should fail without node'
30-
);
7+
t.throws(
8+
function () {
9+
toString();
10+
},
11+
'should fail without node'
12+
);
3113

32-
t.equal('foo', toString({
33-
'value': 'foo'
34-
}, 'should not fail on unrecognised nodes'));
14+
t.equal(
15+
toString({value: 'foo'}),
16+
'foo',
17+
'should not fail on unrecognised nodes'
18+
);
3519

36-
t.equal('foo', toString({
37-
'value': 'foo',
38-
'children': [
39-
{
40-
'value': 'foo'
41-
},
42-
{
43-
'alt': 'bar'
44-
},
45-
{
46-
'title': 'baz'
47-
}
48-
]
49-
}), 'should prefer `value` over all others');
20+
t.equal(
21+
toString({
22+
value: 'foo',
23+
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
24+
}),
25+
'foo',
26+
'should prefer `value` over all others'
27+
);
5028

51-
t.equal('foo', toString({
52-
'value': 'foo',
53-
'alt': 'bar',
54-
'title': 'baz'
55-
}), 'should prefer `value` over `alt` or `title`');
29+
t.equal(
30+
toString({
31+
value: 'foo',
32+
alt: 'bar',
33+
title: 'baz'
34+
}),
35+
'foo',
36+
'should prefer `value` over `alt` or `title`'
37+
);
5638

57-
t.equal('bar', toString({
58-
'alt': 'bar',
59-
'title': 'baz'
60-
}), 'should prefer `alt` over `title`');
39+
t.equal(
40+
toString({alt: 'bar', title: 'baz'}),
41+
'bar',
42+
'should prefer `alt` over `title`'
43+
);
6144

62-
t.equal('baz', toString({
63-
'title': 'baz',
64-
'children': [
65-
{
66-
'value': 'foo'
67-
},
68-
{
69-
'alt': 'bar'
70-
},
71-
{
72-
'title': 'baz'
73-
}
74-
]
75-
}), 'should use `title` over `children`');
45+
t.equal(
46+
toString({
47+
title: 'baz',
48+
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
49+
}),
50+
'baz',
51+
'should use `title` over `children`'
52+
);
7653

77-
t.equal('foobarbaz', toString({
78-
'children': [
79-
{
80-
'value': 'foo'
81-
},
82-
{
83-
'alt': 'bar'
84-
},
85-
{
86-
'title': 'baz'
87-
}
88-
]
89-
}), 'should prefer `children`');
54+
t.equal(
55+
toString({
56+
children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]
57+
}),
58+
'foobarbaz',
59+
'should prefer `children`'
60+
);
9061

91-
t.equal('', toString({}), 'should fall back on an empty string');
62+
t.equal(
63+
toString({}),
64+
'',
65+
'should fall back on an empty string'
66+
);
9267

93-
t.end();
68+
t.end();
9469
});

0 commit comments

Comments
 (0)