@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.
@@ -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 {};
@@ -2,18 +2,43 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.memorySearchTheBadWay = void 0;
4
4
  const __1 = require("../..");
5
+ const errors_1 = require("../../errors");
5
6
  const guards_1 = require("../../guards");
6
7
  const MIN_MATCH = 0.5;
7
8
  const MAX_RESULTS = 10;
9
+ var MatchType;
10
+ (function (MatchType) {
11
+ MatchType[MatchType["Must"] = 0] = "Must";
12
+ MatchType[MatchType["MustNot"] = 1] = "MustNot";
13
+ })(MatchType || (MatchType = {}));
8
14
  const resolveField = (document, field) => field.key.toString().split('.').reduce((result, key) => result[key], document);
9
15
  const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
10
16
  return {
11
17
  ensureIndexCreated: async () => undefined,
12
18
  index: async (_options) => undefined,
13
19
  search: async (options) => {
20
+ const getFieldType = (field) => { var _a; return (_a = options.fields.find(f => f.key == field.key)) === null || _a === void 0 ? void 0 : _a.type; };
14
21
  const results = (await loadAllDocumentsTheBadWay())
15
22
  .map(document => {
16
23
  let weight = 0;
24
+ const matchFilters = (filters, mustMatch) => {
25
+ for (const field of filters) {
26
+ const docValues = (0, __1.coerceArray)(resolveField(document, field));
27
+ const coerceValue = getFieldType(field) === 'boolean'
28
+ ? (input) => {
29
+ if ([true, 'true', '1', 1].includes(input)) {
30
+ return true;
31
+ }
32
+ if ([false, 'false', '0', 0, ''].includes(input)) {
33
+ return false;
34
+ }
35
+ throw new errors_1.InvalidRequestError('input is not a valid boolean filter');
36
+ }
37
+ : (x) => x;
38
+ const hasMatch = (0, __1.coerceArray)(field.value).map(coerceValue).some(v => docValues.includes(v));
39
+ return !((mustMatch === MatchType.Must && !hasMatch) || (mustMatch === MatchType.MustNot && hasMatch));
40
+ }
41
+ };
17
42
  if (options.query !== undefined) {
18
43
  for (const field of options.fields) {
19
44
  if (field.type !== undefined && field.type !== 'text') {
@@ -33,11 +58,13 @@ const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
33
58
  }
34
59
  }
35
60
  }
36
- for (const field of options.filter || []) {
37
- const value = resolveField(document, field);
38
- if (!(0, __1.coerceArray)(field.value).some(v => (0, __1.coerceArray)(value).includes(v))) {
39
- return undefined;
40
- }
61
+ const { must_not } = options;
62
+ const must = 'filter' in options ? options.filter : options.must;
63
+ if ((must === null || must === void 0 ? void 0 : must.length) && !matchFilters(must, MatchType.Must)) {
64
+ return undefined;
65
+ }
66
+ if ((must_not === null || must_not === void 0 ? void 0 : must_not.length) && !matchFilters(must_not, MatchType.MustNot)) {
67
+ return undefined;
41
68
  }
42
69
  return { document, weight };
43
70
  })