-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathutils.js
More file actions
35 lines (31 loc) · 950 Bytes
/
utils.js
File metadata and controls
35 lines (31 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Safely parses settings from either a JSON string or object
* @param {string|object} settings - The settings
* @returns {object} Parsed settings object
*/
function parseSettings(settings) {
// If settings is already an object, return it
if (typeof settings === 'object' && settings !== null) {
return settings
}
// If settings is a string, try to parse it
if (typeof settings === 'string') {
try {
return JSON.parse(settings)
} catch (error) {
console.warn('Invalid json format:', error.message)
return
}
}
console.warn('Cannot parse settings from:', settings)
return
}
module.exports = {
parseSettings
}