-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubstring.ts
166 lines (155 loc) · 6.29 KB
/
substring.ts
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
/*
* @nevware21/ts-utils
* https://github.com/nevware21/ts-utils
*
* Copyright (c) 2022 NevWare21 Solutions LLC
* Licensed under the MIT license.
*/
import { isNullOrUndefined, isUndefined } from "../helpers/base";
import { dumpObj } from "../helpers/diagnostics";
import { throwTypeError } from "../helpers/throw";
import { EMPTY, LENGTH, StrProto } from "../internal/constants";
import { _unwrapFunction, _unwrapFunctionWithPoly } from "../internal/unwrapFunction";
import { mathMax } from "../math/min_max";
import { strSlice } from "./slice";
/**
* The `strSubstring()` method returns the part of the string between the start and end indexes, or to the end of the string.
*
* `strSubstring()` extracts characters from indexStart up to but not including indexEnd. In particular:
* - If `indexEnd` is omitted, strSubstring() extracts characters to the end of the string.
* - If `indexStart` is equal to indexEnd, strSubstring() returns an empty string.
* - If `indexStart` is greater than indexEnd, then the effect of strSubstring() is as if the two arguments were swapped; see example below.
*
* Any argument value that is less than 0 or greater than `value.length` is treated as if it were 0 and `value.length`, respectively.
*
* Any argument value that is NaN is treated as if it were 0.
* @group String
* @param value - The string value to return the substring from.
* @param indexStart - The index of the first character to include in the returned substring.
* @param indexEnd - The index of the first character to exclude from the returned substring.
* @return A new string containing the specified part of the given string
* @example
* ```ts
* const anyString = 'Nevware21';
* // Displays 'N'
* console.log(strSubstring(anyString, 0, 1));
* console.log(strSubstring(anyString, 1, 0));
*
* // Displays 'Nevwar'
* console.log(strSubstring(anyString, 0, 6));
*
* // Displays 'are21'
* console.log(strSubstring(anyString, 4));
*
* // Displays 'are'
* console.log(strSubstring(anyString, 4, 7));
*
* // Displays '21'
* console.log(strSubstring(anyString, 7, 4));
*
* // Displays 'Nevware'
* console.log(strSubstring(anyString, 0, 7));
*
* // Displays 'Nevware21'
* console.log(strSubstring(anyString, 0, 10));
* ```
*/
export const strSubstring: (value: string, indexStart: number, indexEnd?: number) => string = (/*#__PURE__*/_unwrapFunction("substring", StrProto));
/**
* The strSubstr() method returns a portion of the string, starting at the specified index and extending for a given
* number of characters afterwards.
*
* @since 0.4.2
* @group String
* @param value - The string value to return the substring from.
* @param start - The index of the first character to include in the returned substring.
* @param length - The number of characters to extract.
* @returns A new string containing the specified part of the given string.
*/
export const strSubstr: (value: string, start: number, length?: number) => string = (/*#__PURE__*/_unwrapFunctionWithPoly("substr", StrProto, polyStrSubstr));
/**
* The polyStrSubstr() method returns a portion of the string, starting at the specified index and extending for a given
* number of characters afterwards.
*
* @since 0.4.2
* @group String
* @group Polyfill
* @param value - The string value to return the substring from.
* @param start - The index of the first character to include in the returned substring.
* @param length - The number of characters to extract.
* @returns A new string containing the specified part of the given string.
*/
/*#__NO_SIDE_EFFECTS__*/
export function polyStrSubstr(value: string, start: number, length?: number): string {
if (isNullOrUndefined(value)) {
throwTypeError("Invalid " + dumpObj(value));
}
if (length < 0) {
return EMPTY;
}
// If start is omitted or undefined, its treated as zero
start = start || 0;
if (start < 0) {
start = mathMax(start + value[LENGTH], 0);
}
if (isUndefined(length)) {
return strSlice(value, start);
}
return strSlice(value, start, start + length);
}
/**
* Returns a substring of the string starting from the left.
*
* `strLeft()` extracts the number of characters from left of the string up to the count. In particular:
* - If `count` is less than zero, strLeft() returns an empty string.
* - If `count` is less than `value.length`, strLeft() returns a new string with the `count` number of characters from the left of the string.
* - If `count` is greater than `value.length`, then the value original value is returned.
*
* Any argument value that is NaN is treated as if it were 0.
*
* @since 0.4.2
* @group String
* @param value - The string value to return the substring from.
* @param count - The number of characters to extract
* @returns The substring based on the count number of characters from the right
* @example
* ```ts
* strLeft("Nevware21", -1); // ""
* strLeft("Nevware21", 0); // ""
* strLeft("Nevware21", 1); // "N"
* strLeft("Nevware21", 3); // "Nev"
* strLeft("Nevware21", 21); // "Nevware21"
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function strLeft(value: string, count: number): string {
return strSubstring(value, 0, count);
}
/**
* Returns a substring of the string starting from the right.
*
* `strRight()` extracts the number of characters from right of the string up to the count. In particular:
* - If `count` is less than zero, strRight() returns an empty string.
* - If `count` is less than `value.length`, strRight() returns a new string with the `count` number of characters from the right of the string.
* - If `count` is greater than `value.length`, then the value original value is returned.
*
* Any argument value that is NaN is treated as if it were 0.
*
* @since 0.4.2
* @group String
* @param value - The string value to return the substring from.
* @param count - The number of characters to extract
* @returns The substring based on the count number of characters from the right
* @example
* ```ts
* strRight("Nevware21", -1); // ""
* strRight("Nevware21", 0); // ""
* strRight("Nevware21", 1); // "1"
* strRight("Nevware21", 3); // "e21"
* strRight("Nevware21", 21); // "Nevware21"
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function strRight(value: string, count: number): string {
return count <= 0 ? EMPTY : (value[LENGTH] > count ? strSlice(value, -count) : value);
}