shadowly 1.0.3 → 1.0.4

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,63 @@
1
+ type GetKey<T> = T extends readonly any[] ? number : T extends object ? keyof T : never;
2
+ type GetValue<T, K> = T extends readonly (infer U)[] ? U : T extends Record<PropertyKey, any> ? (K extends keyof T ? T[K] : never) : never;
3
+ type RemoveKey<T> = T extends readonly any[] ? number : T extends object ? keyof T : never;
4
+ export default class Shadowly<TRoot, TNode = TRoot> {
5
+ readonly DB_PATH: string;
6
+ private readonly path;
7
+ private content;
8
+ private parent?;
9
+ private parentKey?;
10
+ /**
11
+ * Shadowly의 새 인스턴스를 만듭니다.
12
+ * @param path JSON 파일 경로
13
+ */
14
+ constructor(path: string);
15
+ /**
16
+ * 새 JSON을 만듭니다.
17
+ * @param path JSON 파일 경로
18
+ * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
19
+ */
20
+ static generateNewJson(path: string, isArray?: boolean): void;
21
+ private static create;
22
+ /**
23
+ * key로 이동합니다.
24
+ * @param key 이동할 키 이름
25
+ * @returns 작업 수행 후 Shadowly 인스턴스
26
+ */
27
+ get<K extends GetKey<TNode>>(key: K): Shadowly<TRoot, GetValue<TNode, K>>;
28
+ /**
29
+ * 현재 key의 value를 반환합니다.
30
+ * @returns 현재 key의 value
31
+ */
32
+ value(): TNode;
33
+ /**
34
+ * 현재 key에 값을 씁니다.
35
+ * @param value 쓸 값
36
+ * @returns 작업 수행 후 Shadowly 인스턴스
37
+ */
38
+ set(value: TNode): this;
39
+ /**
40
+ * key를 삭제합니다.
41
+ * @param key 삭제할 key
42
+ * @returns 작업 수행 후 Shadowly 인스턴스
43
+ */
44
+ remove<K extends RemoveKey<TNode>>(key: K): this;
45
+ /**
46
+ * 한 번 뒤로 이동합니다.
47
+ * @returns 작업 수행 후 Shadowly 인스턴스
48
+ */
49
+ back(): Shadowly<TRoot, any>;
50
+ /**
51
+ * steps만큼 뒤로 이동합니다.
52
+ * @param steps 이동할 횟수
53
+ * @returns 작업 수행 후 Shadowly 인스턴스
54
+ */
55
+ up(steps?: number): Shadowly<TRoot, any>;
56
+ /**
57
+ * JSON의 루트로 이동합니다.
58
+ * @returns 작업 수행 후 Shadowly 인스턴스
59
+ */
60
+ root(): Shadowly<TRoot, TRoot>;
61
+ private persist;
62
+ }
63
+ export {};
package/dist/class.js ADDED
@@ -0,0 +1,133 @@
1
+ import * as fs from "node:fs";
2
+ import { ShadowlyError } from "./error";
3
+ function isValidJson(text) {
4
+ if (typeof text !== "string")
5
+ return false;
6
+ try {
7
+ JSON.parse(text);
8
+ return true;
9
+ }
10
+ catch (_a) {
11
+ return false;
12
+ }
13
+ }
14
+ export default class Shadowly {
15
+ /**
16
+ * Shadowly의 새 인스턴스를 만듭니다.
17
+ * @param path JSON 파일 경로
18
+ */
19
+ constructor(path) {
20
+ this.path = path;
21
+ this.DB_PATH = path;
22
+ if (!fs.existsSync(path))
23
+ throw new ShadowlyError("ENOTFOUND", path + " not found.");
24
+ const file = fs.readFileSync(path, "utf-8");
25
+ if (!isValidJson(file))
26
+ throw new ShadowlyError("EINVALID", path + " is not valid.");
27
+ this.content = JSON.parse(file);
28
+ }
29
+ /**
30
+ * 새 JSON을 만듭니다.
31
+ * @param path JSON 파일 경로
32
+ * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
33
+ */
34
+ static generateNewJson(path, isArray) {
35
+ fs.writeFileSync(path, isArray ? "[]" : "{}", "utf-8");
36
+ }
37
+ static create(DB_PATH, path, content, parent, parentKey) {
38
+ const inst = Object.create(Shadowly.prototype);
39
+ inst.DB_PATH = DB_PATH;
40
+ inst.path = path;
41
+ inst.content = content;
42
+ inst.parent = parent;
43
+ inst.parentKey = parentKey;
44
+ return inst;
45
+ }
46
+ /**
47
+ * key로 이동합니다.
48
+ * @param key 이동할 키 이름
49
+ * @returns 작업 수행 후 Shadowly 인스턴스
50
+ */
51
+ get(key) {
52
+ const nextContent = this.content[key];
53
+ return Shadowly.create(this.DB_PATH, this.path, nextContent, this, key);
54
+ }
55
+ /**
56
+ * 현재 key의 value를 반환합니다.
57
+ * @returns 현재 key의 value
58
+ */
59
+ value() {
60
+ return this.content;
61
+ }
62
+ /**
63
+ * 현재 key에 값을 씁니다.
64
+ * @param value 쓸 값
65
+ * @returns 작업 수행 후 Shadowly 인스턴스
66
+ */
67
+ set(value) {
68
+ this.content = value;
69
+ if (this.parent && this.parentKey !== undefined) {
70
+ this.parent.content[this.parentKey] = value;
71
+ }
72
+ this.persist();
73
+ return this;
74
+ }
75
+ /**
76
+ * key를 삭제합니다.
77
+ * @param key 삭제할 key
78
+ * @returns 작업 수행 후 Shadowly 인스턴스
79
+ */
80
+ remove(key) {
81
+ const cur = this.content;
82
+ if (Array.isArray(cur)) {
83
+ const idx = key;
84
+ if (Number.isInteger(idx) && idx >= 0 && idx < cur.length)
85
+ cur.splice(idx, 1);
86
+ }
87
+ else if (cur && typeof cur === "object") {
88
+ delete cur[key];
89
+ }
90
+ else {
91
+ throw new ShadowlyError("ETYPE", "Current value is not an object/array.");
92
+ }
93
+ this.persist();
94
+ return this;
95
+ }
96
+ /**
97
+ * 한 번 뒤로 이동합니다.
98
+ * @returns 작업 수행 후 Shadowly 인스턴스
99
+ */
100
+ back() {
101
+ var _a;
102
+ return (_a = this.parent) !== null && _a !== void 0 ? _a : this;
103
+ }
104
+ /**
105
+ * steps만큼 뒤로 이동합니다.
106
+ * @param steps 이동할 횟수
107
+ * @returns 작업 수행 후 Shadowly 인스턴스
108
+ */
109
+ up(steps = 1) {
110
+ let cur = this;
111
+ for (let i = 0; i < steps; i++) {
112
+ if (!cur.parent)
113
+ break;
114
+ cur = cur.parent;
115
+ }
116
+ return cur;
117
+ }
118
+ /**
119
+ * JSON의 루트로 이동합니다.
120
+ * @returns 작업 수행 후 Shadowly 인스턴스
121
+ */
122
+ root() {
123
+ let cur = this;
124
+ while (cur.parent)
125
+ cur = cur.parent;
126
+ return cur;
127
+ }
128
+ persist() {
129
+ const r = this.root();
130
+ fs.writeFileSync(r.DB_PATH, JSON.stringify(r.content, null, 2), "utf-8");
131
+ }
132
+ }
133
+ //# sourceMappingURL=class.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class.js","sourceRoot":"","sources":["../src/class.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,SAAS,WAAW,CAAC,IAAY;IAC7B,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC3C,IAAI,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;IAChB,CAAC;IAAC,WAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAiBD,MAAM,CAAC,OAAO,OAAO,QAAQ;IASzB;;;OAGG;IACH,YAAY,IAAY;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QAEpB,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC;YACpB,MAAM,IAAI,aAAa,CAAC,WAAW,EAAE,IAAI,GAAG,aAAa,CAAC,CAAC;QAE/D,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC5C,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;YAClB,MAAM,IAAI,aAAa,CAAC,UAAU,EAAE,IAAI,GAAG,gBAAgB,CAAC,CAAC;QAEjE,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAQ,CAAC;IAC3C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,eAAe,CAAC,IAAY,EAAE,OAAiB;QACzD,EAAE,CAAC,aAAa,CAAC,IAAI,EAAE,OAAO,C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AO,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IAC7E,CAAC;CACJ"}
@@ -0,0 +1,4 @@
1
+ export declare class ShadowlyError extends Error {
2
+ readonly code: string;
3
+ constructor(code: string, message: string);
4
+ }
package/dist/error.js ADDED
@@ -0,0 +1,9 @@
1
+ export class ShadowlyError extends Error {
2
+ constructor(code, message) {
3
+ super(code + ": " + message);
4
+ this.name = "Shadowly";
5
+ this.code = code;
6
+ Error.captureStackTrace(this, this.constructor);
7
+ }
8
+ }
9
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAGpC,YACI,IAAY,EACZ,OAAe;QAEf,KAAK,CAAC,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,CAAC;QAE7B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IACpD,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ import Shadowly from "./class";
2
+ export default Shadowly;
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import Shadowly from "./class";
2
+ export default Shadowly;
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,SAAS,CAAC;AAE/B,eAAe,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shadowly",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "TypeScript JSON Database",
5
5
  "keywords": [
6
6
  "database",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "author": "Pro203S",
21
- "type": "commonjs",
21
+ "type": "module",
22
22
  "main": "./dist/index.js",
23
23
  "scripts": {
24
24
  "build": "tsc"
package/tsconfig.json CHANGED
@@ -1,13 +1,24 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "CommonJS",
5
- "rootDir": "src",
6
3
  "outDir": "dist",
4
+ "target": "ES6",
5
+ "module": "ESNext",
6
+ "lib": [
7
+ "ES6",
8
+ "DOM"
9
+ ],
10
+ "moduleResolution": "Node",
11
+ "esModuleInterop": true,
7
12
  "declaration": true,
8
13
  "strict": true,
9
- "esModuleInterop": true,
10
- "resolveJsonModule": true
14
+ "sourceMap": true,
15
+ "types": [
16
+ "node"
17
+ ],
18
+ "rootDir": "src"
11
19
  },
12
- "exclude": ["tests/", "dist/"]
13
- }
20
+ "include": [
21
+ "src/**/*"
22
+ ],
23
+ "exclude": ["tests"]
24
+ }