@augment-vir/common 23.3.4 → 24.0.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,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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
+ const object_entries_1 = require("../object/object-entries");
5
5
  function filterOutIndexes(array, indexes) {
6
6
  return array.filter((_, index) => !indexes.includes(index));
7
7
  }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeDuplicates = void 0;
4
+ function removeDuplicates(originalArray, calculateUniqueIdCallback) {
5
+ const grouped = new Map();
6
+ return originalArray.filter((entry) => {
7
+ const uniqueId = calculateUniqueIdCallback(entry);
8
+ if (grouped.get(uniqueId)) {
9
+ return false;
10
+ }
11
+ else {
12
+ grouped.set(uniqueId, entry);
13
+ return true;
14
+ }
15
+ });
16
+ }
17
+ exports.removeDuplicates = removeDuplicates;
@@ -52,9 +52,9 @@ function ensureErrorAndPrependMessage(maybeError, prependMessage) {
52
52
  return error;
53
53
  }
54
54
  exports.ensureErrorAndPrependMessage = ensureErrorAndPrependMessage;
55
- function wrapInTry(inputs) {
55
+ function wrapInTry(callback, inputs) {
56
56
  try {
57
- const returnValue = inputs.callback();
57
+ const returnValue = callback();
58
58
  if (returnValue instanceof Promise) {
59
59
  return returnValue.catch((error) => {
60
60
  if (inputs.catchCallback) {
package/dist/cjs/index.js CHANGED
@@ -15,7 +15,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./augments/ansi"), exports);
18
- __exportStar(require("./augments/array"), exports);
18
+ __exportStar(require("./augments/array/array"), exports);
19
+ __exportStar(require("./augments/array/remove-duplicates"), exports);
19
20
  __exportStar(require("./augments/async"), exports);
20
21
  __exportStar(require("./augments/boolean"), exports);
21
22
  __exportStar(require("./augments/common-number"), exports);
@@ -50,7 +51,6 @@ __exportStar(require("./augments/regexp"), exports);
50
51
  __exportStar(require("./augments/string/prefixes"), exports);
51
52
  __exportStar(require("./augments/string/search-params"), exports);
52
53
  __exportStar(require("./augments/string/suffixes"), exports);
53
- __exportStar(require("./augments/string/url"), exports);
54
54
  __exportStar(require("./augments/string/uuid"), exports);
55
55
  __exportStar(require("./augments/time"), exports);
56
56
  __exportStar(require("./augments/truncate-number"), exports);
@@ -1,4 +1,4 @@
1
- import { typedObjectFromEntries } from './object/object-entries';
1
+ import { typedObjectFromEntries } from '../object/object-entries';
2
2
  export function filterOutIndexes(array, indexes) {
3
3
  return array.filter((_, index) => !indexes.includes(index));
4
4
  }
@@ -0,0 +1,13 @@
1
+ export function removeDuplicates(originalArray, calculateUniqueIdCallback) {
2
+ const grouped = new Map();
3
+ return originalArray.filter((entry) => {
4
+ const uniqueId = calculateUniqueIdCallback(entry);
5
+ if (grouped.get(uniqueId)) {
6
+ return false;
7
+ }
8
+ else {
9
+ grouped.set(uniqueId, entry);
10
+ return true;
11
+ }
12
+ });
13
+ }
@@ -44,9 +44,9 @@ export function ensureErrorAndPrependMessage(maybeError, prependMessage) {
44
44
  error.message = `${prependMessage}: ${error.message}`;
45
45
  return error;
46
46
  }
47
- export function wrapInTry(inputs) {
47
+ export function wrapInTry(callback, inputs) {
48
48
  try {
49
- const returnValue = inputs.callback();
49
+ const returnValue = callback();
50
50
  if (returnValue instanceof Promise) {
51
51
  return returnValue.catch((error) => {
52
52
  if (inputs.catchCallback) {
package/dist/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './augments/ansi';
2
- export * from './augments/array';
2
+ export * from './augments/array/array';
3
+ export * from './augments/array/remove-duplicates';
3
4
  export * from './augments/async';
4
5
  export * from './augments/boolean';
5
6
  export * from './augments/common-number';
@@ -34,7 +35,6 @@ export * from './augments/regexp';
34
35
  export * from './augments/string/prefixes';
35
36
  export * from './augments/string/search-params';
36
37
  export * from './augments/string/suffixes';
37
- export * from './augments/string/url';
38
38
  export * from './augments/string/uuid';
39
39
  export * from './augments/time';
40
40
  export * from './augments/truncate-number';
@@ -1,5 +1,5 @@
1
- import { AtLeastTuple } from './tuple';
2
- import { ArrayElement } from './type';
1
+ import { AtLeastTuple } from '../tuple';
2
+ import { ArrayElement } from '../type';
3
3
  export declare function filterOutIndexes<T>(array: ReadonlyArray<T>, indexes: ReadonlyArray<number>): T[];
4
4
  export declare function flatten2dArray<T>(array2d: ReadonlyArray<ReadonlyArray<T>>): T[];
5
5
  export type AtLeastOneEntryArray<ArrayGeneric extends ReadonlyArray<any>> = AtLeastTuple<ArrayElement<ArrayGeneric>, 1>;
@@ -0,0 +1 @@
1
+ export declare function removeDuplicates<Entry, UniqueId>(originalArray: ReadonlyArray<Readonly<Entry>>, calculateUniqueIdCallback: (entry: Readonly<Entry>) => UniqueId): Readonly<Entry>[];
@@ -9,12 +9,10 @@ export declare function combineErrorMessages(errors?: ReadonlyArray<Error | stri
9
9
  export declare function extractErrorMessage(maybeError: unknown): string;
10
10
  export declare function ensureError(maybeError: unknown): Error;
11
11
  export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error;
12
- export type TryWrapInputs<CallbackReturn, FallbackReturn> = {
13
- callback: () => CallbackReturn;
14
- } & RequireExactlyOne<{
12
+ export type TryWrapConfig<FallbackReturn> = RequireExactlyOne<{
15
13
  fallbackValue: FallbackReturn;
16
14
  catchCallback: (error: unknown) => FallbackReturn;
17
15
  }>;
18
- export declare function wrapInTry<CallbackReturn, FallbackReturn>(inputs: TryWrapInputs<CallbackReturn, FallbackReturn>): FallbackReturn | CallbackReturn;
16
+ export declare function wrapInTry<CallbackReturn, FallbackReturn>(callback: () => CallbackReturn, inputs: TryWrapConfig<FallbackReturn>): FallbackReturn | CallbackReturn;
19
17
  export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction<PromiseLike<any>>>(callback: CallbackGeneric): Promise<Error | Awaited<ReturnType<CallbackGeneric>>>;
20
18
  export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction>(callback: CallbackGeneric): Error | ReturnType<CallbackGeneric>;
@@ -1,4 +1,4 @@
1
- import { AtLeastOneEntryArray } from '../array';
1
+ import { AtLeastOneEntryArray } from '../array/array';
2
2
  import { NoInfer } from '../type';
3
3
  /**
4
4
  * Checks that the first input, testThisOne, matches the object shape of the second input,
@@ -1,5 +1,6 @@
1
1
  export * from './augments/ansi';
2
- export * from './augments/array';
2
+ export * from './augments/array/array';
3
+ export * from './augments/array/remove-duplicates';
3
4
  export * from './augments/async';
4
5
  export * from './augments/boolean';
5
6
  export * from './augments/common-number';
@@ -34,7 +35,6 @@ export * from './augments/regexp';
34
35
  export * from './augments/string/prefixes';
35
36
  export * from './augments/string/search-params';
36
37
  export * from './augments/string/suffixes';
37
- export * from './augments/string/url';
38
38
  export * from './augments/string/uuid';
39
39
  export * from './augments/time';
40
40
  export * from './augments/truncate-number';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "23.3.4",
3
+ "version": "24.0.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"
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "browser-or-node": "^2.1.1",
28
28
  "run-time-assertions": "^1.0.0",
29
- "type-fest": "^4.10.2"
29
+ "type-fest": "^4.12.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "typescript": "5.3.3"
@@ -1,73 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.joinUrlParts = void 0;
4
- const protocolSplit = '://';
5
- /**
6
- * Joins all given arguments together as if they were parts of a URL. Preserves trailing slashes and
7
- * removes consecutive slashes in the path.
8
- *
9
- * @example: joinToUrl('https://example.com', 'path1', 'path2/', '/path3/') === 'https://example.com/path1/path2/path3/'
10
- */
11
- function joinUrlParts(...urlParts) {
12
- const rawJoined = urlParts.join('/');
13
- const [protocol, rawRest = '',] = rawJoined.includes(protocolSplit)
14
- ? rawJoined.split(protocolSplit)
15
- : [
16
- '',
17
- rawJoined,
18
- ];
19
- let mapSearchParamsStarted = false;
20
- let reduceSearchParamsStarted = false;
21
- const fixedRest = rawRest
22
- .replace(/\/{2,}/g, '/')
23
- .split('/')
24
- .map((part) => {
25
- if (part.includes('?') || mapSearchParamsStarted) {
26
- mapSearchParamsStarted = true;
27
- return part;
28
- }
29
- else {
30
- return part;
31
- }
32
- })
33
- .reduce((fillingUpArray, currentEntry, currentIndex, inputArray) => {
34
- if (reduceSearchParamsStarted) {
35
- return fillingUpArray;
36
- }
37
- const nextEntry = inputArray[currentIndex + 1];
38
- let newEntry = currentEntry;
39
- const nextHasQuestion = !currentEntry.includes('?') && nextEntry?.startsWith('?');
40
- if (nextEntry?.startsWith('?') || nextHasQuestion) {
41
- reduceSearchParamsStarted = true;
42
- let foundHash = false;
43
- const subsequentSearchParams = inputArray
44
- .slice(nextHasQuestion ? currentIndex + 2 : currentIndex + 1)
45
- .reduce((joinedParams, currentParam) => {
46
- if (currentParam.includes('#')) {
47
- foundHash = true;
48
- }
49
- if (foundHash) {
50
- return joinedParams.concat(currentParam);
51
- }
52
- else {
53
- return [
54
- joinedParams,
55
- currentParam,
56
- ].join('&');
57
- }
58
- }, '');
59
- newEntry = [
60
- currentEntry,
61
- nextEntry,
62
- subsequentSearchParams,
63
- ].join('');
64
- }
65
- return fillingUpArray.concat(newEntry);
66
- }, []);
67
- return [
68
- protocol,
69
- protocol ? protocolSplit : '',
70
- fixedRest.join('/'),
71
- ].join('');
72
- }
73
- exports.joinUrlParts = joinUrlParts;
@@ -1,69 +0,0 @@
1
- const protocolSplit = '://';
2
- /**
3
- * Joins all given arguments together as if they were parts of a URL. Preserves trailing slashes and
4
- * removes consecutive slashes in the path.
5
- *
6
- * @example: joinToUrl('https://example.com', 'path1', 'path2/', '/path3/') === 'https://example.com/path1/path2/path3/'
7
- */
8
- export function joinUrlParts(...urlParts) {
9
- const rawJoined = urlParts.join('/');
10
- const [protocol, rawRest = '',] = rawJoined.includes(protocolSplit)
11
- ? rawJoined.split(protocolSplit)
12
- : [
13
- '',
14
- rawJoined,
15
- ];
16
- let mapSearchParamsStarted = false;
17
- let reduceSearchParamsStarted = false;
18
- const fixedRest = rawRest
19
- .replace(/\/{2,}/g, '/')
20
- .split('/')
21
- .map((part) => {
22
- if (part.includes('?') || mapSearchParamsStarted) {
23
- mapSearchParamsStarted = true;
24
- return part;
25
- }
26
- else {
27
- return part;
28
- }
29
- })
30
- .reduce((fillingUpArray, currentEntry, currentIndex, inputArray) => {
31
- if (reduceSearchParamsStarted) {
32
- return fillingUpArray;
33
- }
34
- const nextEntry = inputArray[currentIndex + 1];
35
- let newEntry = currentEntry;
36
- const nextHasQuestion = !currentEntry.includes('?') && nextEntry?.startsWith('?');
37
- if (nextEntry?.startsWith('?') || nextHasQuestion) {
38
- reduceSearchParamsStarted = true;
39
- let foundHash = false;
40
- const subsequentSearchParams = inputArray
41
- .slice(nextHasQuestion ? currentIndex + 2 : currentIndex + 1)
42
- .reduce((joinedParams, currentParam) => {
43
- if (currentParam.includes('#')) {
44
- foundHash = true;
45
- }
46
- if (foundHash) {
47
- return joinedParams.concat(currentParam);
48
- }
49
- else {
50
- return [
51
- joinedParams,
52
- currentParam,
53
- ].join('&');
54
- }
55
- }, '');
56
- newEntry = [
57
- currentEntry,
58
- nextEntry,
59
- subsequentSearchParams,
60
- ].join('');
61
- }
62
- return fillingUpArray.concat(newEntry);
63
- }, []);
64
- return [
65
- protocol,
66
- protocol ? protocolSplit : '',
67
- fixedRest.join('/'),
68
- ].join('');
69
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * Joins all given arguments together as if they were parts of a URL. Preserves trailing slashes and
3
- * removes consecutive slashes in the path.
4
- *
5
- * @example: joinToUrl('https://example.com', 'path1', 'path2/', '/path3/') === 'https://example.com/path1/path2/path3/'
6
- */
7
- export declare function joinUrlParts(...urlParts: ReadonlyArray<string>): string;