Skip to content

Commit cafda66

Browse files
rewrote line break determination code
1 parent 4f2df45 commit cafda66

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

src/ini.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const systemLineBreak = process && process.platform === 'win32' ? '\r\n' : '\n';
2+
13
class Ini {
24
static merge(...inis) {
35
let mergeLines = (section, newSection) => {
@@ -32,7 +34,7 @@ class Ini {
3234
constructor(text = '', lineBreak) {
3335
if (typeof text !== 'string')
3436
throw new Error('Input must be a string.');
35-
this.lineBreak = lineBreak || this.determineLineBreak(text)
37+
this.lineBreak = lineBreak || this.determineLineBreak(text);
3638
this.sections = [];
3739
let currentSection = this.globals = new IniSection();
3840
if (text.length === 0) return;
@@ -47,23 +49,9 @@ class Ini {
4749
}
4850

4951
determineLineBreak(text) {
50-
if(text === '') {
51-
return typeof process !== 'undefined' &&
52-
process.platform === 'win32' ? '\r\n' : '\n'
53-
} else {
54-
let lineBreak
55-
if(['\r\n', '\n'].some((t) => {
56-
if (text.split(t).length > 1) {
57-
lineBreak = t
58-
return true
59-
}
60-
})) {
61-
return lineBreak
62-
} else {
63-
return typeof process !== 'undefined' &&
64-
process.platform === 'win32' ? '\r\n' : '\n'
65-
}
66-
}
52+
return ['\r\n', '\n'].find(lineBreak => {
53+
return text.includes(lineBreak);
54+
}) || systemLineBreak;
6755
}
6856

6957
getSection(name) {

test/ini.test.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ describe('Ini', function() {
4444
expect(foo.stringify()).toBe(text);
4545
});
4646
});
47+
48+
describe('line break parameter', function() {
49+
it('should use parameter if passed', () => {
50+
let newIni = new Ini('[x]\r\nb=3', '\r\n');
51+
expect(newIni.sections[0].lines.length).toBe(2);
52+
53+
let newIni2 = new Ini('[x]\nb=3', '\r\n');
54+
expect(newIni2.sections[0].lines.length).toBe(1);
55+
});
56+
57+
describe('not passed', () => {
58+
it('should determine from text', () => {
59+
let newIni = new Ini('[x]\r\nb=3');
60+
expect(newIni.sections[0].lines.length).toBe(2);
61+
62+
let newIni2 = new Ini('[x]\nb=3');
63+
expect(newIni2.sections[0].lines.length).toBe(2);
64+
});
65+
66+
it('should determine by system if cannot determine by text', () => {
67+
let newIni = new Ini('[x]=3');
68+
let linebreak = process.platform === 'win32' ? '\r\n' : '\n';
69+
expect(newIni.lineBreak).toBe(linebreak);
70+
});
71+
});
72+
});
4773
});
4874

4975
describe('addSection', function() {
@@ -173,28 +199,4 @@ describe('Ini', function() {
173199
});
174200
});
175201
});
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-
194-
it('should determind linebreak by system when by text fail', () => {
195-
const newIni = new Ini('[x]=3')
196-
const linebreak = process.platform === 'win32' ? '\r\n' : '\n'
197-
expect(newIni.lineBreak).toBe(linebreak)
198-
})
199-
})
200-
});
202+
});

0 commit comments

Comments
 (0)