shadowly 1.0.5 → 1.2.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.
package/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright 2025 Pro203S
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright 2025 Pro203S
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -71,6 +71,39 @@ new Shadowly("./object.json")
71
71
  .value();
72
72
  ```
73
73
 
74
+ 배열에서는 `findIndex`, `find`, `findAll`을 사용할 수 있습니다.
75
+
76
+ `findIndex` 함수는 조건에 맞는 첫 번째 요소의 index를 반환합니다.
77
+
78
+ ```typescript
79
+ import Shadowly from 'shadowly';
80
+
81
+ const index = new Shadowly<typeof import("./array.json")>("./array.json")
82
+ .findIndex((value) => value.name === "apple");
83
+ ```
84
+
85
+ `find` 함수는 조건에 맞는 첫 번째 요소로 이동한 Shadowly 인스턴스를 반환합니다.
86
+
87
+ ```typescript
88
+ import Shadowly from 'shadowly';
89
+
90
+ const item = new Shadowly<typeof import("./array.json")>("./array.json")
91
+ .find((value) => value.name === "apple");
92
+
93
+ item?.get("price").value();
94
+ ```
95
+
96
+ `findAll` 함수는 조건에 맞는 모든 요소로 이동한 Shadowly 인스턴스 배열을 반환합니다.
97
+
98
+ ```typescript
99
+ import Shadowly from 'shadowly';
100
+
101
+ const items = new Shadowly<typeof import("./array.json")>("./array.json")
102
+ .findAll((value) => value.category === "fruit");
103
+
104
+ const prices = items.map((item) => item.get("price").value());
105
+ ```
106
+
74
107
  ### JSON에 값 쓰기
75
108
 
76
109
  `set` 함수는 특정 키에 씁니다.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,86 @@
1
- import Shadowly from "./class";
2
- export default Shadowly;
1
+ type GetKey<T> = T extends readonly any[] ? number : T extends object ? keyof T : never;
2
+ type ArrayElement<T> = T extends readonly (infer U)[] ? U : never;
3
+ type ArrayPredicate<T> = T extends readonly (infer U)[] ? (value: U, index: number, array: T) => unknown : never;
4
+ type GetValue<T, K> = T extends readonly (infer U)[] ? U : T extends Record<PropertyKey, any> ? (K extends keyof T ? T[K] : never) : never;
5
+ type RemoveKey<T> = T extends readonly any[] ? number : T extends object ? keyof T : never;
6
+ export default class Shadowly<TRoot, TNode = TRoot> {
7
+ readonly DB_PATH: string;
8
+ private readonly path;
9
+ private content;
10
+ private parent?;
11
+ private parentKey?;
12
+ /**
13
+ * Shadowly의 새 인스턴스를 만듭니다.
14
+ * @param path JSON 파일 경로
15
+ */
16
+ constructor(path: string);
17
+ /**
18
+ * 새 JSON을 만듭니다.
19
+ * @param path JSON 파일 경로
20
+ * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
21
+ */
22
+ static generateNewJson(path: string, isArray?: boolean): void;
23
+ private static create;
24
+ /**
25
+ * key로 이동합니다.
26
+ * @param key 이동할 키 이름
27
+ * @returns 작업 수행 후 Shadowly 인스턴스
28
+ */
29
+ get<K extends GetKey<TNode>>(key: K): Shadowly<TRoot, GetValue<TNode, K>>;
30
+ /**
31
+ * 현재 key의 value를 반환합니다.
32
+ * @returns 현재 key의 value
33
+ */
34
+ value(): TNode;
35
+ /**
36
+ * 배열에서 조건에 맞는 첫 번째 요소의 index를 반환합니다.
37
+ * @param predicate 찾을 조건
38
+ * @param thisArg callback의 this로 사용할 값
39
+ * @returns 찾은 요소의 index, 없으면 -1
40
+ */
41
+ findIndex(predicate: ArrayPredicate<TNode>, thisArg?: any): number;
42
+ /**
43
+ * 배열에서 조건에 맞는 첫 번째 요소로 이동한 Shadowly 인스턴스를 반환합니다.
44
+ * @param predicate 찾을 조건
45
+ * @param thisArg callback의 this로 사용할 값
46
+ * @returns 찾은 요소로 이동한 Shadowly 인스턴스, 없으면 undefined
47
+ */
48
+ find(predicate: ArrayPredicate<TNode>, thisArg?: any): Shadowly<TRoot, ArrayElement<TNode>> | undefined;
49
+ /**
50
+ * 배열에서 조건에 맞는 모든 요소로 이동한 Shadowly 인스턴스를 반환합니다.
51
+ * @param predicate 찾을 조건
52
+ * @param thisArg callback의 this로 사용할 값
53
+ * @returns 찾은 요소들로 이동한 Shadowly 인스턴스 배열
54
+ */
55
+ findAll(predicate: ArrayPredicate<TNode>, thisArg?: any): Shadowly<TRoot, ArrayElement<TNode>>[];
56
+ /**
57
+ * 현재 key에 값을 씁니다.
58
+ * @param value 쓸 값
59
+ * @returns 작업 수행 후 Shadowly 인스턴스
60
+ */
61
+ set(value: TNode): this;
62
+ /**
63
+ * key를 삭제합니다.
64
+ * @param key 삭제할 key
65
+ * @returns 작업 수행 후 Shadowly 인스턴스
66
+ */
67
+ remove<K extends RemoveKey<TNode>>(key: K): this;
68
+ /**
69
+ * 한 번 뒤로 이동합니다.
70
+ * @returns 작업 수행 후 Shadowly 인스턴스
71
+ */
72
+ back(): Shadowly<TRoot, any>;
73
+ /**
74
+ * steps만큼 뒤로 이동합니다.
75
+ * @param steps 이동할 횟수
76
+ * @returns 작업 수행 후 Shadowly 인스턴스
77
+ */
78
+ up(steps?: number): Shadowly<TRoot, any>;
79
+ /**
80
+ * JSON의 루트로 이동합니다.
81
+ * @returns 작업 수행 후 Shadowly 인스턴스
82
+ */
83
+ root(): Shadowly<TRoot, TRoot>;
84
+ private persist;
85
+ }
86
+ export {};
package/dist/index.js CHANGED
@@ -1,3 +1,224 @@
1
- import Shadowly from "./class";
2
- export default Shadowly;
3
- //# sourceMappingURL=index.js.map
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const fs = __importStar(require("node:fs"));
37
+ class ShadowlyError extends Error {
38
+ code;
39
+ constructor(code, message) {
40
+ super(code + ": " + message);
41
+ this.name = "Shadowly";
42
+ this.code = code;
43
+ Error.captureStackTrace(this, this.constructor);
44
+ }
45
+ }
46
+ function isValidJson(text) {
47
+ if (typeof text !== "string")
48
+ return false;
49
+ try {
50
+ JSON.parse(text);
51
+ return true;
52
+ }
53
+ catch {
54
+ return false;
55
+ }
56
+ }
57
+ class Shadowly {
58
+ DB_PATH;
59
+ path;
60
+ content;
61
+ parent;
62
+ parentKey;
63
+ /**
64
+ * Shadowly의 새 인스턴스를 만듭니다.
65
+ * @param path JSON 파일 경로
66
+ */
67
+ constructor(path) {
68
+ this.path = path;
69
+ this.DB_PATH = path;
70
+ if (!fs.existsSync(path))
71
+ throw new ShadowlyError("ENOTFOUND", path + " not found.");
72
+ const file = fs.readFileSync(path, "utf-8");
73
+ if (!isValidJson(file))
74
+ throw new ShadowlyError("EINVALID", path + " is not valid.");
75
+ this.content = JSON.parse(file);
76
+ }
77
+ /**
78
+ * 새 JSON을 만듭니다.
79
+ * @param path JSON 파일 경로
80
+ * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
81
+ */
82
+ static generateNewJson(path, isArray) {
83
+ fs.writeFileSync(path, isArray ? "[]" : "{}", "utf-8");
84
+ }
85
+ static create(DB_PATH, path, content, parent, parentKey) {
86
+ const inst = Object.create(Shadowly.prototype);
87
+ inst.DB_PATH = DB_PATH;
88
+ inst.path = path;
89
+ inst.content = content;
90
+ inst.parent = parent;
91
+ inst.parentKey = parentKey;
92
+ return inst;
93
+ }
94
+ /**
95
+ * key로 이동합니다.
96
+ * @param key 이동할 키 이름
97
+ * @returns 작업 수행 후 Shadowly 인스턴스
98
+ */
99
+ get(key) {
100
+ const nextContent = this.content[key];
101
+ return Shadowly.create(this.DB_PATH, this.path, nextContent, this, key);
102
+ }
103
+ /**
104
+ * 현재 key의 value를 반환합니다.
105
+ * @returns 현재 key의 value
106
+ */
107
+ value() {
108
+ return this.content;
109
+ }
110
+ /**
111
+ * 배열에서 조건에 맞는 첫 번째 요소의 index를 반환합니다.
112
+ * @param predicate 찾을 조건
113
+ * @param thisArg callback의 this로 사용할 값
114
+ * @returns 찾은 요소의 index, 없으면 -1
115
+ */
116
+ findIndex(predicate, thisArg) {
117
+ if (!Array.isArray(this.content))
118
+ throw new ShadowlyError("ETYPE", "Current value is not an array.");
119
+ const callback = predicate;
120
+ return this.content.findIndex((value, index) => callback.call(thisArg, value, index, this.content));
121
+ }
122
+ /**
123
+ * 배열에서 조건에 맞는 첫 번째 요소로 이동한 Shadowly 인스턴스를 반환합니다.
124
+ * @param predicate 찾을 조건
125
+ * @param thisArg callback의 this로 사용할 값
126
+ * @returns 찾은 요소로 이동한 Shadowly 인스턴스, 없으면 undefined
127
+ */
128
+ find(predicate, thisArg) {
129
+ const index = this.findIndex(predicate, thisArg);
130
+ if (index === -1)
131
+ return undefined;
132
+ return this.get(index);
133
+ }
134
+ /**
135
+ * 배열에서 조건에 맞는 모든 요소로 이동한 Shadowly 인스턴스를 반환합니다.
136
+ * @param predicate 찾을 조건
137
+ * @param thisArg callback의 this로 사용할 값
138
+ * @returns 찾은 요소들로 이동한 Shadowly 인스턴스 배열
139
+ */
140
+ findAll(predicate, thisArg) {
141
+ if (!Array.isArray(this.content))
142
+ throw new ShadowlyError("ETYPE", "Current value is not an array.");
143
+ const callback = predicate;
144
+ const array = this.content;
145
+ const result = [];
146
+ for (let index = 0; index < array.length; index++) {
147
+ const value = array[index];
148
+ if (!callback.call(thisArg, value, index, this.content))
149
+ continue;
150
+ result.push(this.get(index));
151
+ }
152
+ return result;
153
+ }
154
+ /**
155
+ * 현재 key에 값을 씁니다.
156
+ * @param value 쓸 값
157
+ * @returns 작업 수행 후 Shadowly 인스턴스
158
+ */
159
+ set(value) {
160
+ this.content = value;
161
+ if (this.parent && this.parentKey !== undefined) {
162
+ this.parent.content[this.parentKey] = value;
163
+ }
164
+ this.persist();
165
+ return this;
166
+ }
167
+ /**
168
+ * key를 삭제합니다.
169
+ * @param key 삭제할 key
170
+ * @returns 작업 수행 후 Shadowly 인스턴스
171
+ */
172
+ remove(key) {
173
+ const cur = this.content;
174
+ if (Array.isArray(cur)) {
175
+ const idx = key;
176
+ if (Number.isInteger(idx) && idx >= 0 && idx < cur.length)
177
+ cur.splice(idx, 1);
178
+ }
179
+ else if (cur && typeof cur === "object") {
180
+ delete cur[key];
181
+ }
182
+ else {
183
+ throw new ShadowlyError("ETYPE", "Current value is not an object/array.");
184
+ }
185
+ this.persist();
186
+ return this;
187
+ }
188
+ /**
189
+ * 한 번 뒤로 이동합니다.
190
+ * @returns 작업 수행 후 Shadowly 인스턴스
191
+ */
192
+ back() {
193
+ return this.parent ?? this;
194
+ }
195
+ /**
196
+ * steps만큼 뒤로 이동합니다.
197
+ * @param steps 이동할 횟수
198
+ * @returns 작업 수행 후 Shadowly 인스턴스
199
+ */
200
+ up(steps = 1) {
201
+ let cur = this;
202
+ for (let i = 0; i < steps; i++) {
203
+ if (!cur.parent)
204
+ break;
205
+ cur = cur.parent;
206
+ }
207
+ return cur;
208
+ }
209
+ /**
210
+ * JSON의 루트로 이동합니다.
211
+ * @returns 작업 수행 후 Shadowly 인스턴스
212
+ */
213
+ root() {
214
+ let cur = this;
215
+ while (cur.parent)
216
+ cur = cur.parent;
217
+ return cur;
218
+ }
219
+ persist() {
220
+ const r = this.root();
221
+ fs.writeFileSync(r.DB_PATH, JSON.stringify(r.content, null, 2), "utf-8");
222
+ }
223
+ }
224
+ exports.default = Shadowly;
package/dist/index.js.map CHANGED
@@ -1 +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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":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OAAO,GAAG,CAAC,MAAM;YAAE,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC;QACpC,OAAO,GAA6B,CAAC;IACzC,CAAC;IAEO,OAAO;QACX,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QACtB,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IAC7E,CAAC;CACJ"}
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "shadowly",
3
- "version": "1.0.5",
4
- "description": "TypeScript JSON Database",
5
- "keywords": [
6
- "database",
7
- "json",
8
- "typescript",
9
- "cli"
10
- ],
11
- "homepage": "https://github.com/Pro203S/Shadowly#readme",
12
- "bugs": {
13
- "url": "https://github.com/Pro203S/Shadowly/issues"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/Pro203S/Shadowly.git"
18
- },
19
- "license": "MIT",
20
- "author": "Pro203S",
21
- "type": "module",
22
- "main": "./dist/index.js",
23
- "scripts": {
24
- "build": "tsc"
25
- },
26
- "devDependencies": {
27
- "@types/bun": "latest"
28
- },
29
- "peerDependencies": {
30
- "typescript": "^5"
31
- }
32
- }
1
+ {
2
+ "name": "shadowly",
3
+ "version": "1.2.0",
4
+ "description": "TypeScript JSON Database",
5
+ "keywords": [
6
+ "database",
7
+ "json",
8
+ "typescript",
9
+ "cli"
10
+ ],
11
+ "homepage": "https://github.com/Pro203S/Shadowly#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/Pro203S/Shadowly/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/Pro203S/Shadowly.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Pro203S",
21
+ "type": "module",
22
+ "main": "./dist/index.js",
23
+ "scripts": {
24
+ "build": "tsc"
25
+ },
26
+ "devDependencies": {
27
+ "@types/bun": "latest"
28
+ },
29
+ "peerDependencies": {
30
+ "typescript": "^5"
31
+ }
32
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,273 @@
1
- import Shadowly from "./class";
2
-
3
- export default Shadowly;
1
+ import * as fs from "node:fs";
2
+
3
+ class ShadowlyError extends Error {
4
+ public readonly code!: string;
5
+
6
+ constructor(
7
+ code: string,
8
+ message: string
9
+ ) {
10
+ super(code + ": " + message);
11
+
12
+ this.name = "Shadowly";
13
+ this.code = code;
14
+
15
+ Error.captureStackTrace(this, this.constructor);
16
+ }
17
+ }
18
+
19
+ function isValidJson(text: string): boolean {
20
+ if (typeof text !== "string") return false;
21
+ try {
22
+ JSON.parse(text);
23
+ return true;
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ type GetKey<T> =
30
+ T extends readonly any[] ? number :
31
+ T extends object ? keyof T :
32
+ never;
33
+
34
+ type ArrayElement<T> =
35
+ T extends readonly (infer U)[] ? U :
36
+ never;
37
+
38
+ type ArrayPredicate<T> =
39
+ T extends readonly (infer U)[]
40
+ ? (value: U, index: number, array: T) => unknown
41
+ : never;
42
+
43
+ type GetValue<T, K> =
44
+ T extends readonly (infer U)[] ? U :
45
+ T extends Record<PropertyKey, any> ? (K extends keyof T ? T[K] : never) :
46
+ never;
47
+
48
+ type RemoveKey<T> =
49
+ T extends readonly any[] ? number :
50
+ T extends object ? keyof T :
51
+ never;
52
+
53
+ export default class Shadowly<TRoot, TNode = TRoot> {
54
+ public readonly DB_PATH: string;
55
+ private readonly path: string;
56
+
57
+ private content: TNode;
58
+
59
+ private parent?: Shadowly<TRoot, any>;
60
+ private parentKey?: PropertyKey;
61
+
62
+ /**
63
+ * Shadowly의 새 인스턴스를 만듭니다.
64
+ * @param path JSON 파일 경로
65
+ */
66
+ constructor(path: string) {
67
+ this.path = path;
68
+ this.DB_PATH = path;
69
+
70
+ if (!fs.existsSync(path))
71
+ throw new ShadowlyError("ENOTFOUND", path + " not found.");
72
+
73
+ const file = fs.readFileSync(path, "utf-8");
74
+ if (!isValidJson(file))
75
+ throw new ShadowlyError("EINVALID", path + " is not valid.");
76
+
77
+ this.content = JSON.parse(file) as any;
78
+ }
79
+
80
+ /**
81
+ * 새 JSON을 만듭니다.
82
+ * @param path JSON 파일 경로
83
+ * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
84
+ */
85
+ public static generateNewJson(path: string, isArray?: boolean) {
86
+ fs.writeFileSync(path, isArray ? "[]" : "{}", "utf-8");
87
+ }
88
+
89
+ private static create<TRoot, T>(
90
+ DB_PATH: string,
91
+ path: string,
92
+ content: T,
93
+ parent?: Shadowly<TRoot, any>,
94
+ parentKey?: PropertyKey
95
+ ): Shadowly<TRoot, T> {
96
+ const inst = Object.create(Shadowly.prototype) as Shadowly<TRoot, T>;
97
+ (inst as any).DB_PATH = DB_PATH;
98
+ (inst as any).path = path;
99
+ (inst as any).content = content;
100
+ (inst as any).parent = parent;
101
+ (inst as any).parentKey = parentKey;
102
+ return inst;
103
+ }
104
+
105
+ /**
106
+ * key로 이동합니다.
107
+ * @param key 이동할 키 이름
108
+ * @returns 작업 수행 후 Shadowly 인스턴스
109
+ */
110
+ get<K extends GetKey<TNode>>(key: K): Shadowly<TRoot, GetValue<TNode, K>> {
111
+ const nextContent = (this.content as any)[key];
112
+ return Shadowly.create<TRoot, GetValue<TNode, K>>(
113
+ this.DB_PATH,
114
+ this.path,
115
+ nextContent,
116
+ this,
117
+ key as any
118
+ );
119
+ }
120
+
121
+ /**
122
+ * 현재 key의 value를 반환합니다.
123
+ * @returns 현재 key의 value
124
+ */
125
+ value(): TNode {
126
+ return this.content;
127
+ }
128
+
129
+ /**
130
+ * 배열에서 조건에 맞는 첫 번째 요소의 index를 반환합니다.
131
+ * @param predicate 찾을 조건
132
+ * @param thisArg callback의 this로 사용할 값
133
+ * @returns 찾은 요소의 index, 없으면 -1
134
+ */
135
+ findIndex(predicate: ArrayPredicate<TNode>, thisArg?: any): number {
136
+ if (!Array.isArray(this.content))
137
+ throw new ShadowlyError("ETYPE", "Current value is not an array.");
138
+
139
+ const callback = predicate as (
140
+ value: ArrayElement<TNode>,
141
+ index: number,
142
+ array: TNode
143
+ ) => unknown;
144
+
145
+ return (this.content as ArrayElement<TNode>[]).findIndex((value, index) =>
146
+ callback.call(thisArg, value, index, this.content)
147
+ );
148
+ }
149
+
150
+ /**
151
+ * 배열에서 조건에 맞는 첫 번째 요소로 이동한 Shadowly 인스턴스를 반환합니다.
152
+ * @param predicate 찾을 조건
153
+ * @param thisArg callback의 this로 사용할 값
154
+ * @returns 찾은 요소로 이동한 Shadowly 인스턴스, 없으면 undefined
155
+ */
156
+ find(
157
+ predicate: ArrayPredicate<TNode>,
158
+ thisArg?: any
159
+ ): Shadowly<TRoot, ArrayElement<TNode>> | undefined {
160
+ const index = this.findIndex(predicate, thisArg);
161
+ if (index === -1) return undefined;
162
+
163
+ return this.get(index as GetKey<TNode>) as Shadowly<TRoot, ArrayElement<TNode>>;
164
+ }
165
+
166
+ /**
167
+ * 배열에서 조건에 맞는 모든 요소로 이동한 Shadowly 인스턴스를 반환합니다.
168
+ * @param predicate 찾을 조건
169
+ * @param thisArg callback의 this로 사용할 값
170
+ * @returns 찾은 요소들로 이동한 Shadowly 인스턴스 배열
171
+ */
172
+ findAll(
173
+ predicate: ArrayPredicate<TNode>,
174
+ thisArg?: any
175
+ ): Shadowly<TRoot, ArrayElement<TNode>>[] {
176
+ if (!Array.isArray(this.content))
177
+ throw new ShadowlyError("ETYPE", "Current value is not an array.");
178
+
179
+ const callback = predicate as (
180
+ value: ArrayElement<TNode>,
181
+ index: number,
182
+ array: TNode
183
+ ) => unknown;
184
+
185
+ const array = this.content as ArrayElement<TNode>[];
186
+ const result: Shadowly<TRoot, ArrayElement<TNode>>[] = [];
187
+
188
+ for (let index = 0; index < array.length; index++) {
189
+ const value = array[index];
190
+ if (!callback.call(thisArg, value, index, this.content)) continue;
191
+
192
+ result.push(
193
+ this.get(index as GetKey<TNode>) as Shadowly<TRoot, ArrayElement<TNode>>
194
+ );
195
+ }
196
+
197
+ return result;
198
+ }
199
+
200
+ /**
201
+ * 현재 key에 값을 씁니다.
202
+ * @param value 쓸 값
203
+ * @returns 작업 수행 후 Shadowly 인스턴스
204
+ */
205
+ set(value: TNode): this {
206
+ this.content = value;
207
+
208
+ if (this.parent && this.parentKey !== undefined) {
209
+ (this.parent.content as any)[this.parentKey] = value;
210
+ }
211
+
212
+ this.persist();
213
+ return this;
214
+ }
215
+
216
+ /**
217
+ * key를 삭제합니다.
218
+ * @param key 삭제할 key
219
+ * @returns 작업 수행 후 Shadowly 인스턴스
220
+ */
221
+ remove<K extends RemoveKey<TNode>>(key: K): this {
222
+ const cur: any = this.content;
223
+
224
+ if (Array.isArray(cur)) {
225
+ const idx = key as unknown as number;
226
+ if (Number.isInteger(idx) && idx >= 0 && idx < cur.length) cur.splice(idx, 1);
227
+ } else if (cur && typeof cur === "object") {
228
+ delete cur[key as any];
229
+ } else {
230
+ throw new ShadowlyError("ETYPE", "Current value is not an object/array.");
231
+ }
232
+
233
+ this.persist();
234
+ return this;
235
+ }
236
+
237
+ /**
238
+ * 한 번 뒤로 이동합니다.
239
+ * @returns 작업 수행 후 Shadowly 인스턴스
240
+ */
241
+ back(): Shadowly<TRoot, any> {
242
+ return this.parent ?? this;
243
+ }
244
+
245
+ /**
246
+ * steps만큼 뒤로 이동합니다.
247
+ * @param steps 이동할 횟수
248
+ * @returns 작업 수행 후 Shadowly 인스턴스
249
+ */
250
+ up(steps: number = 1): Shadowly<TRoot, any> {
251
+ let cur: Shadowly<TRoot, any> = this;
252
+ for (let i = 0; i < steps; i++) {
253
+ if (!cur.parent) break;
254
+ cur = cur.parent;
255
+ }
256
+ return cur;
257
+ }
258
+
259
+ /**
260
+ * JSON의 루트로 이동합니다.
261
+ * @returns 작업 수행 후 Shadowly 인스턴스
262
+ */
263
+ root(): Shadowly<TRoot, TRoot> {
264
+ let cur: Shadowly<TRoot, any> = this;
265
+ while (cur.parent) cur = cur.parent;
266
+ return cur as Shadowly<TRoot, TRoot>;
267
+ }
268
+
269
+ private persist(): void {
270
+ const r = this.root();
271
+ fs.writeFileSync(r.DB_PATH, JSON.stringify(r.content, null, 2), "utf-8");
272
+ }
273
+ }
package/tsconfig.json CHANGED
@@ -1,24 +1,16 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "dist",
4
- "target": "ES6",
5
- "module": "ESNext",
6
- "lib": [
7
- "ES6",
8
- "DOM"
9
- ],
10
- "moduleResolution": "Node",
11
- "esModuleInterop": true,
12
- "declaration": true,
13
- "strict": true,
14
- "sourceMap": true,
15
- "types": [
16
- "node"
17
- ],
18
- "rootDir": "src"
19
- },
20
- "include": [
21
- "src/**/*"
22
- ],
23
- "exclude": ["tests"]
24
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "CommonJS",
5
+ "rootDir": "src",
6
+ "outDir": "dist",
7
+ "declaration": true,
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "resolveJsonModule": true,
11
+ "types": [
12
+ "node"
13
+ ]
14
+ },
15
+ "exclude": ["tests/", "dist/"]
16
+ }
package/dist/class.d.ts DELETED
@@ -1,63 +0,0 @@
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 DELETED
@@ -1,133 +0,0 @@
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
package/dist/class.js.map DELETED
@@ -1 +0,0 @@
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,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AAC,GAAG,CAAC,MAAM;gBAAE,MAAM;YACvB,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC;QACrB,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;;OAGG;IACH,IAAI;QACA,IAAI,GAAG,GAAyB,IAAI,CAAC;QACrC,OAAO,GAAG,CAAC,MAAM;YAAE,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC;QACpC,OAAO,GAA6B,CAAC;IACzC,CAAC;IAEO,OAAO;QACX,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QACtB,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IAC7E,CAAC;CACJ"}
package/dist/error.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export declare class ShadowlyError extends Error {
2
- readonly code: string;
3
- constructor(code: string, message: string);
4
- }
package/dist/error.js DELETED
@@ -1,9 +0,0 @@
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
package/dist/error.js.map DELETED
@@ -1 +0,0 @@
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"}
package/src/class.ts DELETED
@@ -1,178 +0,0 @@
1
- import * as fs from "node:fs";
2
- import { ShadowlyError } from "./error";
3
-
4
- function isValidJson(text: string): boolean {
5
- if (typeof text !== "string") return false;
6
- try {
7
- JSON.parse(text);
8
- return true;
9
- } catch {
10
- return false;
11
- }
12
- }
13
-
14
- type GetKey<T> =
15
- T extends readonly any[] ? number :
16
- T extends object ? keyof T :
17
- never;
18
-
19
- type GetValue<T, K> =
20
- T extends readonly (infer U)[] ? U :
21
- T extends Record<PropertyKey, any> ? (K extends keyof T ? T[K] : never) :
22
- never;
23
-
24
- type RemoveKey<T> =
25
- T extends readonly any[] ? number :
26
- T extends object ? keyof T :
27
- never;
28
-
29
- export default class Shadowly<TRoot, TNode = TRoot> {
30
- public readonly DB_PATH: string;
31
- private readonly path: string;
32
-
33
- private content: TNode;
34
-
35
- private parent?: Shadowly<TRoot, any>;
36
- private parentKey?: PropertyKey;
37
-
38
- /**
39
- * Shadowly의 새 인스턴스를 만듭니다.
40
- * @param path JSON 파일 경로
41
- */
42
- constructor(path: string) {
43
- this.path = path;
44
- this.DB_PATH = path;
45
-
46
- if (!fs.existsSync(path))
47
- throw new ShadowlyError("ENOTFOUND", path + " not found.");
48
-
49
- const file = fs.readFileSync(path, "utf-8");
50
- if (!isValidJson(file))
51
- throw new ShadowlyError("EINVALID", path + " is not valid.");
52
-
53
- this.content = JSON.parse(file) as any;
54
- }
55
-
56
- /**
57
- * 새 JSON을 만듭니다.
58
- * @param path JSON 파일 경로
59
- * @param isArray true면 []를 쓰고, false면 {}를 씁니다.
60
- */
61
- public static generateNewJson(path: string, isArray?: boolean) {
62
- fs.writeFileSync(path, isArray ? "[]" : "{}", "utf-8");
63
- }
64
-
65
- private static create<TRoot, T>(
66
- DB_PATH: string,
67
- path: string,
68
- content: T,
69
- parent?: Shadowly<TRoot, any>,
70
- parentKey?: PropertyKey
71
- ): Shadowly<TRoot, T> {
72
- const inst = Object.create(Shadowly.prototype) as Shadowly<TRoot, T>;
73
- (inst as any).DB_PATH = DB_PATH;
74
- (inst as any).path = path;
75
- (inst as any).content = content;
76
- (inst as any).parent = parent;
77
- (inst as any).parentKey = parentKey;
78
- return inst;
79
- }
80
-
81
- /**
82
- * key로 이동합니다.
83
- * @param key 이동할 키 이름
84
- * @returns 작업 수행 후 Shadowly 인스턴스
85
- */
86
- get<K extends GetKey<TNode>>(key: K): Shadowly<TRoot, GetValue<TNode, K>> {
87
- const nextContent = (this.content as any)[key];
88
- return Shadowly.create<TRoot, GetValue<TNode, K>>(
89
- this.DB_PATH,
90
- this.path,
91
- nextContent,
92
- this,
93
- key as any
94
- );
95
- }
96
-
97
- /**
98
- * 현재 key의 value를 반환합니다.
99
- * @returns 현재 key의 value
100
- */
101
- value(): TNode {
102
- return this.content;
103
- }
104
-
105
- /**
106
- * 현재 key에 값을 씁니다.
107
- * @param value 쓸 값
108
- * @returns 작업 수행 후 Shadowly 인스턴스
109
- */
110
- set(value: TNode): this {
111
- this.content = value;
112
-
113
- if (this.parent && this.parentKey !== undefined) {
114
- (this.parent.content as any)[this.parentKey] = value;
115
- }
116
-
117
- this.persist();
118
- return this;
119
- }
120
-
121
- /**
122
- * key를 삭제합니다.
123
- * @param key 삭제할 key
124
- * @returns 작업 수행 후 Shadowly 인스턴스
125
- */
126
- remove<K extends RemoveKey<TNode>>(key: K): this {
127
- const cur: any = this.content;
128
-
129
- if (Array.isArray(cur)) {
130
- const idx = key as unknown as number;
131
- if (Number.isInteger(idx) && idx >= 0 && idx < cur.length) cur.splice(idx, 1);
132
- } else if (cur && typeof cur === "object") {
133
- delete cur[key as any];
134
- } else {
135
- throw new ShadowlyError("ETYPE", "Current value is not an object/array.");
136
- }
137
-
138
- this.persist();
139
- return this;
140
- }
141
-
142
- /**
143
- * 한 번 뒤로 이동합니다.
144
- * @returns 작업 수행 후 Shadowly 인스턴스
145
- */
146
- back(): Shadowly<TRoot, any> {
147
- return this.parent ?? this;
148
- }
149
-
150
- /**
151
- * steps만큼 뒤로 이동합니다.
152
- * @param steps 이동할 횟수
153
- * @returns 작업 수행 후 Shadowly 인스턴스
154
- */
155
- up(steps: number = 1): Shadowly<TRoot, any> {
156
- let cur: Shadowly<TRoot, any> = this;
157
- for (let i = 0; i < steps; i++) {
158
- if (!cur.parent) break;
159
- cur = cur.parent;
160
- }
161
- return cur;
162
- }
163
-
164
- /**
165
- * JSON의 루트로 이동합니다.
166
- * @returns 작업 수행 후 Shadowly 인스턴스
167
- */
168
- root(): Shadowly<TRoot, TRoot> {
169
- let cur: Shadowly<TRoot, any> = this;
170
- while (cur.parent) cur = cur.parent;
171
- return cur as Shadowly<TRoot, TRoot>;
172
- }
173
-
174
- private persist(): void {
175
- const r = this.root();
176
- fs.writeFileSync(r.DB_PATH, JSON.stringify(r.content, null, 2), "utf-8");
177
- }
178
- }
package/src/error.ts DELETED
@@ -1,15 +0,0 @@
1
- export class ShadowlyError extends Error {
2
- public readonly code!: string;
3
-
4
- constructor(
5
- code: string,
6
- message: string
7
- ) {
8
- super(code + ": " + message);
9
-
10
- this.name = "Shadowly";
11
- this.code = code;
12
-
13
- Error.captureStackTrace(this, this.constructor);
14
- }
15
- }