Skip to content

Commit 5462bcd

Browse files
committed
feat(Util): add efficient trim string functions with chars customizable
1 parent 1525e63 commit 5462bcd

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/Connect.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,11 @@ export enum Type {
6161
* @param params The parameters for which the media extension is to be defined
6262
*/
6363
export function defineMediaExt(type: Type, params: Params) {
64-
// Fix mediaExt
65-
/// remove all the possible '.' prefix
64+
// Fix mediaExt in removing the possible '.' prefix
6665
if (params.mediaExt) {
67-
let i = 0;
68-
while (params.mediaExt.charAt(i) === '.') {
69-
++i;
70-
}
71-
params.mediaExt = params.mediaExt.substring(i);
66+
params.mediaExt = Util.trimStart(params.mediaExt, '.');
7267
}
73-
/// Set mediaExt out parameter if not set!
68+
// Compute appropriate mediaExt out parameter
7469
switch (type) {
7570
case Type.HESP:
7671
params.mediaExt = 'mp4';

src/Util.ts

+57
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,60 @@ export function getExtension(path: string): string {
290290
export function getFile(path: string): string {
291291
return path.substring(path.lastIndexOf('/') + 1);
292292
}
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

Comments
 (0)