@@ -290,3 +290,60 @@ export function getExtension(path: string): string {
290
290
export function getFile ( path : string ) : string {
291
291
return path . substring ( path . lastIndexOf ( '/' ) + 1 ) ;
292
292
}
293
+
294
+ function codesFromString ( value : string ) : Array < number > {
295
+ const codes = [ ] ;
296
+ for ( let i = 0 ; i < value . length ; ++ i ) {
297
+ codes . push ( value . charCodeAt ( i ) ) ;
298
+ }
299
+ return codes ;
300
+ }
301
+
302
+ /**
303
+ * String Trim function with customizable chars
304
+ * @param value string to trim
305
+ * @param chars chars to use to trim
306
+ * @returns string trimmed
307
+ */
308
+ export function trim ( value : string , chars : string = ' ' ) : string {
309
+ const codes = codesFromString ( chars ) ;
310
+ let start = 0 ;
311
+ while ( start < value . length && codes . includes ( value . charCodeAt ( start ) ) ) {
312
+ ++ start ;
313
+ }
314
+ let end = value . length ;
315
+ while ( end > 0 && codes . includes ( value . charCodeAt ( end - 1 ) ) ) {
316
+ -- end ;
317
+ }
318
+ return value . substring ( start , end ) ;
319
+ }
320
+
321
+ /**
322
+ * String Trim Start function with customizable chars
323
+ * @param value string to trim start
324
+ * @param chars chars to use to trim start
325
+ * @returns string trimmed
326
+ */
327
+ export function trimStart ( value : string , chars : string = ' ' ) : string {
328
+ const codes = codesFromString ( chars ) ;
329
+ let i = 0 ;
330
+ while ( i < value . length && codes . includes ( value . charCodeAt ( i ) ) ) {
331
+ ++ i ;
332
+ }
333
+ return value . substring ( i ) ;
334
+ }
335
+
336
+ /**
337
+ * String Trim End function with customizable chars
338
+ * @param value string to trim end
339
+ * @param chars chars to use to trim end
340
+ * @returns string trimmed
341
+ */
342
+ export function trimEnd ( value : string , chars : string = ' ' ) : string {
343
+ const codes = codesFromString ( chars ) ;
344
+ let i = value . length ;
345
+ while ( i > 0 && codes . includes ( value . charCodeAt ( i - 1 ) ) ) {
346
+ -- i ;
347
+ }
348
+ return value . substring ( 0 , i ) ;
349
+ }
0 commit comments