Skip to content

Commit 058e231

Browse files
committed
Refactor code-style
1 parent 1a7b9e8 commit 058e231

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

index.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var own = {}.hasOwnProperty
1+
const own = {}.hasOwnProperty
22

33
/**
4-
* @typedef {Object} ToJsonOptions
4+
* @typedef ToJsonOptions
55
* @property {boolean} [log=true]
66
* @property {string} [delimiter=':']
77
* @property {string[]|string|false} [comment='%']
@@ -15,27 +15,23 @@ var own = {}.hasOwnProperty
1515
* @param {ToJsonOptions} [options={}]
1616
*/
1717
export function toJson(value, options = {}) {
18-
var log =
18+
const log =
1919
options.log === null || options.log === undefined ? true : options.log
20-
var comment =
20+
const comment =
2121
options.comment === null || options.comment === undefined
2222
? '%'
2323
: options.comment
24-
var comments = comment ? (Array.isArray(comment) ? comment : [comment]) : []
25-
var delimiter = options.delimiter || ':'
26-
var forgiving = options.forgiving
27-
var propertyOrValues = {}
28-
/** @type {boolean} */
29-
var isPropertyValuePair
30-
/** @type {Array.<Array.<string>>} */
31-
var pairs
24+
const comments = comment ? (Array.isArray(comment) ? comment : [comment]) : []
25+
const delimiter = options.delimiter || ':'
26+
const forgiving = options.forgiving
27+
const propertyOrValues = {}
3228

33-
var lines = value
29+
const lines = value
3430
.split('\n')
3531
.map((line) => {
36-
var commentIndex = -1
32+
let commentIndex = -1
3733
/** @type {number} */
38-
var index
34+
let index
3935

4036
while (++commentIndex < comments.length) {
4137
index = line.indexOf(comments[commentIndex])
@@ -46,11 +42,11 @@ export function toJson(value, options = {}) {
4642
})
4743
.filter(Boolean)
4844

49-
pairs = lines.map(
45+
const pairs = lines.map(
5046
// Transform `value` to a property--value tuple.
5147
function (value) {
52-
var values = value.split(delimiter)
53-
var result = [values.shift().trim()]
48+
const values = value.split(delimiter)
49+
const result = [values.shift().trim()]
5450

5551
if (values.length > 0) {
5652
result.push(values.join(delimiter).trim())
@@ -60,8 +56,11 @@ export function toJson(value, options = {}) {
6056
}
6157
)
6258

63-
pairs.forEach(function (line, index) {
64-
var currentLineIsPropertyValuePair = line.length === 2
59+
/** @type {boolean} */
60+
let isPropertyValuePair
61+
62+
for (const [index, line] of pairs.entries()) {
63+
const currentLineIsPropertyValuePair = line.length === 2
6564

6665
if (index === 0) {
6766
isPropertyValuePair = currentLineIsPropertyValuePair
@@ -111,7 +110,7 @@ export function toJson(value, options = {}) {
111110
}
112111

113112
propertyOrValues[line[0]] = line[1]
114-
})
113+
}
115114

116115
if (isPropertyValuePair) {
117116
pairs.sort(sortOnFirstIndex)
@@ -123,8 +122,8 @@ export function toJson(value, options = {}) {
123122

124123
/**
125124
* Sort on the first (`0`) index.
126-
* @param {Array.<string>} a
127-
* @param {Array.<string>} b
125+
* @param {Array<string>} a
126+
* @param {Array<string>} b
128127
*/
129128
function sortOnFirstIndex(a, b) {
130129
return a[0].codePointAt(0) - b[0].codePointAt(0)

package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@
5656
"trailingComma": "none"
5757
},
5858
"xo": {
59-
"prettier": true,
60-
"rules": {
61-
"no-var": "off",
62-
"prefer-arrow-callback": "off",
63-
"complexity": "off",
64-
"unicorn/no-array-callback-reference": "off",
65-
"unicorn/no-array-for-each": "off"
66-
}
59+
"prettier": true
6760
},
6861
"remarkConfig": {
6962
"plugins": [

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Specify your own:
4747
###### `options.comment`
4848

4949
Character(s) to use for line-comments, `false` turns off comments (`string`,
50-
`Array.<string>`, or `boolean`, default: `'%'`)
50+
`Array<string>`, or `boolean`, default: `'%'`)
5151

5252
###### `options.delimiter`
5353

test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ test('Invalid lists', function (t) {
155155
)
156156

157157
t.test('should log duplicate values when `forgiving`', function (st) {
158-
var stop = cept(console, 'log', hoist)
159-
/** @type {Array.<unknown>} */
160-
var parameters = []
158+
const stop = cept(console, 'log', hoist)
159+
/** @type {Array<unknown>} */
160+
let parameters = []
161161

162162
toJson('unicorn\nrainbow\nunicorn', {forgiving: true})
163163

@@ -172,9 +172,9 @@ test('Invalid lists', function (t) {
172172
})
173173

174174
t.test('should honour `log: false`', function (st) {
175-
var stop = cept(console, 'log', hoist)
176-
/** @type {Array.<unknown>} */
177-
var parameters
175+
const stop = cept(console, 'log', hoist)
176+
/** @type {Array<unknown>} */
177+
let parameters
178178

179179
toJson('unicorn\nrainbow\nunicorn', {forgiving: true, log: false})
180180

@@ -209,9 +209,9 @@ test('Invalid objects', function (t) {
209209
)
210210

211211
t.test('should log duplicate values when `forgiving`', function (st) {
212-
var stop = cept(console, 'log', hoist)
213-
/** @type {Array.<unknown>} */
214-
var parameters = []
212+
const stop = cept(console, 'log', hoist)
213+
/** @type {Array<unknown>} */
214+
let parameters = []
215215

216216
toJson('doge: so scare\nunicorn: magic creature\ndoge: so scare\n', {
217217
forgiving: true
@@ -228,9 +228,9 @@ test('Invalid objects', function (t) {
228228
})
229229

230230
t.test('should honour `log: false`', function (st) {
231-
var stop = cept(console, 'log', hoist)
232-
/** @type {Array.<unknown>} */
233-
var parameters
231+
const stop = cept(console, 'log', hoist)
232+
/** @type {Array<unknown>} */
233+
let parameters
234234

235235
toJson('doge: so scare\nunicorn: magic creature\ndoge: so scare\n', {
236236
forgiving: true,
@@ -266,9 +266,9 @@ test('Invalid objects', function (t) {
266266
t.test(
267267
'should log for duplicate keys when `forgiving` is `"fix"',
268268
function (st) {
269-
var stop = cept(console, 'log', hoist)
270-
/** @type {Array.<unknown>} */
271-
var parameters = []
269+
const stop = cept(console, 'log', hoist)
270+
/** @type {Array<unknown>} */
271+
let parameters = []
272272

273273
toJson('doge: so scare\nunicorn: magic creature\ndoge: so scare\n', {
274274
forgiving: true

0 commit comments

Comments
 (0)