Skip to content

Commit 090f64e

Browse files
authored
fix: check process on browser (#208)
1 parent d7ee635 commit 090f64e

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/ini.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const encode = (obj, opt = {}) => {
88
opt.newline = opt.newline === true
99
opt.sort = opt.sort === true
1010
opt.whitespace = opt.whitespace === true || opt.align === true
11+
// The `typeof` check is required because accessing the `process` directly fails on browsers.
1112
/* istanbul ignore next */
12-
opt.platform = opt.platform || process?.platform
13+
opt.platform = opt.platform || (typeof process !== 'undefined' && process.platform)
1314
opt.bracketedArray = opt.bracketedArray !== false
1415

1516
/* istanbul ignore next */
@@ -172,8 +173,8 @@ const decode = (str, opt = {}) => {
172173
const remove = []
173174
for (const k of Object.keys(out)) {
174175
if (!hasOwnProperty.call(out, k) ||
175-
typeof out[k] !== 'object' ||
176-
Array.isArray(out[k])) {
176+
typeof out[k] !== 'object' ||
177+
Array.isArray(out[k])) {
177178
continue
178179
}
179180

tap-snapshots/test/foo.js.test.cjs

+10
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,13 @@ label = debug
292292
value = 10
293293
294294
`
295+
296+
exports[`test/foo.js TAP encode within browser context > must match snapshot 1`] = `
297+
[log]
298+
type=file
299+
300+
[log.level]
301+
label=debug
302+
value=10
303+
304+
`

test/foo.js

+10
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ test('encode with align and sort', function (t) {
8484
t.matchSnapshot(e)
8585
t.end()
8686
})
87+
88+
test('encode within browser context', function (t) {
89+
Object.defineProperty(process, 'platform', { value: undefined })
90+
91+
const obj = { log: { type: 'file', level: { label: 'debug', value: 10 } } }
92+
const e = i.encode(obj)
93+
94+
t.matchSnapshot(e)
95+
t.end()
96+
})

0 commit comments

Comments
 (0)