@openstax/ts-utils 1.3.1 → 1.3.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.
- package/dist/cjs/services/searchProvider/index.d.ts +10 -2
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.js +32 -5
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/searchProvider/index.d.ts +10 -2
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +32 -5
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
declare type Filter = {
|
|
1
|
+
export declare type Filter = {
|
|
2
2
|
key: string;
|
|
3
|
-
value: string | string[];
|
|
3
|
+
value: string | string[] | boolean;
|
|
4
4
|
};
|
|
5
5
|
declare type Field = {
|
|
6
6
|
key: string;
|
|
@@ -9,6 +9,9 @@ declare type Field = {
|
|
|
9
9
|
} | {
|
|
10
10
|
key: string;
|
|
11
11
|
type: 'keyword';
|
|
12
|
+
} | {
|
|
13
|
+
key: string;
|
|
14
|
+
type: 'boolean';
|
|
12
15
|
};
|
|
13
16
|
export interface IndexOptions<T> {
|
|
14
17
|
body: T;
|
|
@@ -18,6 +21,11 @@ export interface SearchOptions {
|
|
|
18
21
|
page?: number;
|
|
19
22
|
query: string | undefined;
|
|
20
23
|
fields: Field[];
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated use `must` instead
|
|
26
|
+
*/
|
|
21
27
|
filter?: Filter[];
|
|
28
|
+
must?: Filter[];
|
|
29
|
+
must_not?: Filter[];
|
|
22
30
|
}
|
|
23
31
|
export {};
|
|
@@ -1,16 +1,41 @@
|
|
|
1
1
|
import { coerceArray } from '../..';
|
|
2
|
+
import { InvalidRequestError } from '../../errors';
|
|
2
3
|
import { isDefined } from '../../guards';
|
|
3
4
|
const MIN_MATCH = 0.5;
|
|
4
5
|
const MAX_RESULTS = 10;
|
|
6
|
+
var MatchType;
|
|
7
|
+
(function (MatchType) {
|
|
8
|
+
MatchType[MatchType["Must"] = 0] = "Must";
|
|
9
|
+
MatchType[MatchType["MustNot"] = 1] = "MustNot";
|
|
10
|
+
})(MatchType || (MatchType = {}));
|
|
5
11
|
const resolveField = (document, field) => field.key.toString().split('.').reduce((result, key) => result[key], document);
|
|
6
12
|
export const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
7
13
|
return {
|
|
8
14
|
ensureIndexCreated: async () => undefined,
|
|
9
15
|
index: async (_options) => undefined,
|
|
10
16
|
search: async (options) => {
|
|
17
|
+
const getFieldType = (field) => { var _a; return (_a = options.fields.find(f => f.key == field.key)) === null || _a === void 0 ? void 0 : _a.type; };
|
|
11
18
|
const results = (await loadAllDocumentsTheBadWay())
|
|
12
19
|
.map(document => {
|
|
13
20
|
let weight = 0;
|
|
21
|
+
const matchFilters = (filters, mustMatch) => {
|
|
22
|
+
for (const field of filters) {
|
|
23
|
+
const docValues = coerceArray(resolveField(document, field));
|
|
24
|
+
const coerceValue = getFieldType(field) === 'boolean'
|
|
25
|
+
? (input) => {
|
|
26
|
+
if ([true, 'true', '1', 1].includes(input)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if ([false, 'false', '0', 0, ''].includes(input)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
throw new InvalidRequestError('input is not a valid boolean filter');
|
|
33
|
+
}
|
|
34
|
+
: (x) => x;
|
|
35
|
+
const hasMatch = coerceArray(field.value).map(coerceValue).some(v => docValues.includes(v));
|
|
36
|
+
return !((mustMatch === MatchType.Must && !hasMatch) || (mustMatch === MatchType.MustNot && hasMatch));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
14
39
|
if (options.query !== undefined) {
|
|
15
40
|
for (const field of options.fields) {
|
|
16
41
|
if (field.type !== undefined && field.type !== 'text') {
|
|
@@ -30,11 +55,13 @@ export const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
|
30
55
|
}
|
|
31
56
|
}
|
|
32
57
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
const { must_not } = options;
|
|
59
|
+
const must = 'filter' in options ? options.filter : options.must;
|
|
60
|
+
if ((must === null || must === void 0 ? void 0 : must.length) && !matchFilters(must, MatchType.Must)) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if ((must_not === null || must_not === void 0 ? void 0 : must_not.length) && !matchFilters(must_not, MatchType.MustNot)) {
|
|
64
|
+
return undefined;
|
|
38
65
|
}
|
|
39
66
|
return { document, weight };
|
|
40
67
|
})
|