Refactor code to use Path class
This commit is contained in:
1 parent
006f83a478
commit
f878d828eb
13 files changed
+663
-784
No files matched your search
-134
@@ -17,8 +17,6 @@
|
||||
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { ParamPath } from "./types";
|
||||
|
||||
export const UNDEFINED_TYPE = "[object Undefined]";
|
||||
export const NULL_TYPE = "[object Null]";
|
||||
export const BOOLEAN_TYPE = "[object Boolean]";
|
||||
@@ -55,138 +53,6 @@ export function generateDeviceId(deviceIdStruct): string {
|
||||
return esc(deviceIdStruct["OUI"]) + "-" + esc(deviceIdStruct["SerialNumber"]);
|
||||
}
|
||||
|
||||
export function parseAlias(pattern, start, res): number {
|
||||
const aliases = [];
|
||||
let i = start;
|
||||
while (i < pattern.length && pattern[i] !== "]") {
|
||||
const alias = [];
|
||||
/* eslint-disable-next-line @typescript-eslint/no-use-before-define */
|
||||
let j = (i = parsePath(pattern, i, alias) + 1);
|
||||
while (pattern[j] !== "]" && pattern[j] !== ",") {
|
||||
if (pattern[j] === '"' && i === j) {
|
||||
++j;
|
||||
while (pattern[j] !== '"' || pattern[j - 1] === "\\") {
|
||||
if (++j >= pattern.length)
|
||||
throw new Error("Invalid alias expression");
|
||||
}
|
||||
}
|
||||
if (++j >= pattern.length) throw new Error("Invalid alias expression");
|
||||
}
|
||||
|
||||
let value = pattern.slice(i, j).trim();
|
||||
i = j;
|
||||
if (value[0] === '"') {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (error) {
|
||||
throw new Error("Invalid alias expression");
|
||||
}
|
||||
}
|
||||
|
||||
alias.push(value);
|
||||
aliases.push(alias);
|
||||
if (pattern[i] === ",") ++i;
|
||||
}
|
||||
|
||||
// Sort to ensure identical expressions have idential string representation
|
||||
function srt(a, b): number {
|
||||
const jMax = Math.min(a.length, b.length);
|
||||
for (let j = 0; j < jMax; j += 2) {
|
||||
const kMax = Math.min(a[j].length, b[j].length);
|
||||
for (let k = 0; k < kMax; ++k) {
|
||||
if (Array.isArray(a[j][k])) {
|
||||
if (Array.isArray(b[j][k])) return srt(a[j][k], b[j][k]);
|
||||
else if (b[j][k] == null) return -1;
|
||||
else return 1;
|
||||
} else if (a[j][k] == null) {
|
||||
if (b[j][k] == null) return 0;
|
||||
else return 1;
|
||||
} else if (b[j][k] == null || Array.isArray(b[j][k])) {
|
||||
return -1;
|
||||
} else if (a[j][k] > b[j][k]) {
|
||||
return 1;
|
||||
} else if (a[j][k] < b[j][k]) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (a[j].length > b[j].length) return -1;
|
||||
else if (a[j].length < b[j].length) return 1;
|
||||
|
||||
if (a[j + 1] > b[j + 1]) return -1;
|
||||
else if (a[j + 1] < b[j + 1]) return 1;
|
||||
}
|
||||
|
||||
if (a.length > b.length) return -1;
|
||||
else if (a.length < b.length) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
aliases.sort(srt);
|
||||
res.push([].concat.apply([], aliases));
|
||||
return i;
|
||||
}
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-use-before-define */
|
||||
export function parsePath(pattern, start, res): number;
|
||||
export function parsePath(pattern, start?): ParamPath;
|
||||
export function parsePath(pattern, start?, res?): number | ParamPath {
|
||||
const path = [] as ParamPath;
|
||||
path.wildcard = 0;
|
||||
path.alias = 0;
|
||||
let i = start || 0;
|
||||
|
||||
// Colon separator is needed for parseAlias
|
||||
if (i < pattern.length && pattern[i] !== ":") {
|
||||
for (;;) {
|
||||
if (pattern[i] === "[") {
|
||||
path.alias |= 1 << path.length;
|
||||
i = parseAlias(pattern, i + 1, path) + 1;
|
||||
} else {
|
||||
const j = i;
|
||||
while (i < pattern.length && pattern[i] !== ":" && pattern[i] !== ".")
|
||||
++i;
|
||||
|
||||
const n = pattern.slice(j, i);
|
||||
if (n === "*") path.wildcard |= 1 << path.length;
|
||||
path.push(n);
|
||||
}
|
||||
|
||||
if (i >= pattern.length || pattern[i] === ":") break;
|
||||
else if (pattern[i] !== ".") throw new Error("Invalid alias expression");
|
||||
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(path);
|
||||
|
||||
if (res == null) return path;
|
||||
|
||||
res.push(path);
|
||||
return i;
|
||||
}
|
||||
|
||||
export function addPathMeta(path): ParamPath {
|
||||
if (path.alias != null && path.wildcard != null) return path;
|
||||
|
||||
path.alias = 0;
|
||||
path.wildcard = 0;
|
||||
|
||||
for (const [i, p] of path.entries()) {
|
||||
if (Array.isArray(p)) {
|
||||
path.alias |= 1 << i;
|
||||
for (let j = 0; j < p.length; j += 2) addPathMeta(p[j]);
|
||||
} else if (p === "*") {
|
||||
path.wildcard |= 1 << i;
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(path);
|
||||
return path;
|
||||
}
|
||||
|
||||
// Source: http://stackoverflow.com/a/6969486
|
||||
export function escapeRegExp(str): string {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
|
||||
|
||||
Reference in new issue
Block a user