diff --git a/lib/ini.js b/lib/ini.js index beb390d..457bbe5 100644 --- a/lib/ini.js +++ b/lib/ini.js @@ -110,7 +110,8 @@ const decode = (str, opt = {}) => { let section = null // section |key = value const re = /^\[([^\]]*)\]\s*$|^([^=]+)(=(.*))?$/i - const lines = str.split(/[\r\n]+/g) + // skip a UTF-8 byte order mark, so a section header on the first line is found + const lines = str.replace(/^\uFEFF/, '').split(/[\r\n]+/g) const duplicates = {} for (const line of lines) { diff --git a/test/bom.js b/test/bom.js new file mode 100644 index 0000000..9965084 --- /dev/null +++ b/test/bom.js @@ -0,0 +1,17 @@ +const t = require('tap') +const ini = require('..') + +// files saved as "UTF-8 with BOM" (e.g. by Notepad) start with U+FEFF +t.same(ini.decode('[section]\nkey=value\n'), { + section: { key: 'value' }, +}) + +t.same(ini.decode('[a]\r\nx=1\r\n[b]\r\ny=2\r\n'), { + a: { x: '1' }, + b: { y: '2' }, +}) + +t.same(ini.decode('key=value\n[section]\nother=1\n'), { + key: 'value', + section: { other: '1' }, +})