@ls-stack/utils 3.19.0 → 3.20.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.
@@ -17,7 +17,7 @@
17
17
  - [conversions](conversions.md)
18
18
  - [createThrottleController](createThrottleController/README.md)
19
19
  - [debounce](debounce.md)
20
- - [dedent](dedent.md)
20
+ - [dedent](dedent/README.md)
21
21
  - [deepEqual](deepEqual.md)
22
22
  - [enhancedMap](enhancedMap.md)
23
23
  - [exhaustiveMatch](exhaustiveMatch/README.md)
@@ -0,0 +1,17 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / [dedent](README.md) / \<internal\>
6
+
7
+ # \<internal\>
8
+
9
+ ## Type Aliases
10
+
11
+ ### InterpolationValue
12
+
13
+ ```ts
14
+ type InterpolationValue = string | number | boolean | null | undefined;
15
+ ```
16
+
17
+ Defined in: [packages/utils/src/dedent.ts:30](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L30)
@@ -0,0 +1,204 @@
1
+ [**@ls-stack/utils**](../README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](../modules.md) / dedent
6
+
7
+ # dedent
8
+
9
+ ## Modules
10
+
11
+ - [\<internal\>](-internal-.md)
12
+
13
+ ## Interfaces
14
+
15
+ ### Dedent()
16
+
17
+ Defined in: [packages/utils/src/dedent.ts:35](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L35)
18
+
19
+ Dedent function interface that can be used both as a template tag and a regular function
20
+
21
+ #### Call Signature
22
+
23
+ ```ts
24
+ Dedent(literals): string;
25
+ ```
26
+
27
+ Defined in: [packages/utils/src/dedent.ts:39](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L39)
28
+
29
+ Dedent function interface that can be used both as a template tag and a regular function
30
+
31
+ ##### Parameters
32
+
33
+ ###### literals
34
+
35
+ `string`
36
+
37
+ ##### Returns
38
+
39
+ `string`
40
+
41
+ #### Call Signature
42
+
43
+ ```ts
44
+ Dedent(strings, ...values): string;
45
+ ```
46
+
47
+ Defined in: [packages/utils/src/dedent.ts:43](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L43)
48
+
49
+ Dedent function interface that can be used both as a template tag and a regular function
50
+
51
+ ##### Parameters
52
+
53
+ ###### strings
54
+
55
+ `TemplateStringsArray`
56
+
57
+ ###### values
58
+
59
+ ...[`InterpolationValue`](-internal-.md#interpolationvalue)[]
60
+
61
+ ##### Returns
62
+
63
+ `string`
64
+
65
+ #### Properties
66
+
67
+ ##### withOptions
68
+
69
+ ```ts
70
+ withOptions: CreateDedent;
71
+ ```
72
+
73
+ Defined in: [packages/utils/src/dedent.ts:47](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L47)
74
+
75
+ Create a new dedent function with custom options
76
+
77
+ ***
78
+
79
+ ### DedentOptions
80
+
81
+ Defined in: [packages/utils/src/dedent.ts:4](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L4)
82
+
83
+ Configuration options for the dedent function behavior
84
+
85
+ #### Properties
86
+
87
+ ##### escapeSpecialCharacters?
88
+
89
+ ```ts
90
+ optional escapeSpecialCharacters: boolean;
91
+ ```
92
+
93
+ Defined in: [packages/utils/src/dedent.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L10)
94
+
95
+ Whether to process escape sequences like \n, `, \$, and {
96
+ When true, allows using escaped characters in template literals
97
+
98
+ ###### Default
99
+
100
+ ```ts
101
+ true for template literals, false for plain strings
102
+ ```
103
+
104
+ ##### identInterpolations?
105
+
106
+ ```ts
107
+ optional identInterpolations: boolean;
108
+ ```
109
+
110
+ Defined in: [packages/utils/src/dedent.ts:21](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L21)
111
+
112
+ Whether to preserve relative indentation of interpolated multi-line values
113
+ When true, multi-line interpolations are re-indented to match the surrounding context
114
+
115
+ ###### Default
116
+
117
+ ```ts
118
+ true
119
+ ```
120
+
121
+ ##### showNullishOrFalseValues?
122
+
123
+ ```ts
124
+ optional showNullishOrFalseValues: boolean;
125
+ ```
126
+
127
+ Defined in: [packages/utils/src/dedent.ts:27](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L27)
128
+
129
+ Whether to display nullish or false values (false, null, undefined) in interpolations
130
+ When false, nullish or false values are skipped entirely
131
+
132
+ ###### Default
133
+
134
+ ```ts
135
+ false
136
+ ```
137
+
138
+ ##### trimWhitespace?
139
+
140
+ ```ts
141
+ optional trimWhitespace: boolean;
142
+ ```
143
+
144
+ Defined in: [packages/utils/src/dedent.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L15)
145
+
146
+ Whether to trim leading and trailing whitespace from the final result
147
+
148
+ ###### Default
149
+
150
+ ```ts
151
+ true
152
+ ```
153
+
154
+ ## Type Aliases
155
+
156
+ ### CreateDedent()
157
+
158
+ ```ts
159
+ type CreateDedent = (options) => Dedent;
160
+ ```
161
+
162
+ Defined in: [packages/utils/src/dedent.ts:53](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L53)
163
+
164
+ Factory function type for creating dedent functions with custom options
165
+
166
+ #### Parameters
167
+
168
+ ##### options
169
+
170
+ [`DedentOptions`](#dedentoptions)
171
+
172
+ #### Returns
173
+
174
+ [`Dedent`](#dedent)
175
+
176
+ ## Variables
177
+
178
+ ### dedent
179
+
180
+ ```ts
181
+ const dedent: Dedent;
182
+ ```
183
+
184
+ Defined in: [packages/utils/src/dedent.ts:75](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L75)
185
+
186
+ Remove common leading indentation from multi-line strings while preserving relative indentation.
187
+ Can be used as a tagged template literal or called with a plain string.
188
+
189
+ By default, it will dedent interpolated multi-line strings to match the surrounding context.
190
+ And it will not show falsy values.
191
+
192
+ #### Example
193
+
194
+ ```typescript
195
+ const text = dedent`
196
+ function hello() {
197
+ console.log('world');
198
+ }
199
+ `;
200
+ // Result:
201
+ "function hello() {
202
+ console.log('world');
203
+ }"
204
+ ```
package/docs/modules.md CHANGED
@@ -17,7 +17,7 @@
17
17
  - [conversions](conversions.md)
18
18
  - [createThrottleController](createThrottleController/README.md)
19
19
  - [debounce](debounce.md)
20
- - [dedent](dedent.md)
20
+ - [dedent](dedent/README.md)
21
21
  - [deepEqual](deepEqual.md)
22
22
  - [enhancedMap](enhancedMap.md)
23
23
  - [exhaustiveMatch](exhaustiveMatch/README.md)
package/lib/dedent.cjs CHANGED
@@ -23,7 +23,9 @@ __export(dedent_exports, {
23
23
  dedent: () => dedent
24
24
  });
25
25
  module.exports = __toCommonJS(dedent_exports);
26
- var dedent = createDedent({});
26
+ var dedent = createDedent({
27
+ identInterpolations: true
28
+ });
27
29
  function createDedent(options) {
28
30
  d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
29
31
  return d;
@@ -31,7 +33,9 @@ function createDedent(options) {
31
33
  const raw = typeof strings === "string" ? [strings] : strings.raw;
32
34
  const {
33
35
  escapeSpecialCharacters = Array.isArray(strings),
34
- trimWhitespace = true
36
+ trimWhitespace = true,
37
+ identInterpolations = true,
38
+ showNullishOrFalseValues = false
35
39
  } = options;
36
40
  let result = "";
37
41
  for (let i = 0; i < raw.length; i++) {
@@ -41,7 +45,24 @@ function createDedent(options) {
41
45
  }
42
46
  result += next;
43
47
  if (i < values.length) {
44
- result += values[i];
48
+ let val = values[i];
49
+ if (!showNullishOrFalseValues && (val === false || val === null || val === void 0)) {
50
+ continue;
51
+ }
52
+ val = String(val);
53
+ if (identInterpolations && val.includes("\n")) {
54
+ let withIdent = val;
55
+ const currentIndent = getCurrentIndent(result);
56
+ if (currentIndent && withIdent) {
57
+ const lines2 = withIdent.split("\n");
58
+ withIdent = lines2.map((line, index) => {
59
+ return index === 0 || line === "" ? line : currentIndent + line;
60
+ }).join("\n");
61
+ }
62
+ result += withIdent;
63
+ } else {
64
+ result += val;
65
+ }
45
66
  }
46
67
  }
47
68
  const lines = result.split("\n");
@@ -70,6 +91,13 @@ function createDedent(options) {
70
91
  return result;
71
92
  }
72
93
  }
94
+ function getCurrentIndent(str) {
95
+ const lines = str.split("\n");
96
+ const lastLine = lines[lines.length - 1];
97
+ if (!lastLine) return "";
98
+ const match = lastLine.match(/^(\s*)/);
99
+ return match ? match[1] : "";
100
+ }
73
101
  // Annotate the CommonJS export names for ESM import in node:
74
102
  0 && (module.exports = {
75
103
  dedent
package/lib/dedent.d.cts CHANGED
@@ -1,13 +1,73 @@
1
+ /**
2
+ * Configuration options for the dedent function behavior
3
+ */
1
4
  interface DedentOptions {
5
+ /**
6
+ * Whether to process escape sequences like \n, \`, \$, and \{
7
+ * When true, allows using escaped characters in template literals
8
+ * @default true for template literals, false for plain strings
9
+ */
2
10
  escapeSpecialCharacters?: boolean;
11
+ /**
12
+ * Whether to trim leading and trailing whitespace from the final result
13
+ * @default true
14
+ */
3
15
  trimWhitespace?: boolean;
16
+ /**
17
+ * Whether to preserve relative indentation of interpolated multi-line values
18
+ * When true, multi-line interpolations are re-indented to match the surrounding context
19
+ * @default true
20
+ */
21
+ identInterpolations?: boolean;
22
+ /**
23
+ * Whether to display nullish or false values (false, null, undefined) in interpolations
24
+ * When false, nullish or false values are skipped entirely
25
+ * @default false
26
+ */
27
+ showNullishOrFalseValues?: boolean;
4
28
  }
29
+ type InterpolationValue = string | number | boolean | null | undefined;
30
+ /**
31
+ * Dedent function interface that can be used both as a template tag and a regular function
32
+ */
5
33
  interface Dedent {
34
+ /**
35
+ * Process a plain string to remove common indentation
36
+ */
6
37
  (literals: string): string;
7
- (strings: TemplateStringsArray, ...values: unknown[]): string;
38
+ /**
39
+ * Process a template literal to remove common indentation while handling interpolations
40
+ */
41
+ (strings: TemplateStringsArray, ...values: InterpolationValue[]): string;
42
+ /**
43
+ * Create a new dedent function with custom options
44
+ */
8
45
  withOptions: CreateDedent;
9
46
  }
47
+ /**
48
+ * Factory function type for creating dedent functions with custom options
49
+ */
10
50
  type CreateDedent = (options: DedentOptions) => Dedent;
51
+ /**
52
+ * Remove common leading indentation from multi-line strings while preserving relative indentation.
53
+ * Can be used as a tagged template literal or called with a plain string.
54
+ *
55
+ * By default, it will dedent interpolated multi-line strings to match the surrounding context.
56
+ * And it will not show falsy values.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const text = dedent`
61
+ * function hello() {
62
+ * console.log('world');
63
+ * }
64
+ * `;
65
+ * // Result:
66
+ * "function hello() {
67
+ * console.log('world');
68
+ * }"
69
+ * ```
70
+ */
11
71
  declare const dedent: Dedent;
