@webdeveric/utils 0.81.0 → 0.83.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,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INDENT_CHAR_RAW_PATTERN = exports.NEWLINE_RAW_PATTERN = void 0;
4
+ exports.NEWLINE_RAW_PATTERN = String.raw `\r\n|\r|\n`;
5
+ exports.INDENT_CHAR_RAW_PATTERN = String.raw `[^\S\r\n]`;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dedent = void 0;
4
+ const constants_js_1 = require("./constants.js");
5
+ const findIndentation_js_1 = require("./findIndentation.js");
6
+ const stripIndent_js_1 = require("./stripIndent.js");
7
+ const dedent = (raw, ...values) => {
8
+ const indent = (0, findIndentation_js_1.findIndentation)(raw);
9
+ if (!indent) {
10
+ return String.raw({ raw }, ...values).trim();
11
+ }
12
+ const trailingWhitespace = new RegExp(String.raw `(?:${constants_js_1.NEWLINE_RAW_PATTERN})(?<indent>${constants_js_1.INDENT_CHAR_RAW_PATTERN}*)$`);
13
+ const newlinePattern = new RegExp(constants_js_1.NEWLINE_RAW_PATTERN, 'g');
14
+ const lines = raw.map((line) => (0, stripIndent_js_1.stripIndent)(line, indent));
15
+ return String.raw({ raw: lines }, ...values.map((value, index) => {
16
+ const extraIndent = lines[index]?.match(trailingWhitespace)?.groups?.['indent'];
17
+ return extraIndent ? String(value).replaceAll(newlinePattern, `$&${extraIndent}`) : value;
18
+ })).trim();
19
+ };
20
+ exports.dedent = dedent;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findIndentation = findIndentation;
4
+ const constants_js_1 = require("./constants.js");
5
+ function findIndentation(lines) {
6
+ return lines
7
+ .find((line) => !/^\s*$/.test(line))
8
+ ?.match(new RegExp(String.raw `^(?:${constants_js_1.NEWLINE_RAW_PATTERN})?(?<whiteSpace>${constants_js_1.INDENT_CHAR_RAW_PATTERN}+)`))?.groups?.['whiteSpace'];
9
+ }
package/dist/cjs/index.js CHANGED
@@ -31,6 +31,7 @@ __exportStar(require("./convert.js"), exports);
31
31
  __exportStar(require("./countCharacter.js"), exports);
32
32
  __exportStar(require("./countCharCode.js"), exports);
33
33
  __exportStar(require("./counter.js"), exports);
34
+ __exportStar(require("./dedent.js"), exports);
34
35
  __exportStar(require("./deepDecodeURI.js"), exports);
35
36
  __exportStar(require("./deepFreeze.js"), exports);
36
37
  __exportStar(require("./delay.js"), exports);
@@ -38,6 +39,7 @@ __exportStar(require("./delayAnimationFrame.js"), exports);
38
39
  __exportStar(require("./delayAnimationFrames.js"), exports);
39
40
  __exportStar(require("./describeInput.js"), exports);
40
41
  __exportStar(require("./escapeRegExp.js"), exports);
42
+ __exportStar(require("./findIndentation.js"), exports);
41
43
  __exportStar(require("./get.js"), exports);
42
44
  __exportStar(require("./getDateString.js"), exports);
43
45
  __exportStar(require("./getISODateString.js"), exports);
@@ -71,6 +73,7 @@ __exportStar(require("./redactCredentialsInURL.js"), exports);
71
73
  __exportStar(require("./resultify.js"), exports);
72
74
  __exportStar(require("./secToString.js"), exports);
73
75
  __exportStar(require("./set.js"), exports);
76
+ __exportStar(require("./stripIndent.js"), exports);
74
77
  __exportStar(require("./stripWhitespace.js"), exports);
75
78
  __exportStar(require("./suffix.js"), exports);
