Skip to content

Commit 052a99b

Browse files
Merge pull request #1 from githoniel/master
determind linebreak by text and system
2 parents 0d672ff + 949f7d5 commit 052a99b

File tree

6 files changed

+1809
-1712
lines changed

6 files changed

+1809
-1712
lines changed

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
}

index.js

Lines changed: 0 additions & 2 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,

0 commit comments

Comments
 (0)