Introduce ESLint and Prettier

This commit is contained in:
Zaid Abdulla committed 2018-08-24 05:31:22 -07:00
1 parent 9c943d0bbf
commit e494bdca8a
26 files changed
+1585 -1594

No files matched your search

+40 -53
View File
@@ -28,24 +28,30 @@ const OBJECT_TYPE = "[object Object]";
const REGEXP_TYPE = "[object RegExp]";
const DATE_TYPE = "[object Date]";
const typeOf = (obj) => Object.prototype.toString.call(obj);
const typeOf = obj => Object.prototype.toString.call(obj);
function generateDeviceId(deviceIdStruct) {
// Escapes everything except alphanumerics and underscore
function esc(str) {
return str.replace(/[^A-Za-z0-9_]/g, (chr) => {
return str.replace(/[^A-Za-z0-9_]/g, chr => {
const buf = new Buffer(chr, "utf8");
let rep = "";
for (let b of buf)
rep += "%" + (b.toString(16).toUpperCase());
for (let b of buf) rep += "%" + b.toString(16).toUpperCase();
return rep;
});
}
// Guaranteeing globally unique id as defined in TR-069
if (deviceIdStruct["ProductClass"])
return (esc(deviceIdStruct["OUI"])) + "-" + (esc(deviceIdStruct["ProductClass"])) + "-" + (esc(deviceIdStruct["SerialNumber"]));
return (esc(deviceIdStruct["OUI"])) + "-" + (esc(deviceIdStruct["SerialNumber"]));
if (deviceIdStruct["ProductClass"]) {
return (
esc(deviceIdStruct["OUI"]) +
"-" +
esc(deviceIdStruct["ProductClass"]) +
"-" +
esc(deviceIdStruct["SerialNumber"])
);
}
return esc(deviceIdStruct["OUI"]) + "-" + esc(deviceIdStruct["SerialNumber"]);
}
function parseAlias(pattern, start, res) {
@@ -53,22 +59,21 @@ function parseAlias(pattern, start, res) {
let i = start;
while (i < pattern.length && pattern[i] !== "]") {
const alias = [];
let j = i = parsePath(pattern, i, alias) + 1;
let j = (i = parsePath(pattern, i, alias) + 1);
while (pattern[j] !== "]" && pattern[j] !== ",") {
if (pattern[j] === "\"" && i === j) {
if (pattern[j] === '"' && i === j) {
++j;
while (pattern[j] !== "\"" || pattern[j - 1] === "\\") {
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");
if (++j >= pattern.length) throw new Error("Invalid alias expression");
}
let value = pattern.slice(i, j).trim();
i = j;
if (value[0] === "\"") {
if (value[0] === '"') {
try {
value = JSON.parse(value);
} catch (error) {
@@ -78,8 +83,7 @@ function parseAlias(pattern, start, res) {
alias.push(value);
aliases.push(alias);
if (pattern[i] === ",")
++i;
if (pattern[i] === ",") ++i;
}
// Sort to ensure identical expressions have idential string representation
@@ -87,20 +91,15 @@ function parseAlias(pattern, start, res) {
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) {
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;
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])) {
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;
@@ -109,21 +108,15 @@ function parseAlias(pattern, start, res) {
}
}
if (a[j].length > b[j].length)
return -1;
else if (a[j].length < b[j].length)
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[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;
if (a.length > b.length) return -1;
else if (a.length < b.length) return 1;
return 0;
}
@@ -141,7 +134,7 @@ function parsePath(pattern, start, res) {
// Colon separator is needed for parseAlias
if (i < pattern.length && pattern[i] !== ":") {
while (true) {
for (;;) {
if (pattern[i] === "[") {
path.alias |= 1 << path.length;
i = parseAlias(pattern, i + 1, path) + 1;
@@ -151,15 +144,12 @@ function parsePath(pattern, start, res) {
++i;
const n = pattern.slice(j, i);
if (n === "*")
path.wildcard |= 1 << path.length;
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");
if (i >= pattern.length || pattern[i] === ":") break;
else if (pattern[i] !== ".") throw new Error("Invalid alias expression");
++i;
}
@@ -167,16 +157,14 @@ function parsePath(pattern, start, res) {
Object.freeze(path);
if (res == null)
return path;
if (res == null) return path;
res.push(path);
return i;
}
function addPathMeta(path) {
if (path.alias != null && path.wildcard != null)
return path;
if (path.alias != null && path.wildcard != null) return path;
path.alias = 0;
path.wildcard = 0;
@@ -184,8 +172,7 @@ function addPathMeta(path) {
for (let [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]);
for (let j = 0; j < p.length; j += 2) addPathMeta(p[j]);
} else if (p === "*") {
path.wildcard |= 1 << i;
}
@@ -197,7 +184,7 @@ function addPathMeta(path) {
// Source: http://stackoverflow.com/a/6969486
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
}
exports.UNDEFINED_TYPE = UNDEFINED_TYPE;