@augment-vir/common 23.1.1 → 23.3.0

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,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.repeatArray = exports.typedMap = exports.typedArrayIncludes = exports.trimArrayStrings = exports.flatten2dArray = exports.filterOutIndexes = void 0;
3
+ exports.filterMap = exports.arrayToObject = exports.groupArrayBy = exports.repeatArray = exports.typedMap = exports.typedArrayIncludes = exports.trimArrayStrings = exports.flatten2dArray = exports.filterOutIndexes = void 0;
4
+ const object_entries_1 = require("./object/object-entries");
4
5
  function filterOutIndexes(array, indexes) {
5
6
  return array.filter((_, index) => !indexes.includes(index));
6
7
  }
@@ -27,3 +28,43 @@ function repeatArray(repeatCount, array) {
27
28
  return Array.from({ length: repeatCount }, () => [...array]).flat();
28
29
  }
29
30
  exports.repeatArray = repeatArray;
31
+ /**
32
+ * Polyfill for `Object.groupBy`:
33
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
34
+ */
35
+ function groupArrayBy(inputArray, callback) {
36
+ return inputArray.reduce((accum, entry, index, originalArray) => {
37
+ const key = callback(entry, index, originalArray);
38
+ if (!(key in accum)) {
39
+ accum[key] = [];
40
+ }
41
+ accum[key].push(entry);
42
+ return accum;
43
+ }, {});
44
+ }
45
+ exports.groupArrayBy = groupArrayBy;
46
+ /**
47
+ * Like `groupArrayBy` but maps array entries to a single key. Meaning, the resulting object does
48
+ * not have an array of elements (unless the original array itself contains arrays).
49
+ */
50
+ function arrayToObject(inputArray, callback) {
51
+ return (0, object_entries_1.typedObjectFromEntries)(inputArray.map((entry, index, originalArray) => {
52
+ const key = callback(entry, index, originalArray);
53
+ return [
54
+ key,
55
+ entry,
56
+ ];
57
+ }));
58
+ }
59
+ exports.arrayToObject = arrayToObject;
60
+ function filterMap(inputArray, mapCallback, filterCallback) {
61
+ return inputArray.reduce((accum, entry, index, originalArray) => {
62
+ const mapOutput = mapCallback(entry, index, originalArray);
63
+ const filterOutput = filterCallback(mapOutput, entry, index, originalArray);
64
+ if (filterOutput) {
65
+ accum.push(mapOutput);
66
+ }
67
+ return accum;
68
+ }, []);
69
+ }
70
+ exports.filterMap = filterMap;
@@ -38,12 +38,12 @@ function removeCommasFromNumberString(numberString) {
38
38
  }
39
39
  exports.removeCommasFromNumberString = removeCommasFromNumberString;
40
40
  /** Collapse all consecutive white space into just one space and trim surrounding whitespace. */
