diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e6e7934..be147d9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,16 @@ "**/.hg": true, "**/CVS": true, "**/.DS_Store": true, - "**/Thumbs.db": true + "**/Thumbs.db": true, + ".vscode": true, + ".github": true, + "static": true, + "node_modules": true }, - "hide-files.files": [] + "hide-files.files": [ + ".vscode", + ".github", + "static", + "node_modules" + ] } \ No newline at end of file diff --git a/dist/cjs/index.js b/dist/cjs/index.js index 0fc4866..02340d8 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.Table = exports.Action = exports.ElementInData = exports.Database = exports.Collection = void 0; +exports.Toml = exports.Table = exports.Action = exports.ElementInData = exports.Database = exports.Collection = void 0; const Collection_1 = require("./src/Collection"); Object.defineProperty(exports, "Collection", { enumerable: true, get: function () { return Collection_1.Collection; } }); const ElementInData_1 = require("./src/ElementInData"); @@ -11,3 +11,5 @@ const Table_1 = require("./src/Table"); Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return Table_1.Table; } }); const Database_1 = require("./src/Database"); Object.defineProperty(exports, "Database", { enumerable: true, get: function () { return Database_1.Database; } }); +const Toml_1 = require("./src/Toml"); +Object.defineProperty(exports, "Toml", { enumerable: true, get: function () { return Toml_1.Toml; } }); diff --git a/dist/cjs/src/Toml.js b/dist/cjs/src/Toml.js new file mode 100644 index 0000000..d16fe74 --- /dev/null +++ b/dist/cjs/src/Toml.js @@ -0,0 +1,213 @@ +"use strict"; +//@ts-nocheck +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Toml = void 0; +const fs_1 = require("fs"); +class Toml { +} +exports.Toml = Toml; +Toml.parse = parse; +Toml.read = read; +const indexOf = [].indexOf || + function (item) { + for (var i = 0, l = this.length; i < l; i++) { + if (i in this && this[i] === item) + return i; + } + return -1; + }; +const isNumeric = function (n) { + return !isNaN(parseInt(n, 10)); +}; +const uescape = function (str) { + return str + .replace("\\n", "\n") + .replace("\\t", "\t") + .replace(/\\(["'])/, "$1"); +}; +const newlines = "\n\r"; +const whitespace = "\t "; +const quotes = "\"'"; +const ignore = [null, "newline", "whitespace"]; +const values = ["number", "string", "date"]; +function read(input) { + let content = (0, fs_1.readFileSync)(input, "utf8"); + return parse(content); +} +function parse(input) { + let char, group, i, j, k, len, len1, part, ref, ref1; + let root = {}; + let context = root; + let state = null; + let skip = 0; + let accum = ""; + let token = null; + let key = null; + let value = null; + let list = null; + let lists = {}; + let nesting = -1; + let quote = null; + let prev = null; + const eat = function (char, reg, st) { + if (!reg.test(char)) { + state = st; + token = accum; + accum = ""; + return true; + } + else { + accum += char; + return false; + } + }; + ref = input.toString() + "\n"; + for (i = j = 0, len = ref.length; j < len; i = ++j) { + char = ref[i]; + if (--skip > 0) { + continue; + } + if (parse.debug) { + console.log(char, state); + } + if ((state != null ? state.slice(-4) : void 0) === "_end") { + state = null; + } + if (!state && indexOf.call(newlines, char) >= 0) { + state = "newline"; + } + if (indexOf.call(ignore, state) >= 0 && char === "#") { + state = "comment"; + } + if (state === "comment") { + if (indexOf.call(newlines, char) < 0) { + continue; + } + else { + state = "newline"; + } + } + if ((state === "whitespace" || state === "expect_value") && + indexOf.call(whitespace, char) >= 0) { + continue; + } + if (indexOf.call(newlines, prev) >= 0 && + indexOf.call(whitespace, char) >= 0) { + state = "whitespace"; + continue; + } + if (indexOf.call(ignore, state) >= 0 && char === "[") { + state = "group"; + continue; + } + if (state === "group" && eat(char, /[^\]]/)) { + group = token; + } + if (group) { + context = root; + ref1 = group.split("."); + for (k = 0, len1 = ref1.length; k < len1; k++) { + part = ref1[k]; + context = context[part] != null ? context[part] : (context[part] = {}); + } + group = null; + } + if (indexOf.call(ignore, state) >= 0 && /\w/.test(char)) { + state = "key"; + } + if (state === "key" && eat(char, /[^=]/)) { + key = token.trim(); + } + if (key && char === "=") { + state = "expect_value"; + continue; + } + if (state === "expect_value") { + if (indexOf.call(quotes, char) >= 0) { + state = "string"; + quote = char; + continue; + } + if (char === "t" && input.slice(i, +(i + 3) + 1 || 9e9) === "true") { + value = true; + skip = 4; + state = null; + } + if (char === "f" && input.slice(i, +(i + 4) + 1 || 9e9) === "false") { + value = false; + skip = 5; + state = null; + } + if (char === "-") { + state = "number"; + accum = "-"; + continue; + } + if (isNumeric(char)) { + state = "number"; + } + if (char === "[") { + list = lists[++nesting] = []; + continue; + } + } + if (state === "string" && eat(char, /[^"']/, "string_end")) { + value = uescape(token); + } + if (state === "number" && eat(char, /[\d.]/, "number_end")) { + value = +token; + } + if (state === "date" && eat(char, /[\d-:TZ]/)) { + value = new Date(token); + } + if (state === "string_end") { + if (char !== quote || (char === quote && prev === "\\")) { + state = "string"; + accum = value + char; + value = null; + } + else { + state = null; + quote = null; + } + } + if (state === "number_end") { + if (char === "-") { + state = "date"; + accum = token + char; + value = null; + } + else { + state = null; + } + } + if (list != null) { + if (value != null) { + list.push(value); + value = null; + state = "expect_value"; + } + if (char === ",") { + continue; + } + if (char === "]" && indexOf.call(values, state) < 0) { + if (nesting === 0) { + value = list; + list = null; + nesting = -1; + state = null; + } + if (nesting > 0) { + lists[--nesting].push(list); + list = lists[nesting]; + } + } + } + if (key && value != null) { + context[key] = value; + key = value = null; + } + prev = char; + } + return root; +} diff --git a/dist/cjs/tests/tests.js b/dist/cjs/tests/tests.js index 4dfaecb..d66adcd 100644 --- a/dist/cjs/tests/tests.js +++ b/dist/cjs/tests/tests.js @@ -1,10 +1,4 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); -const db = new index_1.Database("./tests/myDb.yaml"); -db.on("ready", () => console.log("Database Ready!")); -db.on("elementAdd", (El, value) => console.log(`New "${El.ID}" element Value: ${value}`)); -db.on("elementEdit", (El, newValue, oldValue) => { - console.log(`"${El.ID}" element Value: ${oldValue} -> ${newValue}`); -}); -console.log("hello world"); // hi +console.log(index_1.Toml.read("tests/test.toml")); diff --git a/dist/esm/index.js b/dist/esm/index.js index ab258f6..66498fa 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -3,4 +3,5 @@ import { ElementInData } from "./src/ElementInData"; import { Action } from "./src/Action"; import { Table } from "./src/Table"; import { Database } from "./src/Database"; -export { Collection, Database, ElementInData, Action, Table }; +import { Toml } from "./src/Toml"; +export { Collection, Database, ElementInData, Action, Table, Toml }; diff --git a/dist/esm/src/Toml.js b/dist/esm/src/Toml.js new file mode 100644 index 0000000..4cb0dbf --- /dev/null +++ b/dist/esm/src/Toml.js @@ -0,0 +1,209 @@ +//@ts-nocheck +import { readFileSync } from "fs"; +export class Toml { +} +Toml.parse = parse; +Toml.read = read; +const indexOf = [].indexOf || + function (item) { + for (var i = 0, l = this.length; i < l; i++) { + if (i in this && this[i] === item) + return i; + } + return -1; + }; +const isNumeric = function (n) { + return !isNaN(parseInt(n, 10)); +}; +const uescape = function (str) { + return str + .replace("\\n", "\n") + .replace("\\t", "\t") + .replace(/\\(["'])/, "$1"); +}; +const newlines = "\n\r"; +const whitespace = "\t "; +const quotes = "\"'"; +const ignore = [null, "newline", "whitespace"]; +const values = ["number", "string", "date"]; +function read(input) { + let content = readFileSync(input, "utf8"); + return parse(content); +} +function parse(input) { + let char, group, i, j, k, len, len1, part, ref, ref1; + let root = {}; + let context = root; + let state = null; + let skip = 0; + let accum = ""; + let token = null; + let key = null; + let value = null; + let list = null; + let lists = {}; + let nesting = -1; + let quote = null; + let prev = null; + const eat = function (char, reg, st) { + if (!reg.test(char)) { + state = st; + token = accum; + accum = ""; + return true; + } + else { + accum += char; + return false; + } + }; + ref = input.toString() + "\n"; + for (i = j = 0, len = ref.length; j < len; i = ++j) { + char = ref[i]; + if (--skip > 0) { + continue; + } + if (parse.debug) { + console.log(char, state); + } + if ((state != null ? state.slice(-4) : void 0) === "_end") { + state = null; + } + if (!state && indexOf.call(newlines, char) >= 0) { + state = "newline"; + } + if (indexOf.call(ignore, state) >= 0 && char === "#") { + state = "comment"; + } + if (state === "comment") { + if (indexOf.call(newlines, char) < 0) { + continue; + } + else { + state = "newline"; + } + } + if ((state === "whitespace" || state === "expect_value") && + indexOf.call(whitespace, char) >= 0) { + continue; + } + if (indexOf.call(newlines, prev) >= 0 && + indexOf.call(whitespace, char) >= 0) { + state = "whitespace"; + continue; + } + if (indexOf.call(ignore, state) >= 0 && char === "[") { + state = "group"; + continue; + } + if (state === "group" && eat(char, /[^\]]/)) { + group = token; + } + if (group) { + context = root; + ref1 = group.split("."); + for (k = 0, len1 = ref1.length; k < len1; k++) { + part = ref1[k]; + context = context[part] != null ? context[part] : (context[part] = {}); + } + group = null; + } + if (indexOf.call(ignore, state) >= 0 && /\w/.test(char)) { + state = "key"; + } + if (state === "key" && eat(char, /[^=]/)) { + key = token.trim(); + } + if (key && char === "=") { + state = "expect_value"; + continue; + } + if (state === "expect_value") { + if (indexOf.call(quotes, char) >= 0) { + state = "string"; + quote = char; + continue; + } + if (char === "t" && input.slice(i, +(i + 3) + 1 || 9e9) === "true") { + value = true; + skip = 4; + state = null; + } + if (char === "f" && input.slice(i, +(i + 4) + 1 || 9e9) === "false") { + value = false; + skip = 5; + state = null; + } + if (char === "-") { + state = "number"; + accum = "-"; + continue; + } + if (isNumeric(char)) { + state = "number"; + } + if (char === "[") { + list = lists[++nesting] = []; + continue; + } + } + if (state === "string" && eat(char, /[^"']/, "string_end")) { + value = uescape(token); + } + if (state === "number" && eat(char, /[\d.]/, "number_end")) { + value = +token; + } + if (state === "date" && eat(char, /[\d-:TZ]/)) { + value = new Date(token); + } + if (state === "string_end") { + if (char !== quote || (char === quote && prev === "\\")) { + state = "string"; + accum = value + char; + value = null; + } + else { + state = null; + quote = null; + } + } + if (state === "number_end") { + if (char === "-") { + state = "date"; + accum = token + char; + value = null; + } + else { + state = null; + } + } + if (list != null) { + if (value != null) { + list.push(value); + value = null; + state = "expect_value"; + } + if (char === ",") { + continue; + } + if (char === "]" && indexOf.call(values, state) < 0) { + if (nesting === 0) { + value = list; + list = null; + nesting = -1; + state = null; + } + if (nesting > 0) { + lists[--nesting].push(list); + list = lists[nesting]; + } + } + } + if (key && value != null) { + context[key] = value; + key = value = null; + } + prev = char; + } + return root; +} diff --git a/dist/esm/tests/tests.js b/dist/esm/tests/tests.js index dbea4d8..92ae091 100644 --- a/dist/esm/tests/tests.js +++ b/dist/esm/tests/tests.js @@ -1,8 +1,2 @@ -import { Database } from "../index"; -const db = new Database("./tests/myDb.yaml"); -db.on("ready", () => console.log("Database Ready!")); -db.on("elementAdd", (El, value) => console.log(`New "${El.ID}" element Value: ${value}`)); -db.on("elementEdit", (El, newValue, oldValue) => { - console.log(`"${El.ID}" element Value: ${oldValue} -> ${newValue}`); -}); -console.log("hello world"); // hi +import { Toml } from "../index"; +console.log(Toml.read("tests/test.toml")); diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index ab258f6..66498fa 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -3,4 +3,5 @@ import { ElementInData } from "./src/ElementInData"; import { Action } from "./src/Action"; import { Table } from "./src/Table"; import { Database } from "./src/Database"; -export { Collection, Database, ElementInData, Action, Table }; +import { Toml } from "./src/Toml"; +export { Collection, Database, ElementInData, Action, Table, Toml }; diff --git a/dist/types/src/Toml.d.ts b/dist/types/src/Toml.d.ts new file mode 100644 index 0000000..c72986a --- /dev/null +++ b/dist/types/src/Toml.d.ts @@ -0,0 +1,7 @@ +export declare class Toml { + static parse: typeof parse; + static read: typeof read; +} +declare function read(input: string): {}; +declare function parse(input: string): {}; +export {}; diff --git a/dist/x.md b/dist/x.md deleted file mode 100644 index e69de29..0000000 diff --git a/index.ts b/index.ts index 6820c96..6c4285c 100644 --- a/index.ts +++ b/index.ts @@ -3,5 +3,6 @@ import { ElementInData } from "./src/ElementInData" import { Action } from "./src/Action" import { Table } from "./src/Table" import { Database } from "./src/Database" +import { Toml } from "./src/Toml" -export { Collection, Database, ElementInData, Action, Table } \ No newline at end of file +export { Collection, Database, ElementInData, Action, Table, Toml } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ac9afe6..8452e89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ydb", - "version": "1.1.0", + "version": "1.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ydb", - "version": "1.1.0", + "version": "1.3.4", "license": "MIT", "dependencies": { "string-crypto": "^2.0.2", diff --git a/src/Toml.ts b/src/Toml.ts new file mode 100644 index 0000000..64fb93c --- /dev/null +++ b/src/Toml.ts @@ -0,0 +1,218 @@ +//@ts-nocheck + +import { readFileSync } from "fs" + +export class Toml { + static parse = parse; + static read = read; +} + + +const indexOf = + [].indexOf || + function (item: an4y) { + for (var i = 0, l = this.length; i < l; i++) { + if (i in this && this[i] === item) return i; + } + return -1; + }; + +const isNumeric = function (n) { + return !isNaN(parseInt(n, 10)); +}; + +const uescape = function (str: string) { + return str + .replace("\\n", "\n") + .replace("\\t", "\t") + .replace(/\\(["'])/, "$1"); +}; + +const newlines = "\n\r"; +const whitespace = "\t "; +const quotes = "\"'"; +const ignore = [null, "newline", "whitespace"]; +const values = ["number", "string", "date"]; + +function read(input: string) { + let content = readFileSync(input, "utf8"); + return parse(content); +} + +function parse(input: string) { + let char, group, i, j, k, len, len1, part, ref, ref1; + let root = {}; + let context = root; + let state = null; + let skip = 0; + let accum = ""; + let token = null; + let key = null; + let value = null; + let list = null; + let lists = {}; + let nesting = -1; + let quote = null; + let prev = null; + const eat = function (char, reg, st) { + if (!reg.test(char)) { + state = st; + token = accum; + accum = ""; + return true; + } else { + accum += char; + return false; + } + }; + ref = input.toString() + "\n"; + for (i = j = 0, len = ref.length; j < len; i = ++j) { + char = ref[i]; + if (--skip > 0) { + continue; + } + if (parse.debug) { + console.log(char, state); + } + if ((state != null ? state.slice(-4) : void 0) === "_end") { + state = null; + } + if (!state && indexOf.call(newlines, char) >= 0) { + state = "newline"; + } + if (indexOf.call(ignore, state) >= 0 && char === "#") { + state = "comment"; + } + if (state === "comment") { + if (indexOf.call(newlines, char) < 0) { + continue; + } else { + state = "newline"; + } + } + if ( + (state === "whitespace" || state === "expect_value") && + indexOf.call(whitespace, char) >= 0 + ) { + continue; + } + if ( + indexOf.call(newlines, prev) >= 0 && + indexOf.call(whitespace, char) >= 0 + ) { + state = "whitespace"; + continue; + } + if (indexOf.call(ignore, state) >= 0 && char === "[") { + state = "group"; + continue; + } + if (state === "group" && eat(char, /[^\]]/)) { + group = token; + } + if (group) { + context = root; + ref1 = group.split("."); + for (k = 0, len1 = ref1.length; k < len1; k++) { + part = ref1[k]; + context = context[part] != null ? context[part] : (context[part] = {}); + } + group = null; + } + if (indexOf.call(ignore, state) >= 0 && /\w/.test(char)) { + state = "key"; + } + if (state === "key" && eat(char, /[^=]/)) { + key = token.trim(); + } + if (key && char === "=") { + state = "expect_value"; + continue; + } + if (state === "expect_value") { + if (indexOf.call(quotes, char) >= 0) { + state = "string"; + quote = char; + continue; + } + if (char === "t" && input.slice(i, +(i + 3) + 1 || 9e9) === "true") { + value = true; + skip = 4; + state = null; + } + if (char === "f" && input.slice(i, +(i + 4) + 1 || 9e9) === "false") { + value = false; + skip = 5; + state = null; + } + if (char === "-") { + state = "number"; + accum = "-"; + continue; + } + if (isNumeric(char)) { + state = "number"; + } + if (char === "[") { + list = lists[++nesting] = []; + continue; + } + } + if (state === "string" && eat(char, /[^"']/, "string_end")) { + value = uescape(token); + } + if (state === "number" && eat(char, /[\d.]/, "number_end")) { + value = +token; + } + if (state === "date" && eat(char, /[\d-:TZ]/)) { + value = new Date(token); + } + if (state === "string_end") { + if (char !== quote || (char === quote && prev === "\\")) { + state = "string"; + accum = value + char; + value = null; + } else { + state = null; + quote = null; + } + } + if (state === "number_end") { + if (char === "-") { + state = "date"; + accum = token + char; + value = null; + } else { + state = null; + } + } + if (list != null) { + if (value != null) { + list.push(value); + value = null; + state = "expect_value"; + } + if (char === ",") { + continue; + } + if (char === "]" && indexOf.call(values, state) < 0) { + if (nesting === 0) { + value = list; + list = null; + nesting = -1; + state = null; + } + if (nesting > 0) { + lists[--nesting].push(list); + list = lists[nesting]; + } + } + } + if (key && value != null) { + context[key] = value; + key = value = null; + } + prev = char; + } + return root; +} diff --git a/tests/test.toml b/tests/test.toml new file mode 100644 index 0000000..0b75cb4 --- /dev/null +++ b/tests/test.toml @@ -0,0 +1 @@ +x = "hi" \ No newline at end of file diff --git a/tests/tests.ts b/tests/tests.ts index 4353a57..43c3e97 100644 --- a/tests/tests.ts +++ b/tests/tests.ts @@ -1,14 +1,2 @@ -import { Database } from "../index"; -const db = new Database("./tests/myDb.yaml"); - -db.on("ready", () => console.log("Database Ready!")); - -db.on("elementAdd", (El, value) => - console.log(`New "${El.ID}" element Value: ${value}`) -); - -db.on("elementEdit", (El, newValue, oldValue) => { - console.log(`"${El.ID}" element Value: ${oldValue} -> ${newValue}`); -}); - -console.log("hello world"); // hi \ No newline at end of file +import { Toml } from "../index"; +console.log(Toml.read("tests/test.toml")); \ No newline at end of file