Skip to content

Commit eb3ebc6

Browse files
committed
Refactor code-style
1 parent 84188b1 commit eb3ebc6

File tree

5 files changed

+207
-192
lines changed

5 files changed

+207
-192
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
plain-text-data-to-json.js
3+
plain-text-data-to-json.min.js

index.js

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
1-
'use strict';
1+
'use strict'
22

3-
var trim = require('trim');
3+
var trim = require('trim')
44

5-
module.exports = toJSON;
5+
module.exports = toJSON
66

7-
var own = {}.hasOwnProperty;
7+
var own = {}.hasOwnProperty
88

99
/* Transform a string into an array or object of values. */
1010
function toJSON(value, options) {
11-
var propertyOrValues = {};
12-
var lines;
13-
var isPropertyValuePair;
14-
var pairs;
15-
var values;
11+
var propertyOrValues = {}
12+
var lines
13+
var isPropertyValuePair
14+
var pairs
15+
var values
1616

1717
if (!options) {
18-
options = {};
18+
options = {}
1919
}
2020

2121
if (options.log === null || options.log === undefined) {
22-
options.log = true;
22+
options.log = true
2323
}
2424

2525
if (options.comment === null || options.comment === undefined) {
26-
options.comment = '%';
26+
options.comment = '%'
2727
}
2828

29-
lines = value.split('\n');
29+
lines = value.split('\n')
3030

3131
if (options.comment) {
32-
lines = lines.map(stripComments(options.comment));
32+
lines = lines.map(stripComments(options.comment))
3333
}
3434

35-
lines = lines.map(trim).filter(Boolean);
35+
lines = lines.map(trim).filter(Boolean)
3636

37-
pairs = lines.map(toPropertyValuePairs(options.delimiter || ':'));
37+
pairs = lines.map(toPropertyValuePairs(options.delimiter || ':'))
3838

39-
pairs.forEach(function (line, index) {
40-
var currentLineIsPropertyValuePair;
39+
pairs.forEach(function(line, index) {
40+
var currentLineIsPropertyValuePair
4141

42-
currentLineIsPropertyValuePair = line.length === 2;
42+
currentLineIsPropertyValuePair = line.length === 2
4343

4444
if (index === 0) {
45-
isPropertyValuePair = currentLineIsPropertyValuePair;
45+
isPropertyValuePair = currentLineIsPropertyValuePair
4646
} else if (currentLineIsPropertyValuePair !== isPropertyValuePair) {
4747
throw new Error(
48-
'Error at `' + line + '`: ' +
49-
'Both property-value pairs and array values found. ' +
50-
'Make sure either exists.'
51-
);
48+
'Error at `' +
49+
line +
50+
'`: ' +
51+
'Both property-value pairs and array values found. ' +
52+
'Make sure either exists.'
53+
)
5254
}
5355

5456
if (own.call(propertyOrValues, line[0])) {
5557
if (
5658
!options.forgiving ||
57-
(
58-
options.forgiving === true &&
59+
(options.forgiving === true &&
5960
currentLineIsPropertyValuePair &&
60-
line[1] !== propertyOrValues[line[0]]
61-
)
61+
line[1] !== propertyOrValues[line[0]])
6262
) {
6363
throw new Error(
64-
'Error at `' + line + '`: ' +
65-
'Duplicate data found. ' +
66-
'Make sure, in objects, no duplicate properties exist;' +
67-
'in arrays, no duplicate values.'
68-
);
64+
'Error at `' +
65+
line +
66+
'`: ' +
67+
'Duplicate data found. ' +
68+
'Make sure, in objects, no duplicate properties exist;' +
69+
'in arrays, no duplicate values.'
70+
)
6971
}
7072

7173
if (options.log) {
@@ -74,75 +76,79 @@ function toJSON(value, options) {
7476
propertyOrValues[line[0]] !== line[1]
7577
) {
7678
console.log(
77-
'Overwriting `' + propertyOrValues[line[0]] + '` ' +
78-
'to `' + line[1] + '` for `' + line[0] + '`'
79-
);
79+
'Overwriting `' +
80+
propertyOrValues[line[0]] +
81+
'` ' +
82+
'to `' +
83+
line[1] +
84+
'` for `' +
85+
line[0] +
86+
'`'
87+
)
8088
} else {
81-
console.log(
82-
'Ignoring duplicate key for `' + line[0] + '`'
83-
);
89+
console.log('Ignoring duplicate key for `' + line[0] + '`')
8490
}
8591
}
8692
}
8793

88-
propertyOrValues[line[0]] = line[1];
89-
});
94+
propertyOrValues[line[0]] = line[1]
95+
})
9096

