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