@augment-vir/core 31.36.0 → 31.37.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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Removes duplicates from an array. Optionally provide a callback for calculating a unique id for
3
+ * entries.
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @example No callback
8
+ *
9
+ * ```ts
10
+ * import {removeDuplicates} from '@augment-vir/common';
11
+ *
12
+ * const result = removeDuplicates([
13
+ * 1,
14
+ * 1,
15
+ * 1,
16
+ * 1,
17
+ * 1,
18
+ * 2,
19
+ * 4,
20
+ * ]);
21
+ * // result is `[1, 2, 4]`
22
+ *
23
+ * const exampleEntry = {id: 5};
24
+ *
25
+ * const result2 = removeDuplicates([
26
+ * {id: 1},
27
+ * // this entry will not get filtered out because it's a new object reference
28
+ * {id: 1},
29
+ * exampleEntry,
30
+ * // this `exampleEntry` will get filtered out because it's the same reference as the one above
31
+ * exampleEntry,
32
+ * {id: 4},
33
+ * ]);
34
+ * // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
35
+ * ```
36
+ *
37
+ * @example With callback
38
+ *
39
+ * ```ts
40
+ * import {removeDuplicates} from '@augment-vir/common';
41
+ *
42
+ * const exampleEntry = {id: 5};
43
+ *
44
+ * const result2 = removeDuplicates(
45
+ * [
46
+ * {id: 1},
47
+ * {id: 1},
48
+ * exampleEntry,
49
+ * exampleEntry,
50
+ * {id: 4},
51
+ * ],
52
+ * (entry) => entry.id,
53
+ * );
54
+ * // result2 is `[{id: 1}, exampleEntry, {id: 4}]`
55
+ * ```
56
+ *
57
+ * @returns A new array (does not mutate).
58
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
59
+ */
60
+ export declare function removeDuplicates<const Entry>(originalArray: ReadonlyArray<Entry>, calculateUniqueId?: (entry: Readonly<Entry>) => unknown): Entry[];
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Removes duplicates from an array. Optionally provide a callback for calculating a unique id for
3
+ * entries.
4
+ *
5
+ * @category Array
6
+ * @category Package : @augment-vir/common
7
+ * @example No callback
8
+ *
9
+ * ```ts
10
+ * import {removeDuplicates} from '@augment-vir/common';
11
+ *
12
+ * const result = removeDuplicates([
13
+ * 1,
14
+ * 1,
15
+ * 1,
16
+ * 1,
17
+ * 1,
18
+ * 2,
19
+ * 4,
20
+ * ]);
21
+ * // result is `[1, 2, 4]`
22
+ *
23
+ * const exampleEntry = {id: 5};
24
+ *
25
+ * const result2 = removeDuplicates([
26
+ * {id: 1},
27
+ * // this entry will not get filtered out because it's a new object reference
28
+ * {id: 1},
29
+ * exampleEntry,
30
+ * // this `exampleEntry` will get filtered out because it's the same reference as the one above
31
+ * exampleEntry,
32
+ * {id: 4},
33
+ * ]);
34
+ * // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
35
+ * ```
36
+ *
37
+ * @example With callback
38
+ *
39
+ * ```ts
40
+ * import {removeDuplicates} from '@augment-vir/common';
41
+ *
42
+ * const exampleEntry = {id: 5};
43
+ *
44
+ * const result2 = removeDuplicates(
45
+ * [
46
+ * {id: 1},
47
+ * {id: 1},
48
+ * exampleEntry,
49
+ * exampleEntry,
50
+ * {id: 4},
51
+ * ],
52
+ * (entry) => entry.id,
53
+ * );
54
+ * // result2 is `[{id: 1}, exampleEntry, {id: 4}]`
55
+ * ```
56
+ *
57
+ * @returns A new array (does not mutate).
58
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
59
+ */
60
+ export function removeDuplicates(originalArray, calculateUniqueId = (entry) => entry) {
61
+ const grouped = new Map();
62
+ return originalArray.filter((entry) => {
63
+ const uniqueId = calculateUniqueId(entry);
64
+ if (grouped.get(uniqueId)) {
65
+ return false;
66
+ }
67
+ else {
68
+ grouped.set(uniqueId, entry);
69
+ return true;
70
+ }
71
+ });
72
+ }
@@ -25,6 +25,9 @@ space) {
25
25
  if (value === undefined) {
26
26
  return undefinedSentinel;
27
27
  }
28
+ else if (typeof value === 'bigint') {
29
+ return Number(value);
30
+ }
28
31
  return value;
29
32
  }, space || undefined);