41
- function collapseWhiteSpace(input) {
42
- return (input
43
- // sometimes \n isn't included in \s
44
- .replace(/\n/g, ' ')
45
- .trim()
46
- .replace(/\s{2,}/g, ' '));
41
+ function collapseWhiteSpace(input, { keepNewLines } = {}) {
42
+ const newLineReplacement = keepNewLines
43
+ ? input.replace(/[\s\n]*\n+[\s\n]*/g, '\n')
44
+ : // sometimes \n isn't included in \s
45
+ input.replace(/\n/g, ' ');
46
+ return newLineReplacement.trim().replace(/\s{2,}/g, ' ');
47
47
  }
48
48
  exports.collapseWhiteSpace = collapseWhiteSpace;
49
49
  /** Same as String.prototype.split but includes the delimiter to split by in the output array. */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.typedObjectFromEntries = exports.getEntriesSortedByKey = exports.getObjectTypedValues = exports.getObjectTypedKeys = exports.isKeyof = void 0;
3
+ exports.typedObjectFromEntries = exports.getEntriesSortedByKey = exports.getObjectTypedEntries = exports.getObjectTypedValues = exports.getObjectTypedKeys = exports.isKeyof = void 0;
4
4
  const typed_has_property_1 = require("./typed-has-property");
5
5
  /** @deprecated This is the same as hasKey */
6
6
  function isKeyof(key, object) {
@@ -24,6 +24,13 @@ function getObjectTypedValues(input) {
24
24
  return getObjectTypedKeys(input).map((key) => input[key]);
25
25
  }
26
26
  exports.getObjectTypedValues = getObjectTypedValues;
27
+ function getObjectTypedEntries(input) {
28
+ return getObjectTypedKeys(input).map((key) => [
29
+ key,
30
+ input[key],
31
+ ]);
32
+ }
33
+ exports.getObjectTypedEntries = getObjectTypedEntries;
27
34
  function getEntriesSortedByKey(input) {
28
35
  return Object.entries(input).sort((tupleA, tupleB) => tupleA[0].localeCompare(tupleB[0]));
29
36
  }
@@ -1,3 +1,4 @@
1
+ import { typedObjectFromEntries } from './object/object-entries';
1
2
  export function filterOutIndexes(array, indexes) {
2
3
  return array.filter((_, index) => !indexes.includes(index));
3
4
  }
@@ -18,3 +19,40 @@ export function typedMap(arrayToMap, mapCallback) {
18
19
  export function repeatArray(repeatCount, array) {
19
20
  return Array.from({ length: repeatCount }, () => [...array]).flat();
20
21
  }
22
+ /**
23
+ * Polyfill for `Object.groupBy`:
24
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
25
+ */
26
+ export function groupArrayBy(inputArray, callback) {
27
+ return inputArray.reduce((accum, entry, index, originalArray) => {
28
+ const key = callback(entry, index, originalArray);
29
+ if (!(key in accum)) {
30
+ accum[key] = [];
31
+ }
32
+ accum[key].push(entry);
33
+ return accum;
34
+ }, {});
35
+ }
36
+ /**
37
+ * Like `groupArrayBy` but maps array entries to a single key. Meaning, the resulting object does
38
+ * not have an array of elements (unless the original array itself contains arrays).
39
+ */
40
+ export function arrayToObject(inputArray, callback) {
41
+ return typedObjectFromEntries(inputArray.map((entry, index, originalArray) => {
42
+ const key = callback(entry, index, originalArray);
43
+ return [
44
+ key,
45
+ entry,
46
+ ];
47
+ }));
48
+ }
49
+ export function filterMap(inputArray, mapCallback, filterCallback) {
50
+ return inputArray.reduce((accum, entry, index, originalArray) => {
51
+ const mapOutput = mapCallback(entry, index, originalArray);
52
+ const filterOutput = filterCallback(mapOutput, entry, index, originalArray);
53
+ if (filterOutput) {
54
+ accum.push(mapOutput);
55
+ }
56
+ return accum;
57
+ }, []);
58
+ }
@@ -32,12 +32,12 @@ export function removeCommasFromNumberString(numberString) {
32
32
  return numberString.replace(/,/g, '');
33
33
  }
34
34
  /** Collapse all consecutive white space into just one space and trim surrounding whitespace. */
35
- export function collapseWhiteSpace(input) {
36
- return (input
37
- // sometimes \n isn't included in \s
38
- .replace(/\n/g, ' ')
39
- .trim()
40
- .replace(/\s{2,}/g, ' '));
35
+ export function collapseWhiteSpace(input, { keepNewLines } = {}) {
36
+ const newLineReplacement = keepNewLines
37
+ ? input.replace(/[\s\n]*\n+[\s\n]*/g, '\n')
38
+ : // sometimes \n isn't included in \s
39
+ input.replace(/\n/g, ' ');
40
+ return newLineReplacement.trim().replace(/\s{2,}/g, ' ');
41
41
  }
42
42
  /** Same as String.prototype.split but includes the delimiter to split by in the output array. */
43
43
  export function splitIncludeSplit(original, splitterInput, caseSensitive) {
@@ -18,6 +18,12 @@ export function getObjectTypedKeys(input) {
18
18
  export function getObjectTypedValues(input) {
19
19
  return getObjectTypedKeys(input).map((key) => input[key]);
20
20
  }
21
+ export function getObjectTypedEntries(input) {
22
+ return getObjectTypedKeys(input).map((key) => [
23
+ key,
24
+ input[key],
25
+ ]);
26
+ }
21
27
  export function getEntriesSortedByKey(input) {
22
28
  return Object.entries(input).sort((tupleA, tupleB) => tupleA[0].localeCompare(tupleB[0]));
23
29
  }
@@ -11,4 +11,15 @@ export declare function typedMap<InputArrayGeneric extends ReadonlyArray<any>, O
11
11
  [Index in keyof InputArrayGeneric]: OutputType;
12
12
  };
13
13
  export declare function repeatArray<T>(repeatCount: number, array: T[]): T[];
14
+ /**
15
+ * Polyfill for `Object.groupBy`:
16
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
17
+ */
18
+ export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Record<NewKey, ElementType[]>;
19
+ /**
20
+ * Like `groupArrayBy` but maps array entries to a single key. Meaning, the resulting object does
21
+ * not have an array of elements (unless the original array itself contains arrays).
22
+ */
23
+ export declare function arrayToObject<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Record<NewKey, ElementType>;
24
+ export declare function filterMap<ElementType, MapReturn>(inputArray: ReadonlyArray<ElementType>, mapCallback: (entry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MapReturn, filterCallback: (mappedOutput: MapReturn, originalEntry: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => boolean): MapReturn[];
14
25
  export {};
@@ -14,7 +14,9 @@ export declare function removeAnsiEscapeCodes(input: string): string;
14
14
  export declare const removeColor: typeof removeAnsiEscapeCodes;
15
15
  export declare function removeCommasFromNumberString(numberString: string): string;
16
16
  /** Collapse all consecutive white space into just one space and trim surrounding whitespace. */
17
- export declare function collapseWhiteSpace(input: string): string;
17
+ export declare function collapseWhiteSpace(input: string, { keepNewLines }?: PartialAndUndefined<{
18
+ keepNewLines: boolean;
19
+ }>): string;
18
20
  /** Same as String.prototype.split but includes the delimiter to split by in the output array. */
19
21
  export declare function splitIncludeSplit(original: string, splitterInput: string | RegExp, caseSensitive: boolean): readonly string[];
20
22
  export type CasingOptions = {
@@ -2,5 +2,6 @@
2
2
  export declare function isKeyof<ObjectGeneric>(key: PropertyKey, object: ObjectGeneric): key is keyof ObjectGeneric;
3
3
  export declare function getObjectTypedKeys<ObjectGeneric extends unknown>(input: ObjectGeneric): Array<keyof ObjectGeneric>;
4
4
  export declare function getObjectTypedValues<ObjectGeneric extends unknown>(input: ObjectGeneric): ObjectGeneric[keyof ObjectGeneric][];
5
+ export declare function getObjectTypedEntries<ObjectGeneric extends unknown>(input: ObjectGeneric): [keyof ObjectGeneric, ObjectGeneric[keyof ObjectGeneric]][];
5
6
  export declare function getEntriesSortedByKey(input: object): [string, unknown][];
6
7
  export declare function typedObjectFromEntries<KeyType extends PropertyKey, ValueType>(entries: ReadonlyArray<Readonly<[KeyType, ValueType]>>): Record<KeyType, ValueType>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "23.1.1",
3
+ "version": "23.3.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"