@tiledesk/tiledesk-server
Version:
The Tiledesk server module
50 lines (42 loc) • 1.25 kB
JavaScript
// unused
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
// If you don't care about the order of the elements inside
// the array, you should sort both arrays here.
// Please note that calling sort on an array will modify that array.
// you might want to clone your array first.
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
/**
* Parse a field as an array of strings.
* Returns the array if valid, otherwise undefined.
*/
function parseStringArrayField(field) {
if (!field) return undefined;
let arr;
if (typeof field === 'string') {
try {
arr = JSON.parse(field); // prova a parsare come JSON
} catch {
return undefined; // se malformato, skippa
}
} else if (Array.isArray(field)) {
arr = field;
} else {
return undefined; // non stringa né array
}
// controlla che tutti gli elementi siano stringhe
if (Array.isArray(arr) && arr.every(e => typeof e === 'string')) {
return arr;
}
return undefined; // non è un array di stringhe
}
module.exports = {
arraysEqual,
parseStringArrayField
};