qlcodes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ import fs from "fs";
2
+ import { reargv } from "./utils/args.mjs";
3
+
4
+ console.log(fs.readFileSync(reargv().files[0], "utf8"));
package/src/regex.mjs ADDED
@@ -0,0 +1,11 @@
1
+ export const ERROR_CODE_HEADER_RGX =
2
+ /^(?:Table \d+\.\sClass\sCode|Class)\s+(?<class_code>[0-9A-Z]{2})[^\w]+(?<label>.+)\n?/;
3
+
4
+ // Class after generate header
5
+ export const CLASS_RE =
6
+ /^Class\s+(?<classCode>[0-9A-Z]{2})\s+—\s+(?<label>.+)$/;
7
+
8
+ export const CODE_RE =
9
+ /^(?<code>[0-9A-Z]{5})\*?(?<flags>[\w:]*)\s+(?<key>!?[a-z_]+)$/;
10
+
11
+ export const COMMENT_RE = /^\*/;
@@ -0,0 +1,32 @@
1
+ export const reargv = () => {
2
+ const args = process.argv.slice(2);
3
+ const argvs = { misc: [], options: {}, files: [] };
4
+ const len = args.length;
5
+ let jump = 0;
6
+ for (const [i, arg] of args.entries()) {
7
+ if (jump) {
8
+ jump--;
9
+ continue;
10
+ }
11
+ if (arg.startsWith("--")) {
12
+ const option = arg.replace(/^-+/g, "");
13
+ if (i + 1 < len) {
14
+ const nextArg = args[i + 1];
15
+ if (!nextArg.startsWith("--")) {
16
+ argvs.options[option] = args[i + 1];
17
+ jump++;
18
+ } else {
19
+ argvs.options[option] = true;
20
+ jump++;
21
+ }
22
+ } else {
23
+ argvs.options[option] = true;
24
+ }
25
+ } else if (/(\.(?:[a-zA-Z]+)+)$/.test(arg)) {
26
+ argvs.files.push(arg);
27
+ } else {
28
+ argvs.misc.push(arg);
29
+ }
30
+ }
31
+ return argvs;
32
+ };
@@ -0,0 +1,55 @@
1
+ import { reargv } from "./args.mjs";
2
+ import { toSnakeCase } from "./str.mjs";
3
+
4
+ const argv = reargv();
5
+
6
+ export const normalizeCode = (raw, classCode) => {
7
+ if (raw === "0") return "00000";
8
+ if (raw.endsWith("xxx")) return `${classCode}000`;
9
+ if (!raw.length) return "";
10
+ return raw.padStart(5, "0");
11
+ };
12
+
13
+ const getFlags = () => (argv.options.flag || "").split(",").join(":");
14
+ const isMakeKeys = process.env.MAKE_KEYS === "true";
15
+ export const newCode = (line, currentClassCode) => {
16
+ const flags = getFlags();
17
+ const [left, right] = line.split(";", 2);
18
+
19
+ const code = normalizeCode(left.trim(), currentClassCode);
20
+ const description = right.trim();
21
+ let key = toSnakeCase(description.split(".")[0]);
22
+
23
+ if (!isMakeKeys && !/^[a-z_]+/.test(description.split(".")[0])) {
24
+ key = "_";
25
+ }
26
+
27
+ return code.length
28
+ ? [`${code}${flags ? "*" + flags : ""} !${key}`, description]
29
+ : [description];
30
+ };
31
+
32
+ export const insertHeadersAndFormat = (sanitized) => {
33
+ const lines = [];
34
+ let lastClass = "";
35
+ sanitized.split(/[\n]/g).forEach((line) => {
36
+ const match = /^(?<class_code>[0-9A-Z]{1,5});(?<label>.+)/.exec(line);
37
+
38
+ if (match) {
39
+ let { class_code, label } = match.groups;
40
+ class_code == "0" ? (class_code = "00000") : class_code;
41
+ if (class_code) {
42
+ const idClass = class_code.substring(0, 2);
43
+ if (lastClass != idClass) {
44
+ lines.push(`Class ${idClass} — ${label.split(";")[0]}`);
45
+ }
46
+ lastClass = idClass;
47
+ }
48
+ }
49
+ const [right, left] = line.split(";");
50
+ if (left) {
51
+ lines.push([right.padStart(5, "0"), left].join(";"));
52
+ }
53
+ });
54
+ return lines.join("\n");
55
+ };
@@ -0,0 +1,11 @@
1
+ export const onStdIn = (cb) => {
2
+ let input = "";
3
+ process.stdin.setEncoding("utf8");
4
+ process.stdin.on("data", (chunk) => {
5
+ input += chunk;
6
+ });
7
+
8
+ process.stdin.on("end", () => {
9
+ cb(input);
10
+ });
11
+ };
@@ -0,0 +1,5 @@
1
+ export const toSnakeCase = (text) =>
2
+ text
3
+ .toLowerCase()
4
+ .replace(/[^a-z0-9]+/g, "_")
5
+ .replace(/^_|_$/g, "");
@@ -0,0 +1,31 @@
1
+ export interface QLLens {
2
+ /**
3
+ * Normalized SQLSTATE code.
4
+ * "-1" indicates that the code was not found.
5
+ */
6
+ code: string;
7
+
8
+ /**
9
+ * Semantic identifiers for the error.
10
+ */
11
+ keys: string[];
12
+
13
+ /**
14
+ * DBMS where the error code is applicable
15
+ * (e.g. "ibm", "postgres", "oracle").
16
+ */
17
+ use: string[];
18
+
19
+ /**
20
+ * Human-readable explanation of the error.
21
+ */
22
+ reason: string;
23
+ }
24
+
25
+ /**
26
+ * Returns a normalized SQL error description.
27
+ *
28
+ * @param code SQLSTATE error code
29
+ * @returns A QLLens object (never null)
30
+ */
31
+ export function lens(code: string): QLLens;