shadowly 1.0.6 → 1.2.1
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 +6 -6
- package/README.md +33 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +52 -4
- package/dist/index.js.map +1 -1
- package/package.json +40 -32
- package/src/index.ts +273 -193
- package/tsconfig.json +14 -21
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,4 +1,6 @@
|
|
|
1
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;
|
|
2
4
|
type GetValue<T, K> = T extends readonly (infer U)[] ? U : T extends Record<PropertyKey, any> ? (K extends keyof T ? T[K] : never) : never;
|
|
3
5
|
type RemoveKey<T> = T extends readonly any[] ? number : T extends object ? keyof T : never;
|
|
4
6
|
export default class Shadowly<TRoot, TNode = TRoot> {
|
|
@@ -30,6 +32,27 @@ export default class Shadowly<TRoot, TNode = TRoot> {
|
|
|
30
32
|
* @returns 현재 key의 value
|
|
31
33
|
*/
|
|
32
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>>[];
|
|
33
56
|
/**
|
|
34
57
|
* 현재 key에 값을 씁니다.
|
|
35
58
|
* @param value 쓸 값
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
class ShadowlyError extends Error {
|
|
3
|
+
code;
|
|
3
4
|
constructor(code, message) {
|
|
4
5
|
super(code + ": " + message);
|
|
5
6
|
this.name = "Shadowly";
|
|
@@ -14,11 +15,16 @@ function isValidJson(text) {
|
|
|
14
15
|
JSON.parse(text);
|
|
15
16
|
return true;
|
|
16
17
|
}
|
|
17
|
-
catch
|
|
18
|
+
catch {
|
|
18
19
|
return false;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
export default class Shadowly {
|
|
23
|
+
DB_PATH;
|
|
24
|
+
path;
|
|
25
|
+
content;
|
|
26
|
+
parent;
|
|
27
|
+
parentKey;
|
|
22
28
|
/**
|
|
23
29
|
* Shadowly의 새 인스턴스를 만듭니다.
|
|
24
30
|
* @param path JSON 파일 경로
|
|
@@ -66,6 +72,50 @@ export default class Shadowly {
|
|
|
66
72
|
value() {
|
|
67
73
|
return this.content;
|
|
68
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* 배열에서 조건에 맞는 첫 번째 요소의 index를 반환합니다.
|
|
77
|
+
* @param predicate 찾을 조건
|
|
78
|
+
* @param thisArg callback의 this로 사용할 값
|
|
79
|
+
* @returns 찾은 요소의 index, 없으면 -1
|
|
80
|
+
*/
|
|
81
|
+
findIndex(predicate, thisArg) {
|
|
82
|
+
if (!Array.isArray(this.content))
|
|
83
|
+
throw new ShadowlyError("ETYPE", "Current value is not an array.");
|
|
84
|
+
const callback = predicate;
|
|
85
|
+
return this.content.findIndex((value, index) => callback.call(thisArg, value, index, this.content));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 배열에서 조건에 맞는 첫 번째 요소로 이동한 Shadowly 인스턴스를 반환합니다.
|
|
89
|
+
* @param predicate 찾을 조건
|
|
90
|
+
* @param thisArg callback의 this로 사용할 값
|
|
91
|
+
* @returns 찾은 요소로 이동한 Shadowly 인스턴스, 없으면 undefined
|
|
92
|
+
*/
|
|
93
|
+
find(predicate, thisArg) {
|
|
94
|
+
const index = this.findIndex(predicate, thisArg);
|
|
95
|
+
if (index === -1)
|
|
96
|
+
return undefined;
|
|
97
|
+
return this.get(index);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 배열에서 조건에 맞는 모든 요소로 이동한 Shadowly 인스턴스를 반환합니다.
|
|
101
|
+
* @param predicate 찾을 조건
|
|
102
|
+
* @param thisArg callback의 this로 사용할 값
|
|
103
|
+
* @returns 찾은 요소들로 이동한 Shadowly 인스턴스 배열
|
|
104
|
+
*/
|
|
105
|
+
findAll(predicate, thisArg) {
|
|
106
|
+
if (!Array.isArray(this.content))
|
|
107
|
+
throw new ShadowlyError("ETYPE", "Current value is not an array.");
|
|
108
|
+
const callback = predicate;
|
|
109
|
+
const array = this.content;
|
|
110
|
+
const result = [];
|
|
111
|
+
for (let index = 0; index < array.length; index++) {
|
|
112
|
+
const value = array[index];
|
|
113
|
+
if (!callback.call(thisArg, value, index, this.content))
|
|
114
|
+
continue;
|
|
115
|
+
result.push(this.get(index));
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
69
119
|
/**
|
|
70
120
|
* 현재 key에 값을 씁니다.
|
|
71
121
|
* @param value 쓸 값
|
|
@@ -105,8 +155,7 @@ export default class Shadowly {
|
|
|
105
155
|
* @returns 작업 수행 후 Shadowly 인스턴스
|
|
106
156
|
*/
|
|
107
157
|
back() {
|
|
108
|
-
|
|
109
|
-
return (_a = this.parent) !== null && _a !== void 0 ? _a : this;
|
|
158
|
+
return this.parent ?? this;
|
|
110
159
|
}
|
|
111
160
|
/**
|
|
112
161
|
* steps만큼 뒤로 이동합니다.
|
|
@@ -137,4 +186,3 @@ export default class Shadowly {
|
|
|
137
186
|
fs.writeFileSync(r.DB_PATH, JSON.stringify(r.content, null, 2), "utf-8");
|
|
138
187
|
}
|
|
139
188
|
}
|
|
140
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAE9B,MAAM,aAAc,SAAQ,KAAK;IAG7B,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;AAED,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;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAE9B,MAAM,aAAc,SAAQ,KAAK;IAG7B,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;AAED,SAAS,WAAW,CAAC,IAAY;IA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
1
|
-
{
|
|
2
|
-
"name": "shadowly",
|
|
3
|
-
"version": "1.
|
|
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
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "shadowly",
|
|
3
|
+
"version": "1.2.1",
|
|
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
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/bun": "latest"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"typescript": "^5"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,193 +1,273 @@
|
|
|
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
|
|
35
|
-
T extends readonly (infer U)[] ? U :
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
* @returns
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
this.content
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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,17 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"rootDir": "src",
|
|
3
7
|
"outDir": "dist",
|
|
4
|
-
"target": "ES6",
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"lib": [
|
|
7
|
-
"ES6",
|
|
8
|
-
"DOM"
|
|
9
|
-
],
|
|
10
|
-
"moduleResolution": "Node",
|
|
11
|
-
"esModuleInterop": true,
|
|
12
8
|
"declaration": true,
|
|
13
|
-
"strict": true,
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
},
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
],
|
|
23
|
-
"exclude": ["tests"]
|
|
24
|
-
}
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"types": [
|
|
13
|
+
"node"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"exclude": ["tests/", "dist/"]
|
|
17
|
+
}
|