12
72
 
13
73
  export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.d.ts CHANGED
@@ -1,13 +1,73 @@
1
+ /**
2
+ * Configuration options for the dedent function behavior
3
+ */
1
4
  interface DedentOptions {
5
+ /**
6
+ * Whether to process escape sequences like \n, \`, \$, and \{
7
+ * When true, allows using escaped characters in template literals
8
+ * @default true for template literals, false for plain strings
9
+ */
2
10
  escapeSpecialCharacters?: boolean;
11
+ /**
12
+ * Whether to trim leading and trailing whitespace from the final result
13
+ * @default true
14
+ */
3
15
  trimWhitespace?: boolean;
16
+ /**
17
+ * Whether to preserve relative indentation of interpolated multi-line values
18
+ * When true, multi-line interpolations are re-indented to match the surrounding context
19
+ * @default true
20
+ */
21
+ identInterpolations?: boolean;
22
+ /**
23
+ * Whether to display nullish or false values (false, null, undefined) in interpolations
24
+ * When false, nullish or false values are skipped entirely
25
+ * @default false
26
+ */
27
+ showNullishOrFalseValues?: boolean;
4
28
  }
29
+ type InterpolationValue = string | number | boolean | null | undefined;
30
+ /**
31
+ * Dedent function interface that can be used both as a template tag and a regular function
32
+ */
5
33
  interface Dedent {
34
+ /**
35
+ * Process a plain string to remove common indentation
36
+ */
6
37
  (literals: string): string;
7
- (strings: TemplateStringsArray, ...values: unknown[]): string;
38
+ /**
39
+ * Process a template literal to remove common indentation while handling interpolations
40
+ */
41
+ (strings: TemplateStringsArray, ...values: InterpolationValue[]): string;
42
+ /**
43
+ * Create a new dedent function with custom options
44
+ */
8
45
  withOptions: CreateDedent;
9
46
  }
