Spaces:
Running
Running
File size: 2,664 Bytes
30c32c8 |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
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;
}
};
|