-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
2430 lines (2096 loc) · 59.6 KB
/
index.ts
File metadata and controls
2430 lines (2096 loc) · 59.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2025 Subhajit Sahu
// SPDX-License-Identifier: AGPL-3.0-or-later
// See LICENSE for full terms
//#region CONSTANTS
/** Decimal digits 0-9. */
export const DIGITS: string = "0123456789";
/** Octal digits 0-7. */
export const OCT_DIGITS: string = "01234567";
/** Hexadecimal digits 0-9, A-F, a-f. */
export const HEX_DIGITS: string = "0123456789ABCDEFabcdef";
/** English letters A-Z. */
export const UPPERCASE: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/** English letters a-z. */
export const LOWERCASE: string = "abcdefghijklmnopqrstuvwxyz";
/** Combination of uppercase, lowercase english letters. */
export const LETTERS: string = UPPERCASE + LOWERCASE;
/** Punctuation symbols (ASCII). */
export const PUNCTUATION: string = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
/** The string "\t\n\x0b\x0c\r ". */
export const WHITESPACE: string = "\t\n\x0b\x0c\r ";
/** Combination of digits, letters, punctuation, and whitespace (ASCII). */
export const PRINTABLE: string = DIGITS + LETTERS + PUNCTUATION + WHITESPACE;
/** Minimum unicode code point. */
export const MIN_CODE_POINT: number = 0x000000;
/** Maximum unicode code point. */
export const MAX_CODE_POINT: number = 0x10FFFF;
//#endregion
//#region UTILITY FUNCTIONS
/**
* Return the same (first) value.
* @param v a value
* @returns v
*/
function IDENTITY<T>(v: T): T {
return v;
}
function mod(x: number, y: number): number {
return x - y * Math.floor(x/y);
}
//#endregion
//#region BUILT-IN
//#region GENERATE
/**
* Get characters whose UTF-16 code units are given.
* @param codes UTF-16 code units
* @returns characters
*/
export function fromCharCode(...codes: number[]): string {
return String.fromCharCode(...codes);
}
/**
* Get characters whose unicode code points are given.
* @param codes unicode code points
* @returns characters
*/
export function fromCodePoint(...codes: number[]): string {
return String.fromCodePoint(...codes);
}
/**
* Combine multiple values into a string.
* @param values values
* @returns combined string
*/
export function concat(...values: unknown[]): string {
return "".concat(...values as string[]);
}
/**
* Repeat string given number of times.
* @param x a string
* @param times number of times
* @returns repeated string
*/
export function repeat(x: string, times: number): string {
return x.repeat(times);
}
//#endregion
//#region ABOUT
/**
* Get primitive value of string object.
* @param x a string object
* @returns primitive value
*/
export function valueOf(x: string): string {
return x.valueOf();
}
/**
* Get length of string.
* @param x a string
* @returns length of string
*/
export function length(x: string): number {
return x.length;
}
export {length as size};
// TODO: countCharacters ([...str].length)
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
/**
* Get character at given index in string.
* @param x a string
* @param at character index
* @return character
*/
export function charAt(x: string, at: number): string {
return x.charAt(at);
}
// TODO: Getting whole characters (non-BMP, surrogate pairs)
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
/**
* Get UTF-16 code unit of a character in string.
* @param x a string
* @param at character index
* @returns UTF-16 code unit
*/
export function charCodeAt(x: string, at: number): number {
return x.charCodeAt(at);
}
// TODO: Getting whole characters (non-BMP, surrogate pairs)
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
/**
* Get unicode code point of a character in string.
* @param x a string
* @param at character index
* @returns unicode code point
*/
export function codePointAt(x: string, at: number): number | undefined {
return x.codePointAt(at);
}
//#endregion
//#region COMPARE
/**
* Compare two strings in the current or given locale.
* @param x a string
* @param y another string
* @param locales language or locale tag(s)
* @param options comparison options
* @returns x<y: -ve, x=y: 0, x>y: +ve
* @example
* ```javascript
* xstring.localeCompare('abra', 'bulbasaur');
* // → -1
*
* xstring.localeCompare('bulbasaur', 'bulbasaur');
* // → 0
*
* xstring.localeCompare('charmeleon', 'bulbasaur');
* // → 1
* ```
*/
export function localeCompare(x: string, y: string, locales?: string | string[], options?: Intl.CollatorOptions): number {
return x.localeCompare(y, locales, options);
}
//#endregion
//#region SEARCH
/**
* Check if string has a given infix.
* @param x a string
* @param infix infix to look for
* @param start start index [0]
* @returns has infix?
*/
export function includes(x: string, infix: string, start?: number): boolean {
return x.includes(infix, start);
}
/**
* Check if string has a given prefix.
* @param x a string
* @param prefix prefix to look for
* @param start start index [0]
* @returns has prefix?
*/
export function startsWith(x: string, prefix: string, start?: number): boolean {
return x.startsWith(prefix, start);
}
/**
* Check if string has a given suffix.
* @param x a string
* @param suffix suffix to look for
* @param end end index [end]
* @returns has suffix?
*/
export function endsWith(x: string, suffix: string, end?: number): boolean {
return x.endsWith(suffix, end);
}
/**
* Get first index of a given infix in string.
* @param x a string
* @param infix infix to look for
* @param start start index [0]
* @returns first index of infix
*/
export function indexOf(x: string, infix: string, start?: number): number {
return x.indexOf(infix, start);
}
/**
* Get last index of a given infix in string.
* @param x a string
* @param infix infix to look for
* @param rstart reverse start index [end-1]
* @returns last index of infix
*/
export function lastIndexOf(x: string, infix: string, rstart?: number): number {
return x.lastIndexOf(infix, rstart);
}
/**
* Get first index of regular expression match in string.
* @param x a string
* @param regexp regular expression
* @returns first index of match
*/
export function search(x: string, regexp: string | RegExp): number {
return x.search(regexp);
}
/**
* Get results of matching string with regular expression.
* @param x a string
* @param regexp regular expression
* @returns /g: all matches, else: match with capturing groups or null
*/
export function match(x: string, regexp: string | RegExp): RegExpMatchArray | null {
return x.match(regexp);
}
/**
* Get detailed results of matching string with regular expression.
* @param x a string
* @param regexp regular expression (with /g)
* @returns match with capturing groups ...
*/
export function matchAll(x: string, regexp: RegExp): IterableIterator<RegExpMatchArray> {
return x.matchAll(regexp);
}
//#endregion
//#region CONVERT
/**
* Get string representation of string.
* @param x a string
* @returns string representation
*/
export function toString(x: string): string {
return x.toString();
}
//#endregion
//#region PART
/**
* Extract section of string.
* @param x a string
* @param start start index (-ve ⇒ from right) [0]
* @param end end index (-ve ⇒ from right) [end]
* @returns section of string
*/
export function slice(x: string, start?: number, end?: number): string {
return x.slice(start, end);
}
/**
* Extract section of string.
* @param x a string
* @param start start index (-ve ⇒ 0) [0]
* @param end end index (-ve ⇒ 0) [end]
* @returns section of string
*/
export function substring(x: string, start: number, end?: number): string {
return x.substring(start, end);
}
/**
* Split string by a given separator into substrings.
* @param x a string
* @param separator separator string or regular expression
* @param limit maximum number of substrings
* @returns substrings
*/
export function split(x: string, separator?: string | RegExp, limit?: number): string[] {
return x.split(separator as string | RegExp, limit);
}
// splitBy(x: string, separator: function, limit?: number): string[]?
//#endregion
//#region WHITESPACE
/**
* Remove whitespace from begining of string.
* @param x a string
* @returns trimmed string
*/
export function trimStart(x: string): string {
return x.trimStart();
}
/**
* Remove whitespace from end of string.
* @param x a string
* @returns trimmed string
*/
export function trimEnd(x: string): string {
return x.trimEnd();
}
/**
* Remove whitespace from begining and end of string.
* @param x a string
* @returns trimmed string
*/
export function trim(x: string): string {
return x.trim();
}
/**
* Pad start of string to fit a desired length.
* @param x a string
* @param length desired length
* @param padding pad with [ ]
* @returns padded string
*/
export function padStart(x: string, length: number, padding: string=" "): string {
return x.padStart(length, padding);
}
/**
* Pad end of string to fit a desired length.
* @param x a string
* @param length desired length
* @param padding pad with [ ]
* @returns padded string
*/
export function padEnd(x: string, length: number, padding: string=" "): string {
return x.padEnd(length, padding);
}
//#endregion
//#region TRANSFORM (CASE)
/**
* Convert string to upper case.
* @param x a string
* @returns upper cased string
*/
export function toUpperCase(x: string): string {
return x.toUpperCase();
}
/**
* Convert string to upper case, as per locale-specific case mappings.
* @param x a string
* @param locales BCP 47 language tag(s)
* @returns upper cased string
*/
export function toLocaleUpperCase(x: string, locales?: string | string[]): string {
return x.toLocaleUpperCase(locales);
}
/**
* Convert string to lower case.
* @param x a string
* @returns lower cased string
*/
export function toLowerCase(x: string): string {
return x.toLowerCase();
}
/**
* Convert string to lower case, as per locale-specific case mappings.
* @param x a string
* @param locales BCP 47 language tag(s)
* @returns lower cased string
*/
export function toLocaleLowerCase(x: string, locales?: string | string[]): string {
return x.toLocaleLowerCase(locales);
}
//#endregion
//#region TRANSFORM
/** Handle replacement of matched string and parameters with another string. */
export type ReplaceFunction = (substring: string, ...args: unknown[]) => string;
/**
* Replace first match of given pattern by replacement.
* @param x a string
* @param pattern substring to match
* @param replacement replacement substring
* @returns replaced string
*/
export function replace(x: string, pattern: string, replacement: string): string;
/**
* Replace first or all matches of given pattern by replacement.
* @param x a string
* @param pattern pattern to match
* @param replacement replacement substring or replacer
* @returns replaced string
*/
export function replace(x: string, pattern: string | RegExp, replacement: string | ReplaceFunction): string;
export function replace(x: string, pattern: string | RegExp, replacement: string | ReplaceFunction): string {
return x.replace(pattern, replacement as string);
}
// export function replaceAll(x: string, org: string, to: string): string {
// return x.replaceAll(org, to);
// }
/**
* Normalize string by given form, as per Unicode Standard Annex #15.
* @param x a string
* @param form normalization form (NFC, NFD, NFKC, NFKD)
* @returns normalized string
*/
export function normalize(x: string, form?: string): string {
return x.normalize(form);
}
//#endregion
//#endregion
//#region BUILT-IN (ARRAY)
//#region GENERATE
/**
* Create string from arguments, like `Array.of()`.
* @param args arguments
* @returns p + q + ... | [p, q, ...] = args
* @example
* ```javascript
* xstring.of('a', 1);
* // → 'a1'
*
* xstring.of(1, 2);
* // → '12'
*
* xstring.of();
* // → ''
* ```
*/
export function of(...args: string[]): string {
let a = "";
for (let i=0, I=args.length; i<I; i++)
a += args[i];
return a;
}
/**
* Handle transformation of a substring (or character) to another.
* @param v a substring (or character)
* @param i index of substring
* @param x string containing the substring
* @returns transformed substring
*/
export type MapFunction = (v: string, i: number, x?: string) => string;
/**
* Create string from iterable, like `Array.from()`.
* @param xs list of strings (iterable)
* @param fm map function (x, i)
* @param ths this argument
* @returns p + q + ... | [p, q, ...] = xs
* @example
* ```javascript
* xstring.from(['a', 'b']);
* // → 'ab'
*
* xstring.from('abc', (v) => String.fromCharCode(v.charCodeAt()+1));
* // → 'bcd'
*
* xstring.from(new Set().add('a').add('b'));
* // → 'ab'
*
* xstring.from(new Map().set('a', 1).set('b', 2));
* // → 'a,1b,2'
* ```
*/
export function from(xs: Iterable<string>, fm?: MapFunction, ths?: unknown): string {
let a = "", i = 0;
for (const x of xs) {
a += fm? fm.call(ths, x, i) : x;
i++;
}
return a;
}
//#endregion
//#region ABOUT
/**
* Get keys of string, like `Array.keys()`.
* @param x a string
* @returns 0, 1, ...
* @example
* ```javascript
* xstring.keys('ash');
* // → [0, 1, 2]
*
* xstring.keys('');
* // → []
* ```
*/
function* _keys(x: string): IterableIterator<number> {
let i = 0;
for (const _c of x)
yield i++;
}
/**
* Get characters of string, like `Array.values()`.
* @param x a string
* @returns x[0], x[1], ...
*/
function* _values(x: string): IterableIterator<string> {
for (const c of x)
yield c;
}
// export {values as characters};
/**
* Get entries of string, like `Array.entries()`.
* @param x a string
* @returns [0, x[0]], [1, x[1]], ...
* @example
* ```javascript
* xstring.entries('ship');
* // → [[0, 's'], [1, 'h'], [2, 'i'], [3, 'p']]
*
* xstring.entries('');
* // → []
* ```
*/
function* _entries(x: string): IterableIterator<[number, string]> {
let i = 0;
for (const c of x)
yield [i++, c];
}
//#endregion
//#region TRANSFORM
/**
* Remove/replace characters in a string.
* @param x a string
* @param start start index
* @param remove number of characters to remove
* @param add substring to add
* @returns x[0:i] + add + x[i+r] | i = start, r = remove
*/
export function splice(x: string, start: number, remove: number=0, add: string=""): string {
return x.slice(0, start) + add + x.slice(start+remove);
}
/**
* Reverse a string.
* @param x a string
* @returns reversed string
*/
export function reverse(x: string): string {
return [...x].reverse().join("");
}
// - https://stackoverflow.com/a/959004/1413259
// - https://github.com/mathiasbynens/esrever
/**
* Handle comparison of two strings.
* @param a a string
* @param b another string
* @returns x<y: -ve, x=y: 0, x>y: +ve
*/
export type CompareFunction = (a: string, b: string) => number;
/**
* Arrange characters in an order.
* @param x a string
* @param fc compare function (a, b)
*/
export function sort(x: string, fc?: CompareFunction): string {
return [...x].sort(fc).join("");
}
//#endregion
//#region FUNCTIONAL
/**
* Handle selection of a substring (or character) in string.
* @param v a substring (or character)
* @param i index of substring in string
* @param x string containing the substring
* @returns whether it is selected
*/
export type TestFunction = (v: string, i: number, x: string) => boolean;
/**
* Filter characters which pass a test.
* @param x a string
* @param ft test function (v, i, x)
* @param ths this argument
* @returns characters which pass the test
* @example
* ```javascript
* xstring.filter('g00df00d', (v) => v<='f');
* // → '00df00d'
*
* xstring.filter('badfood', (v) => v<='f');
* // → 'badfd'
*
* xstring.filter('', (v) => v<='f');
* // → ''
* ```
*/
export function filter(x: string, ft: TestFunction, ths?: unknown): string {
let a = "", i = 0;
for (const c of x)
if (ft.call(ths, c, i++, x)) a += c;
return a;
}
//#endregion
//#endregion
//#region CUSTOM
//#region GENERATE
/** A buffer containing only spaces, for `spaces()`. */
const SPACE_BUFFER: string = " ".repeat(1024);
/**
* Get a string of spaces.
* @param n number of spaces
* @returns string of spaces
* @example
* ```javascript
* xstring.spaces(4);
* // → ' '
*
* xstring.spaces(6);
* // → ' '
*
* xstring.spaces(0);
* // → ''
*
* xstring.spaces(-1);
* // → ''
* ```
*/
export function spaces(n: number): string {
const S = SPACE_BUFFER.length;
if (n<S) return SPACE_BUFFER.slice(0, n);
return SPACE_BUFFER.repeat(n/S) + " ".repeat(n%S);
}
//#endregion
//#region ABOUT
/**
* Check if value is a string.
* @param v a value
* @returns whether value is a string
* @example
* ```javascript
* xstring.is('panini');
* // → true
*
* xstring.is("valmiki");
* // → true
*
* xstring.is(String('vyasa'));
* // → true
*
* xstring.is({shakespeare: 'literature'});
* // → false
*
* xstring.is(71811313518);
* // → false (what's this?)
*
* xstring.is();
* // → false
* ```
*/
export function is(v: unknown): boolean {
return typeof v==="string";
}
/**
* Check if string is empty.
* @param x a string
* @returns whether string is empty
*/
export function isEmpty(x: string): boolean {
return x.length===0;
}
/**
* Check if string is a character.
* @param x a string
* @returns whether string is a character
*/
export function isCharacter(x: string): boolean {
return x.length===1;
}
/**
* Get non-negative index within string.
* @param x a string
* @param at character index
* @returns +ve index
*/
export function index(x: string, at: number): number {
// Can be rewritten with a math/number function?
if (at<0) return Math.max(0, x.length + at);
return Math.min(at, x.length);
}
/**
* Get non-negative index range within string.
* @param x a string
* @param start start index
* @param end end index
* @returns +ve index range [start, end]
*/
export function indexRange(x: string, start: number, end: number): [number, number] {
// Can be rewritten with a math/number function?
start = index(x, start);
end = index(x, end);
return [Math.min(start, end), Math.max(start, end)];
}
/**
* Get unicode code point range of string.
* @param x a string
* @returns code point range [min, max]
*/
export function codePointRange(x: string): [number, number] {
let min = MAX_CODE_POINT;
let max = MIN_CODE_POINT;
for (const c of x) {
const v = c.codePointAt(0) as number;
min = Math.min(min, v);
max = Math.max(max, v);
}
return [min, max];
}
//#endregion
//#region COMPARE
/**
* Compare two strings.
* @param x a string
* @param y another string
* @returns x<y: -ve, x=y: 0, x>y: +ve
*/
export function compare(x: string, y: string): number {
return x<y? -1 : (x>y? 1 : 0);
}
/**
* Check if two strings are equal.
* @param x a string
* @param y another string
* @returns x=y?
*/
export function isEqual(x: string, y: string): boolean {
return x===y;
}
//#endregion
//#region SEARCH
// Check if string has a given prefix.
export {startsWith as isPrefix};
// Check if string has a given suffix.
export {endsWith as isSuffix};
// Check if string has a given infix.
export {includes as isInfix};
//#endregion
//#region PART
/**
* Get character at a given index in string.
* @param x a string
* @param at character index (-ve ⇒ from right)
* @returns x[i] | i = at
*/
export function get(x: string, at: number): string {
return x.charAt(at>=0? at : x.length+at);
}
export {get as at};
/**
* Get characters at indices.
* @param x a string
* @param ats character indices (-ve ⇒ from right)
* @returns x[i] + x[j] + ... | [i, j, ...] = ats
*/
export function getAll(x: string, ats: Iterable<number>): string {
let a = "";
for (const at of ats)
a += get(x, at);
return a;
}
/**
* Write a substring at specified index in string.
* @param x a string
* @param at write index (-ve ⇒ from right)
* @param write substring to write
* @returns x[0:i] + w + x[i+|w|:] | i = at, w = write
*/
export function set(x: string, at: number, write: string): string {
return x.slice(0, at) + write + x.slice(at+write.length);
}
/**
* Get leftmost part of string.
* @param x a string
* @param count number of characters [1]
* @returns x[0:n] | n = count
* @example
* ```javascript
* xstring.begin('Thai Sweet Chilli Sauce', 4);
* // → 'Thai'
*
* xstring.begin('Thai Sweet Chilli Sauce', 10);
* // → 'Thai Sweet'
*
* xstring.begin('Thai Sweet Chilli Sauce', 80);
* // → 'Thai Sweet Chilli Sauce'
*
* xstring.begin('Thai Sweet Chilli Sauce', -1);
* // → ''
* ```
*/
export function begin(x: string, count: number=1): string {
return x.slice(0, count);
}
export {begin as prefix};
export {begin as left};
/**
* Get a portion of string from middle.
* @param x a string
* @param start start index
* @param count number of characters [1]
* @returns x[i:i+n] | i = start, n = count
* @example
* ```javascript
* xstring.mid('Thai Sweet Chilli Sauce', 5, 5);
* // → 'Sweet'
*
* xstring.mid('Thai Sweet Chilli Sauce', 5, 12);
* // → 'Sweet Chilli'
*
* xstring.mid('Thai Sweet Chilli Sauce', 5);
* // → 'Sweet Chilli Sauce'
*
* xstring.mid('Thai Sweet Chilli Sauce', 5, -1);
* // → ''
*
* xstring.mid('Thai Sweet Chilli Sauce', -5);
* // → 'Sauce'
* ```
*/
export function middle(x: string, start: number, count: number=1): string {
return x.slice(start, start+count);
}
export {middle as infix};
/**
* Get rightmost part of string.
* @param x a string
* @param count number of characters [1]
* @returns x[|x|-n:] | n = count
* @example
* ```javascript
* xstring.end('Thai Sweet Chilli Sauce', 5);
* // → 'Sauce'
*
* xstring.end('Thai Sweet Chilli Sauce', 12);
* // → 'Chilli Sauce'
*
* xstring.end('Thai Sweet Chilli Sauce', 80);
* // → 'Thai Sweet Chilli Sauce'
*
* xstring.end('Thai Sweet Chilli Sauce', -1);
* // → ''
* ```
*/
export function end(x: string, count: number=1): string {
return x.slice(x.length-count);
}
export {end as suffix};
export {end as right};