inibase 1.0.0-rc.4 → 1.0.0-rc.42

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/file.ts DELETED
@@ -1,327 +0,0 @@
1
- import { createWriteStream, unlinkSync, renameSync, existsSync } from "fs";
2
- import { open } from "fs/promises";
3
- import { parse } from "path";
4
- import { ComparisonOperator, FieldType } from ".";
5
- import Utils from "./utils";
6
-
7
- export const encodeFileName = (fileName: string, extension?: string) => {
8
- return (
9
- fileName.replaceAll("%", "%25").replaceAll("*", "%") +
10
- (extension ? `.${extension}` : "")
11
- );
12
- };
13
-
14
- export const decodeFileName = (fileName: string) => {
15
- return fileName.replaceAll("%", "*").replaceAll("*25", "%");
16
- };
17
-
18
- export const get = async (
19
- filePath: string,
20
- fieldType?: FieldType,
21
- lineNumbers?: number | number[]
22
- ) => {
23
- const file = await open(filePath);
24
- let lines: Record<
25
- number,
26
- | string
27
- | number
28
- | boolean
29
- | (string | number | boolean | (string | number | boolean)[] | null)[]
30
- | null
31
- > = {},
32
- lineCount = 0;
33
-
34
- if (!lineNumbers) {
35
- for await (const line of file.readLines())
36
- lineCount++, (lines[lineCount] = Utils.decode(line, fieldType));
37
- } else if (lineNumbers === -1) {
38
- let lastLine;
39
- for await (const line of file.readLines()) lineCount++, (lastLine = line);
40
- if (lastLine) lines = { [lineCount]: Utils.decode(lastLine, fieldType) };
41
- } else {
42
- let lineNumbersArray = [
43
- ...(Array.isArray(lineNumbers) ? lineNumbers : [lineNumbers]),
44
- ];
45
- for await (const line of file.readLines()) {
46
- lineCount++;
47
- if (!lineNumbersArray.includes(lineCount)) continue;
48
- const indexOfLineCount = lineNumbersArray.indexOf(lineCount);
49
- lines[lineCount] = Utils.decode(line, fieldType);
50
- lineNumbersArray[indexOfLineCount] = 0;
51
- if (!lineNumbersArray.filter((lineN) => lineN !== 0).length) break;
52
- }
53
- }
54
-
55
- return lines ?? null;
56
- };
57
-
58
- export const replace = async (
59
- filePath: string,
60
- replacements:
61
- | string
62
- | number
63
- | boolean
64
- | null
65
- | (string | number | boolean | null)[]
66
- | Record<
67
- number,
68
- string | boolean | number | null | (string | boolean | number | null)[]
69
- >
70
- ) => {
71
- if (existsSync(filePath)) {
72
- const file = await open(filePath, "w+"),
73
- writeStream = file.createWriteStream();
74
- if (typeof replacements === "object" && !Array.isArray(replacements)) {
75
- let lineCount = 0;
76
- for await (const line of file.readLines()) {
77
- lineCount++;
78
- writeStream.write(
79
- (lineCount in replacements
80
- ? Utils.encode(replacements[lineCount])
81
- : line) + "\n"
82
- );
83
- }
84
- } else
85
- for await (const _line of file.readLines())
86
- writeStream.write(Utils.encode(replacements) + "\n");
87
-
88
- writeStream.end();
89
- } else if (typeof replacements === "object" && !Array.isArray(replacements)) {
90
- const file = await open(filePath, "w"),
91
- writeStream = file.createWriteStream(),
92
- largestLinesNumbers =
93
- Math.max(...Object.keys(replacements).map(Number)) + 1;
94
- for (let lineCount = 1; lineCount < largestLinesNumbers; lineCount++) {
95
- writeStream.write(
96
- (lineCount in replacements
97
- ? Utils.encode(replacements[lineCount])
98
- : "") + "\n"
99
- );
100
- }
101
- writeStream.end();
102
- }
103
- };
104
-
105
- export const remove = async (
106
- filePath: string,
107
- linesToDelete: number | number[]
108
- ): Promise<void> => {
109
- let lineCount = 0;
110
-
111
- const tempFilePath = `${filePath}.tmp`,
112
- linesToDeleteArray = [
113
- ...(Array.isArray(linesToDelete) ? linesToDelete : [linesToDelete]),
114
- ],
115
- writeStream = createWriteStream(tempFilePath),
116
- file = await open(filePath);
117
-
118
- for await (const line of file.readLines()) {
119
- lineCount++;
120
- if (!linesToDeleteArray.includes(lineCount)) {
121
- writeStream.write(`${line}\n`);
122
- }
123
- }
124
- writeStream.end();
125
- writeStream.on("finish", () => {
126
- unlinkSync(filePath); // Remove the original file
127
- renameSync(tempFilePath, filePath); // Rename the temp file to the original file name
128
- });
129
- };
130
-
131
- export const count = async (filePath: string): Promise<number> => {
132
- let lineCount = 0;
133
-
134
- const file = await open(filePath);
135
-
136
- for await (const line of file.readLines()) lineCount++;
137
-
138
- return lineCount;
139
- };
140
-
141
- export const search = async (
142
- filePath: string,
143
- fieldType: FieldType,
144
- operator: ComparisonOperator | ComparisonOperator[],
145
- comparedAtValue:
146
- | string
147
- | number
148
- | boolean
149
- | null
150
- | (string | number | boolean | null)[],
151
- logicalOperator?: "and" | "or",
152
- limit?: number,
153
- offset?: number,
154
- readWholeFile?: boolean
155
- ): Promise<
156
- [
157
- Record<
158
- number,
159
- Record<
160
- string,
161
- string | number | boolean | (string | number | boolean | null)[] | null
162
- >
163
- > | null,
164
- number
165
- ]
166
- > => {
167
- const handleComparisonOperator = (
168
- operator: ComparisonOperator,
169
- value:
170
- | string
171
- | number
172
- | boolean
173
- | null
174
- | (string | number | boolean | null)[],
175
- comparedAtValue:
176
- | string
177
- | number
178
- | boolean
179
- | null
180
- | (string | number | boolean | null)[],
181
- fieldType: FieldType
182
- ): boolean => {
183
- // check if not array or object
184
- switch (operator) {
185
- case "=":
186
- switch (fieldType) {
187
- case "password":
188
- return typeof value === "string" &&
189
- typeof comparedAtValue === "string"
190
- ? Utils.comparePassword(value, comparedAtValue)
191
- : false;
192
- case "boolean":
193
- return Number(value) - Number(comparedAtValue) === 0;
194
- default:
195
- return value === comparedAtValue;
196
- }
197
- case "!=":
198
- return !handleComparisonOperator(
199
- "=",
200
- value,
201
- comparedAtValue,
202
- fieldType
203
- );
204
- case ">":
205
- return (
206
- value !== null && comparedAtValue !== null && value > comparedAtValue
207
- );
208
- case "<":
209
- return (
210
- value !== null && comparedAtValue !== null && value < comparedAtValue
211
- );
212
- case ">=":
213
- return (
214
- value !== null && comparedAtValue !== null && value >= comparedAtValue
215
- );
216
- case "<=":
217
- return (
218
- value !== null && comparedAtValue !== null && value <= comparedAtValue
219
- );
220
- case "[]":
221
- return (
222
- (Array.isArray(value) &&
223
- Array.isArray(comparedAtValue) &&
224
- value.some(comparedAtValue.includes)) ||
225
- (Array.isArray(value) &&
226
- !Array.isArray(comparedAtValue) &&
227
- value.includes(comparedAtValue)) ||
228
- (!Array.isArray(value) &&
229
- Array.isArray(comparedAtValue) &&
230
- comparedAtValue.includes(value))
231
- );
232
- case "![]":
233
- return !handleComparisonOperator(
234
- "[]",
235
- value,
236
- comparedAtValue,
237
- fieldType
238
- );
239
- case "*":
240
- return (
241
- value !== null &&
242
- comparedAtValue !== null &&
243
- new RegExp(
244
- `^${comparedAtValue.toString().replace(/%/g, ".*")}$`,
245
- "i"
246
- ).test(value.toString())
247
- );
248
- case "!*":
249
- return !handleComparisonOperator(
250
- "*",
251
- value,
252
- comparedAtValue,
253
- fieldType
254
- );
255
- default:
256
- throw new Error(operator);
257
- }
258
- };
259
-
260
- let RETURN: Record<
261
- number,
262
- Record<
263
- string,
264
- string | number | boolean | null | (string | number | boolean | null)[]
265
- >
266
- > = {},
267
- lineCount = 0,
268
- foundItems = 0;
269
-
270
- const file = await open(filePath),
271
- columnName = decodeFileName(parse(filePath).name);
272
-
273
- for await (const line of file.readLines()) {
274
- lineCount++;
275
- const decodedLine = Utils.decode(line, fieldType);
276
- if (
277
- (Array.isArray(operator) &&
278
- Array.isArray(comparedAtValue) &&
279
- ((logicalOperator &&
280
- logicalOperator === "or" &&
281
- operator.some((single_operator, index) =>
282
- handleComparisonOperator(
283
- single_operator,
284
- decodedLine,
285
- comparedAtValue[index],
286
- fieldType
287
- )
288
- )) ||
289
- operator.every((single_operator, index) =>
290
- handleComparisonOperator(
291
- single_operator,
292
- decodedLine,
293
- comparedAtValue[index],
294
- fieldType
295
- )
296
- ))) ||
297
- (!Array.isArray(operator) &&
298
- handleComparisonOperator(
299
- operator,
300
- decodedLine,
301
- comparedAtValue,
302
- fieldType
303
- ))
304
- ) {
305
- foundItems++;
306
- if (offset && foundItems < offset) continue;
307
- if (limit && foundItems > limit)
308
- if (readWholeFile) continue;
309
- else break;
310
- if (!RETURN[lineCount]) RETURN[lineCount] = {};
311
- RETURN[lineCount][columnName] = decodedLine;
312
- }
313
- }
314
- if (foundItems) {
315
- return [RETURN, readWholeFile ? foundItems : foundItems - 1];
316
- } else return [null, 0];
317
- };
318
-
319
- export default class File {
320
- static get = get;
321
- static count = count;
322
- static remove = remove;
323
- static search = search;
324
- static replace = replace;
325
- static encodeFileName = encodeFileName;
326
- static decodeFileName = decodeFileName;
327
- }