76
79
  __exportStar(require("./toPascalCase.js"), exports);
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripIndent = stripIndent;
4
+ const constants_js_1 = require("./constants.js");
5
+ function stripIndent(input, indent) {
6
+ return input.replaceAll(new RegExp(String.raw `(?<newLine>${constants_js_1.NEWLINE_RAW_PATTERN})${indent}`, 'g'), '$<newLine>');
7
+ }
@@ -1,15 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.trimIndentation = trimIndentation;
4
+ const constants_js_1 = require("./constants.js");
5
+ const findIndentation_js_1 = require("./findIndentation.js");
6
+ const stripIndent_js_1 = require("./stripIndent.js");
4
7
  function trimIndentation(input) {
5
- const lines = input.split('\n');
6
- const whiteSpace = lines.find((line) => !/^\s*$/.test(line))?.match(/^(?<whiteSpace>\s*)/)?.groups?.['whiteSpace'];
7
- if (whiteSpace) {
8
- const wsPattern = new RegExp(`^${whiteSpace}`);
9
- return lines
10
- .map((line) => line.replace(wsPattern, ''))
11
- .join('\n')
12
- .trim();
13
- }
14
- return input;
8
+ const whiteSpace = (0, findIndentation_js_1.findIndentation)(input.split(new RegExp(constants_js_1.NEWLINE_RAW_PATTERN)));
9
+ return (whiteSpace ? (0, stripIndent_js_1.stripIndent)(input, whiteSpace) : input).trim();
15
10
  }
@@ -4,16 +4,15 @@ exports.unique = unique;
4
4
  const isAsyncIterable_js_1 = require("./predicate/isAsyncIterable.js");
5
5
  const isIterable_js_1 = require("./predicate/isIterable.js");