30
33
  return json5String.split(undefinedSentinelStringRegExp).join('undefined');
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Creates a new RegExp by adding the given `flags` to the original RegExp.
3
+ *
4
+ * @category RegExp
5
+ * @category Package : @augment-vir/common
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {addRegExpFlags} from '@augment-vir/common';
10
+ *
11
+ * addRegExpFlags(/a/i, 'gm');
12
+ * // output is `/a/igm`
13
+ * ```
14
+ *
15
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
16
+ */
17
+ export declare function addRegExpFlags(originalRegExpOrString: RegExp | string, flags: string): RegExp;
18
+ /**
19
+ * Creates a new RegExp with the given `flags`.
20
+ *
21
+ * @category RegExp
22
+ * @category Package : @augment-vir/common
23
+ * @example
24
+ *
25
+ * ```ts
26
+ * import {setRegExpFlags} from '@augment-vir/common';
27
+ *
28
+ * setRegExpFlags(/a/i, 'gm');
29
+ * // output is `/a/gm`
30
+ * ```
31
+ *
32
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
33
+ */
34
+ export declare function setRegExpFlags(originalRegExpOrString: RegExp | string, flags: string): RegExp;
35
+ /**
36
+ * Creates a new RegExp by adding or removing the case insensitivity flag `'i'`, based on the given
37
+ * `caseSensitive` input. The first input can also be a string and it will be converted into a
38
+ * RegExp.
39
+ *
40
+ * @category RegExp
41
+ * @category Package : @augment-vir/common
42
+ * @example
43
+ *
44
+ * ```ts
45
+ * import {setRegExpCaseSensitivity} from '@augment-vir/common';
46
+ *
47
+ * setRegExpCaseSensitivity(/abc/i, {caseSensitive: true}); // output is `/abc/`
48
+ * setRegExpCaseSensitivity(/abc/, {caseSensitive: false}); // output is `/abc/i`
49
+ * setRegExpCaseSensitivity('abc', {caseSensitive: true}); // output is `/abc/i`
50
+ * ```
51
+ *
52
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
53
+ */
54
+ export declare function setRegExpCaseSensitivity(originalRegExpOrString: string | RegExp, { caseSensitive }: {
55
+ caseSensitive: boolean;
56
+ }): RegExp;
@@ -0,0 +1,80 @@
1
+ import { removeDuplicateCharacters } from '../string/remove-duplicate-characters.js';
2
+ import { escapeStringForRegExp } from './regexp-string.js';
3
+ /**
4
+ * Creates a new RegExp by adding the given `flags` to the original RegExp.
5
+ *
6
+ * @category RegExp
7
+ * @category Package : @augment-vir/common
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import {addRegExpFlags} from '@augment-vir/common';
12
+ *
13
+ * addRegExpFlags(/a/i, 'gm');
14
+ * // output is `/a/igm`
15
+ * ```
16
+ *
17
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
18
+ */
19
+ export function addRegExpFlags(originalRegExpOrString, flags) {
20
+ const allFlags = removeDuplicateCharacters([
21
+ typeof originalRegExpOrString === 'string' ? '' : originalRegExpOrString.flags,
22
+ flags,
23
+ ]
24
+ .join('')
25
+ .toLowerCase());
26
+ return setRegExpFlags(originalRegExpOrString, allFlags);
27
+ }
28
+ /**
29
+ * Creates a new RegExp with the given `flags`.
30
+ *
31
+ * @category RegExp
32
+ * @category Package : @augment-vir/common
33
+ * @example
34
+ *
35
+ * ```ts
36
+ * import {setRegExpFlags} from '@augment-vir/common';
37
+ *
38
+ * setRegExpFlags(/a/i, 'gm');
39
+ * // output is `/a/gm`
40
+ * ```
41
+ *
42
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
43
+ */
44
+ export function setRegExpFlags(originalRegExpOrString, flags) {
45
+ const allFlags = removeDuplicateCharacters(flags);
46
+ if (typeof originalRegExpOrString === 'string') {
47
+ return new RegExp(escapeStringForRegExp(originalRegExpOrString), allFlags);
48
+ }
49
+ else {
50
+ return new RegExp(originalRegExpOrString.source, allFlags);
51
+ }
52
+ }
53
+ /**
54
+ * Creates a new RegExp by adding or removing the case insensitivity flag `'i'`, based on the given
55
+ * `caseSensitive` input. The first input can also be a string and it will be converted into a
56
+ * RegExp.
57
+ *
58
+ * @category RegExp
59
+ * @category Package : @augment-vir/common
60
+ * @example
61
+ *
62
+ * ```ts
63
+ * import {setRegExpCaseSensitivity} from '@augment-vir/common';
64
+ *
65
+ * setRegExpCaseSensitivity(/abc/i, {caseSensitive: true}); // output is `/abc/`
66
+ * setRegExpCaseSensitivity(/abc/, {caseSensitive: false}); // output is `/abc/i`
67
+ * setRegExpCaseSensitivity('abc', {caseSensitive: true}); // output is `/abc/i`
68
+ * ```
69
+ *
70
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
71
+ */
72
+ export function setRegExpCaseSensitivity(originalRegExpOrString, { caseSensitive }) {
73
+ const originalFlags = typeof originalRegExpOrString === 'string'
74
+ ? ''
75
+ : originalRegExpOrString.flags.toLowerCase();
76
+ const newFlags = caseSensitive
77
+ ? originalFlags.replaceAll('i', '')
78
+ : removeDuplicateCharacters(originalFlags + 'i');
79
+ return setRegExpFlags(originalRegExpOrString, newFlags);
80
+ }
@@ -1,8 +1,8 @@
1
1
  /**
2
- * A case insensitive match between strings.
2
+ * A case insensitive match between strings or RegExp.
3
3
  *
4
4
  * @category String
5
5
  * @category Package : @augment-vir/common
6
6
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
7
  */
