From db70b7fcc57e6a326ea16320a6371f60e85f9c89 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Wed, 1 Jul 2026 14:29:41 +0200 Subject: [PATCH] fix: strip leading UTF-8 BOM Fixes #28 --- src/index.js | 5 +++++ test/index.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/index.js b/src/index.js index e2b793e..e5c24fc 100755 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,11 @@ export function parse(text, ...args) { const reviver = args.length > 1 || !firstOptions ? args[0] : undefined; const options = (args.length > 1 && args[1]) || firstOptions || {}; + // Strip a leading UTF-8 BOM + if (typeof text === 'string' && text.charCodeAt(0) === 0xfeff) { + text = text.slice(1); + } + // Parse normally, allowing exceptions const obj = JSON.parse(text, reviver); diff --git a/test/index.js b/test/index.js index 19dc0d3..411004e 100755 --- a/test/index.js +++ b/test/index.js @@ -91,6 +91,10 @@ describe('Bourne', () => { ); }); + it('ignores leading BOM', () => { + expect(Bourne.parse('\uFEFF{"a": 5, "b": 6}')).toEqual({ a: 5, b: 6 }); + }); + it('errors on proto property (unicode)', () => { expect(() => Bourne.parse('{ "a": 5, "b": 6, "\\u005f_proto__": { "x": 7 } }')).toThrow(SyntaxError); expect(() => Bourne.parse('{ "a": 5, "b": 6, "_\\u005fp\\u0072oto__": { "x": 7 } }')).toThrow(SyntaxError);