@webdeveric/utils 0.80.1 → 0.82.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;
package/dist/cjs/delay.js CHANGED
@@ -1,13 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.delay = delay;
4
- function delay(milliseconds, value) {
4
+ const isPositiveInteger_js_1 = require("./predicate/isPositiveInteger.js");
5
+ const MAX_DELAY_MS = 2 ** 31 - 1;
6
+ function delay(milliseconds, value, signal) {
5
7
  return new Promise((resolve, reject) => {
6
- const maxDelayMs = 2 ** 31 - 1;
7
- if (typeof milliseconds !== 'number' || milliseconds < 0 || milliseconds > maxDelayMs) {
8
- reject(new TypeError(`milliseconds must be a number between 0 and ${maxDelayMs}`));
8
+ if (!(0, isPositiveInteger_js_1.isPositiveInteger)(milliseconds)) {
9
+ reject(new TypeError('milliseconds must be a positive integer'));
9
10
  return;
10
11
  }
11
- setTimeout(resolve, milliseconds, value);
12
+ if (milliseconds > MAX_DELAY_MS) {
13
+ reject(new RangeError(`milliseconds must be between 0 and ${MAX_DELAY_MS}`));
14
+ return;
15
+ }
16
+ const valueIsAbortSignal = value instanceof AbortSignal;
17
+ const abortSignal = valueIsAbortSignal ? value : signal;
18
+ const returnValue = valueIsAbortSignal ? undefined : value;
19
+ if (abortSignal?.aborted) {
20
+ reject(abortSignal.reason);
21
+ return;
22
+ }
23
+ let timer;
24
+ if (abortSignal) {
25
+ const onAbort = () => {
26
+ clearTimeout(timer);
27
+ reject(abortSignal.reason);
28
+ };
29
+ timer = setTimeout(() => {
30
+ abortSignal.removeEventListener('abort', onAbort);
31
+ resolve(returnValue);
32
+ }, milliseconds);
33
+ abortSignal.addEventListener('abort', onAbort, { once: true });
34
+ }
35
+ else {
36
+ timer = setTimeout(resolve, milliseconds, returnValue);
37
+ }
12
38
  });
13
39
  }
@@ -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
  }
@@ -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
+ };
package/dist/mjs/delay.js CHANGED
@@ -1,10 +1,36 @@
1
- export function delay(milliseconds, value) {
1
+ import { isPositiveInteger } from './predicate/isPositiveInteger.js';
2
+ const MAX_DELAY_MS = 2 ** 31 - 1;
3
+ export function delay(milliseconds, value, signal) {
2
4
  return new Promise((resolve, reject) => {
3
- const maxDelayMs = 2 ** 31 - 1;
4
- if (typeof milliseconds !== 'number' || milliseconds < 0 || milliseconds > maxDelayMs) {
5
- reject(new TypeError(`milliseconds must be a number between 0 and ${maxDelayMs}`));
5
+ if (!isPositiveInteger(milliseconds)) {
6
+ reject(new TypeError('milliseconds must be a positive integer'));
6
7
  return;
7
8
  }
8
- setTimeout(resolve, milliseconds, value);
9
+ if (milliseconds > MAX_DELAY_MS) {
10
+ reject(new RangeError(`milliseconds must be between 0 and ${MAX_DELAY_MS}`));
11
+ return;
12
+ }
13
+ const valueIsAbortSignal = value instanceof AbortSignal;
14
+ const abortSignal = valueIsAbortSignal ? value : signal;
15
+ const returnValue = valueIsAbortSignal ? undefined : value;
16
+ if (abortSignal?.aborted) {
17
+ reject(abortSignal.reason);
18
+ return;
19
+ }
20
+ let timer;
21
+ if (abortSignal) {
22
+ const onAbort = () => {
23
+ clearTimeout(timer);
24
+ reject(abortSignal.reason);
25
+ };
26
+ timer = setTimeout(() => {
27
+ abortSignal.removeEventListener('abort', onAbort);
28
+ resolve(returnValue);
29
+ }, milliseconds);
30
+ abortSignal.addEventListener('abort', onAbort, { once: true });
31
+ }
32
+ else {
33
+ timer = setTimeout(resolve, milliseconds, returnValue);
34
+ }
9
35
  });
10
36
  }
@@ -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
  }
@@ -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;
@@ -1,2 +1,2 @@
1
- export declare function delay(milliseconds: number): Promise<undefined>;
2
- export declare function delay<T>(milliseconds: number, value: T): Promise<T>;
1
+ export declare function delay(milliseconds: number, signal?: AbortSignal): Promise<undefined>;
2
+ export declare function delay<T>(milliseconds: number, value: T, signal?: AbortSignal): Promise<T>;
@@ -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;
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.80.1",
4
+ "version": "0.82.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -66,7 +66,7 @@
66
66
  "url": "https://github.com/webdeveric/utils/issues"
67
67
  },
68
68
  "homepage": "https://github.com/webdeveric/utils/#readme",
69
- "packageManager": "pnpm@11.8.0+sha512.c1f5e7c4cb241c8f174b743851d82f42b802324afc8b0f116b96adb15aa06664948dde36960a3ba1079ba5b4b29dd0140135b94b5b5f5263592249d68e555f26",
69
+ "packageManager": "pnpm@11.9.0+sha512.bd682d5d03fe525ef7c9fd6780c6884d1e756ac4c9c9fe00c538782824310dcf90e3ddc4f53835f06dfaebd5085e41855e0bcbb3b60de2ac5bbab89e5036f03b",
70
70
  "scripts": {
71
71
  "clean": "rimraf ./dist/ ./cache/ ./coverage/",
72
72
  "prebuild": "pnpm clean",
@@ -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.20260617.2",
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",
@@ -102,11 +102,11 @@
102
102
  "eslint-plugin-import": "^2.32.0",
103
103
  "husky": "^9.1.7",
104
104
  "jsdom": "^29.1.1",
105
- "lint-staged": "^17.0.7",
106
- "prettier": "^3.8.4",
105
+ "lint-staged": "^17.0.8",
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
  }