2021-08-24 14:15:56 +02:00
|
|
|
const Path = require('path')
|
2021-08-26 00:36:54 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const Logger = require('../Logger')
|
2021-11-09 03:05:12 +01:00
|
|
|
const { parseString } = require("xml2js")
|
2021-08-24 14:15:56 +02:00
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
const levenshteinDistance = (str1, str2, caseSensitive = false) => {
|
|
|
|
if (!caseSensitive) {
|
|
|
|
str1 = str1.toLowerCase()
|
|
|
|
str2 = str2.toLowerCase()
|
|
|
|
}
|
|
|
|
const track = Array(str2.length + 1).fill(null).map(() =>
|
|
|
|
Array(str1.length + 1).fill(null));
|
|
|
|
for (let i = 0; i <= str1.length; i += 1) {
|
|
|
|
track[0][i] = i;
|
|
|
|
}
|
|
|
|
for (let j = 0; j <= str2.length; j += 1) {
|
|
|
|
track[j][0] = j;
|
|
|
|
}
|
|
|
|
for (let j = 1; j <= str2.length; j += 1) {
|
|
|
|
for (let i = 1; i <= str1.length; i += 1) {
|
|
|
|
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
|
|
|
|
track[j][i] = Math.min(
|
|
|
|
track[j][i - 1] + 1, // deletion
|
|
|
|
track[j - 1][i] + 1, // insertion
|
|
|
|
track[j - 1][i - 1] + indicator, // substitution
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return track[str2.length][str1.length];
|
|
|
|
}
|
2021-08-22 15:52:37 +02:00
|
|
|
module.exports.levenshteinDistance = levenshteinDistance
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
module.exports.isObject = (val) => {
|
|
|
|
return val !== null && typeof val === 'object'
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
module.exports.comparePaths = (path1, path2) => {
|
|
|
|
return path1 === path2 || Path.normalize(path1) === Path.normalize(path2)
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
module.exports.getIno = (path) => {
|
|
|
|
return fs.promises.stat(path, { bigint: true }).then((data => String(data.ino))).catch((err) => {
|
2021-09-11 02:55:02 +02:00
|
|
|
Logger.error('[Utils] Failed to get ino for path', path, err)
|
2021-08-26 00:36:54 +02:00
|
|
|
return null
|
|
|
|
})
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
2021-11-09 03:05:12 +01:00
|
|
|
|
|
|
|
const xmlToJSON = (xml) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
parseString(xml, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
Logger.error(`[xmlToJSON] Error`, err)
|
|
|
|
resolve(null)
|
|
|
|
} else {
|
|
|
|
resolve(results)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
module.exports.xmlToJSON = xmlToJSON
|