@wxn0brp/db-core 0.11.0-alpha.1 → 0.11.0-alpha.2

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.
@@ -1,6 +1,4 @@
1
- import { hasFieldsAdvanced } from "./utils/hasFieldsAdvanced.js";
2
- import { updateFindObject } from "./utils/updateFindObject.js";
3
- import { updateObjectAdvanced } from "./utils/updateObject.js";
1
+ import { match, findProcessLine, update } from "./utils/process.js";
4
2
  export function pathRepair(path) {
5
3
  return path.replaceAll("//", "/");
6
4
  }
@@ -20,31 +18,28 @@ export class CustomFileCpu {
20
18
  async find(file, config) {
21
19
  file = pathRepair(file);
22
20
  const entries = await this._readFile(file);
23
- const { search, context = {}, findOpts = {} } = config;
24
- const results = entries.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
25
- return results.length ?
26
- results.map(res => updateFindObject(res, findOpts)) :
27
- [];
21
+ return entries
22
+ .map((entry) => findProcessLine(config, entry))
23
+ .filter(Boolean);
28
24
  }
29
25
  async findOne(file, config) {
30
26
  file = pathRepair(file);
31
27
  const entries = await this._readFile(file);
32
- const { search, context = {}, findOpts = {} } = config;
33
- const result = entries.find(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
34
- return result ?
35
- updateFindObject(result, findOpts) :
36
- false;
28
+ for (const entry of entries) {
29
+ const result = findProcessLine(config, entry);
30
+ if (result)
31
+ return result;
32
+ }
33
+ return false;
37
34
  }
38
35
  async remove(file, config, one) {
39
36
  file = pathRepair(file);
40
37
  let entries = await this._readFile(file);
41
38
  const removed = [];
42
- const { search, context = {} } = config;
43
- entries = entries.filter(entry => {
39
+ entries = entries.filter((entry) => {
44
40
  if (removed.length && one)
45
41
  return true;
46
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
47
- if (match) {
42
+ if (match(config, entry)) {
48
43
  removed.push(entry);
49
44
  return false;
50
45
  }
@@ -58,13 +53,11 @@ export class CustomFileCpu {
58
53
  file = pathRepair(file);
59
54
  let entries = await this._readFile(file);
60
55
  const updated = [];
61
- const { search, updater, context = {} } = config;
62
- entries = entries.map(entry => {
56
+ entries = entries.map((entry) => {
63
57
  if (updated.length && one)
64
58
  return entry;
65
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
66
- if (match) {
67
- const updatedEntry = typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
59
+ if (match(config, entry)) {
60
+ const updatedEntry = update(config, entry);
68
61
  updated.push(updatedEntry);
69
62
  return updatedEntry;
70
63
  }
@@ -1,3 +1,4 @@
1
+ import { Data } from "./data.js";
1
2
  import { SearchOptions } from "./searchOpts.js";
2
3
  import { VContext } from "./types.js";
3
4
  import { UpdaterArg } from "./updater.js";
@@ -5,6 +6,6 @@ export type Arg<T = any> = {
5
6
  [K in keyof T]?: any;
6
7
  } & Record<string, any>;
7
8
  export type SearchFunc<T = any> = (data: T, context: VContext) => boolean;
8
- export type UpdaterFunc<T = any> = (data: T, context: VContext) => boolean;
9
+ export type UpdaterFunc<T = any> = (data: T, context: VContext) => Data | void;
9
10
  export type Search<T = any> = SearchOptions<T> | SearchFunc<T>;
10
- export type Updater<T = any> = UpdaterArg<T> | UpdaterArg<T>[] | UpdaterFunc<T>;
11
+ export type Updater<T = any> = UpdaterArg<T> | UpdaterFunc<T>;
@@ -0,0 +1,5 @@
1
+ import { Data } from "../types/data.js";
2
+ import { VQuery, VQueryT } from "../types/query.js";
3
+ export declare function findProcessLine(config: VQueryT.Find | VQueryT.FindOne, obj: Data): Object;
4
+ export declare function match(config: VQuery, obj: Data): boolean;
5
+ export declare function update(config: VQueryT.Update, obj: Data): Data;
@@ -0,0 +1,30 @@
1
+ import { hasFieldsAdvanced } from "./hasFieldsAdvanced.js";
2
+ import { updateFindObject } from "./updateFindObject.js";
3
+ import { updateObjectAdvanced } from "./updateObject.js";
4
+ export function findProcessLine(config, obj) {
5
+ if (match(config, obj))
6
+ return updateFindObject(obj, config.findOpts || {});
7
+ return null;
8
+ }
9
+ export function match(config, obj) {
10
+ const { search, context } = config;
11
+ if (search === undefined || search === null)
12
+ return true;
13
+ if (typeof search === "function" && search(obj, context))
14
+ return true;
15
+ if (typeof search === "object" && !Array.isArray(search) && hasFieldsAdvanced(obj, search))
16
+ return true;
17
+ return false;
18
+ }
19
+ export function update(config, obj) {
20
+ const { updater, context } = config;
21
+ if (typeof updater === "object" && !Array.isArray(updater))
22
+ return updateObjectAdvanced(obj, updater);
23
+ if (typeof updater === "function") {
24
+ const updateObjValue = updater(obj, context);
25
+ if (updateObjValue)
26
+ return updateObjValue;
27
+ return obj;
28
+ }
29
+ return obj;
30
+ }
@@ -5,12 +5,10 @@ import { _deepMerge } from "./merge.js";
5
5
  * @param field - An object containing new values to update in the target object.
6
6
  */
7
7
  export function updateObjectAdvanced(obj, field) {
8
- if (typeof field !== "object" || field === null) {
9
- throw new Error("Fields must be an object or object array");
10
- }
11
- if (Object.keys(field).length === 0) {
8
+ if (typeof field !== "object" || Array.isArray(field) || field === null)
9
+ throw new Error("Fields must be an object");
10
+ if (Object.keys(field).length === 0)
12
11
  return obj;
13
- }
14
12
  const newObj = structuredClone(obj);
15
13
  const $fields = {};
16
14
  const subsetFields = {};
@@ -106,11 +104,9 @@ function mainUpdate(obj, fields) {
106
104
  * @param newVal - An object containing new values to update in the target object.
107
105
  */
108
106
  function updateObject(obj, newVal) {
109
- for (let key in newVal) {
110
- if (newVal.hasOwnProperty(key)) {
107
+ for (let key in newVal)
108
+ if (newVal.hasOwnProperty(key))
111
109
  obj[key] = newVal[key];
112
- }
113
- }
114
110
  return obj;
115
111
  }
116
112
  function _for(fields, obj, opts) {
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.11.0-alpha.1";
1
+ export const version = "0.11.0-alpha.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.11.0-alpha.1",
3
+ "version": "0.11.0-alpha.2",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",