add convert json to toml by toml.js

This commit is contained in:
2023-12-29 19:31:55 +08:00
parent 91c15936ee
commit 83ad03eca3
7 changed files with 8590 additions and 171 deletions

View File

@@ -0,0 +1,183 @@
(function (){
//param names in Basic tab
var basicParams = [
{
name: 'name',
defaultValue: '-'
}, {
name: 'type',
defaultValue: '-'
}, {
name: 'localIP',
defaultValue: '-'
}, {
name: 'localPort',
defaultValue: '-'
}, {
name: 'customDomains',
defaultValue: '-'
}, {
name: 'subdomain',
defaultValue: '-'
}, {
name: 'remotePort',
defaultValue: '-'
}, {
name: 'transport.useEncryption',
defaultValue: 'true',
}, {
name: 'transport.useCompression',
defaultValue: 'true',
}
];
var mapParams = [{
name: 'plugin.ClientPluginOptions',
map: 'plugin'
}];
var paramTypes = {
number: [
'healthCheck.timeoutSeconds',
'healthCheck.maxFailed',
'healthCheck.intervalSeconds',
'localPort',
],
boolean: [
'transport.useEncryption',
'transport.useCompression'
],
array: [
'customDomains',
'locations',
'allowUsers'
],
map: [
'metadatas',
'requestHeaders.set',
'plugin.ClientPluginOptions.requestHeaders.set'
]
}
/**
* a.b.c = 1
* a.b.d = [2,3]
* to
* {a: {
* b: {
* c: 1
* d: "[2,3]"
* }
* }
* }
*
* @param obj json object
* @param keys all keys split from key string like 'a.b.c'
* @param value default value
* @param stringifyArray if true, when value is an array, it will stringify by JSON.stringify(value)
*/
function expandJSONKeys(obj, keys, value, stringifyArray) {
stringifyArray = stringifyArray == null ? true : stringifyArray;
var currentIndex = this.index || 0;
var childrenIndex = (currentIndex + 1) > keys.length ? null : (currentIndex + 1);
var currentKey = keys[currentIndex], currentValue = {};
var childrenKey = childrenIndex == null ? null : keys[childrenIndex];
if (obj.hasOwnProperty(currentKey)) {
currentValue = obj[currentKey];
} else {
obj[currentKey] = currentValue;
}
if (childrenKey != null) {
this.index = childrenIndex;
expandJSONKeys(currentValue, keys, value, stringifyArray);
} else {
if (value != null) {
if (Array.isArray(value) && stringifyArray) {
obj[currentKey] = JSON.stringify(value);
} else {
obj[currentKey] = value;
}
}
this.index = 0;
}
}
function expandJSON(obj) {
var newObj = {};
for (var name in obj) {
var value = obj[name];
if (value === '') {
continue;
}
if (paramTypes.number.indexOf(name) !== -1) {
value = parseInt(value) || 0;
} else if (paramTypes.boolean.indexOf(name) !== -1) {
value = Boolean(value) || false;
} else if (paramTypes.array.indexOf(name) !== -1) {
value = eval('(' + value + ')') || [];
} else {
for (var i = 0; i < paramTypes.map.length; i++) {
var key = paramTypes.map[i];
if (name.startsWith(key)) {
var json = {};
json[name.substring(key.length + 1, name.length)] = value;
value = json;
name = name.substring(0, key.length)
break;
}
}
}
expandJSONKeys(newObj, name.split("."), value, false);
}
return newObj;
}
/**
* {a: {
* b: {
* c: 1
* d: [2,3]
* }
* }
* }
* to
* {
* 'a.b.c': 1,
* 'a.b.d': '[2,3]'
* }
*
* @param obj json object
* @returns {*} flatted json key array
*/
function flatJSON(obj) {
var flat = function (obj, prentKey, flattedJSON) {
flattedJSON = flattedJSON || {};
prentKey = prentKey || '';
if (prentKey !== '')
prentKey = prentKey + '.';
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
flat(value, prentKey + key, flattedJSON);
} else {
for (var mapParam of mapParams) {
if (prentKey.startsWith(mapParam.name)) {
prentKey = mapParam.map + '.';
break;
}
}
if (Array.isArray(value)) {
flattedJSON[prentKey + key] = JSON.stringify(value);
} else {
flattedJSON[prentKey + key] = value;
}
}
}
return flattedJSON;
}
return flat(obj);
}
window.expandJSONKeys = expandJSONKeys;
window.expandJSON = expandJSON;
window.flatJSON = flatJSON;
})();