6
6
  function unique(items, options = {}) {
7
- const ids = new Set();
8
- const { filter, identity } = options;
7
+ const { filter, identity, store = new Set() } = options;
9
8
  if (typeof items === 'string' || (0, isIterable_js_1.isIterable)(items)) {
10
9
  return {
11
10
  *[Symbol.iterator]() {
12
11
  for (const item of items) {
13
12
  if (!filter || filter(item) === true) {
14
13
  const id = identity?.(item) ?? item;
15
- if (!ids.has(id)) {
16
- ids.add(id);
14
+ if (!store.has(id)) {
15
+ store.add(id);
17
16
  yield item;
18
17
  }
19
18
  }
@@ -27,8 +26,8 @@ function unique(items, options = {}) {
27
26
  for await (const item of items) {
28
27
  if (!filter || filter(item) === true) {
29
28
  const id = identity?.(item) ?? item;
30
- if (!ids.has(id)) {
31
- ids.add(id);
29
+ if (!store.has(id)) {
30
+ store.add(id);
32
31
  yield item;
33
32
  }
34
33
  }
@@ -0,0 +1,2 @@
1
+ export const NEWLINE_RAW_PATTERN = String.raw `\r\n|\r|\n`;
2
+ export const INDENT_CHAR_RAW_PATTERN = String.raw `[^\S\r\n]`;
@@ -0,0 +1,16 @@
1
+ import { INDENT_CHAR_RAW_PATTERN, NEWLINE_RAW_PATTERN } from './constants.js';
2
+ import { findIndentation } from './findIndentation.js';
3
+ import { stripIndent } from './stripIndent.js';
4
+ export const dedent = (raw, ...values) => {
5
+ const indent = findIndentation(raw);
6
+ if (!indent) {
7
+ return String.raw({ raw }, ...values).trim();
8
+ }
9
+ const trailingWhitespace = new RegExp(String.raw `(?:${NEWLINE_RAW_PATTERN})(?<indent>${INDENT_CHAR_RAW_PATTERN}*)$`);
10
+ const newlinePattern = new RegExp(NEWLINE_RAW_PATTERN, 'g');
11
+ const lines = raw.map((line) => stripIndent(line, indent));
12
+ return String.raw({ raw: lines }, ...values.map((value, index) => {
13
+ const extraIndent = lines[index]?.match(trailingWhitespace)?.groups?.['indent'];
14
+ return extraIndent ? String(value).replaceAll(newlinePattern, `$&${extraIndent}`) : value;
15
+ })).trim();
16
+ };
@@ -0,0 +1,6 @@
1
+ import { INDENT_CHAR_RAW_PATTERN, NEWLINE_RAW_PATTERN } from './constants.js';
2
+ export function findIndentation(lines) {
3
+ return lines
4
+ .find((line) => !/^\s*$/.test(line))
5
+ ?.match(new RegExp(String.raw `^(?:${NEWLINE_RAW_PATTERN})?(?<whiteSpace>${INDENT_CHAR_RAW_PATTERN}+)`))?.groups?.['whiteSpace'];
6
+ }
package/dist/mjs/index.js CHANGED
@@ -15,6 +15,7 @@ export * from './convert.js';
15
15
  export * from './countCharacter.js';
16
16
  export * from './countCharCode.js';
17
17
  export * from './counter.js';
18
+ export * from './dedent.js';
18
19
  export * from './deepDecodeURI.js';
19
20
  export * from './deepFreeze.js';
20
21
  export * from './delay.js';
@@ -22,6 +23,7 @@ export * from './delayAnimationFrame.js';
22
23
  export * from './delayAnimationFrames.js';
23
24
  export * from './describeInput.js';
24
25
  export * from './escapeRegExp.js';
26
+ export * from './findIndentation.js';
25
27
  export * from './get.js';
26
28
  export * from './getDateString.js';
27
29
  export * from './getISODateString.js';
@@ -55,6 +57,7 @@ export * from './redactCredentialsInURL.js';
55
57
  export * from './resultify.js';
56
58
  export * from './secToString.js';
57
59
  export * from './set.js';
60
+ export * from './stripIndent.js';
58
61
  export * from './stripWhitespace.js';
59
62
  export * from './suffix.js';
60
63
  export * from './toPascalCase.js';
@@ -0,0 +1,4 @@
1
+ import { NEWLINE_RAW_PATTERN } from './constants.js';
2
+ export function stripIndent(input, indent) {
3
+ return input.replaceAll(new RegExp(String.raw `(?<newLine>${NEWLINE_RAW_PATTERN})${indent}`, 'g'), '$<newLine>');
4
+ }
@@ -1,12 +1,7 @@
1
+ import { NEWLINE_RAW_PATTERN } from './constants.js';
2
+ import { findIndentation } from './findIndentation.js';
3
+ import { stripIndent } from './stripIndent.js';
1
4
  export function trimIndentation(input) {
2
- const lines = input.split('\n');
3
- const whiteSpace = lines.find((line) => !/^\s*$/.test(line))?.match(/^(?<whiteSpace>\s*)/)?.groups?.['whiteSpace'];
4
- if (whiteSpace) {
5
- const wsPattern = new RegExp(`^${whiteSpace}`);
6
- return lines
7
- .map((line) => line.replace(wsPattern, ''))
8
- .join('\n')
9
- .trim();
10
- }
11
- return input;
5
+ const whiteSpace = findIndentation(input.split(new RegExp(NEWLINE_RAW_PATTERN)));
6
+ return (whiteSpace ? stripIndent(input, whiteSpace) : input).trim();
12
7
  }
@@ -1,16 +1,15 @@
1
1
  import { isAsyncIterable } from './predicate/isAsyncIterable.js';
2
2
  import { isIterable } from './predicate/isIterable.js';
3
3
  export function unique(items, options = {}) {
4
- const ids = new Set();
5
- const { filter, identity } = options;
4
+ const { filter, identity, store = new Set() } = options;
6
5
  if (typeof items === 'string' || isIterable(items)) {
7
6
  return {
8
7
  *[Symbol.iterator]() {
9
8
  for (const item of items) {
10
9
  if (!filter || filter(item) === true) {
11
10
  const id = identity?.(item) ?? item;
12
- if (!ids.has(id)) {
13
- ids.add(id);
11
+ if (!store.has(id)) {
12
+ store.add(id);
14
13
  yield item;
15
14
  }
16
15
  }
@@ -24,8 +23,8 @@ export function unique(items, options = {}) {
24
23
  for await (const item of items) {
25
24
  if (!filter || filter(item) === true) {
26
25
  const id = identity?.(item) ?? item;
27
- if (!ids.has(id)) {
28
- ids.add(id);
26
+ if (!store.has(id)) {
27
+ store.add(id);
29
28
  yield item;
30
29
  }
31
30
  }
@@ -0,0 +1,2 @@
1
+ export declare const NEWLINE_RAW_PATTERN: string;
2
+ export declare const INDENT_CHAR_RAW_PATTERN: string;
@@ -0,0 +1 @@
1
+ export declare const dedent: (raw: readonly string[], ...values: unknown[]) => string;
@@ -0,0 +1 @@
1
+ export declare function findIndentation(lines: readonly string[]): string | undefined;
@@ -15,6 +15,7 @@ export * from './convert.js';
15
15
  export * from './countCharacter.js';
16
16
  export * from './countCharCode.js';
17
17
  export * from './counter.js';
18
+ export * from './dedent.js';
18
19
  export * from './deepDecodeURI.js';
19
20
  export * from './deepFreeze.js';
20
21
  export * from './delay.js';
@@ -22,6 +23,7 @@ export * from './delayAnimationFrame.js';
22
23
  export * from './delayAnimationFrames.js';
23
24
  export * from './describeInput.js';
24
25
  export * from './escapeRegExp.js';
26
+ export * from './findIndentation.js';
25
27
  export * from './get.js';
26
28
  export * from './getDateString.js';
27
29
  export * from './getISODateString.js';
@@ -55,6 +57,7 @@ export * from './redactCredentialsInURL.js';
55
57
  export * from './resultify.js';
56
58
  export * from './secToString.js';
57
59
  export * from './set.js';
60
+ export * from './stripIndent.js';
58
61
  export * from './stripWhitespace.js';
59
62
  export * from './suffix.js';
60
63
  export * from './toPascalCase.js';
@@ -0,0 +1 @@
1
+ export declare function stripIndent(input: string, indent: string): string;
@@ -1,6 +1,11 @@
1
+ export type MembershipStore<T> = {
2
+ has(value: T): boolean;
3
+ add(value: T): void;
4
+ };
1
5
  export type UniqueOptions<Type> = {
2
6
  identity?: (item: Type) => unknown;
3
7
  filter?: (item: Type) => boolean;
8
+ store?: MembershipStore<unknown>;
4
9
  };
5
10
  export declare function unique<Type>(items: AsyncIterable<Type>, options?: UniqueOptions<Type>): AsyncIterable<Type>;
6
11
  export declare function unique<Type>(items: Iterable<Type>, options?: UniqueOptions<Type>): Iterable<Type>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webdeveric/utils",
3
3
  "description": "Just another collection of utilities.",
4
- "version": "0.81.0",
4
+ "version": "0.83.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -85,15 +85,15 @@
85
85
  },
86
86
  "prettier": "@webdeveric/prettier-config",
87
87
  "devDependencies": {
88
- "@commitlint/config-conventional": "^21.0.2",
89
- "@commitlint/types": "^21.0.1",
88
+ "@commitlint/config-conventional": "^21.1.0",
89
+ "@commitlint/types": "^21.1.0",
90
90
  "@types/node": "^24.13.2",
91
- "@typescript/native-preview": "7.0.0-dev.20260622.1",
91
+ "@typescript/native-preview": "7.0.0-dev.20260629.1",
92
92
  "@vitest/coverage-v8": "^4.1.9",
93
93
  "@webdeveric/eslint-config-ts": "^0.12.0",
94
94
  "@webdeveric/prettier-config": "^0.4.0",
95
- "commitlint": "^21.0.2",
96
- "commitlint-plugin-cspell": "^0.9.2",
95
+ "commitlint": "^21.1.0",
96
+ "commitlint-plugin-cspell": "^0.9.3",
97
97
  "conventional-changelog-conventionalcommits": "^9.3.1",
98
98
  "cspell": "^10.0.1",
99
99
  "eslint": "^8.57.1",
@@ -103,10 +103,10 @@
103
103
  "husky": "^9.1.7",
104
104
  "jsdom": "^29.1.1",
105
105
  "lint-staged": "^17.0.8",
106
- "prettier": "^3.8.4",
106
+ "prettier": "^3.9.3",
107
107
  "rimraf": "^6.1.3",
108
108
  "semantic-release": "^25.0.5",
109
- "validate-package-exports": "^1.1.0",
109
+ "validate-package-exports": "^1.1.1",
110
110
  "vitest": "^4.1.9"
111
111
  }
112
112
  }