Skip to content

Commit 0b41089

Browse files
committed
add test case && fix test fail
1 parent 5e897cc commit 0b41089

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

dist/index.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ const sectionExpr = /^\[(.*)\]/,
22
commentExpr = /[;#](?: )?(.+)/,
33
lineExpr = /(^\s*[;#])|(^\[[^\]]*\])|(^.+$)/,
44
quotedExpr = /^(\s*['"]).+$/,
5-
lineBreak = typeof process !== 'undefined' &&
6-
process.platform === 'win32' ? '\r\n' : '\n',
75
lineTypes = {
86
blank: 0,
97
comment: 1,
@@ -240,13 +238,14 @@ class Ini {
240238
}, new Ini());
241239
}
242240

243-
constructor(text = '') {
241+
constructor(text = '', lineBreak) {
244242
if (typeof text !== 'string')
245243
throw new Error('Input must be a string.');
244+
this.lineBreak = lineBreak || this.determineLineBreak(text)
246245
this.sections = [];
247246
let currentSection = this.globals = new IniSection();
248247
if (text.length === 0) return;
249-
text.split(lineBreak).forEach(line => {
248+
text.split(this.lineBreak).forEach(line => {
250249
if (isSectionLine(line)) {
251250
currentSection = new IniSection(line);
252251
this.sections.push(currentSection);
@@ -256,6 +255,26 @@ class Ini {
256255
});
257256
}
258257

258+
determineLineBreak(text) {
259+
if(text === '') {
260+
return typeof process !== 'undefined' &&
261+
process.platform === 'win32' ? '\r\n' : '\n'
262+
} else {
263+
let lineBreak
264+
if(['\r\n', '\n'].some((t) => {
265+
if (text.split(t).length > 1) {
266+
lineBreak = t
267+
return true
268+
}
269+
})) {
270+
return lineBreak
271+
} else {
272+
return typeof process !== 'undefined' &&
273+
process.platform === 'win32' ? '\r\n' : '\n'
274+
}
275+
}
276+
}
277+
259278
getSection(name) {
260279
return this.sections.find(section => section.name === name);
261280
}
@@ -287,12 +306,12 @@ class Ini {
287306
(!options.removeCommentLines || !isCommentLine(line));
288307
}).map(line => line.text);
289308
if (!lines.length) return;
290-
str += lines.join(lineBreak);
309+
str += lines.join(this.lineBreak);
291310
if (index === sections.length - 1) return;
292311
let lastLine = lines[lines.length - 1];
293312
if (options.blankLineBeforeSection && !!lastLine.trim())
294-
str += lineBreak;
295-
str += lineBreak;
313+
str += this.lineBreak;
314+
str += this.lineBreak;
296315
});
297316
return str;
298317
}

src/ini.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Ini {
3030
}
3131

3232
constructor(text = '', lineBreak) {
33-
this.lineBreak = lineBreak || this.determineLineBreak(text)
3433
if (typeof text !== 'string')
3534
throw new Error('Input must be a string.');
35+
this.lineBreak = lineBreak || this.determineLineBreak(text)
3636
this.sections = [];
3737
let currentSection = this.globals = new IniSection();
3838
if (text.length === 0) return;

test/ini.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,22 @@ describe('Ini', function() {
173173
});
174174
});
175175
});
176+
177+
describe('linebreak', function() {
178+
it('should accept linebreak', () => {
179+
const newIni = new Ini('[x]\r\nb=3', '\r\n')
180+
expect(newIni.sections[0].lines.length).toBe(2)
181+
182+
const newIni2 = new Ini('[x]\nb=3', '\r\n')
183+
expect(newIni2.sections[0].lines.length).toBe(1)
184+
})
185+
186+
it('should determind linebreak by text', () => {
187+
const newIni = new Ini('[x]\r\nb=3')
188+
expect(newIni.sections[0].lines.length).toBe(2)
189+
190+
const newIni2 = new Ini('[x]\nb=3')
191+
expect(newIni2.sections[0].lines.length).toBe(2)
192+
})
193+
})
176194
});

0 commit comments

Comments
 (0)