8
- export declare function match(haystack: string, needle: string): boolean;
8
+ export declare function match(haystack: string, needle: string | RegExp): boolean;
@@ -1,11 +1,20 @@
1
+ import { addRegExpFlags } from '../regexp/regexp-flags.js';
1
2
  import { escapeStringForRegExp } from '../regexp/regexp-string.js';
2
3
  /**
3
- * A case insensitive match between strings.
4
+ * A case insensitive match between strings or RegExp.
4
5
  *
5
6
  * @category String
6
7
  * @category Package : @augment-vir/common
7
8
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
9
  */
9
10
  export function match(haystack, needle) {
10
- return !!needle && !!new RegExp(escapeStringForRegExp(needle), 'i').exec(haystack);
11
+ if (!needle) {
12
+ return false;
13
+ }
14
+ else if (typeof needle === 'string') {
15
+ return !!new RegExp(escapeStringForRegExp(needle), 'i').exec(haystack);
16
+ }
17
+ else {
18
+ return !!addRegExpFlags(needle, 'i').exec(haystack);
19
+ }
11
20
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Removes duplicate characters from any number of strings.
3
+ *
4
+ * @category String
5
+ * @category Package : @augment-vir/common
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {removeDuplicateCharacters} from '@augment-vir/common';
10
+ *
11
+ * removeDuplicateCharacters('aAaBc', 'QrsAa');
12
+ * // output is `'aABcQrs'`
13
+ * ```
14
+ *
15
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
16
+ */
17
+ export declare function removeDuplicateCharacters(...values: ReadonlyArray<string>): string;
@@ -0,0 +1,22 @@
1
+ import { removeDuplicates } from '../array/remove-duplicates.js';
2
+ /**
3
+ * Removes duplicate characters from any number of strings.
4
+ *
5
+ * @category String
6
+ * @category Package : @augment-vir/common
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import {removeDuplicateCharacters} from '@augment-vir/common';
11
+ *
12
+ * removeDuplicateCharacters('aAaBc', 'QrsAa');
13
+ * // output is `'aABcQrs'`
14
+ * ```
15
+ *
16
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
17
+ */
18
+ export function removeDuplicateCharacters(...values) {
19
+ const combined = values.join('');
20
+ const deDuped = removeDuplicates(Array.from(combined));
21
+ return Array.from(deDuped).join('');
22
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/array/array.js';
2
+ export * from './augments/array/remove-duplicates.js';
2
3
  export * from './augments/array/tuple.js';
3
4
  export * from './augments/enum/enum-type.js';
4
5
  export * from './augments/enum/enum-values.js';
@@ -21,10 +22,12 @@ export * from './augments/partial-type.js';
21
22
  export * from './augments/promise/deferred-promise.js';
22
23
  export * from './augments/promise/maybe-promise.js';
23
24
  export * from './augments/promise/wait.js';
25
+ export * from './augments/regexp/regexp-flags.js';
24
26
  export * from './augments/regexp/regexp-string.js';
25
27
  export * from './augments/runtime-env.js';
26
28
  export * from './augments/string/ansi.js';
27
29
  export * from './augments/string/casing.js';
28
30
  export * from './augments/string/match.js';
29
31
  export * from './augments/string/punctuation.js';
32
+ export * from './augments/string/remove-duplicate-characters.js';
30
33
  export * from './augments/string/uuid.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/array/array.js';
2
+ export * from './augments/array/remove-duplicates.js';
2
3
  export * from './augments/array/tuple.js';
3
4
  export * from './augments/enum/enum-type.js';
4
5
  export * from './augments/enum/enum-values.js';
@@ -21,10 +22,12 @@ export * from './augments/partial-type.js';
21
22
  export * from './augments/promise/deferred-promise.js';
22
23
  export * from './augments/promise/maybe-promise.js';
23
24
  export * from './augments/promise/wait.js';
25
+ export * from './augments/regexp/regexp-flags.js';
24
26
  export * from './augments/regexp/regexp-string.js';
25
27
  export * from './augments/runtime-env.js';
26
28
  export * from './augments/string/ansi.js';
27
29
  export * from './augments/string/casing.js';
28
30
  export * from './augments/string/match.js';
29
31
  export * from './augments/string/punctuation.js';
32
+ export * from './augments/string/remove-duplicate-characters.js';
30
33
  export * from './augments/string/uuid.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/core",
3
- "version": "31.36.0",
3
+ "version": "31.37.0",
4
4
  "description": "Core augment-vir augments. Use @augment-vir/common instead.",
5
5
  "homepage": "https://github.com/electrovir/augment-vir",
6
6
  "bugs": {