@openstax/ts-utils 1.1.30 → 1.1.31

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,29 +1,4 @@
1
- import { UnionToIntersection } from './types';
2
- export declare const getKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O) => O[K];
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
- 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[];
6
- declare type FlowFn<A, R> = (...args: [A]) => R;
7
- declare type AnyFlowFn = FlowFn<any, any>;
8
- declare type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
9
- declare type FlowResult<C, A> = C extends [infer F1, ...infer Cr] ? F1 extends AnyFlowFn ? Cr extends never[] ? FlowFnResult<F1, A> : FlowResult<Cr, FlowFnResult<F1, A>> : never : never;
10
- declare type FlowChainArg<C> = C extends [infer F1, ...infer _] ? F1 extends FlowFn<infer A, any> ? A : never : never;
11
- export declare const flow: <C extends AnyFlowFn[]>(...chain: C) => (param: FlowChainArg<C>) => FlowResult<C, FlowChainArg<C>>;
12
- export declare const fnIf: <T1, T2>(condition: boolean, trueValue: T1, falseValue: T2) => T1 | T2;
13
- export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predicate?: (result: R) => boolean) => R | undefined;
14
- declare type HashValue = string | number | boolean | null | HashCompoundValue;
15
- declare type HashCompoundValue = Array<HashValue> | {
16
- [key: string]: HashValue;
17
- };
18
- export declare const hashValue: (value: HashValue) => string;
19
- export declare const once: <F extends (...args: any[]) => any>(fn: F) => F;
20
- export declare const partitionSequence: <T, P>(getPartition: (thing: T, previous?: P | undefined) => {
21
- matches?: boolean | undefined;
22
- value: P;
23
- }, sequence: T[]) => [P, T[]][];
24
- export declare const memoize: <F extends (...args: any[]) => any>(fn: F) => F;
25
- export declare const roundToPrecision: (num: number, places: number) => number;
26
- export declare const getCommonProperties: <T1 extends {}, T2 extends {}>(thing1: T1, thing2: T2) => (keyof T1 & keyof T2)[];
27
- export declare const merge: <T extends {}[]>(...[thing1, ...tail]: T) => UnionToIntersection<T[number]>;
28
- export declare const tuple: <A extends any[]>(...args: A) => A;
29
- export {};
1
+ export * from './misc/partitionSequence';
2
+ export * from './misc/helpers';
3
+ export * from './misc/merge';
4
+ export * from './misc/hashValue';
package/dist/cjs/index.js CHANGED
@@ -1,231 +1,20 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
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.coerceArray = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
7
- const crypto_1 = require("crypto");
8
- const deep_equal_1 = __importDefault(require("deep-equal"));
9
- const guards_1 = require("./guards");
10
- /*
11
- * there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
12
- * these do more validation that the second function gets a compatible object.
13
- *
14
- * const getAuthor = getKeyValue('author');
15
- * const author = getAuthor(book);
16
- *
17
- * const getAuthorOrNope = getKeyValueOr('author', 'nope');
18
- * const authorOrNope = getAuthorOrNope(book);
19
- *
20
- * const putAuthor = putKeyValue('author');
21
- * const newBook = putAuthor(book, 'tom');
22
- * */
23
- const getKeyValue = (key) => (obj) => obj[key];
24
- exports.getKeyValue = getKeyValue;
25
- const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
26
- exports.getKeyValueOr = getKeyValueOr;
27
- const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
28
- exports.putKeyValue = putKeyValue;
29
- const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
30
- exports.coerceArray = coerceArray;
31
- /*
32
- * this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
33
- */
34
- const flow = (...chain) => (param) => {
35
- let result = param;
36
- for (const fn of chain) {
37
- result = fn(result);
38
- }
39
- return result;
40
- };
41
- exports.flow = flow;
42
- /*
43
- * a shameful helper to avoid needing to test code coverage of branches
44
- */
45
- const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
46
- exports.fnIf = fnIf;
47
- /*
48
- * maps the array and returns the first result that matches the predicate
49
- * avoids processing extra elements that would happen with .map().find() or .reduce
50
- *
51
- * eg the third element of the array is never processed:
52
- * const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
53
- */
54
- const mapFind = (array, mapper, predicate = (r) => !!r) => {
55
- for (const item of array) {
56
- const mapped = mapper(item);
57
- if (predicate(mapped)) {
58
- return mapped;
59
- }
60
- }
61
- };
62
- exports.mapFind = mapFind;
63
- /*
64
- * creates a string hash of lots of different kinds of things.
65
- *
66
- * eg:
67
- * hashValue({someKey: 'someValue'})
68
- * */
69
- const hashValue = (value) => {
70
- // hack for sorting keys https://stackoverflow.com/a/53593328/14809536
71
- const allKeys = new Set();
72
- JSON.stringify(value, (k, v) => (allKeys.add(k), v));
73
- const strValue = JSON.stringify(value, Array.from(allKeys).sort());
74
- return (0, crypto_1.createHash)('sha1').update(strValue).digest('hex');
75
- };
76
- exports.hashValue = hashValue;
77
- /*
78
- * returns a function that will only ever call the given function once, returning the first result for every subsequent call
79
- *
80
- * eg:
81
- * const heavyFunction = () => 'hello';
82
- * const butOnlyOnce = once(() => 'hello');
83
- *
84
- * heavyFunction() // returns `hello`;
85
- * butOnlyOnce() // returns `hello`;
86
- */
87
- const once = (fn) => {
88
- const initialValue = {};
89
- let result = initialValue;
90
- return ((...args) => result === initialValue ? (result = fn(...args)) : result);
91
- };
92
- exports.once = once;
93
- /*
94
- * partitions a sequence based on a partition function returning {value: any; matches?: boolean}
95
- * - if the function returns `matches` explicitly then adjacent matching elements will
96
- * be grouped and the predicate value in the result will be from the last item in the group
97
- * - if the function returns only a value then matching will be evaluated based on the deep
98
- * equality of the value with its neighbors
99
- *
100
- * this is different from lodash/partition and lodash/groupBy because:
101
- * - it preserves the order of the items, items will only be grouped if they are already adjacent
102
- * - there can be any number of groups
103
- * - it tells you the partition value
104
- * - the partition value can be reduced, if you care (so you can like, partition on sequential values)
105
- *
106
- * simple predicate:
107
- * returns: [[0, [1,2]], [1, [3,4,5]]]
108
- * partitionSequence((n: number) => ({value: Math.floor(n / 3)}), [1,2,3,4,5])
109
- *
110
- * mutating partition:
111
- * returns: [
112
- * [{min: 1,max: 3}, [1,2,3]],
113
- * [{min: 5,max: 6}, [5,6]],
114
- * [{min: 8,max: 8}, [8]],
115
- * ]
116
- * partitionSequence(
117
- * (n: number, p?: {min: number; max: number}) =>
118
- * p && p.max + 1 === n
119
- * ? {value: {...p, max: n}, matches: true}
120
- * : {value: {min: n, max: n}, matches: false}
121
- * , [1,2,3,5,6,8]
122
- * )
123
- */
124
- const partitionSequence = (getPartition, sequence) => {
125
- const appendItem = (result, item) => {
126
- const current = result[result.length - 1];
127
- const itemPartition = getPartition(item, current === null || current === void 0 ? void 0 : current[0]);
128
- if (current && ((itemPartition.matches === undefined && (0, deep_equal_1.default)(current[0], itemPartition.value))
129
- || itemPartition.matches)) {
130
- current[0] = itemPartition.value;
131
- current[1].push(item);
132
- }
133
- else {
134
- result.push([itemPartition.value, [item]]);
135
- }
136
- return result;
137
- };
138
- return sequence.reduce(appendItem, []);
139
- };
140
- exports.partitionSequence = partitionSequence;
141
- /*
142
- * memoizes a function with any number of arguments
143
- */
144
- const memoize = (fn) => {
145
- const cache = {};
146
- const resolveCache = (cacheLayer, [first, ...rest]) => {
147
- if (!first) {
148
- return cacheLayer;
149
- }
150
- const layers = first instanceof Object
151
- ? (cacheLayer.weakLayers = (cacheLayer.weakLayers || (typeof WeakMap === 'undefined' ? undefined : new WeakMap())))
152
- : (cacheLayer.strongLayers = (cacheLayer.strongLayers || new Map()));
153
- // argument is an object and WeakMap is not supported
154
- if (!layers) {
155
- return {};
156
- }
157
- const layer = layers.get(first) || {};
158
- if (!layers.has(first)) {
159
- layers.set(first, layer);
160
- }
161
- return resolveCache(layer, rest);
162
- };
163
- return ((...args) => {
164
- const thisCache = resolveCache(cache, args);
165
- return thisCache.result = (thisCache.result || fn(...args));
166
- });
167
- };
168
- exports.memoize = memoize;
169
- /*
170
- * rounds number to given number of places
171
- *
172
- * eg:
173
- * roundToPrecision(1234.123, 2); // returns 1200
174
- * roundToPrecision(1234.123, -2); // returns 1234.12
175
- * roundToPrecision(1234.125, -2); // returns 1234.13
176
- */
177
- const roundToPrecision = (num, places) => {
178
- const multiplier = Math.pow(10, -1 * places);
179
- // https://stackoverflow.com/a/11832950/14809536
180
- return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
181
- };
182
- exports.roundToPrecision = roundToPrecision;
183
- const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
184
- exports.getCommonProperties = getCommonProperties;
185
- /*
186
- * recursive merge properties of inputs. values are merged if they are
187
- * plain objects or arrays, otherwise if the same property exists in both
188
- * objects the value from the second argument will win.
189
- *
190
- * unlike lodash merge, this will not change object references for values that
191
- * exist only in one parameter.
192
- *
193
- * eg:
194
- * merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
195
- */
196
- const merge = (...[thing1, ...tail]) => {
197
- const mergedTail = tail.length > 0
198
- ? (0, exports.merge)(...tail)
199
- : null;
200
- if (!mergedTail) {
201
- return thing1;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
202
7
  }
203
- return {
204
- ...thing1,
205
- ...mergedTail,
206
- ...(0, exports.getCommonProperties)(thing1, mergedTail).reduce((result, property) => ({
207
- ...result,
208
- ...((0, guards_1.isPlainObject)(thing1[property]) && (0, guards_1.isPlainObject)(mergedTail[property])
209
- ? { [property]: (0, exports.merge)(thing1[property], mergedTail[property]) }
210
- : (Array.isArray(thing1[property]) && Array.isArray(mergedTail[property]))
211
- ? { [property]: [...thing1[property], ...mergedTail[property]] }
212
- : {}),
213
- }), {}),
214
- };
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
215
15
  };
