@synstack/str 1.1.3 → 1.2.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.
package/dist/str.index.js CHANGED
@@ -99,245 +99,502 @@ var leadingSpacesCount = (text) => {
99
99
  var isEmpty = (text) => {
100
100
  return text.trim() === "";
101
101
  };
102
+ var replace = (text, searchValue, replaceValue) => {
103
+ return text.replace(searchValue, replaceValue);
104
+ };
105
+ var replaceAll = (text, searchValue, replaceValue) => {
106
+ if (typeof searchValue === "string") {
107
+ return text.replaceAll(searchValue, replaceValue);
108
+ }
109
+ const flags = searchValue.flags.includes("g") ? searchValue.flags : searchValue.flags + "g";
110
+ const globalRegex = new RegExp(searchValue.source, flags);
111
+ return text.replace(globalRegex, replaceValue);
112
+ };
102
113
 
103
114
  // src/str.chainable.ts
104
115
  var Str = class _Str extends Pipeable {
105
116
  text;
117
+ /**
118
+ * Create a new Str instance
119
+ * @param text - The input string to wrap
120
+ * @example
121
+ * ```typescript
122
+ * const s = new Str('Hello World')
123
+ * // or use the convenience function
124
+ * const s = str('Hello World')
125
+ * ```
126
+ */
106
127
  constructor(text) {
107
128
  super();
108
129
  this.text = text;
109
130
  }
131
+ /**
132
+ * Get the underlying string value
133
+ * @returns The wrapped string value
134
+ */
110
135
  valueOf() {
111
136
  return this.text;
112
137
  }
138
+ /**
139
+ * Convert the Str instance to a string
140
+ * @returns The wrapped string value
141
+ */
113
142
  toString() {
114
143
  return this.text;
115
144
  }
145
+ /**
146
+ * Get the current Str instance
147
+ * @returns The current Str instance
148
+ * @internal Used by Pipeable
149
+ */
116
150
  instanceOf() {
117
151
  return this;
118
152
  }
119
153
  /**
120
- * Remove empty lines at the start of the text but leave whitespace on the first line with content
154
+ * Remove empty lines at the start of the text
155
+ * @returns A new Str instance with empty lines removed from the start
156
+ * @example
157
+ * ```typescript
158
+ * str('\n\n Hello').chopEmptyLinesStart().$ // ' Hello'
159
+ * ```
121
160
  */
122
161
  chopEmptyLinesStart() {
123
162
  return new _Str(chopEmptyLinesStart(this.text));
124
163
  }
125
164
  /**
126
- * Remove empty lines at the end of the text but leave whitespace on the last line with content
165
+ * Remove empty lines at the end of the text
166
+ * @returns A new Str instance with empty lines removed from the end
167
+ * @example
168
+ * ```typescript
169
+ * str('Hello\n\n').chopEmptyLinesEnd().$ // 'Hello'
170
+ * ```
127
171
  */
128
172
  chopEmptyLinesEnd() {
129
173
  return new _Str(chopEmptyLinesEnd(this.text));
130
174
  }
131
175
  /**
132
- * Remove all space (\s) characters in lines without content
176
+ * Remove whitespace from empty lines
177
+ * @returns A new Str instance with whitespace removed from empty lines
178
+ * @example
179
+ * ```typescript
180
+ * str('Hello\n \nWorld').trimEmptyLines().$ // 'Hello\n\nWorld'
181
+ * ```
133
182
  */
134
183
  trimEmptyLines() {
135
184
  return new _Str(trimEmptyLines(this.text));
136
185
  }
137
186
  /**
138
- * Remove all spaces (\s) characters at the end of lines
187
+ * Remove trailing spaces from all lines
188
+ * @returns A new Str instance with trailing spaces removed from all lines
189
+ * @example
190
+ * ```typescript
191
+ * str('Hello \nWorld ').trimLinesTrailingSpaces().$ // 'Hello\nWorld'
192
+ * ```
139
193
  */
140
194
  trimLinesTrailingSpaces() {
141
195
  return new _Str(trimLinesTrailingSpaces(this.text));
142
196
  }
143
197
  /**
144
- * Removes the leading and trailing white space and line terminator characters
198
+ * Remove leading and trailing whitespace
199
+ * @returns A new Str instance with whitespace removed from both ends
200
+ * @example
201
+ * ```typescript
202
+ * str(' Hello ').trim().$ // 'Hello'
203
+ * ```
145
204
  */
146
205
  trim() {
147
206
  return new _Str(trim(this.text));
148
207
  }
149
208
  /**
150
- * Removes the leading white space and line terminator characters
209
+ * Remove leading whitespace
210
+ * @returns A new Str instance with leading whitespace removed
211
+ * @example
212
+ * ```typescript
213
+ * str(' Hello ').trimStart().$ // 'Hello '
214
+ * ```
151
215
  */
152
216
  trimStart() {
153
217
  return new _Str(trimStart(this.text));
154
218
  }
155
219
  /**
156
- * Removes the trailing white space and line terminator characters
220
+ * Remove trailing whitespace
221
+ * @returns A new Str instance with trailing whitespace removed
222
+ * @example
223
+ * ```typescript
224
+ * str(' Hello ').trimEnd().$ // ' Hello'
225
+ * ```
157
226
  */
158
227
  trimEnd() {
159
228
  return new _Str(trimEnd(this.text));
160
229
  }
161
230
  /**
162
- * Split a string into substrings using the specified separator and return them as an array
231
+ * Split the string into an array of Str instances
232
+ * @param separator - String or RegExp to split on
233
+ * @param limit - Maximum number of splits to perform
234
+ * @returns Array of Str instances
235
+ * @example
236
+ * ```typescript
237
+ * str('a,b,c').split(',') // [Str('a'), Str('b'), Str('c')]
238
+ * str('a b c').split(' ', 2) // [Str('a'), Str('b')]
239
+ * ```
163
240
  */
164
241
  split(separator, limit) {
165
242
  return split(this.text, separator, limit).map((v) => new _Str(v));
166
243
  }
167
244
  /**
168
- * Add line numbers to a string
169
- * @param separator The separator to use between the line number and the line content.
170
- * Defaults to ":"
245
+ * Add line numbers to each line
246
+ * @param separator - String to separate line numbers from content
247
+ * @returns A new Str instance with line numbers added
248
+ * @example
249
+ * ```typescript
250
+ * str('A\nB').addLineNumbers().$ // '0:A\n1:B'
251
+ * str('A\nB').addLineNumbers(' -> ').$ // '0 -> A\n1 -> B'
252
+ * ```
171
253
  */
172
254
  addLineNumbers(separator = ":") {
173
255
  return new _Str(addLineNumbers(this.text, separator));
174
256
  }
175
257
  /**
176
- * Returns the character at the specified index
177
- * @return string or undefined if the index is out of bounds
258
+ * Get the character at a specific index
259
+ * @param index - Zero-based position in the string
260
+ * @returns The character at the index, or undefined if out of bounds
261
+ * @example
262
+ * ```typescript
263
+ * str('Hello').at(0) // 'H'
264
+ * str('Hello').at(-1) // 'o'
265
+ * ```
178
266
  */
179
267
  at(index) {
180
268
  return this.text.at(index);
181
269
  }
182
270
  /**
183
- * Returns the length of the string
271
+ * Get the length of the string
272
+ * @returns The number of characters in the string
273
+ * @example
274
+ * ```typescript
275
+ * str('Hello').length() // 5
276
+ * str('').length() // 0
277
+ * ```
184
278
  */
185
279
  length() {
186
280
  return this.text.length;
187
281
  }
188
282
  /**
189
- * Indent the string by the specified number of spaces
190
- * @param size The number of spaces to indent by
191
- * @param char The character to use for indentation. Defaults to " "
283
+ * Indent each line by a specified number of spaces
284
+ * @param size - Number of spaces to add
285
+ * @param char - Character to use for indentation
286
+ * @returns A new Str instance with added indentation
287
+ * @example
288
+ * ```typescript
289
+ * str('Hello\nWorld').indent(2).$ // ' Hello\n World'
290
+ * str('A\nB').indent(2, '-').$ // '--A\n--B'
291
+ * ```
192
292
  */
193
293
  indent(size, char = " ") {
194
294
  return new _Str(indent(this.text, size, char));
195
295
  }
196
296
  /**
197
- * Dedent the string by the specified number of spaces
198
- * @param indentation The number of spaces to dedent by.
199
- * If not provided, it will be calculated automatically based on the maximum indentation in the string.
297
+ * Remove indentation from each line
298
+ * @param indentation - Optional number of spaces to remove
299
+ * @returns A new Str instance with indentation removed
300
+ * @example
301
+ * ```typescript
302
+ * str(' Hello\n World').dedent().$ // 'Hello\n World'
303
+ * str(' A\n B').dedent(2).$ // ' A\nB'
304
+ * ```
200
305
  */
201
306
  dedent(indentation2) {
202
307
  return new _Str(dedent(this.text, indentation2));
203
308
  }
204
309
  /**
205
- * Chop the string at the start by the specified number of characters
310
+ * Remove characters from the start of the string
311
+ * @param count - Number of characters to remove
312
+ * @returns A new Str instance with characters removed from the start
313
+ * @example
314
+ * ```typescript
315
+ * str('Hello').chopStart(2).$ // 'llo'
316
+ * ```
206
317
  */
207
318
  chopStart(count) {
208
319
  return new _Str(chopStart(this.text, count));
209
320
  }
210
321
  /**
211
- * Chop the string at the end by the specified number of characters
322
+ * Remove characters from the end of the string
323
+ * @param count - Number of characters to remove
324
+ * @returns A new Str instance with characters removed from the end
325
+ * @example
326
+ * ```typescript
327
+ * str('Hello').chopEnd(2).$ // 'Hel'
328
+ * ```
212
329
  */
213
330
  chopEnd(count) {
214
331
  if (count === 0) return this;
215
332
  return new _Str(chopEnd(this.text, count));
216
333
  }
217
334
  /**
218
- * Remove successive newlines of the specified repetition or more
219
- * @param maxRepeat the maximum number of newlines to allow
335
+ * Limit consecutive newlines to a maximum count
336
+ * @param maxRepeat - Maximum number of consecutive newlines to allow
337
+ * @returns A new Str instance with limited consecutive newlines
338
+ * @example
339
+ * ```typescript
340
+ * str('A\n\n\nB').chopRepeatNewlines(1).$ // 'A\nB'
341
+ * str('A\n\n\nB').chopRepeatNewlines(2).$ // 'A\n\nB'
342
+ * ```
220
343
  */
221
344
  chopRepeatNewlines(maxRepeat) {
222
345
  if (maxRepeat === 0) return this;
223
346
  return new _Str(chopRepeatNewlines(this.text, maxRepeat));
224
347
  }
225
348
  /**
226
- * Take the first n characters of the string
349
+ * Take characters from the start of the string
350
+ * @param count - Number of characters to take
351
+ * @returns A new Str instance with the first n characters
352
+ * @example
353
+ * ```typescript
354
+ * str('Hello').takeStart(2).$ // 'He'
355
+ * ```
227
356
  */
228
357
  takeStart(count) {
229
358
  return new _Str(takeStart(this.text, count));
230
359
  }
231
360
  /**
232
- * Take the last n characters of the string
361
+ * Take characters from the end of the string
362
+ * @param count - Number of characters to take
363
+ * @returns A new Str instance with the last n characters
364
+ * @example
365
+ * ```typescript
366
+ * str('Hello').takeEnd(2).$ // 'lo'
367
+ * ```
233
368
  */
234
369
  takeEnd(count) {
235
370
  return new _Str(takeEnd(this.text, count));
236
371
  }
237
372
  /**
238
- * Returns the last line of the string
373
+ * Get the last line of the string
374
+ * @returns A new Str instance containing the last line
375
+ * @example
376
+ * ```typescript
377
+ * str('Hello\nWorld').lastLine().$ // 'World'
378
+ * ```
239
379
  */
240
380
  lastLine() {
241
381
  return new _Str(lastLine(this.text));
242
382
  }
243
383
  /**
244
- * Returns the first line of the string
384
+ * Get the first line of the string
385
+ * @returns A new Str instance containing the first line
386
+ * @example
387
+ * ```typescript
388
+ * str('Hello\nWorld').firstLine().$ // 'Hello'
389
+ * ```
245
390
  */
246
391
  firstLine() {
247
392
  return new _Str(firstLine(this.text));
248
393
  }
249
394
  /**
250
- * Returns the number of leading spaces in the string
395
+ * Count leading space characters
396
+ * @returns The number of leading space characters
397
+ * @example
398
+ * ```typescript
399
+ * str(' Hello').leadingSpacesCount() // 2
400
+ * str('Hello').leadingSpacesCount() // 0
401
+ * ```
251
402
  */
252
403
  leadingSpacesCount() {
253
404
  return leadingSpacesCount(this.text);
254
405
  }
255
406
  /**
256
- * Returns the indentation level of the string skipping empty lines in the process
407
+ * Get the minimum indentation level of non-empty lines
408
+ * @returns The number of spaces in the minimum indentation
409
+ * @example
410
+ * ```typescript
411
+ * str(' Hello\n World').indentation() // 2
412
+ * str('Hello\n World').indentation() // 0
413
+ * ```
257
414
  */
258
415
  indentation() {
259
416
  return indentation(this.text);
260
417
  }
261
418
  /**
262
- * Returns true if the string is empty or contains only whitespace
419
+ * Check if the string is empty or contains only whitespace
420
+ * @returns True if empty or whitespace-only, false otherwise
421
+ * @example
422
+ * ```typescript
423
+ * str('').isEmpty() // true
424
+ * str(' \n').isEmpty() // true
425
+ * str('Hello').isEmpty() // false
426
+ * ```
263
427
  */
264
428
  isEmpty() {
265
429
  return isEmpty(this.text);
266
430
  }
267
431
  /**
268
- * Converts the string to camel case
432
+ * Replace the first occurrence of a substring or pattern
433
+ * @param searchValue - The string or pattern to search for
434
+ * @param replaceValue - The string to replace the match with
435
+ * @returns A new Str instance with the first match replaced
436
+ * @example
437
+ * ```typescript
438
+ * str('Hello World').replace('o', '0').$ // 'Hell0 World'
439
+ * str('abc abc').replace(/[a-z]/, 'X').$ // 'Xbc abc'
440
+ * ```
441
+ */
442
+ replace(searchValue, replaceValue) {
443
+ return new _Str(replace(this.text, searchValue, replaceValue));
444
+ }
445
+ /**
446
+ * Replace all occurrences of a substring or pattern
447
+ * @param searchValue - The string or pattern to search for
448
+ * @param replaceValue - The string to replace the matches with
449
+ * @returns A new Str instance with all matches replaced
450
+ * @example
451
+ * ```typescript
452
+ * str('Hello World').replaceAll('o', '0').$ // 'Hell0 W0rld'
453
+ * str('abc abc').replaceAll(/[a-z]/g, 'X').$ // 'XXX XXX'
454
+ * ```
455
+ */
456
+ replaceAll(searchValue, replaceValue) {
457
+ return new _Str(replaceAll(this.text, searchValue, replaceValue));
458
+ }
459
+ /**
460
+ * Convert string to camelCase
461
+ * @returns A new Str instance in camelCase
462
+ * @example
463
+ * ```typescript
464
+ * str('hello-world').camelCase().$ // 'helloWorld'
465
+ * ```
269
466
  */
270
467
  camelCase() {
271
468
  return new _Str(changeCase.camelCase(this.text));
272
469
  }
273
470
  /**
274
- * Converts the string to capital case
471
+ * Convert string to Capital Case
472
+ * @returns A new Str instance in Capital Case
473
+ * @example
474
+ * ```typescript
475
+ * str('hello-world').capitalCase().$ // 'Hello World'
476
+ * ```
275
477
  */
276
478
  capitalCase() {
277
479
  return new _Str(changeCase.capitalCase(this.text));
278
480
  }
279
481
  /**
280
- * Converts the string to constant case
482
+ * Convert string to CONSTANT_CASE
483
+ * @returns A new Str instance in CONSTANT_CASE
484
+ * @example
485
+ * ```typescript
486
+ * str('hello-world').constantCase().$ // 'HELLO_WORLD'
487
+ * ```
281
488
  */
282
489
  constantCase() {
283
490
  return new _Str(changeCase.constantCase(this.text));
284
491
  }
285
492
  /**
286
- * Converts the string to dot case
493
+ * Convert string to dot.case
494
+ * @returns A new Str instance in dot.case
495
+ * @example
496
+ * ```typescript
497
+ * str('hello-world').dotCase().$ // 'hello.world'
498
+ * ```
287
499
  */
288
500
  dotCase() {
289
501
  return new _Str(changeCase.dotCase(this.text));
290
502
  }
291
503
  /**
292
- * Converts the string to kebab case
504
+ * Convert string to kebab-case
505
+ * @returns A new Str instance in kebab-case
506
+ * @example
507
+ * ```typescript
508
+ * str('helloWorld').kebabCase().$ // 'hello-world'
509
+ * ```
293
510
  */
294
511
  kebabCase() {
295
512
  return new _Str(changeCase.kebabCase(this.text));
296
513
  }
297
514
  /**
298
- * Converts the string to no case
515
+ * Convert string to no case
516
+ * @returns A new Str instance with no case
517
+ * @example
518
+ * ```typescript
519
+ * str('helloWorld').noCase().$ // 'hello world'
520
+ * ```
299
521
  */
300
522
  noCase() {
301
523
  return new _Str(changeCase.noCase(this.text));
302
524
  }
303
525
  /**
304
- * Converts the string to pascal case
526
+ * Convert string to PascalCase
527
+ * @returns A new Str instance in PascalCase
528
+ * @example
529
+ * ```typescript
530
+ * str('hello-world').pascalCase().$ // 'HelloWorld'
531
+ * ```
305
532
  */
306
533
  pascalCase() {
307
534
  return new _Str(changeCase.pascalCase(this.text));
308
535
  }
309
536
  /**
310
- * Converts the string to pascal snake case
537
+ * Convert string to Pascal_Snake_Case
538
+ * @returns A new Str instance in Pascal_Snake_Case
539
+ * @example
540
+ * ```typescript
541
+ * str('hello-world').pascalSnakeCase().$ // 'Hello_World'
542
+ * ```
311
543
  */
312
544
  pascalSnakeCase() {
313
545
  return new _Str(changeCase.pascalSnakeCase(this.text));
314
546
  }
315
547
  /**
316
- * Converts the string to path case
548
+ * Convert string to path/case
549
+ * @returns A new Str instance in path/case
550
+ * @example
551
+ * ```typescript
552
+ * str('hello-world').pathCase().$ // 'hello/world'
553
+ * ```
317
554
  */
318
555
  pathCase() {
319
556
  return new _Str(changeCase.pathCase(this.text));
320
557
  }
321
558
  /**
322
- * Converts the string to sentence case
559
+ * Convert string to Sentence case
560
+ * @returns A new Str instance in Sentence case
561
+ * @example
562
+ * ```typescript
563
+ * str('hello-world').sentenceCase().$ // 'Hello world'
564
+ * ```
323
565
  */
324
566
  sentenceCase() {
325
567
  return new _Str(changeCase.sentenceCase(this.text));
326
568
  }
327
569
  /**
328
- * Converts the string to snake case
570
+ * Convert string to snake_case
571
+ * @returns A new Str instance in snake_case
572
+ * @example
573
+ * ```typescript
574
+ * str('helloWorld').snakeCase().$ // 'hello_world'
575
+ * ```
329
576
  */
330
577
  snakeCase() {
331
578
  return new _Str(changeCase.snakeCase(this.text));
332
579
  }
333
580
  /**
334
- * Converts the string to train case
581
+ * Convert string to Train-Case
582
+ * @returns A new Str instance in Train-Case
583
+ * @example
584
+ * ```typescript
585
+ * str('hello-world').trainCase().$ // 'Hello-World'
586
+ * ```
335
587
  */
336
588
  trainCase() {
337
589
  return new _Str(changeCase.trainCase(this.text));
338
590
  }
339
591
  /**
340
- * Shorthand for `.toString()`
592
+ * Get the underlying string value
593
+ * @returns The wrapped string value
594
+ * @example
595
+ * ```typescript
596
+ * str('hello').str // 'hello'
597
+ * ```
341
598
  */
342
599
  get str() {
343
600
  return this.toString();
@@ -370,6 +627,8 @@ export {
370
627
  pascalCase2 as pascalCase,
371
628
  pascalSnakeCase2 as pascalSnakeCase,
372
629
  pathCase2 as pathCase,
630
+ replace,
631
+ replaceAll,
373
632
  sentenceCase2 as sentenceCase,
374
633
  snakeCase2 as snakeCase,
375
634
  split,