9197
if (isPropertyValuePair) {
92-
pairs.sort(sortOnFirstIndex);
93-
values = propertyValuePairsToObject(pairs);
98+
pairs.sort(sortOnFirstIndex)
99+
values = propertyValuePairsToObject(pairs)
94100
} else {
95-
lines.sort();
101+
lines.sort()
96102
}
97103

98-
return values || lines;
104+
return values || lines
99105
}
100106

101107
/* Transform a list of property--value tuples to an object. */
102108
function propertyValuePairsToObject(pairs) {
103-
var values = {};
109+
var values = {}
104110

105-
pairs.forEach(function (pair) {
106-
values[pair[0]] = pair[1];
107-
});
111+
pairs.forEach(function(pair) {
112+
values[pair[0]] = pair[1]
113+
})
108114

109-
return values;
115+
return values
110116
}
111117

112118
/* Sort on the first (`0`) index. */
113119
function sortOnFirstIndex(a, b) {
114-
return a[0].charCodeAt(0) - b[0].charCodeAt(0);
120+
return a[0].charCodeAt(0) - b[0].charCodeAt(0)
115121
}
116122

117123
/* Factory to transform lines to property--value tuples. */
118124
function toPropertyValuePairs(token) {
119-
return toPropValuePairs;
125+
return toPropValuePairs
120126

121127
/* Transform `value` to a property--value tuple. */
122128
function toPropValuePairs(value) {
123-
var values = value.split(token);
124-
var result = [trim(values.shift())];
129+
var values = value.split(token)
130+
var result = [trim(values.shift())]
125131

126132
if (values.length !== 0) {
127-
result.push(trim(values.join(token)));
133+
result.push(trim(values.join(token)))
128134
}
129135

130-
return result;
136+
return result
131137
}
132138
}
133139

134140
/* Strip comments factory. */
135141
function stripComments(token) {
136-
return strip;
142+
return strip
137143

138144
/* Strip comments. */
139145
function strip(value) {
140-
var index = value.indexOf(token);
146+
var index = value.indexOf(token)
141147

142148
if (index !== -1) {
143-
value = value.substr(0, index);
149+
value = value.substr(0, index)
144150
}
145151

146-
return value;
152+
return value
147153
}
148154
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,31 @@
2525
"cept": "^1.0.1",
2626
"esmangle": "^1.0.1",
2727
"nyc": "^11.0.0",
28+
"prettier": "^1.12.1",
2829
"remark-cli": "^5.0.0",
2930
"remark-preset-wooorm": "^4.0.0",
3031
"tape": "^4.0.0",
3132
"xo": "^0.20.0"
3233
},
3334
"scripts": {
34-
"build-md": "remark . -qfo",
35+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3536
"build-bundle": "browserify index.js --bare -s plain-text-data-to-json > plain-text-data-to-json.js",
3637
"build-mangle": "esmangle plain-text-data-to-json.js > plain-text-data-to-json.min.js",
37-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
38-
"lint": "xo",
38+
"build": "npm run build-bundle && npm run build-mangle",
3939
"test-api": "node test",
4040
"test-coverage": "nyc --reporter lcov tape test.js",
41-
"test": "npm run build && npm run lint && npm run test-coverage"
41+
"test": "npm run format && npm run build && npm run test-coverage"
42+
},
43+
"prettier": {
44+
"tabWidth": 2,
45+
"useTabs": false,
46+
"singleQuote": true,
47+
"bracketSpacing": false,
48+
"semi": false,
49+
"trailingComma": "none"
4250
},
4351
"xo": {
44-
"space": true,
52+
"prettier": true,
4553
"esnext": false,
4654
"rules": {
4755
"no-var": "off",

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ npm install plain-text-data-to-json
1414
## Usage
1515

1616
```js
17-
var fs = require('fs');
18-
var toJSON = require('plain-text-data-to-json');
17+
var fs = require('fs')
18+
var toJSON = require('plain-text-data-to-json')
1919

20-
var doc = fs.readFileSync('input.txt', 'utf8');
20+
var doc = fs.readFileSync('input.txt', 'utf8')
2121

22-
var data = toJSON(doc);
22+
var data = toJSON(doc)
2323

24-
fs.writeFileSync('output.json', JSON.stringify(data, null, 2) + '\n');
24+
fs.writeFileSync('output.json', JSON.stringify(data, null, 2) + '\n')
2525
```
2626

2727
## API

0 commit comments

Comments
 (0)