qweery 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cat++
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # qweery
2
+
3
+ A simple type-safe array query builder.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install qweery
9
+ ```
10
+
11
+ ## Highlights
12
+ - Typescript first design, ensuring type safety and autocompletion.
13
+ - ORM-like querying.
14
+ - Chainable API for building complex queries.
15
+
16
+ ## Usage
17
+
18
+ ```typescript
19
+ import { Qweery } from 'qweery';
20
+
21
+ const qweery = new Qweery([
22
+ { name: 'Alice', age: 30 },
23
+ { name: 'Bob', age: 25 },
24
+ { name: 'Charlie', age: 35 },
25
+ { name: 'David', age: 28 },
26
+ ]);
27
+ ```
28
+
29
+ ### Basic Queries
30
+
31
+ ```typescript
32
+ console.log(
33
+ qweery
34
+ // name == 'Alice' && age > 30
35
+ .where({
36
+ name: 'Alice',
37
+ age: { greaterThan: 30 }
38
+ })
39
+ .skip(0)
40
+ .take(10)
41
+ .toArray()
42
+ );
43
+
44
+ // Output: []
45
+ ```
46
+
47
+ ```typescript
48
+ console.log(
49
+ qweery
50
+ // name == 'Alice' || age > 30
51
+ .where({
52
+ $OR: [
53
+ { name: 'Alice' },
54
+ { age: { greaterThan: 30 } }
55
+ ]
56
+ })
57
+ .skip(0)
58
+ .take(10)
59
+ .toArray()
60
+ );
61
+
62
+ // Output: [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]
63
+ ```
64
+
65
+ ```typescript
66
+ console.log(
67
+ qweery
68
+ // name.includes('a') && (age > 28 || age < 28)
69
+ .where({
70
+ name: {
71
+ includes: 'a'
72
+ },
73
+ $OR: [
74
+ { age: { greaterThan: 28 } },
75
+ { age: { lessThan: 28 } }
76
+ ]
77
+ })
78
+ .skip(0)
79
+ .take(10)
80
+ .toArray()
81
+ );
82
+
83
+ // Output: [{ name: 'Charlie', age: 35 }]
84
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,117 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ //#region src/classes/Qweery.ts
4
+ var Qweery = class Qweery {
5
+ data;
6
+ constructor(data) {
7
+ this.data = Array.from(data);
8
+ }
9
+ query(options) {
10
+ let result = this.data.filter((_, index) => options.where ? this.isItemMatch(index, options.where) : true);
11
+ if (options.skip) result = result.slice(options.skip);
12
+ if (options.limit) result = result.slice(0, options.limit);
13
+ return result;
14
+ }
15
+ where(options) {
16
+ return new Qweery(this.data.filter((_, index) => this.isItemMatch(index, options)));
17
+ }
18
+ skip(count) {
19
+ return new Qweery(this.data.slice(count));
20
+ }
21
+ take(count) {
22
+ return new Qweery(this.data.slice(0, count));
23
+ }
24
+ count() {
25
+ return this.data.length;
26
+ }
27
+ first() {
28
+ return this.data[0];
29
+ }
30
+ last() {
31
+ return this.data[this.data.length - 1];
32
+ }
33
+ toArray() {
34
+ return this.data;
35
+ }
36
+ isItemMatch(index, options) {
37
+ const item = this.data.at(index);
38
+ if (!item) return false;
39
+ const keys = Object.keys(options);
40
+ for (const key of keys) {
41
+ if (key.toString().startsWith("$") || options[key] === void 0) continue;
42
+ let value;
43
+ if (typeof options[key] === "function") value = options[key](item[key], item, index, this.data);
44
+ else if (typeof options[key] === "object") if (typeof item[key] === "string") value = this.evaluateStringOperator(index, key, options[key]);
45
+ else if (typeof item[key] === "number" || typeof item[key] === "bigint" || item[key] instanceof Date) value = this.evaluateNumericalOperator(index, key, options[key]);
46
+ else if (Array.isArray(item[key])) value = this.evaluateArrayOperator(index, key, options[key]);
47
+ else value = this.evaluateValueOperator(index, key, options[key]);
48
+ else value = item[key] === options[key];
49
+ if (!value) return false;
50
+ }
51
+ return this.evaluateLogicalOperator(index, options);
52
+ }
53
+ evaluateLogicalOperator(index, options) {
54
+ if (options.$NOT) {
55
+ for (const option of Array.isArray(options.$NOT) ? options.$NOT : [options.$NOT]) if (this.isItemMatch(index, option)) return false;
56
+ }
57
+ if (options.$AND) {
58
+ for (const option of Array.isArray(options.$AND) ? options.$AND : [options.$AND]) if (!this.isItemMatch(index, option)) return false;
59
+ }
60
+ if (options.$OR) {
61
+ let hasMatch = false;
62
+ for (const option of Array.isArray(options.$OR) ? options.$OR : [options.$OR]) if (this.isItemMatch(index, option)) {
63
+ hasMatch = true;
64
+ break;
65
+ }
66
+ if (!hasMatch) return false;
67
+ }
68
+ return true;
69
+ }
70
+ evaluateValueOperator(index, property, operator) {
71
+ if ("equals" in operator && operator.equals !== this.data[index][property]) return false;
72
+ if ("notEquals" in operator && operator.notEquals === this.data[index][property]) return false;
73
+ return this.evaluateLogicalOperator(index, operator);
74
+ }
75
+ evaluateNumericalOperator(index, property, operator) {
76
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
77
+ const value = Qweery.normalizeNumericalValue(this.data[index][property]);
78
+ if (operator.greaterThan !== void 0 && !(value > Qweery.normalizeNumericalValue(operator.greaterThan))) return false;
79
+ if (operator.greaterThanOrEqual !== void 0 && !(value >= Qweery.normalizeNumericalValue(operator.greaterThanOrEqual))) return false;
80
+ if (operator.lessThan !== void 0 && !(value < Qweery.normalizeNumericalValue(operator.lessThan))) return false;
81
+ if (operator.lessThanOrEqual !== void 0 && !(value <= Qweery.normalizeNumericalValue(operator.lessThanOrEqual))) return false;
82
+ return true;
83
+ }
84
+ evaluateStringOperator(index, property, operator) {
85
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
86
+ let value = this.data[index][property];
87
+ if (operator.caseInsensitive) value = value.toLowerCase();
88
+ if (operator.includes && !value.includes(operator.caseInsensitive ? operator.includes.toLowerCase() : operator.includes)) return false;
89
+ if (operator.startsWith && !value.startsWith(operator.caseInsensitive ? operator.startsWith.toLowerCase() : operator.startsWith)) return false;
90
+ if (operator.endsWith && !value.endsWith(operator.caseInsensitive ? operator.endsWith.toLowerCase() : operator.endsWith)) return false;
91
+ if (operator.matches && !operator.matches.test(value)) return false;
92
+ return true;
93
+ }
94
+ evaluateArrayOperator(index, property, operator) {
95
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
96
+ const value = this.data[index][property];
97
+ if ("includesEvery" in operator && operator.includesEvery?.every((v) => value.includes(v))) return true;
98
+ if ("includesSome" in operator && operator.includesSome?.some((v) => value.includes(v))) return true;
99
+ if ("includesNone" in operator && operator.includesNone?.every((v) => !value.includes(v))) return true;
100
+ return true;
101
+ }
102
+ };
103
+ (function(_Qweery) {
104
+ function normalizeNumericalValue(value) {
105
+ return value instanceof Date ? value.getTime() : value;
106
+ }
107
+ _Qweery.normalizeNumericalValue = normalizeNumericalValue;
108
+ })(Qweery || (Qweery = {}));
109
+
110
+ //#endregion
111
+ Object.defineProperty(exports, 'Qweery', {
112
+ enumerable: true,
113
+ get: function () {
114
+ return Qweery;
115
+ }
116
+ });
117
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../src/classes/Qweery.ts"],"sourcesContent":["import type { ValueOrArray } from '../helpers/types.js';\n\nexport class Qweery<T extends Qweery.Object> {\n public readonly data: T[];\n\n constructor(data: Iterable<T>) {\n this.data = Array.from(data);\n }\n\n public query(options: {\n where?: Qweery.WhereOptions<T>;\n skip?: number;\n limit?: number;\n }): T[] {\n let result = this.data.filter((_, index) => options.where ? this.isItemMatch(index, options.where) : true);\n\n if (options.skip) {\n result = result.slice(options.skip);\n }\n\n if (options.limit) {\n result = result.slice(0, options.limit);\n }\n\n return result;\n }\n\n public where(options: Qweery.WhereOptions<T>): Qweery<T> {\n return new Qweery(this.data.filter((_, index) => this.isItemMatch(index, options)));\n }\n\n public skip(count: number): Qweery<T> {\n return new Qweery(this.data.slice(count));\n }\n\n public take(count: number): Qweery<T> {\n return new Qweery(this.data.slice(0, count));\n }\n\n public count(): number {\n return this.data.length;\n }\n\n public first(): T|undefined {\n return this.data[0];\n }\n\n public last(): T|undefined {\n return this.data[this.data.length - 1];\n }\n\n public toArray(): T[] {\n return this.data;\n }\n\n private isItemMatch(index: number, options: Qweery.WhereOptions<T>): boolean {\n const item = this.data.at(index);\n if (!item) return false\n\n const keys = Object.keys(options) as (keyof T)[];\n\n for (const key of keys) {\n if (key.toString().startsWith('$') || options[key] === undefined) continue;\n\n let value: boolean;\n\n if (typeof options[key] === 'function') {\n value = options[key](item[key], item, index, this.data);\n } else if (typeof options[key] === 'object') {\n if (typeof item[key] === 'string') {\n value = this.evaluateStringOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionStringOperators<T, Qweery.StringPrimitive>\n );\n } else if (\n typeof item[key] === 'number' ||\n typeof item[key] === 'bigint' ||\n item[key] instanceof Date\n ) {\n value = this.evaluateNumericalOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionNumericalOperators<T, Qweery.NumericalPrimitive>\n );\n } else if (Array.isArray(item[key])) {\n value = this.evaluateArrayOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionArrayOperators<T, Qweery.ArrayPrimitive>\n );\n } else {\n value = this.evaluateValueOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionValueOperators<T, any>\n );\n }\n } else {\n value = item[key] === options[key];\n }\n\n if (!value) return false;\n }\n\n return this.evaluateLogicalOperator(index, options);\n }\n\n private evaluateLogicalOperator(index: number, options: Qweery.WhereOptionLogicalOperators<T>): boolean {\n let value = true;\n\n if (options.$NOT) {\n for (const option of Array.isArray(options.$NOT) ? options.$NOT : [options.$NOT]) {\n if (this.isItemMatch(index, option)) return false;\n }\n }\n\n if (options.$AND) {\n for (const option of Array.isArray(options.$AND) ? options.$AND : [options.$AND]) {\n if (!this.isItemMatch(index, option)) return false;\n }\n }\n\n if (options.$OR) {\n let hasMatch = false;\n\n for (const option of Array.isArray( options.$OR) ? options.$OR : [ options.$OR]) {\n if (this.isItemMatch(index, option)) {\n hasMatch = true;\n break;\n }\n }\n\n if (!hasMatch) return false;\n }\n\n return true;\n }\n\n private evaluateValueOperator(index: number, property: keyof T, operator: Qweery.WhereOptionValueOperators<T, any>): boolean {\n if ('equals' in operator && operator.equals !== this.data[index][property]) {\n return false;\n }\n\n if ('notEquals' in operator && operator.notEquals === this.data[index][property]) {\n return false;\n }\n\n return true && this.evaluateLogicalOperator(index, operator);\n }\n\n private evaluateNumericalOperator(index: number, property: keyof T, operator: Qweery.WhereOptionNumericalOperators<T, Qweery.NumericalPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n const value = Qweery.normalizeNumericalValue(this.data[index][property] as Qweery.NumericalPrimitive);\n\n if (operator.greaterThan !== undefined && !(value > Qweery.normalizeNumericalValue(operator.greaterThan))) {\n return false;\n }\n\n if (operator.greaterThanOrEqual !== undefined && !(value >= Qweery.normalizeNumericalValue(operator.greaterThanOrEqual))) {\n return false;\n }\n\n if (operator.lessThan !== undefined && !(value < Qweery.normalizeNumericalValue(operator.lessThan))) {\n return false;\n }\n\n if (operator.lessThanOrEqual !== undefined && !(value <= Qweery.normalizeNumericalValue(operator.lessThanOrEqual))) {\n return false;\n }\n\n return true;\n }\n\n private evaluateStringOperator(index: number, property: keyof T, operator: Qweery.WhereOptionStringOperators<T, Qweery.StringPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n let value = this.data[index][property] as Qweery.StringPrimitive;\n\n if (operator.caseInsensitive) {\n value = value.toLowerCase();\n }\n\n if (\n operator.includes &&\n !value.includes(\n operator.caseInsensitive\n ? operator.includes.toLowerCase()\n : operator.includes\n )\n ) {\n return false;\n }\n\n if (\n operator.startsWith && \n !value.startsWith(\n operator.caseInsensitive\n ? operator.startsWith.toLowerCase()\n : operator.startsWith\n )\n ) {\n return false;\n }\n\n if (\n operator.endsWith &&\n !value.endsWith(\n operator.caseInsensitive\n ? operator.endsWith.toLowerCase()\n : operator.endsWith\n )\n ) {\n return false;\n }\n\n if (operator.matches && !operator.matches.test(value)) {\n return false;\n }\n\n return true;\n }\n\n private evaluateArrayOperator(index: number, property: keyof T, operator: Qweery.WhereOptionArrayOperators<T, Qweery.ArrayPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n const value = this.data[index][property] as Qweery.ArrayPrimitive;\n\n if ('includesEvery' in operator && operator.includesEvery?.every(v => value.includes(v))) {\n return true;\n }\n\n if ('includesSome' in operator && operator.includesSome?.some(v => value.includes(v))) {\n return true;\n }\n\n if ('includesNone' in operator && operator.includesNone?.every(v => !value.includes(v))) {\n return true;\n }\n\n return true;\n }\n}\n\nexport namespace Qweery {\n export type Object = object;\n export type StringPrimitive = string;\n export type NumericalPrimitive = number|bigint|Date;\n export type ArrayPrimitive = Array<any>|ReadonlyArray<any>;\n\n export type WhereOptionKeys<T extends Object> = {\n [K in keyof T]?:\n |T[K]\n |((value: T[K], object: T, index: number, array: T[]) => boolean)\n |WhereOptionValueOperatorsType<T, T[K]>;\n }\n\n export type WhereOptionValueOperatorsType<T extends Object, V> = V extends StringPrimitive\n ? WhereOptionStringOperators<T, V>\n : V extends NumericalPrimitive\n ? WhereOptionNumericalOperators<T, V>\n : V extends ArrayPrimitive\n ? WhereOptionArrayOperators<T, V>\n : WhereOptionValueOperators<T, V>\n\n export interface WhereOptionLogicalOperators<T extends Object> {\n $AND?: ValueOrArray<WhereOptions<T>>;\n $OR?: ValueOrArray<WhereOptions<T>>;\n $NOT?: ValueOrArray<WhereOptions<T>>;\n }\n\n export interface WhereOptionValueOperators<T extends Object, V> extends WhereOptionLogicalOperators<T> {\n equals?: V;\n notEquals?: V;\n }\n\n export interface WhereOptionNumericalOperators<T extends Object, V extends NumericalPrimitive> extends WhereOptionValueOperators<T, V> {\n greaterThan?: V;\n greaterThanOrEqual?: V;\n lessThan?: V;\n lessThanOrEqual?: V;\n }\n\n export interface WhereOptionStringOperators<T extends Object, V extends StringPrimitive> extends WhereOptionValueOperators<T, V> {\n includes?: V;\n startsWith?: V;\n endsWith?: V;\n caseInsensitive?: boolean;\n matches?: RegExp;\n }\n\n export interface WhereOptionArrayOperators<T extends Object, V extends ArrayPrimitive> extends WhereOptionValueOperators<T, V> {\n includesEvery?: V[number][];\n includesSome?: V[number][];\n includesNone?: V[number][];\n }\n\n export type WhereOptions<T extends Object> = WhereOptionKeys<T> & WhereOptionLogicalOperators<T>;\n\n export function normalizeNumericalValue(value: Qweery.NumericalPrimitive): number|bigint {\n return value instanceof Date ? value.getTime() : value;\n }\n}"],"mappings":";;;AAEA,IAAa,SAAb,MAAa,OAAgC;CACzC,AAAgB;CAEhB,YAAY,MAAmB;AAC3B,OAAK,OAAO,MAAM,KAAK,KAAK;;CAGhC,AAAO,MAAM,SAIL;EACJ,IAAI,SAAS,KAAK,KAAK,QAAQ,GAAG,UAAU,QAAQ,QAAQ,KAAK,YAAY,OAAO,QAAQ,MAAM,GAAG,KAAK;AAE1G,MAAI,QAAQ,KACR,UAAS,OAAO,MAAM,QAAQ,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SAAS,wBAAwB,OAAiD;AACrF,SAAO,iBAAiB,OAAO,MAAM,SAAS,GAAG"}
@@ -0,0 +1,66 @@
1
+ //#region src/helpers/types.d.ts
2
+ type ValueOrArray<T> = T | T[];
3
+ //#endregion
4
+ //#region src/classes/Qweery.d.ts
5
+ declare class Qweery<T extends Qweery.Object> {
6
+ readonly data: T[];
7
+ constructor(data: Iterable<T>);
8
+ query(options: {
9
+ where?: Qweery.WhereOptions<T>;
10
+ skip?: number;
11
+ limit?: number;
12
+ }): T[];
13
+ where(options: Qweery.WhereOptions<T>): Qweery<T>;
14
+ skip(count: number): Qweery<T>;
15
+ take(count: number): Qweery<T>;
16
+ count(): number;
17
+ first(): T | undefined;
18
+ last(): T | undefined;
19
+ toArray(): T[];
20
+ private isItemMatch;
21
+ private evaluateLogicalOperator;
22
+ private evaluateValueOperator;
23
+ private evaluateNumericalOperator;
24
+ private evaluateStringOperator;
25
+ private evaluateArrayOperator;
26
+ }
27
+ declare namespace Qweery {
28
+ type Object = object;
29
+ type StringPrimitive = string;
30
+ type NumericalPrimitive = number | bigint | Date;
31
+ type ArrayPrimitive = Array<any> | ReadonlyArray<any>;
32
+ type WhereOptionKeys<T extends Object> = { [K in keyof T]?: T[K] | ((value: T[K], object: T, index: number, array: T[]) => boolean) | WhereOptionValueOperatorsType<T, T[K]> };
33
+ type WhereOptionValueOperatorsType<T extends Object, V> = V extends StringPrimitive ? WhereOptionStringOperators<T, V> : V extends NumericalPrimitive ? WhereOptionNumericalOperators<T, V> : V extends ArrayPrimitive ? WhereOptionArrayOperators<T, V> : WhereOptionValueOperators<T, V>;
34
+ interface WhereOptionLogicalOperators<T extends Object> {
35
+ $AND?: ValueOrArray<WhereOptions<T>>;
36
+ $OR?: ValueOrArray<WhereOptions<T>>;
37
+ $NOT?: ValueOrArray<WhereOptions<T>>;
38
+ }
39
+ interface WhereOptionValueOperators<T extends Object, V> extends WhereOptionLogicalOperators<T> {
40
+ equals?: V;
41
+ notEquals?: V;
42
+ }
43
+ interface WhereOptionNumericalOperators<T extends Object, V extends NumericalPrimitive> extends WhereOptionValueOperators<T, V> {
44
+ greaterThan?: V;
45
+ greaterThanOrEqual?: V;
46
+ lessThan?: V;
47
+ lessThanOrEqual?: V;
48
+ }
49
+ interface WhereOptionStringOperators<T extends Object, V extends StringPrimitive> extends WhereOptionValueOperators<T, V> {
50
+ includes?: V;
51
+ startsWith?: V;
52
+ endsWith?: V;
53
+ caseInsensitive?: boolean;
54
+ matches?: RegExp;
55
+ }
56
+ interface WhereOptionArrayOperators<T extends Object, V extends ArrayPrimitive> extends WhereOptionValueOperators<T, V> {
57
+ includesEvery?: V[number][];
58
+ includesSome?: V[number][];
59
+ includesNone?: V[number][];
60
+ }
61
+ type WhereOptions<T extends Object> = WhereOptionKeys<T> & WhereOptionLogicalOperators<T>;
62
+ function normalizeNumericalValue(value: Qweery.NumericalPrimitive): number | bigint;
63
+ }
64
+ //#endregion
65
+ export { Qweery, ValueOrArray };
66
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/helpers/types.ts","../src/classes/Qweery.ts"],"mappings":";KAAY,YAAA,MAAkB,CAAA,GAAE,CAAA;;;cCEnB,MAAA,WAAiB,MAAA,CAAO,MAAA;EAAA,SACjB,IAAA,EAAM,CAAA;cAEV,IAAA,EAAM,QAAA,CAAS,CAAA;EAIpB,KAAA,CAAM,OAAA;IACT,KAAA,GAAQ,MAAA,CAAO,YAAA,CAAa,CAAA;IAC5B,IAAA;IACA,KAAA;EAAA,IACA,CAAA;EAcG,KAAA,CAAM,OAAA,EAAS,MAAA,CAAO,YAAA,CAAa,CAAA,IAAK,MAAA,CAAO,CAAA;EAI/C,IAAA,CAAK,KAAA,WAAgB,MAAA,CAAO,CAAA;EAI5B,IAAA,CAAK,KAAA,WAAgB,MAAA,CAAO,CAAA;EAI5B,KAAA,CAAA;EAIA,KAAA,CAAA,GAAS,CAAA;EAIT,IAAA,CAAA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AA;IACX,eAAA,GAAkB,CAAA;EAAA;EAAA,UAGL,0BAAA,WAAqC,MAAA,YAAkB,eAAA,UAAyB,yBAAA,CAA0B,CAAA,EAAG,CAAA;IAC1H,QAAA,GAAW,CAAA;IACX,UAAA,GAAa,CAAA;IACb,QAAA,GAAW,CAAA;IACX,eAAA;IACA,OAAA,GAAU,MAAA;EAAA;EAAA,UAGG,yBAAA,WAAoC,MAAA,YAAkB,cAAA,UAAwB,yBAAA,CAA0B,CAAA,EAAG,CAAA;IACxH,aAAA,GAAgB,CAAA;IAChB,YAAA,GAAe,CAAA;IACf,YAAA,GAAe,CAAA;EAAA;EAAA,KAGP,YAAA,WAAuB,MAAA,IAAU,eAAA,CAAgB,CAAA,IAAK,2BAAA,CAA4B,CAAA;EAAA,SAE9E,uBAAA,CAAwB,KAAA,EAAO,MAAA,CAAO,kBAAA;AAAA"}
@@ -0,0 +1,66 @@
1
+ //#region src/helpers/types.d.ts
2
+ type ValueOrArray<T> = T | T[];
3
+ //#endregion
4
+ //#region src/classes/Qweery.d.ts
5
+ declare class Qweery<T extends Qweery.Object> {
6
+ readonly data: T[];
7
+ constructor(data: Iterable<T>);
8
+ query(options: {
9
+ where?: Qweery.WhereOptions<T>;
10
+ skip?: number;
11
+ limit?: number;
12
+ }): T[];
13
+ where(options: Qweery.WhereOptions<T>): Qweery<T>;
14
+ skip(count: number): Qweery<T>;
15
+ take(count: number): Qweery<T>;
16
+ count(): number;
17
+ first(): T | undefined;
18
+ last(): T | undefined;
19
+ toArray(): T[];
20
+ private isItemMatch;
21
+ private evaluateLogicalOperator;
22
+ private evaluateValueOperator;
23
+ private evaluateNumericalOperator;
24
+ private evaluateStringOperator;
25
+ private evaluateArrayOperator;
26
+ }
27
+ declare namespace Qweery {
28
+ type Object = object;
29
+ type StringPrimitive = string;
30
+ type NumericalPrimitive = number | bigint | Date;
31
+ type ArrayPrimitive = Array<any> | ReadonlyArray<any>;
32
+ type WhereOptionKeys<T extends Object> = { [K in keyof T]?: T[K] | ((value: T[K], object: T, index: number, array: T[]) => boolean) | WhereOptionValueOperatorsType<T, T[K]> };
33
+ type WhereOptionValueOperatorsType<T extends Object, V> = V extends StringPrimitive ? WhereOptionStringOperators<T, V> : V extends NumericalPrimitive ? WhereOptionNumericalOperators<T, V> : V extends ArrayPrimitive ? WhereOptionArrayOperators<T, V> : WhereOptionValueOperators<T, V>;
34
+ interface WhereOptionLogicalOperators<T extends Object> {
35
+ $AND?: ValueOrArray<WhereOptions<T>>;
36
+ $OR?: ValueOrArray<WhereOptions<T>>;
37
+ $NOT?: ValueOrArray<WhereOptions<T>>;
38
+ }
39
+ interface WhereOptionValueOperators<T extends Object, V> extends WhereOptionLogicalOperators<T> {
40
+ equals?: V;
41
+ notEquals?: V;
42
+ }
43
+ interface WhereOptionNumericalOperators<T extends Object, V extends NumericalPrimitive> extends WhereOptionValueOperators<T, V> {
44
+ greaterThan?: V;
45
+ greaterThanOrEqual?: V;
46
+ lessThan?: V;
47
+ lessThanOrEqual?: V;
48
+ }
49
+ interface WhereOptionStringOperators<T extends Object, V extends StringPrimitive> extends WhereOptionValueOperators<T, V> {
50
+ includes?: V;
51
+ startsWith?: V;
52
+ endsWith?: V;
53
+ caseInsensitive?: boolean;
54
+ matches?: RegExp;
55
+ }
56
+ interface WhereOptionArrayOperators<T extends Object, V extends ArrayPrimitive> extends WhereOptionValueOperators<T, V> {
57
+ includesEvery?: V[number][];
58
+ includesSome?: V[number][];
59
+ includesNone?: V[number][];
60
+ }
61
+ type WhereOptions<T extends Object> = WhereOptionKeys<T> & WhereOptionLogicalOperators<T>;
62
+ function normalizeNumericalValue(value: Qweery.NumericalPrimitive): number | bigint;
63
+ }
64
+ //#endregion
65
+ export { Qweery, ValueOrArray };
66
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/helpers/types.ts","../src/classes/Qweery.ts"],"mappings":";KAAY,YAAA,MAAkB,CAAA,GAAE,CAA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B,CAAA,EAAG,CAAA;EAAA,UAE1B,2BAAA,WAAsC,MAAA;IACnD,IAAA,GAAO,YAAA,CAAa,YAAA,CAAa,CAAA;IACjC,GAAA,GAAM,YAAA,CAAa,YAAA,CAAa,CAAA;IAChC,IAAA,GAAO,YAAA,CAAa,YAAA,CAAa,CAAA;EAAA;EAAA,UAGpB,yBAAA,WAAoC,MAAA,aAAmB,2BAAA,CAA4B,CAAA;IAChG,MAAA,GAAS,CAAA;IACT,SAAA,GAAY,CAAA;EAAA;EAAA,UAGC,6BAAA,WAAwC,MAAA,YAAkB,kBAAA,UAA4B,yBAAA,CAA0B,CAAA,EAAG,CAAA;IAChI,WAAA,GAAc,CAAA;IACd,kBAAA,GAAqB,CAAA;IACrB,QAAA,GAAW,CAAA;IACX,eAAA,GAAkB,CAAA;EAAA;EAAA,UAGL,0BAAA,WAAqC,MAAA,YAAkB,eAAA,UAAyB,yBAAA,CAA0B,CAAA,EAAG,CAAA;IAC1H,QAAA,GAAW,CAAA;IACX,UAAA,GAAa,CAAA;IACb,QAAA,GAAW,CAAA;IACX,eAAA;IACA,OAAA,GAAU,MAAA;EAAA;EAAA,UAGG,yBAAA,WAAoC,MAAA,YAAkB,cAAA,UAAwB,yBAAA,CAA0B,CAAA,EAAG,CAAA;IACxH,aAAA,GAAgB,CAAA;IAChB,YAAA,GAAe,CAAA;IACf,YAAA,GAAe,CAAA;EAAA;EAAA,KAGP,YAAA,WAAuB,MAAA,IAAU,eAAA,CAAgB,CAAA,IAAK,2BAAA,CAA4B,CAAA;EAAA,SAE9E,uBAAA,CAAwB,KAAA,EAAO,MAAA,CAAO,kBAAA;AAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,110 @@
1
+ //#region src/classes/Qweery.ts
2
+ var Qweery = class Qweery {
3
+ data;
4
+ constructor(data) {
5
+ this.data = Array.from(data);
6
+ }
7
+ query(options) {
8
+ let result = this.data.filter((_, index) => options.where ? this.isItemMatch(index, options.where) : true);
9
+ if (options.skip) result = result.slice(options.skip);
10
+ if (options.limit) result = result.slice(0, options.limit);
11
+ return result;
12
+ }
13
+ where(options) {
14
+ return new Qweery(this.data.filter((_, index) => this.isItemMatch(index, options)));
15
+ }
16
+ skip(count) {
17
+ return new Qweery(this.data.slice(count));
18
+ }
19
+ take(count) {
20
+ return new Qweery(this.data.slice(0, count));
21
+ }
22
+ count() {
23
+ return this.data.length;
24
+ }
25
+ first() {
26
+ return this.data[0];
27
+ }
28
+ last() {
29
+ return this.data[this.data.length - 1];
30
+ }
31
+ toArray() {
32
+ return this.data;
33
+ }
34
+ isItemMatch(index, options) {
35
+ const item = this.data.at(index);
36
+ if (!item) return false;
37
+ const keys = Object.keys(options);
38
+ for (const key of keys) {
39
+ if (key.toString().startsWith("$") || options[key] === void 0) continue;
40
+ let value;
41
+ if (typeof options[key] === "function") value = options[key](item[key], item, index, this.data);
42
+ else if (typeof options[key] === "object") if (typeof item[key] === "string") value = this.evaluateStringOperator(index, key, options[key]);
43
+ else if (typeof item[key] === "number" || typeof item[key] === "bigint" || item[key] instanceof Date) value = this.evaluateNumericalOperator(index, key, options[key]);
44
+ else if (Array.isArray(item[key])) value = this.evaluateArrayOperator(index, key, options[key]);
45
+ else value = this.evaluateValueOperator(index, key, options[key]);
46
+ else value = item[key] === options[key];
47
+ if (!value) return false;
48
+ }
49
+ return this.evaluateLogicalOperator(index, options);
50
+ }
51
+ evaluateLogicalOperator(index, options) {
52
+ if (options.$NOT) {
53
+ for (const option of Array.isArray(options.$NOT) ? options.$NOT : [options.$NOT]) if (this.isItemMatch(index, option)) return false;
54
+ }
55
+ if (options.$AND) {
56
+ for (const option of Array.isArray(options.$AND) ? options.$AND : [options.$AND]) if (!this.isItemMatch(index, option)) return false;
57
+ }
58
+ if (options.$OR) {
59
+ let hasMatch = false;
60
+ for (const option of Array.isArray(options.$OR) ? options.$OR : [options.$OR]) if (this.isItemMatch(index, option)) {
61
+ hasMatch = true;
62
+ break;
63
+ }
64
+ if (!hasMatch) return false;
65
+ }
66
+ return true;
67
+ }
68
+ evaluateValueOperator(index, property, operator) {
69
+ if ("equals" in operator && operator.equals !== this.data[index][property]) return false;
70
+ if ("notEquals" in operator && operator.notEquals === this.data[index][property]) return false;
71
+ return this.evaluateLogicalOperator(index, operator);
72
+ }
73
+ evaluateNumericalOperator(index, property, operator) {
74
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
75
+ const value = Qweery.normalizeNumericalValue(this.data[index][property]);
76
+ if (operator.greaterThan !== void 0 && !(value > Qweery.normalizeNumericalValue(operator.greaterThan))) return false;
77
+ if (operator.greaterThanOrEqual !== void 0 && !(value >= Qweery.normalizeNumericalValue(operator.greaterThanOrEqual))) return false;
78
+ if (operator.lessThan !== void 0 && !(value < Qweery.normalizeNumericalValue(operator.lessThan))) return false;
79
+ if (operator.lessThanOrEqual !== void 0 && !(value <= Qweery.normalizeNumericalValue(operator.lessThanOrEqual))) return false;
80
+ return true;
81
+ }
82
+ evaluateStringOperator(index, property, operator) {
83
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
84
+ let value = this.data[index][property];
85
+ if (operator.caseInsensitive) value = value.toLowerCase();
86
+ if (operator.includes && !value.includes(operator.caseInsensitive ? operator.includes.toLowerCase() : operator.includes)) return false;
87
+ if (operator.startsWith && !value.startsWith(operator.caseInsensitive ? operator.startsWith.toLowerCase() : operator.startsWith)) return false;
88
+ if (operator.endsWith && !value.endsWith(operator.caseInsensitive ? operator.endsWith.toLowerCase() : operator.endsWith)) return false;
89
+ if (operator.matches && !operator.matches.test(value)) return false;
90
+ return true;
91
+ }
92
+ evaluateArrayOperator(index, property, operator) {
93
+ if (!this.evaluateValueOperator(index, property, operator)) return false;
94
+ const value = this.data[index][property];
95
+ if ("includesEvery" in operator && operator.includesEvery?.every((v) => value.includes(v))) return true;
96
+ if ("includesSome" in operator && operator.includesSome?.some((v) => value.includes(v))) return true;
97
+ if ("includesNone" in operator && operator.includesNone?.every((v) => !value.includes(v))) return true;
98
+ return true;
99
+ }
100
+ };
101
+ (function(_Qweery) {
102
+ function normalizeNumericalValue(value) {
103
+ return value instanceof Date ? value.getTime() : value;
104
+ }
105
+ _Qweery.normalizeNumericalValue = normalizeNumericalValue;
106
+ })(Qweery || (Qweery = {}));
107
+
108
+ //#endregion
109
+ export { Qweery };
110
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/classes/Qweery.ts"],"sourcesContent":["import type { ValueOrArray } from '../helpers/types.js';\n\nexport class Qweery<T extends Qweery.Object> {\n public readonly data: T[];\n\n constructor(data: Iterable<T>) {\n this.data = Array.from(data);\n }\n\n public query(options: {\n where?: Qweery.WhereOptions<T>;\n skip?: number;\n limit?: number;\n }): T[] {\n let result = this.data.filter((_, index) => options.where ? this.isItemMatch(index, options.where) : true);\n\n if (options.skip) {\n result = result.slice(options.skip);\n }\n\n if (options.limit) {\n result = result.slice(0, options.limit);\n }\n\n return result;\n }\n\n public where(options: Qweery.WhereOptions<T>): Qweery<T> {\n return new Qweery(this.data.filter((_, index) => this.isItemMatch(index, options)));\n }\n\n public skip(count: number): Qweery<T> {\n return new Qweery(this.data.slice(count));\n }\n\n public take(count: number): Qweery<T> {\n return new Qweery(this.data.slice(0, count));\n }\n\n public count(): number {\n return this.data.length;\n }\n\n public first(): T|undefined {\n return this.data[0];\n }\n\n public last(): T|undefined {\n return this.data[this.data.length - 1];\n }\n\n public toArray(): T[] {\n return this.data;\n }\n\n private isItemMatch(index: number, options: Qweery.WhereOptions<T>): boolean {\n const item = this.data.at(index);\n if (!item) return false\n\n const keys = Object.keys(options) as (keyof T)[];\n\n for (const key of keys) {\n if (key.toString().startsWith('$') || options[key] === undefined) continue;\n\n let value: boolean;\n\n if (typeof options[key] === 'function') {\n value = options[key](item[key], item, index, this.data);\n } else if (typeof options[key] === 'object') {\n if (typeof item[key] === 'string') {\n value = this.evaluateStringOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionStringOperators<T, Qweery.StringPrimitive>\n );\n } else if (\n typeof item[key] === 'number' ||\n typeof item[key] === 'bigint' ||\n item[key] instanceof Date\n ) {\n value = this.evaluateNumericalOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionNumericalOperators<T, Qweery.NumericalPrimitive>\n );\n } else if (Array.isArray(item[key])) {\n value = this.evaluateArrayOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionArrayOperators<T, Qweery.ArrayPrimitive>\n );\n } else {\n value = this.evaluateValueOperator(\n index,\n key,\n options[key] as Qweery.WhereOptionValueOperators<T, any>\n );\n }\n } else {\n value = item[key] === options[key];\n }\n\n if (!value) return false;\n }\n\n return this.evaluateLogicalOperator(index, options);\n }\n\n private evaluateLogicalOperator(index: number, options: Qweery.WhereOptionLogicalOperators<T>): boolean {\n let value = true;\n\n if (options.$NOT) {\n for (const option of Array.isArray(options.$NOT) ? options.$NOT : [options.$NOT]) {\n if (this.isItemMatch(index, option)) return false;\n }\n }\n\n if (options.$AND) {\n for (const option of Array.isArray(options.$AND) ? options.$AND : [options.$AND]) {\n if (!this.isItemMatch(index, option)) return false;\n }\n }\n\n if (options.$OR) {\n let hasMatch = false;\n\n for (const option of Array.isArray( options.$OR) ? options.$OR : [ options.$OR]) {\n if (this.isItemMatch(index, option)) {\n hasMatch = true;\n break;\n }\n }\n\n if (!hasMatch) return false;\n }\n\n return true;\n }\n\n private evaluateValueOperator(index: number, property: keyof T, operator: Qweery.WhereOptionValueOperators<T, any>): boolean {\n if ('equals' in operator && operator.equals !== this.data[index][property]) {\n return false;\n }\n\n if ('notEquals' in operator && operator.notEquals === this.data[index][property]) {\n return false;\n }\n\n return true && this.evaluateLogicalOperator(index, operator);\n }\n\n private evaluateNumericalOperator(index: number, property: keyof T, operator: Qweery.WhereOptionNumericalOperators<T, Qweery.NumericalPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n const value = Qweery.normalizeNumericalValue(this.data[index][property] as Qweery.NumericalPrimitive);\n\n if (operator.greaterThan !== undefined && !(value > Qweery.normalizeNumericalValue(operator.greaterThan))) {\n return false;\n }\n\n if (operator.greaterThanOrEqual !== undefined && !(value >= Qweery.normalizeNumericalValue(operator.greaterThanOrEqual))) {\n return false;\n }\n\n if (operator.lessThan !== undefined && !(value < Qweery.normalizeNumericalValue(operator.lessThan))) {\n return false;\n }\n\n if (operator.lessThanOrEqual !== undefined && !(value <= Qweery.normalizeNumericalValue(operator.lessThanOrEqual))) {\n return false;\n }\n\n return true;\n }\n\n private evaluateStringOperator(index: number, property: keyof T, operator: Qweery.WhereOptionStringOperators<T, Qweery.StringPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n let value = this.data[index][property] as Qweery.StringPrimitive;\n\n if (operator.caseInsensitive) {\n value = value.toLowerCase();\n }\n\n if (\n operator.includes &&\n !value.includes(\n operator.caseInsensitive\n ? operator.includes.toLowerCase()\n : operator.includes\n )\n ) {\n return false;\n }\n\n if (\n operator.startsWith && \n !value.startsWith(\n operator.caseInsensitive\n ? operator.startsWith.toLowerCase()\n : operator.startsWith\n )\n ) {\n return false;\n }\n\n if (\n operator.endsWith &&\n !value.endsWith(\n operator.caseInsensitive\n ? operator.endsWith.toLowerCase()\n : operator.endsWith\n )\n ) {\n return false;\n }\n\n if (operator.matches && !operator.matches.test(value)) {\n return false;\n }\n\n return true;\n }\n\n private evaluateArrayOperator(index: number, property: keyof T, operator: Qweery.WhereOptionArrayOperators<T, Qweery.ArrayPrimitive>): boolean {\n if (!this.evaluateValueOperator(index, property, operator)) return false;\n\n const value = this.data[index][property] as Qweery.ArrayPrimitive;\n\n if ('includesEvery' in operator && operator.includesEvery?.every(v => value.includes(v))) {\n return true;\n }\n\n if ('includesSome' in operator && operator.includesSome?.some(v => value.includes(v))) {\n return true;\n }\n\n if ('includesNone' in operator && operator.includesNone?.every(v => !value.includes(v))) {\n return true;\n }\n\n return true;\n }\n}\n\nexport namespace Qweery {\n export type Object = object;\n export type StringPrimitive = string;\n export type NumericalPrimitive = number|bigint|Date;\n export type ArrayPrimitive = Array<any>|ReadonlyArray<any>;\n\n export type WhereOptionKeys<T extends Object> = {\n [K in keyof T]?:\n |T[K]\n |((value: T[K], object: T, index: number, array: T[]) => boolean)\n |WhereOptionValueOperatorsType<T, T[K]>;\n }\n\n export type WhereOptionValueOperatorsType<T extends Object, V> = V extends StringPrimitive\n ? WhereOptionStringOperators<T, V>\n : V extends NumericalPrimitive\n ? WhereOptionNumericalOperators<T, V>\n : V extends ArrayPrimitive\n ? WhereOptionArrayOperators<T, V>\n : WhereOptionValueOperators<T, V>\n\n export interface WhereOptionLogicalOperators<T extends Object> {\n $AND?: ValueOrArray<WhereOptions<T>>;\n $OR?: ValueOrArray<WhereOptions<T>>;\n $NOT?: ValueOrArray<WhereOptions<T>>;\n }\n\n export interface WhereOptionValueOperators<T extends Object, V> extends WhereOptionLogicalOperators<T> {\n equals?: V;\n notEquals?: V;\n }\n\n export interface WhereOptionNumericalOperators<T extends Object, V extends NumericalPrimitive> extends WhereOptionValueOperators<T, V> {\n greaterThan?: V;\n greaterThanOrEqual?: V;\n lessThan?: V;\n lessThanOrEqual?: V;\n }\n\n export interface WhereOptionStringOperators<T extends Object, V extends StringPrimitive> extends WhereOptionValueOperators<T, V> {\n includes?: V;\n startsWith?: V;\n endsWith?: V;\n caseInsensitive?: boolean;\n matches?: RegExp;\n }\n\n export interface WhereOptionArrayOperators<T extends Object, V extends ArrayPrimitive> extends WhereOptionValueOperators<T, V> {\n includesEvery?: V[number][];\n includesSome?: V[number][];\n includesNone?: V[number][];\n }\n\n export type WhereOptions<T extends Object> = WhereOptionKeys<T> & WhereOptionLogicalOperators<T>;\n\n export function normalizeNumericalValue(value: Qweery.NumericalPrimitive): number|bigint {\n return value instanceof Date ? value.getTime() : value;\n }\n}"],"mappings":";AAEA,IAAa,SAAb,MAAa,OAAgC;CACzC,AAAgB;CAEhB,YAAY,MAAmB;AAC3B,OAAK,OAAO,MAAM,KAAK,KAAK;;CAGhC,AAAO,MAAM,SAIL;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AAK,MAAM,SAAS,EAAE,CAAC,CACjF,QAAO;AAGX,MAAI,kBAAkB,YAAY,SAAS,cAAc,OAAM,MAAK,CAAC,MAAM,SAAS,EAAE,CAAC,CACnF,QAAO;AAGX,SAAO;;;;CA2DJ,SAAS,wBAAwB,OAAiD;AACrF,SAAO,iBAAiB,OAAO,MAAM,SAAS,GAAG"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "qweery",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "description": "A simple array query library",
6
+ "type": "module",
7
+ "main": "./dist/index.cjs",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.cts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/index.d.mts",
14
+ "default": "./dist/index.mjs"
15
+ },
16
+ "require": {
17
+ "types": "./dist/index.d.cts",
18
+ "default": "./dist/index.cjs"
19
+ }
20
+ }
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/catplvsplus/qweery.git"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "LICENSE",
29
+ "README.md"
30
+ ],
31
+ "scripts": {
32
+ "test": "bun test/*.test.ts",
33
+ "build": "bunx tsdown",
34
+ "prepack": "bunx tsdown"
35
+ },
36
+ "dependencies": {
37
+ "@faker-js/faker": "^10.3.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/bun": "^1.3.9",
41
+ "tsdown": "^0.20.3",
42
+ "typescript": "^5.9.3"
43
+ }
44
+ }