s4s-editor / local-scratch-vm /src /util /json-block-utilities.js
soiz1's picture
Upload 811 files
30c32c8 verified
raw
history blame
2.66 kB
module.exports = {
/**
* Parse a json into an object. if the json is invalid return an empty object
* @param {string} json the json to parse.
*/
validateJSON: json => {
let valid = false;
let object = {};
try {
if (!json.startsWith('{')) throw new Error('error lol');
object = JSON.parse(json);
valid = true;
} catch {}
return {
object: object,
json: json,
isValid: valid
};
},
/**
* Parse a json array into an array object. if the json is invalid return an empty array
* @param {string} array the array to parse.
*/
validateArray: array => {
let valid = false;
let allay = [];
try {
if (!array.startsWith('[')) throw new Error('error lol');
allay = JSON.parse(array);
valid = true;
} catch {}
return {
array: allay,
json: array,
isValid: valid
};
},
/**
* Convert a string to a value that is equal to it when made into a string
* @param {string} value the the string to convert.
*/
stringToEqivalint: value => {
// is the value a valid json? if so convert to one else do nothing
try {
if (!(value.startsWith('{') || value.startsWith('['))) throw new Error('not actualy a json!!!!!!!!!!');
value = JSON.parse(value);
} catch {
// well its not a json so what is it?
if (String(Number(value)) === value) {
value = Number(value);
} else if (value.toLowerCase() === 'true') {
value = true;
} else if (value.toLowerCase() === 'false') {
value = false;
} else if (value === 'undefined') {
value = undefined;
} else if (value === 'null') {
value = null;
}
}
return value;
},
/**
* Convert a value to a string (pretty much entirly pointless)
* @param {any} value the value to convert.
*/
valueToString: value => {
if (typeof value === 'object') {
value = JSON.stringify(value);
} else {
value = String(value);
}
return value;
},
/**
* Check if a regex is valid or not
* @param {any} value the value to convert.
*/
validateRegex: (value, regrule) => {
let valid = false;
try {
new RegExp(value, regrule);
valid = true;
} catch {}
return valid;
}
};