pacc 3.2.0 → 3.4.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/package.json +5 -5
- package/src/attribute.mjs +9 -1
- package/src/filter.mjs +228 -0
- package/types/attribute.d.mts +1 -0
- package/types/filter.d.mts +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pacc",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -34,14 +34,14 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"ava": "^6.2.0",
|
|
37
|
-
"browser-ava": "^2.3.
|
|
37
|
+
"browser-ava": "^2.3.16",
|
|
38
38
|
"c8": "^10.1.3",
|
|
39
39
|
"documentation": "^14.0.3",
|
|
40
|
-
"semantic-release": "^24.2.
|
|
41
|
-
"typescript": "^5.
|
|
40
|
+
"semantic-release": "^24.2.3",
|
|
41
|
+
"typescript": "^5.8.3"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
|
-
"node": ">=22.
|
|
44
|
+
"node": ">=22.14.0"
|
|
45
45
|
},
|
|
46
46
|
"repository": {
|
|
47
47
|
"type": "git",
|
package/src/attribute.mjs
CHANGED
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
STAR
|
|
34
34
|
} from "./tokens.mjs";
|
|
35
35
|
export * from "./tokens.mjs";
|
|
36
|
+
export * from "./filter.mjs";
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* Set Object attribute.
|
|
@@ -94,7 +95,11 @@ export function getAttribute(object, expression) {
|
|
|
94
95
|
export function getAttributeAndOperator(object, expression) {
|
|
95
96
|
switch (typeof object?.[expression]) {
|
|
96
97
|
case "function":
|
|
97
|
-
|
|
98
|
+
object = object[expression]();
|
|
99
|
+
if (typeof object[Symbol.iterator] === "function") {
|
|
100
|
+
object = [...object];
|
|
101
|
+
}
|
|
102
|
+
return [object, EQUAL];
|
|
98
103
|
case "undefined":
|
|
99
104
|
break;
|
|
100
105
|
default:
|
|
@@ -141,6 +146,9 @@ export function getAttributeAndOperator(object, expression) {
|
|
|
141
146
|
switch (typeof object[token]) {
|
|
142
147
|
case "function":
|
|
143
148
|
object = object[token]();
|
|
149
|
+
if (typeof object[Symbol.iterator] === "function") {
|
|
150
|
+
object = [...object];
|
|
151
|
+
}
|
|
144
152
|
break;
|
|
145
153
|
default:
|
|
146
154
|
object = object[token];
|
package/src/filter.mjs
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAttributeAndOperator,
|
|
3
|
+
EQUAL,
|
|
4
|
+
NOT_EQUAL,
|
|
5
|
+
LESS,
|
|
6
|
+
LESS_EQUAL,
|
|
7
|
+
GREATER,
|
|
8
|
+
GREATER_EQUAL
|
|
9
|
+
} from "pacc";
|
|
10
|
+
|
|
11
|
+
function dateOp(value, against, op) {
|
|
12
|
+
return numberOp(value.getTime(), against.getTime(), op);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function collectionOp(value, against, op) {
|
|
16
|
+
for (const v of value) {
|
|
17
|
+
if (allOp(v, against, op)) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function allOp(value, against, op) {
|
|
26
|
+
switch (typeof value) {
|
|
27
|
+
case "undefined":
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
case "object":
|
|
31
|
+
if (value === null) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (value instanceof Map) {
|
|
36
|
+
return collectionOp(value.keys(), against, op);
|
|
37
|
+
}
|
|
38
|
+
if (value instanceof RegExp) {
|
|
39
|
+
return value.test(against);
|
|
40
|
+
}
|
|
41
|
+
if (value[Symbol.iterator]) {
|
|
42
|
+
return collectionOp(value, against, op);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
switch (typeof against) {
|
|
46
|
+
case "object":
|
|
47
|
+
if (value instanceof Date) {
|
|
48
|
+
if (
|
|
49
|
+
Array.isArray(against) &&
|
|
50
|
+
against[0] instanceof Date &&
|
|
51
|
+
against[1] instanceof Date
|
|
52
|
+
) {
|
|
53
|
+
return dateOp(value, against[0], GREATER_EQUAL) && dateOp(value, against[1], LESS_EQUAL);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (against instanceof Date) {
|
|
57
|
+
return dateOp(value, against, op);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (value[Symbol.toPrimitive] && against[Symbol.toPrimitive]) {
|
|
62
|
+
return numberOp(
|
|
63
|
+
value[Symbol.toPrimitive]("number"),
|
|
64
|
+
against[Symbol.toPrimitive]("number"),
|
|
65
|
+
op
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
|
|
70
|
+
case "bigint":
|
|
71
|
+
case "number":
|
|
72
|
+
return allOp(value[Symbol.toPrimitive]("number"), against, op);
|
|
73
|
+
|
|
74
|
+
case "string":
|
|
75
|
+
if (against.length === 0) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (value instanceof Date) {
|
|
80
|
+
return dateOp(value, new Date(against), op);
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case "boolean":
|
|
84
|
+
return numberOp(value ? true : false, against, op);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (against instanceof RegExp) {
|
|
88
|
+
return against.test(value.toString());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return value.toString().match(against);
|
|
92
|
+
case "string":
|
|
93
|
+
switch (typeof against) {
|
|
94
|
+
case "boolean":
|
|
95
|
+
return numberOp(value.length !== 0, against, op);
|
|
96
|
+
case "string":
|
|
97
|
+
if (
|
|
98
|
+
op === EQUAL &&
|
|
99
|
+
(against.length === 0 || value.indexOf(against) >= 0)
|
|
100
|
+
) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!value.match(/^-?\d/)) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case "bigint":
|
|
108
|
+
case "number":
|
|
109
|
+
return value.length ? numberOp(value, against, op) : true;
|
|
110
|
+
case "object":
|
|
111
|
+
if (value.length === 0) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (against instanceof Date) {
|
|
116
|
+
return dateOp(new Date(value), against, op);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (against instanceof RegExp) {
|
|
120
|
+
return against.test(value);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (against instanceof Map) {
|
|
124
|
+
for (const [k, v] of against) {
|
|
125
|
+
if (allOp(value, k, op) || allOp(value, v, op)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (against[Symbol.iterator]) {
|
|
132
|
+
for (const i of against) {
|
|
133
|
+
if (allOp(value, i, op)) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return value.match(against);
|
|
142
|
+
case "bigint":
|
|
143
|
+
case "number":
|
|
144
|
+
switch (typeof against) {
|
|
145
|
+
case "object":
|
|
146
|
+
if (against instanceof RegExp) {
|
|
147
|
+
return against.test(String(value));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (against instanceof Map) {
|
|
151
|
+
for (const [k, v] of against) {
|
|
152
|
+
if (numberOp(value, k, op) || numberOp(value, v, op)) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (against[Symbol.iterator]) {
|
|
160
|
+
for (const i of against) {
|
|
161
|
+
if (numberOp(value, i, op)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return numberOp(value, against, op);
|
|
169
|
+
case "boolean":
|
|
170
|
+
switch (typeof against) {
|
|
171
|
+
case "object":
|
|
172
|
+
if (against[Symbol.iterator]) {
|
|
173
|
+
for (const i of against) {
|
|
174
|
+
if (allOp(value, i, op)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return value == against;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function numberOp(value, against, op) {
|
|
189
|
+
switch (op) {
|
|
190
|
+
case NOT_EQUAL:
|
|
191
|
+
return value != against;
|
|
192
|
+
case EQUAL:
|
|
193
|
+
return value == against;
|
|
194
|
+
case GREATER:
|
|
195
|
+
return value > against;
|
|
196
|
+
case LESS:
|
|
197
|
+
return value < against;
|
|
198
|
+
case GREATER_EQUAL:
|
|
199
|
+
return value >= against;
|
|
200
|
+
case LESS_EQUAL:
|
|
201
|
+
return value <= against;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Generate filter function.
|
|
207
|
+
* @param {Object} [filterBy]
|
|
208
|
+
* @returns {Function}
|
|
209
|
+
*/
|
|
210
|
+
export function filter(filterBy) {
|
|
211
|
+
if (filterBy) {
|
|
212
|
+
const filters = Object.entries(filterBy).map(([key, against]) => {
|
|
213
|
+
return a => {
|
|
214
|
+
const [value, op] = getAttributeAndOperator(a, key);
|
|
215
|
+
return allOp(value, against, op);
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (filters.length === 1) {
|
|
220
|
+
return filters[0];
|
|
221
|
+
}
|
|
222
|
+
if (filters.length > 1) {
|
|
223
|
+
return a => !filters.some(f => !f(a));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return () => true;
|
|
228
|
+
}
|
package/types/attribute.d.mts
CHANGED
|
@@ -23,6 +23,7 @@ export function getAttribute(object: any, expression: string): any;
|
|
|
23
23
|
*/
|
|
24
24
|
export function getAttributeAndOperator(object: any, expression: string): [any, Token];
|
|
25
25
|
export * from "./tokens.mjs";
|
|
26
|
+
export * from "./filter.mjs";
|
|
26
27
|
export type AttributeDefinition = {
|
|
27
28
|
type: string;
|
|
28
29
|
isKey: boolean;
|