@openstax/ts-utils 1.1.19 → 1.1.21
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/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/services/searchProvider/index.d.ts +11 -17
- package/dist/services/searchProvider/memorySearchTheBadWay.d.ts +1 -1
- package/dist/services/searchProvider/memorySearchTheBadWay.js +6 -10
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/tsconfig.withoutspecs.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { UnionToIntersection } from './types';
|
|
|
2
2
|
export declare const getKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O) => O[K];
|
|
3
3
|
export declare const getKeyValueOr: <K extends string, V, Od extends { [key in K]?: any; } = { [key_1 in K]?: any; }>(key: K, defaultValue: V) => <O extends Od extends undefined ? { [key_2 in K]?: any; } : Od>(obj: O) => V | NonNullable<O[K]>;
|
|
4
4
|
export declare const putKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O, value: O[K]) => O;
|
|
5
|
+
export declare const coerceArray: <T>(thing: T | T[] | undefined) => T[];
|
|
5
6
|
declare type FlowFn<A, R> = (...args: [A]) => R;
|
|
6
7
|
declare type AnyFlowFn = FlowFn<any, any>;
|
|
7
8
|
declare type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.tuple = exports.merge = exports.getCommonProperties = exports.roundToPrecision = exports.memoize = exports.partitionSequence = exports.once = exports.hashValue = exports.mapFind = exports.fnIf = exports.flow = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
|
|
6
|
+
exports.tuple = exports.merge = exports.getCommonProperties = exports.roundToPrecision = exports.memoize = exports.partitionSequence = exports.once = exports.hashValue = exports.mapFind = exports.fnIf = exports.flow = exports.coerceArray = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
|
|
7
7
|
const crypto_1 = require("crypto");
|
|
8
8
|
const deep_equal_1 = __importDefault(require("deep-equal"));
|
|
9
9
|
const guards_1 = require("./guards");
|
|
@@ -26,6 +26,8 @@ const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
|
|
|
26
26
|
exports.getKeyValueOr = getKeyValueOr;
|
|
27
27
|
const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
|
|
28
28
|
exports.putKeyValue = putKeyValue;
|
|
29
|
+
const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
|
|
30
|
+
exports.coerceArray = coerceArray;
|
|
29
31
|
/*
|
|
30
32
|
* this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
|
|
31
33
|
*/
|
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
declare type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare type
|
|
6
|
-
|
|
7
|
-
key: F;
|
|
8
|
-
value: UnArray<T[F]> | UnArray<T[F]>[];
|
|
9
|
-
} : never;
|
|
10
|
-
declare type QueryFieldTypes = string | string[] | undefined | null;
|
|
11
|
-
declare type Field<F extends keyof T, T> = F extends KeysMatching<T, QueryFieldTypes> ? ({
|
|
12
|
-
key: F;
|
|
1
|
+
declare type Filter = {
|
|
2
|
+
key: string;
|
|
3
|
+
value: string | string[];
|
|
4
|
+
};
|
|
5
|
+
declare type Field = {
|
|
6
|
+
key: string;
|
|
13
7
|
type?: 'text';
|
|
14
8
|
weight: number;
|
|
15
9
|
} | {
|
|
16
|
-
key:
|
|
10
|
+
key: string;
|
|
17
11
|
type: 'keyword';
|
|
18
|
-
}
|
|
19
|
-
export interface SearchOptions
|
|
12
|
+
};
|
|
13
|
+
export interface SearchOptions {
|
|
20
14
|
page?: number;
|
|
21
15
|
query: string | undefined;
|
|
22
|
-
fields: Field
|
|
23
|
-
filter?: Filter
|
|
16
|
+
fields: Field[];
|
|
17
|
+
filter?: Filter[];
|
|
24
18
|
}
|
|
25
19
|
export {};
|
|
@@ -2,7 +2,7 @@ import { SearchOptions } from '.';
|
|
|
2
2
|
export declare const memorySearchTheBadWay: <T>({ loadAllDocumentsTheBadWay }: {
|
|
3
3
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
4
4
|
}) => {
|
|
5
|
-
search: (options: SearchOptions
|
|
5
|
+
search: (options: SearchOptions) => Promise<{
|
|
6
6
|
items: T[];
|
|
7
7
|
pageSize: number;
|
|
8
8
|
currentPage: number;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.memorySearchTheBadWay = void 0;
|
|
4
|
+
const __1 = require("../..");
|
|
4
5
|
const guards_1 = require("../../guards");
|
|
5
6
|
const MIN_MATCH = 0.5;
|
|
6
7
|
const MAX_RESULTS = 10;
|
|
8
|
+
const resolveField = (document, field) => field.key.toString().split('.').reduce((result, key) => result[key], document);
|
|
7
9
|
const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
8
10
|
return {
|
|
9
11
|
search: async (options) => {
|
|
@@ -15,12 +17,12 @@ const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
|
15
17
|
if (field.type !== undefined && field.type !== 'text') {
|
|
16
18
|
continue;
|
|
17
19
|
}
|
|
18
|
-
const value = document
|
|
20
|
+
const value = resolveField(document, field);
|
|
19
21
|
if (value === undefined || value === null) {
|
|
20
22
|
continue;
|
|
21
23
|
}
|
|
22
24
|
if (typeof value === 'string') {
|
|
23
|
-
if (value.indexOf(options.query) !== -1) {
|
|
25
|
+
if (value.toUpperCase().indexOf(options.query.toUpperCase()) !== -1) {
|
|
24
26
|
weight += (1 * field.weight);
|
|
25
27
|
}
|
|
26
28
|
}
|
|
@@ -30,14 +32,8 @@ const memorySearchTheBadWay = ({ loadAllDocumentsTheBadWay }) => {
|
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
for (const field of options.filter || []) {
|
|
33
|
-
const value = document
|
|
34
|
-
if (value
|
|
35
|
-
if (!(field.value instanceof Array && field.value.some(v => value.includes(v)))
|
|
36
|
-
&& !(!(field.value instanceof Array) && value.includes(field.value))) {
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
else if (value !== field.value) {
|
|
35
|
+
const value = resolveField(document, field);
|
|
36
|
+
if (!(0, __1.coerceArray)(field.value).some(v => (0, __1.coerceArray)(value).includes(v))) {
|
|
41
37
|
return undefined;
|
|
42
38
|
}
|
|
43
39
|
}
|