@dereekb/util 9.7.4 → 9.7.5

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/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [9.7.5](https://github.com/dereekb/dbx-components/compare/v9.7.4-dev...v9.7.5) (2022-09-10)
6
+
7
+
8
+
5
9
  ## [9.7.4](https://github.com/dereekb/dbx-components/compare/v9.7.3-dev...v9.7.4) (2022-09-08)
6
10
 
7
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/util",
3
- "version": "9.7.4",
3
+ "version": "9.7.5",
4
4
  "type": "commonjs",
5
5
  "exports": {
6
6
  ".": {
@@ -0,0 +1,67 @@
1
+ import { FactoryWithRequiredInput, PrimativeKey } from '@dereekb/util';
2
+ /**
3
+ * Map object of PrimativeKey dencoder values, keyed by the encoded value.
4
+ */
5
+ export declare type PrimativeKeyDencoderMap<D extends PrimativeKey, E extends PrimativeKey> = {
6
+ [key in E]: D;
7
+ };
8
+ export declare type PrimativeKeyDencoderTuple<D extends PrimativeKey, E extends PrimativeKey> = [E, D];
9
+ export declare type PrimativeKeyDencoderTupleArray<D extends PrimativeKey, E extends PrimativeKey> = PrimativeKeyDencoderTuple<D, E>[];
10
+ /**
11
+ * PrimativeKeyDencoder values. No key or value should be repeated.
12
+ */
13
+ export declare type PrimativeKeyDencoderValues<D extends PrimativeKey, E extends PrimativeKey> = PrimativeKeyDencoderTupleArray<D, E> | PrimativeKeyDencoderMap<D, E>;
14
+ export declare type PrimativeKeyDeconderMap<D extends PrimativeKey, E extends PrimativeKey> = Map<D | E, E | D> & {
15
+ readonly _tuples: PrimativeKeyDencoderTupleArray<D, E>;
16
+ };
17
+ /**
18
+ * Creates a Map of the PrimativeKeyDencoder values.
19
+ *
20
+ * If any repeat values are found, an error will be thrown.
21
+ *
22
+ * @param values
23
+ */
24
+ export declare function primativeKeyDencoderMap<D extends PrimativeKey, E extends PrimativeKey>(values: PrimativeKeyDencoderValues<D, E>): PrimativeKeyDeconderMap<D, E>;
25
+ export interface PrimativeKeyDencoderConfig<D extends PrimativeKey, E extends PrimativeKey> {
26
+ readonly values: PrimativeKeyDencoderValues<D, E>;
27
+ readonly defaultValue?: FactoryWithRequiredInput<D | E, D | E>;
28
+ }
29
+ /**
30
+ * Used for encoding/decoding pre-configured strings values from a map.
31
+ *
32
+ * If a single value is input that produces a nullish value, an error is thrown.
33
+ */
34
+ export declare type PrimativeKeyDencoderFunction<D extends PrimativeKey, E extends PrimativeKey> = ((encodedValue: E) => D) & ((decodedValue: D) => E) & ((encodedValues: E[]) => D[]) & ((decodedValues: D[]) => E[]) & {
35
+ readonly _map: PrimativeKeyDeconderMap<D, E>;
36
+ };
37
+ export declare const PRIMATIVE_KEY_DENCODER_VALUE: (input: unknown) => null;
38
+ /**
39
+ * Creates a new PrimiativeKeyDencoder.
40
+ */
41
+ export declare function primativeKeyDencoder<D extends PrimativeKey, E extends PrimativeKey>(config: PrimativeKeyDencoderConfig<D, E>): PrimativeKeyDencoderFunction<D, E>;
42
+ /**
43
+ * Used to decode
44
+ */
45
+ export interface PrimativeKeyStringDencoderConfig<D extends PrimativeKey, E extends PrimativeKey> {
46
+ /**
47
+ * Dencoder to use, or config to create one.
48
+ */
49
+ readonly dencoder: PrimativeKeyDencoderConfig<D, E> | PrimativeKeyDencoderFunction<D, E>;
50
+ /**
51
+ * Splitter value. If not defined then the max size of the "encoded" values must be 1 character.
52
+ */
53
+ readonly splitter?: string;
54
+ }
55
+ /**
56
+ * Maps the input encode/decode value to the proper value.
57
+ *
58
+ * If a single value is input that produces a nullish value, an error is thrown.
59
+ */
60
+ export declare type PrimativeKeyStringDencoderFunction<D extends PrimativeKey, E extends PrimativeKey> = ((encodedValues: string) => (E | D)[]) & ((decodedValues: (E | D)[]) => string);
61
+ /**
62
+ * Creates a new PrimativeKeyStringDencoderFunction.
63
+ *
64
+ * @param config
65
+ * @returns
66
+ */
67
+ export declare function primativeKeyStringDencoder<D extends PrimativeKey, E extends PrimativeKey>(config: PrimativeKeyStringDencoderConfig<D, E>): PrimativeKeyStringDencoderFunction<D, E>;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.primativeKeyStringDencoder = exports.primativeKeyDencoder = exports.PRIMATIVE_KEY_DENCODER_VALUE = exports.primativeKeyDencoderMap = void 0;
4
+ const util_1 = require("@dereekb/util");
5
+ /**
6
+ * Creates a Map of the PrimativeKeyDencoder values.
7
+ *
8
+ * If any repeat values are found, an error will be thrown.
9
+ *
10
+ * @param values
11
+ */
12
+ function primativeKeyDencoderMap(values) {
13
+ const map = new Map();
14
+ let valuesArray;
15
+ if (!Array.isArray(values)) {
16
+ valuesArray = [];
17
+ (0, util_1.forEachKeyValue)(values, {
18
+ forEach: (pair) => {
19
+ valuesArray.push(pair);
20
+ },
21
+ filter: util_1.KeyValueTypleValueFilter.UNDEFINED
22
+ });
23
+ }
24
+ else {
25
+ valuesArray = values;
26
+ }
27
+ valuesArray.forEach((value) => {
28
+ const [d, e] = value;
29
+ if (map.has(d) || map.has(e)) {
30
+ throw new Error(`primativeKeyDencoderMap() encountered a repeat key/value: ${d}/${e}. Keys and values must be unique.`);
31
+ }
32
+ map.set(d, e);
33
+ map.set(e, d);
34
+ });
35
+ map._tuples = valuesArray;
36
+ return map;
37
+ }
38
+ exports.primativeKeyDencoderMap = primativeKeyDencoderMap;
39
+ const PRIMATIVE_KEY_DENCODER_VALUE = (input) => null;
40
+ exports.PRIMATIVE_KEY_DENCODER_VALUE = PRIMATIVE_KEY_DENCODER_VALUE;
41
+ /**
42
+ * Creates a new PrimiativeKeyDencoder.
43
+ */
44
+ function primativeKeyDencoder(config) {
45
+ const { defaultValue = exports.PRIMATIVE_KEY_DENCODER_VALUE } = config;
46
+ const map = primativeKeyDencoderMap(config.values);
47
+ const fn = ((input) => {
48
+ if (Array.isArray(input)) {
49
+ const values = (0, util_1.filterMaybeValues)(input.map((x) => map.get(x)));
50
+ return values;
51
+ }
52
+ else {
53
+ let value = map.get(input);
54
+ if (value == null) {
55
+ value = defaultValue(input);
56
+ if (value == null) {
57
+ throw new Error(`Encountered unknown value ${input} in primativeKeyDencoder.`);
58
+ }
59
+ }
60
+ return value;
61
+ }
62
+ });
63
+ fn._map = map;
64
+ return fn;
65
+ }
66
+ exports.primativeKeyDencoder = primativeKeyDencoder;
67
+ /**
68
+ * Creates a new PrimativeKeyStringDencoderFunction.
69
+ *
70
+ * @param config
71
+ * @returns
72
+ */
73
+ function primativeKeyStringDencoder(config) {
74
+ const dencoder = typeof config.dencoder === 'function' ? config.dencoder : primativeKeyDencoder(config.dencoder);
75
+ const { splitter } = config;
76
+ const { _map: dencoderMap } = dencoder;
77
+ if (splitter) {
78
+ dencoderMap._tuples.forEach((x) => {
79
+ if (x[0].toString().indexOf(splitter) !== -1) {
80
+ throw new Error(`primativeKeyStringDencoder() encountered a value (${x[0]}) that contains the splitter (${splitter}).`);
81
+ }
82
+ });
83
+ }
84
+ else {
85
+ // Assert all encoded values are 1 character long max.
86
+ dencoderMap._tuples.forEach((x) => {
87
+ if (x[0].toString().length !== 1) {
88
+ throw new Error(`primativeKeyStringDencoder() without a splitter defined cannot use encoded values of a length greater than 1. Encountered encoded "${x[0]}".`);
89
+ }
90
+ });
91
+ }
92
+ const joiner = splitter || '';
93
+ const splitEncodedValues = splitter ? (encodedValues) => encodedValues.split(splitter) : (encodedValues) => new Array(encodedValues);
94
+ return (input) => {
95
+ if (typeof input === 'string') {
96
+ const split = splitEncodedValues(input);
97
+ return dencoder(split);
98
+ }
99
+ else {
100
+ const encoded = dencoder(input);
101
+ return encoded.join(joiner);
102
+ }
103
+ };
104
+ }
105
+ exports.primativeKeyStringDencoder = primativeKeyStringDencoder;
106
+ //# sourceMappingURL=dencoder.js.map
@@ -0,0 +1 @@
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
1
  export * from './char';
2
+ export * from './dencoder';
2
3
  export * from './string';
3
4
  export * from './replace';
4
5
  export * from './transform';
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./char"), exports);
5
+ tslib_1.__exportStar(require("./dencoder"), exports);
5
6
  tslib_1.__exportStar(require("./string"), exports);
6
7
  tslib_1.__exportStar(require("./replace"), exports);
7
8
  tslib_1.__exportStar(require("./transform"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/string/index.ts"],"names":[],"mappings":";;;AAAA,iDAAuB;AACvB,mDAAyB;AACzB,oDAA0B;AAC1B,sDAA4B;AAC5B,gDAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/string/index.ts"],"names":[],"mappings":";;;AAAA,iDAAuB;AACvB,qDAA2B;AAC3B,mDAAyB;AACzB,oDAA0B;AAC1B,sDAA4B;AAC5B,gDAAsB"}
package/test/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [9.7.5](https://github.com/dereekb/dbx-components/compare/v9.7.4-dev...v9.7.5) (2022-09-10)
6
+
7
+
8
+
5
9
  ## [9.7.4](https://github.com/dereekb/dbx-components/compare/v9.7.3-dev...v9.7.4) (2022-09-08)
6
10
 
7
11
 
package/test/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@dereekb/util/test",
3
- "version": "9.7.4",
3
+ "version": "9.7.5",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./src/index.d.ts",
7
7
  "dependencies": {},
8
8
  "peerDependencies": {
9
- "@dereekb/util": "9.7.4",
9
+ "@dereekb/util": "9.7.5",
10
10
  "lodash.isequal": "^4.5.0",
11
11
  "make-error": "^1.3.0",
12
12
  "ts-essentials": "^9.1.2",