1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/ini/ini.js

207 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
exports.parse = exports.decode = decode
exports.stringify = exports.encode = encode
exports.safe = safe
exports.unsafe = unsafe
var eol = typeof process !== 'undefined' &&
process.platform === 'win32' ? '\r\n' : '\n'
function encode (obj, opt) {
var children = []
var out = ''
if (typeof opt === 'string') {
opt = {
section: opt,
2024-01-16 21:26:16 +08:00
whitespace: false,
2023-12-18 13:12:25 +08:00
}
} else {
opt = opt || {}
opt.whitespace = opt.whitespace === true
}
var separator = opt.whitespace ? ' = ' : '='
Object.keys(obj).forEach(function (k, _, __) {
var val = obj[k]
if (val && Array.isArray(val)) {
val.forEach(function (item) {
out += safe(k + '[]') + separator + safe(item) + '\n'
})
2024-01-16 21:26:16 +08:00
} else if (val && typeof val === 'object')
2023-12-18 13:12:25 +08:00
children.push(k)
2024-01-16 21:26:16 +08:00
else
2023-12-18 13:12:25 +08:00
out += safe(k) + separator + safe(val) + eol
})
2024-01-16 21:26:16 +08:00
if (opt.section && out.length)
2023-12-18 13:12:25 +08:00
out = '[' + safe(opt.section) + ']' + eol + out
children.forEach(function (k, _, __) {
var nk = dotSplit(k).join('\\.')
var section = (opt.section ? opt.section + '.' : '') + nk
var child = encode(obj[k], {
section: section,
2024-01-16 21:26:16 +08:00
whitespace: opt.whitespace,
2023-12-18 13:12:25 +08:00
})
2024-01-16 21:26:16 +08:00
if (out.length && child.length)
2023-12-18 13:12:25 +08:00
out += eol
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
out += child
})
return out
}
function dotSplit (str) {
return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
.replace(/\\\./g, '\u0001')
.split(/\./).map(function (part) {
return part.replace(/\1/g, '\\.')
2024-01-16 21:26:16 +08:00
.replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
2023-12-18 13:12:25 +08:00
})
}
function decode (str) {
var out = {}
var p = out
var section = null
// section |key = value
var re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
var lines = str.split(/[\r\n]+/g)
lines.forEach(function (line, _, __) {
2024-01-16 21:26:16 +08:00
if (!line || line.match(/^\s*[;#]/))
return
2023-12-18 13:12:25 +08:00
var match = line.match(re)
2024-01-16 21:26:16 +08:00
if (!match)
return
2023-12-18 13:12:25 +08:00
if (match[1] !== undefined) {
section = unsafe(match[1])
2024-01-16 21:26:16 +08:00
if (section === '__proto__') {
// not allowed
// keep parsing the section, but don't attach it.
p = {}
return
}
2023-12-18 13:12:25 +08:00
p = out[section] = out[section] || {}
return
}
var key = unsafe(match[2])
2024-01-16 21:26:16 +08:00
if (key === '__proto__')
return
2023-12-18 13:12:25 +08:00
var value = match[3] ? unsafe(match[4]) : true
switch (value) {
case 'true':
case 'false':
case 'null': value = JSON.parse(value)
}
// Convert keys with '[]' suffix to an array
if (key.length > 2 && key.slice(-2) === '[]') {
key = key.substring(0, key.length - 2)
2024-01-16 21:26:16 +08:00
if (key === '__proto__')
return
if (!p[key])
2023-12-18 13:12:25 +08:00
p[key] = []
2024-01-16 21:26:16 +08:00
else if (!Array.isArray(p[key]))
2023-12-18 13:12:25 +08:00
p[key] = [p[key]]
}
// safeguard against resetting a previously defined
// array by accidentally forgetting the brackets
2024-01-16 21:26:16 +08:00
if (Array.isArray(p[key]))
2023-12-18 13:12:25 +08:00
p[key].push(value)
2024-01-16 21:26:16 +08:00
else
2023-12-18 13:12:25 +08:00
p[key] = value
})
// {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
// use a filter to return the keys that have to be deleted.
Object.keys(out).filter(function (k, _, __) {
if (!out[k] ||
typeof out[k] !== 'object' ||
2024-01-16 21:26:16 +08:00
Array.isArray(out[k]))
2023-12-18 13:12:25 +08:00
return false
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
// see if the parent section is also an object.
// if so, add it to that, and mark this one for deletion
var parts = dotSplit(k)
var p = out
var l = parts.pop()
var nl = l.replace(/\\\./g, '.')
parts.forEach(function (part, _, __) {
2024-01-16 21:26:16 +08:00
if (part === '__proto__')
return
if (!p[part] || typeof p[part] !== 'object')
p[part] = {}
2023-12-18 13:12:25 +08:00
p = p[part]
})
2024-01-16 21:26:16 +08:00
if (p === out && nl === l)
2023-12-18 13:12:25 +08:00
return false
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
p[nl] = out[k]
return true
}).forEach(function (del, _, __) {
delete out[del]
})
return out
}
function isQuoted (val) {
return (val.charAt(0) === '"' && val.slice(-1) === '"') ||
(val.charAt(0) === "'" && val.slice(-1) === "'")
}
function safe (val) {
return (typeof val !== 'string' ||
val.match(/[=\r\n]/) ||
val.match(/^\[/) ||
(val.length > 1 &&
isQuoted(val)) ||
val !== val.trim())
2024-01-16 21:26:16 +08:00
? JSON.stringify(val)
: val.replace(/;/g, '\\;').replace(/#/g, '\\#')
2023-12-18 13:12:25 +08:00
}
function unsafe (val, doUnesc) {
val = (val || '').trim()
if (isQuoted(val)) {
// remove the single quotes before calling JSON.parse
2024-01-16 21:26:16 +08:00
if (val.charAt(0) === "'")
2023-12-18 13:12:25 +08:00
val = val.substr(1, val.length - 2)
2024-01-16 21:26:16 +08:00
try {
val = JSON.parse(val)
} catch (_) {}
2023-12-18 13:12:25 +08:00
} else {
// walk the val to find the first not-escaped ; character
var esc = false
var unesc = ''
for (var i = 0, l = val.length; i < l; i++) {
var c = val.charAt(i)
if (esc) {
2024-01-16 21:26:16 +08:00
if ('\\;#'.indexOf(c) !== -1)
2023-12-18 13:12:25 +08:00
unesc += c
2024-01-16 21:26:16 +08:00
else
2023-12-18 13:12:25 +08:00
unesc += '\\' + c
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
esc = false
2024-01-16 21:26:16 +08:00
} else if (';#'.indexOf(c) !== -1)
2023-12-18 13:12:25 +08:00
break
2024-01-16 21:26:16 +08:00
else if (c === '\\')
2023-12-18 13:12:25 +08:00
esc = true
2024-01-16 21:26:16 +08:00
else
2023-12-18 13:12:25 +08:00
unesc += c
}
2024-01-16 21:26:16 +08:00
if (esc)
2023-12-18 13:12:25 +08:00
unesc += '\\'
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
return unesc.trim()
}
return val
}