@ls-stack/utils 3.17.0 → 3.17.1

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.
package/lib/dedent.cjs CHANGED
@@ -23,33 +23,52 @@ __export(dedent_exports, {
23
23
  dedent: () => dedent
24
24
  });
25
25
  module.exports = __toCommonJS(dedent_exports);
26
- function dedent(strings, ...values) {
27
- const raw = typeof strings === "string" ? [strings] : strings.raw;
28
- let result = "";
29
- for (let i = 0; i < raw.length; i++) {
30
- result += raw[i].replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`");
31
- if (i < values.length) {
32
- result += values[i];
26
+ var dedent = createDedent({});
27
+ function createDedent(options) {
28
+ d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
29
+ return d;
30
+ function d(strings, ...values) {
31
+ const raw = typeof strings === "string" ? [strings] : strings.raw;
32
+ const {
33
+ escapeSpecialCharacters = Array.isArray(strings),
34
+ trimWhitespace = true
35
+ } = options;
36
+ let result = "";
37
+ for (let i = 0; i < raw.length; i++) {
38
+ let next = raw[i];
39
+ if (escapeSpecialCharacters) {
40
+ next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\\{/g, "{");
41
+ }
42
+ result += next;
43
+ if (i < values.length) {
44
+ result += values[i];
45
+ }
33
46
  }
34
- }
35
- const lines = result.split("\n");
36
- let mindent = null;
37
- lines.forEach((l) => {
38
- const m = l.match(/^(\s+)\S+/);
39
- if (m) {
40
- const indent = m[1].length;
41
- if (!mindent) {
42
- mindent = indent;
43
- } else {
44
- mindent = Math.min(mindent, indent);
47
+ const lines = result.split("\n");
48
+ let mindent = null;
49
+ for (const l of lines) {
50
+ const m = l.match(/^(\s+)\S+/);
51
+ if (m) {
52
+ const indent = m[1].length;
53
+ if (!mindent) {
54
+ mindent = indent;
55
+ } else {
56
+ mindent = Math.min(mindent, indent);
57
+ }
45
58
  }
46
59
  }
47
- });
48
- if (mindent !== null) {
49
- const m = mindent;
50
- result = lines.map((l) => l.startsWith(" ") ? l.slice(m) : l).join("\n");
60
+ if (mindent !== null) {
61
+ const m = mindent;
62
+ result = lines.map((l) => l[0] === " " || l[0] === " " ? l.slice(m) : l).join("\n");
63
+ }
64
+ if (trimWhitespace) {
65
+ result = result.trim();
66
+ }
67
+ if (escapeSpecialCharacters) {
68
+ result = result.replace(/\\n/g, "\n");
69
+ }
70
+ return result;
51
71
  }
52
- return result.trim().replace(/\\n/g, "\n");
53
72
  }
54
73
  // Annotate the CommonJS export names for ESM import in node:
55
74
  0 && (module.exports = {
package/lib/dedent.d.cts CHANGED
@@ -1,5 +1,13 @@
1
- type InterpolateValue = string | number | boolean;
2
- declare function dedent(strings: TemplateStringsArray, ...values: InterpolateValue[]): string;
3
- declare function dedent(strings: string): string;
1
+ interface DedentOptions {
2
+ escapeSpecialCharacters?: boolean;
3
+ trimWhitespace?: boolean;
4
+ }
5
+ interface Dedent {
6
+ (literals: string): string;
7
+ (strings: TemplateStringsArray, ...values: unknown[]): string;
8
+ withOptions: CreateDedent;
9
+ }
10
+ type CreateDedent = (options: DedentOptions) => Dedent;
11
+ declare const dedent: Dedent;
4
12
 
5
- export { dedent };
13
+ export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.d.ts CHANGED
@@ -1,5 +1,13 @@
1
- type InterpolateValue = string | number | boolean;
2
- declare function dedent(strings: TemplateStringsArray, ...values: InterpolateValue[]): string;
3
- declare function dedent(strings: string): string;
1
+ interface DedentOptions {
2
+ escapeSpecialCharacters?: boolean;
3
+ trimWhitespace?: boolean;
4
+ }
5
+ interface Dedent {
6
+ (literals: string): string;
7
+ (strings: TemplateStringsArray, ...values: unknown[]): string;
8
+ withOptions: CreateDedent;
9
+ }
10
+ type CreateDedent = (options: DedentOptions) => Dedent;
11
+ declare const dedent: Dedent;
4
12
 
5
- export { dedent };
13
+ export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.js CHANGED
@@ -1,31 +1,50 @@
1
1
  // src/dedent.ts
2
- function dedent(strings, ...values) {
3
- const raw = typeof strings === "string" ? [strings] : strings.raw;
4
- let result = "";
5
- for (let i = 0; i < raw.length; i++) {
6
- result += raw[i].replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`");
7
- if (i < values.length) {
8
- result += values[i];
2
+ var dedent = createDedent({});
3
+ function createDedent(options) {
4
+ d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
5
+ return d;
6
+ function d(strings, ...values) {
7
+ const raw = typeof strings === "string" ? [strings] : strings.raw;
8
+ const {
9
+ escapeSpecialCharacters = Array.isArray(strings),
10
+ trimWhitespace = true
11
+ } = options;
12
+ let result = "";
13
+ for (let i = 0; i < raw.length; i++) {
14
+ let next = raw[i];
15
+ if (escapeSpecialCharacters) {
16
+ next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\\{/g, "{");
17
+ }
18
+ result += next;
19
+ if (i < values.length) {
20
+ result += values[i];
21
+ }
9
22
  }
10
- }
11
- const lines = result.split("\n");
12
- let mindent = null;
13
- lines.forEach((l) => {
14
- const m = l.match(/^(\s+)\S+/);
15
- if (m) {
16
- const indent = m[1].length;
17
- if (!mindent) {
18
- mindent = indent;
19
- } else {
20
- mindent = Math.min(mindent, indent);
23
+ const lines = result.split("\n");
24
+ let mindent = null;
25
+ for (const l of lines) {
26
+ const m = l.match(/^(\s+)\S+/);
27
+ if (m) {
28
+ const indent = m[1].length;
29
+ if (!mindent) {
30
+ mindent = indent;
31
+ } else {
32
+ mindent = Math.min(mindent, indent);
33
+ }
21
34
  }
22
35
  }
23
- });
24
- if (mindent !== null) {
25
- const m = mindent;
26
- result = lines.map((l) => l.startsWith(" ") ? l.slice(m) : l).join("\n");
36
+ if (mindent !== null) {
37
+ const m = mindent;
38
+ result = lines.map((l) => l[0] === " " || l[0] === " " ? l.slice(m) : l).join("\n");
39
+ }
40
+ if (trimWhitespace) {
41
+ result = result.trim();
42
+ }
43
+ if (escapeSpecialCharacters) {
44
+ result = result.replace(/\\n/g, "\n");
45
+ }
46
+ return result;
27
47
  }
28
- return result.trim().replace(/\\n/g, "\n");
29
48
  }
30
49
  export {
31
50
  dedent
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "3.17.0",
4
+ "version": "3.17.1",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib"
@@ -18,6 +18,10 @@
18
18
  "import": "./lib/main.js",
19
19
  "require": "./lib/main.cjs"
20
20
  },
21
+ "./__snapshots__": {
22
+ "import": "./lib/__snapshots__.js",
23
+ "require": "./lib/__snapshots__.cjs"
24
+ },
21
25
  "./arrayUtils": {
22
26
  "import": "./lib/arrayUtils.js",
23
27
  "require": "./lib/arrayUtils.cjs"