From 090f64e92a7ff722adf723dc13e7b468e591f057 Mon Sep 17 00:00:00 2001 From: Kenan Soylu Date: Mon, 15 May 2023 16:04:10 +0200 Subject: [PATCH] fix: check process on browser (#208) --- lib/ini.js | 7 ++++--- tap-snapshots/test/foo.js.test.cjs | 10 ++++++++++ test/foo.js | 10 ++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/ini.js b/lib/ini.js index 763c829..724d69d 100644 --- a/lib/ini.js +++ b/lib/ini.js @@ -8,8 +8,9 @@ const encode = (obj, opt = {}) => { opt.newline = opt.newline === true opt.sort = opt.sort === true opt.whitespace = opt.whitespace === true || opt.align === true + // The `typeof` check is required because accessing the `process` directly fails on browsers. /* istanbul ignore next */ - opt.platform = opt.platform || process?.platform + opt.platform = opt.platform || (typeof process !== 'undefined' && process.platform) opt.bracketedArray = opt.bracketedArray !== false /* istanbul ignore next */ @@ -172,8 +173,8 @@ const decode = (str, opt = {}) => { const remove = [] for (const k of Object.keys(out)) { if (!hasOwnProperty.call(out, k) || - typeof out[k] !== 'object' || - Array.isArray(out[k])) { + typeof out[k] !== 'object' || + Array.isArray(out[k])) { continue } diff --git a/tap-snapshots/test/foo.js.test.cjs b/tap-snapshots/test/foo.js.test.cjs index 26b81ce..2646323 100644 --- a/tap-snapshots/test/foo.js.test.cjs +++ b/tap-snapshots/test/foo.js.test.cjs @@ -292,3 +292,13 @@ label = debug value = 10 ` + +exports[`test/foo.js TAP encode within browser context > must match snapshot 1`] = ` +[log] +type=file + +[log.level] +label=debug +value=10 + +` diff --git a/test/foo.js b/test/foo.js index 4d380e3..fe92435 100644 --- a/test/foo.js +++ b/test/foo.js @@ -84,3 +84,13 @@ test('encode with align and sort', function (t) { t.matchSnapshot(e) t.end() }) + +test('encode within browser context', function (t) { + Object.defineProperty(process, 'platform', { value: undefined }) + + const obj = { log: { type: 'file', level: { label: 'debug', value: 10 } } } + const e = i.encode(obj) + + t.matchSnapshot(e) + t.end() +})