47
+ /**
48
+ * Factory function type for creating dedent functions with custom options
49
+ */
10
50
  type CreateDedent = (options: DedentOptions) => Dedent;
51
+ /**
52
+ * Remove common leading indentation from multi-line strings while preserving relative indentation.
53
+ * Can be used as a tagged template literal or called with a plain string.
54
+ *
55
+ * By default, it will dedent interpolated multi-line strings to match the surrounding context.
56
+ * And it will not show falsy values.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const text = dedent`
61
+ * function hello() {
62
+ * console.log('world');
63
+ * }
64
+ * `;
65
+ * // Result:
66
+ * "function hello() {
67
+ * console.log('world');
68
+ * }"
69
+ * ```
70
+ */
11
71
  declare const dedent: Dedent;
12
72
 
13
73
  export { type CreateDedent, type Dedent, type DedentOptions, dedent };
package/lib/dedent.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // src/dedent.ts
2
- var dedent = createDedent({});
2
+ var dedent = createDedent({
3
+ identInterpolations: true
4
+ });
3
5
  function createDedent(options) {
4
6
  d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
5
7
  return d;
@@ -7,7 +9,9 @@ function createDedent(options) {
7
9
  const raw = typeof strings === "string" ? [strings] : strings.raw;
8
10
  const {
9
11
  escapeSpecialCharacters = Array.isArray(strings),
10
- trimWhitespace = true
12
+ trimWhitespace = true,
13
+ identInterpolations = true,
14
+ showNullishOrFalseValues = false
11
15
  } = options;
12
16
  let result = "";
13
17
  for (let i = 0; i < raw.length; i++) {
@@ -17,7 +21,24 @@ function createDedent(options) {
17
21
  }
18
22
  result += next;
19
23
  if (i < values.length) {
20
- result += values[i];
24
+ let val = values[i];
25
+ if (!showNullishOrFalseValues && (val === false || val === null || val === void 0)) {
26
+ continue;
27
+ }
28
+ val = String(val);
29
+ if (identInterpolations && val.includes("\n")) {
30
+ let withIdent = val;
31
+ const currentIndent = getCurrentIndent(result);
32
+ if (currentIndent && withIdent) {
33
+ const lines2 = withIdent.split("\n");
34
+ withIdent = lines2.map((line, index) => {
35
+ return index === 0 || line === "" ? line : currentIndent + line;
36
+ }).join("\n");
37
+ }
38
+ result += withIdent;
39
+ } else {
40
+ result += val;
41
+ }
21
42
  }
22
43
  }
23
44
  const lines = result.split("\n");
@@ -46,6 +67,13 @@ function createDedent(options) {
46
67
  return result;
47
68
  }
48
69
  }
70
+ function getCurrentIndent(str) {
71
+ const lines = str.split("\n");
72
+ const lastLine = lines[lines.length - 1];
73
+ if (!lastLine) return "";
74
+ const match = lastLine.match(/^(\s*)/);
75
+ return match ? match[1] : "";
76
+ }
49
77
  export {
50
78
  dedent
51
79
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.19.0",
4
+ "version": "3.20.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib",
package/docs/dedent.md DELETED
@@ -1,117 +0,0 @@
1
- [**@ls-stack/utils**](README.md)
2
-
3
- ***
4
-
5
- [@ls-stack/utils](modules.md) / dedent
6
-
7
- # dedent
8
-
9
- ## Interfaces
10
-
11
- ### Dedent()
12
-
13
- Defined in: [packages/utils/src/dedent.ts:6](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L6)
14
-
15
- #### Call Signature
16
-
17
- ```ts
18
- Dedent(literals): string;
19
- ```
20
-
21
- Defined in: [packages/utils/src/dedent.ts:7](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L7)
22
-
23
- ##### Parameters
24
-
25
- ###### literals
26
-
27
- `string`
28
-
29
- ##### Returns
30
-
31
- `string`
32
-
33
- #### Call Signature
34
-
35
- ```ts
36
- Dedent(strings, ...values): string;
37
- ```
38
-
39
- Defined in: [packages/utils/src/dedent.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L8)
40
-
41
- ##### Parameters
42
-
43
- ###### strings
44
-
45
- `TemplateStringsArray`
46
-
47
- ###### values
48
-
49
- ...`unknown`[]
50
-
51
- ##### Returns
52
-
53
- `string`
54
-
55
- #### Properties
56
-
57
- ##### withOptions
58
-
59
- ```ts
60
- withOptions: CreateDedent;
61
- ```
62
-
63
- Defined in: [packages/utils/src/dedent.ts:9](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L9)
64
-
65
- ***
66
-
67
- ### DedentOptions
68
-
69
- Defined in: [packages/utils/src/dedent.ts:1](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L1)
70
-
71
- #### Properties
72
-
73
- ##### escapeSpecialCharacters?
74
-
75
- ```ts
76
- optional escapeSpecialCharacters: boolean;
77
- ```
78
-
79
- Defined in: [packages/utils/src/dedent.ts:2](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L2)
80
-
81
- ##### trimWhitespace?
82
-
83
- ```ts
84
- optional trimWhitespace: boolean;
85
- ```
86
-
87
- Defined in: [packages/utils/src/dedent.ts:3](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L3)
88
-
89
- ## Type Aliases
90
-
91
- ### CreateDedent()
92
-
93
- ```ts
94
- type CreateDedent = (options) => Dedent;
95
- ```
96
-
97
- Defined in: [packages/utils/src/dedent.ts:12](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L12)
98
-
99
- #### Parameters
100
-
101
- ##### options
102
-
103
- [`DedentOptions`](#dedentoptions)
104
-
105
- #### Returns
106
-
107
- [`Dedent`](#dedent)
108
-
109
- ## Variables
110
-
111
- ### dedent
112
-
113
- ```ts
114
- const dedent: Dedent;
115
- ```
116
-
117
- Defined in: [packages/utils/src/dedent.ts:14](https://github.com/lucasols/utils/blob/main/packages/utils/src/dedent.ts#L14)