216
- exports.merge = merge;
217
- /*
218
- * a silly utility to help typescript realize an array is a tuple
219
- *
220
- * eg:
221
- * const a = [5, 'string'] // type is `Array<string | number>`
222
- * const t = tuple(5, 'string') type is `[5, 'string']`
223
- *
224
- * both have the same javascript value, but one is forced to be a tuple, which
225
- * is nice if its structure is important. examples are like the React.useState
226
- * pattern where there are two return values in a tuple, or if you're feeding
227
- * Object.fromEntries
228
- *
229
- */
230
- const tuple = (...args) => args;
231
- exports.tuple = tuple;
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./misc/partitionSequence"), exports);
18
+ __exportStar(require("./misc/helpers"), exports);
19
+ __exportStar(require("./misc/merge"), exports);
20
+ __exportStar(require("./misc/hashValue"), exports);
@@ -0,0 +1,6 @@
1
+ declare type HashValue = string | number | boolean | null | HashCompoundValue;
2
+ declare type HashCompoundValue = Array<HashValue> | {
3
+ [key: string]: HashValue;
4
+ };
5
+ export declare const hashValue: (value: HashValue) => string;
6
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hashValue = void 0;
4
+ const crypto_1 = require("crypto");
5
+ /*
6
+ * creates a string hash of lots of different kinds of things.
7
+ *
8
+ * eg:
9
+ * hashValue({someKey: 'someValue'})
10
+ * */
11
+ const hashValue = (value) => {
12
+ // hack for sorting keys https://stackoverflow.com/a/53593328/14809536
13
+ const allKeys = new Set();
14
+ JSON.stringify(value, (k, v) => (allKeys.add(k), v));
15
+ const strValue = JSON.stringify(value, Array.from(allKeys).sort());
16
+ return (0, crypto_1.createHash)('sha1').update(strValue).digest('hex');
17
+ };
18
+ exports.hashValue = hashValue;
@@ -0,0 +1,17 @@
1
+ export declare const getKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O) => O[K];
2
+ 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]>;
3
+ export declare const putKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O, value: O[K]) => O;
4
+ export declare const coerceArray: <T>(thing: T | T[] | undefined) => T[];
5
+ declare type FlowFn<A, R> = (...args: [A]) => R;
6
+ declare type AnyFlowFn = FlowFn<any, any>;
7
+ declare type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
8
+ declare type FlowResult<C, A> = C extends [infer F1, ...infer Cr] ? F1 extends AnyFlowFn ? Cr extends never[] ? FlowFnResult<F1, A> : FlowResult<Cr, FlowFnResult<F1, A>> : never : never;
9
+ declare type FlowChainArg<C> = C extends [infer F1, ...infer _] ? F1 extends FlowFn<infer A, any> ? A : never : never;
10
+ export declare const flow: <C extends AnyFlowFn[]>(...chain: C) => (param: FlowChainArg<C>) => FlowResult<C, FlowChainArg<C>>;
11
+ export declare const fnIf: <T1, T2>(condition: boolean, trueValue: T1, falseValue: T2) => T1 | T2;
12
+ export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predicate?: (result: R) => boolean) => R | undefined;
13
+ export declare const once: <F extends (...args: any[]) => any>(fn: F) => F;
14
+ export declare const memoize: <F extends (...args: any[]) => any>(fn: F) => F;
15
+ export declare const roundToPrecision: (num: number, places: number) => number;
16
+ export declare const tuple: <A extends any[]>(...args: A) => A;
17
+ export {};
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tuple = exports.roundToPrecision = exports.memoize = exports.once = exports.mapFind = exports.fnIf = exports.flow = exports.coerceArray = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
4
+ /*
5
+ * there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
6
+ * these do more validation that the second function gets a compatible object.
7
+ *
8
+ * const getAuthor = getKeyValue('author');
9
+ * const author = getAuthor(book);
10
+ *
11
+ * const getAuthorOrNope = getKeyValueOr('author', 'nope');
12
+ * const authorOrNope = getAuthorOrNope(book);
13
+ *
14
+ * const putAuthor = putKeyValue('author');
15
+ * const newBook = putAuthor(book, 'tom');
16
+ * */
17
+ const getKeyValue = (key) => (obj) => obj[key];
18
+ exports.getKeyValue = getKeyValue;
19
+ const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
20
+ exports.getKeyValueOr = getKeyValueOr;
21
+ const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
22
+ exports.putKeyValue = putKeyValue;
23
+ const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
24
+ exports.coerceArray = coerceArray;
25
+ /*
26
+ * this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
27
+ */
28
+ const flow = (...chain) => (param) => {
29
+ let result = param;
30
+ for (const fn of chain) {
31
+ result = fn(result);
32
+ }
33
+ return result;
34
+ };
35
+ exports.flow = flow;
36
+ /*
37
+ * a shameful helper to avoid needing to test code coverage of branches
38
+ */
39
+ const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
40
+ exports.fnIf = fnIf;
41
+ /*
42
+ * maps the array and returns the first result that matches the predicate
43
+ * avoids processing extra elements that would happen with .map().find() or .reduce
44
+ *
45
+ * eg the third element of the array is never processed:
46
+ * const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
47
+ */
48
+ const mapFind = (array, mapper, predicate = (r) => !!r) => {
49
+ for (const item of array) {
50
+ const mapped = mapper(item);
51
+ if (predicate(mapped)) {
52
+ return mapped;
53
+ }
54
+ }
55
+ };
56
+ exports.mapFind = mapFind;
57
+ /*
58
+ * returns a function that will only ever call the given function once, returning the first result for every subsequent call
59
+ *
60
+ * eg:
61
+ * const heavyFunction = () => 'hello';
62
+ * const butOnlyOnce = once(() => 'hello');
63
+ *
64
+ * heavyFunction() // returns `hello`;
65
+ * butOnlyOnce() // returns `hello`;
66
+ */
67
+ const once = (fn) => {
68
+ const initialValue = {};
69
+ let result = initialValue;
70
+ return ((...args) => result === initialValue ? (result = fn(...args)) : result);
71
+ };
72
+ exports.once = once;
73
+ /*
74
+ * memoizes a function with any number of arguments
75
+ */
76
+ const memoize = (fn) => {
77
+ const cache = {};
78
+ const resolveCache = (cacheLayer, [first, ...rest]) => {
79
+ if (!first) {
80
+ return cacheLayer;
81
+ }
82
+ const layers = first instanceof Object
83
+ ? (cacheLayer.weakLayers = (cacheLayer.weakLayers || (typeof WeakMap === 'undefined' ? undefined : new WeakMap())))
84
+ : (cacheLayer.strongLayers = (cacheLayer.strongLayers || new Map()));
85
+ // argument is an object and WeakMap is not supported
86
+ if (!layers) {
87
+ return {};
88
+ }
89
+ const layer = layers.get(first) || {};
90
+ if (!layers.has(first)) {
91
+ layers.set(first, layer);
92
+ }
93
+ return resolveCache(layer, rest);
94
+ };
95
+ return ((...args) => {
96
+ const thisCache = resolveCache(cache, args);
97
+ return thisCache.result = (thisCache.result || fn(...args));
98
+ });
99
+ };
100
+ exports.memoize = memoize;
101
+ /*
102
+ * rounds number to given number of places
103
+ *
104
+ * eg:
105
+ * roundToPrecision(1234.123, 2); // returns 1200
106
+ * roundToPrecision(1234.123, -2); // returns 1234.12
107
+ * roundToPrecision(1234.125, -2); // returns 1234.13
108
+ */
109
+ const roundToPrecision = (num, places) => {
110
+ const multiplier = Math.pow(10, -1 * places);
111
+ // https://stackoverflow.com/a/11832950/14809536
112
+ return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
113
+ };
114
+ exports.roundToPrecision = roundToPrecision;
115
+ /*
116
+ * a silly utility to help typescript realize an array is a tuple
117
+ *
118
+ * eg:
119
+ * const a = [5, 'string'] // type is `Array<string | number>`
120
+ * const t = tuple(5, 'string') type is `[5, 'string']`
121
+ *
122
+ * both have the same javascript value, but one is forced to be a tuple, which
123
+ * is nice if its structure is important. examples are like the React.useState
124
+ * pattern where there are two return values in a tuple, or if you're feeding
125
+ * Object.fromEntries
126
+ *
127
+ */
128
+ const tuple = (...args) => args;
129
+ exports.tuple = tuple;
@@ -0,0 +1,3 @@
1
+ import type { UnionToIntersection } from '../types';
2
+ export declare const getCommonProperties: <T1 extends {}, T2 extends {}>(thing1: T1, thing2: T2) => (keyof T1 & keyof T2)[];
3
+ export declare const merge: <T extends {}[]>(...[thing1, ...tail]: T) => UnionToIntersection<T[number]>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.merge = exports.getCommonProperties = void 0;
4
+ const guards_1 = require("../guards");
5
+ const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
6
+ exports.getCommonProperties = getCommonProperties;
7
+ /*
8
+ * recursive merge properties of inputs. values are merged if they are
9
+ * plain objects or arrays, otherwise if the same property exists in both
10
+ * objects the value from the second argument will win.
11
+ *
12
+ * unlike lodash merge, this will not change object references for values that
13
+ * exist only in one parameter.
14
+ *
15
+ * eg:
16
+ * merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
17
+ */
18
+ const merge = (...[thing1, ...tail]) => {
19
+ const mergedTail = tail.length > 0
20
+ ? (0, exports.merge)(...tail)
21
+ : null;
22
+ if (!mergedTail) {
23
+ return thing1;
24
+ }
25
+ return {
26
+ ...thing1,
27
+ ...mergedTail,
28
+ ...(0, exports.getCommonProperties)(thing1, mergedTail).reduce((result, property) => ({
29
+ ...result,
30
+ ...((0, guards_1.isPlainObject)(thing1[property]) && (0, guards_1.isPlainObject)(mergedTail[property])
31
+ ? { [property]: (0, exports.merge)(thing1[property], mergedTail[property]) }
32
+ : (Array.isArray(thing1[property]) && Array.isArray(mergedTail[property]))
33
+ ? { [property]: [...thing1[property], ...mergedTail[property]] }
34
+ : {}),
35
+ }), {}),
36
+ };
37
+ };
38
+ exports.merge = merge;
@@ -0,0 +1,4 @@
1
+ export declare const partitionSequence: <T, P>(getPartition: (thing: T, previous?: P | undefined) => {
2
+ matches?: boolean | undefined;
3
+ value: P;
4
+ }, sequence: T[]) => [P, T[]][];
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.partitionSequence = void 0;
7
+ const deep_equal_1 = __importDefault(require("deep-equal"));
8
+ /*
9
+ * partitions a sequence based on a partition function returning {value: any; matches?: boolean}
10
+ * - if the function returns `matches` explicitly then adjacent matching elements will
11
+ * be grouped and the predicate value in the result will be from the last item in the group
12
+ * - if the function returns only a value then matching will be evaluated based on the deep
13
+ * equality of the value with its neighbors
14
+ *
15
+ * this is different from lodash/partition and lodash/groupBy because:
16
+ * - it preserves the order of the items, items will only be grouped if they are already adjacent
17
+ * - there can be any number of groups
18
+ * - it tells you the partition value
19
+ * - the partition value can be reduced, if you care (so you can like, partition on sequential values)
20
+ *
21
+ * simple predicate:
22
+ * returns: [[0, [1,2]], [1, [3,4,5]]]
23
+ * partitionSequence((n: number) => ({value: Math.floor(n / 3)}), [1,2,3,4,5])
24
+ *
25
+ * mutating partition:
26
+ * returns: [
27
+ * [{min: 1,max: 3}, [1,2,3]],
28
+ * [{min: 5,max: 6}, [5,6]],
29
+ * [{min: 8,max: 8}, [8]],
30
+ * ]
31
+ * partitionSequence(
32
+ * (n: number, p?: {min: number; max: number}) =>
33
+ * p && p.max + 1 === n
34
+ * ? {value: {...p, max: n}, matches: true}
35
+ * : {value: {min: n, max: n}, matches: false}
36
+ * , [1,2,3,5,6,8]
37
+ * )
38
+ */
39
+ const partitionSequence = (getPartition, sequence) => {
40
+ const appendItem = (result, item) => {
41
+ const current = result[result.length - 1];
42
+ const itemPartition = getPartition(item, current === null || current === void 0 ? void 0 : current[0]);
43
+ if (current && ((itemPartition.matches === undefined && (0, deep_equal_1.default)(current[0], itemPartition.value))
44
+ || itemPartition.matches)) {
45
+ current[0] = itemPartition.value;
46
+ current[1].push(item);
47
+ }
48
+ else {
49
+ result.push([itemPartition.value, [item]]);
50
+ }
51
+ return result;
52
+ };
53
+ return sequence.reduce(appendItem, []);
54
+ };
55
+ exports.partitionSequence = partitionSequence;
@@ -2,15 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.decryptionAuthProvider = void 0;
4
4
  const jose_1 = require("jose");
5
- const __1 = require("../..");
6
5
  const resolveConfigValue_1 = require("../../config/resolveConfigValue");
7
6
  const guards_1 = require("../../guards");
7
+ const helpers_1 = require("../../misc/helpers");
8
8
  const _1 = require(".");
9
9
  const decryptionAuthProvider = (initializer) => (configProvider) => {
10
10
  const config = configProvider[(0, guards_1.ifDefined)(initializer.configSpace, 'decryption')];
11
- const cookieName = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.cookieName));
12
- const encryptionPrivateKey = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.encryptionPrivateKey));
13
- const signaturePublicKey = (0, __1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.signaturePublicKey));
11
+ const cookieName = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.cookieName));
12
+ const encryptionPrivateKey = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.encryptionPrivateKey));
13
+ const signaturePublicKey = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.signaturePublicKey));
14
14
  const decryptAndVerify = async (jwt) => {
15
15
  try {
16
16
  // Decrypt SSO cookie