Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TypeScript): ltrim #71

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions typescript/src/string/count_chars.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
/**
* chr — Return information about characters used in a string.
* @param string - The examined string.
* count_chars — Return information about characters used in a string.
* @param str - The examined string.
* @param mode - See return values.
* @returns Depending on mode count_chars() returns one of the following:
* 0 - an array with the byte-value as key and the frequency of every byte as value.
* 1 - same as 0 but only byte-values with a frequency greater than zero are listed.
* 2 - same as 0 but only byte-values with a frequency equal to zero are listed.
* 3 - a string containing all unique characters is returned.
* 4 - a string containing all not used characters is returned.
* 0 - an array with the byte-value as key and the frequency of every byte as value.
* 1 - same as 0 but only byte-values with a frequency greater than zero are listed.
* 2 - same as 0 but only byte-values with a frequency equal to zero are listed.
* 3 - a string containing all unique characters is returned.
* 4 - a string containing all not used characters is returned.
*/
export const count_chars = (string: string, mode: number): number[]|{[key: number]: number}|string => {
export const count_chars = (str: string, mode: number): number[]|{[key: number]: number}|string => {
switch (mode) {
case 0: {
return string.split("").reduce((prevArray: number[], char: string) => {
return str.split("").reduce((prevArray: number[], char: string) => {
const charByte = char.charCodeAt(0);
prevArray[charByte]++;
return prevArray;
}, new Array(255).fill(0) as number[]);
}
case 1: {
return string.split("").reduce((prevObject: {[key: number]: number}, char: string) => {
return str.split("").reduce((prevMap: {[key: number]: number}, char: string) => {
const charByte = char.charCodeAt(0);
prevObject[charByte] = !prevObject[charByte] ? 1 : prevObject[charByte] + 1;
return prevObject;
prevMap[charByte] = !prevMap[charByte] ? 1 : prevMap[charByte] + 1;
return prevMap;
}, {});
}
case 2: {
const byteObject: {[key: number]: number} = {};
for (let i = 0; i < 255; i++) {
byteObject[i] = 0;
}
string.split("").forEach((char: string) => {
str.split("").forEach((char: string) => {
const charByte = char.charCodeAt(0);
delete byteObject[charByte];
});
return byteObject;
}
case 3: {
return Array.from(new Set(string)).sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join("");
return Array.from(new Set(str)).sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join("");
}
case 4: {
return new Array(255).fill(0).map((_, i) => {
const char = String.fromCharCode(i);
return string.includes(char) ? "" : char;
return str.includes(char) ? "" : char;
}).join("");
}
default: {
Expand Down
5 changes: 4 additions & 1 deletion typescript/src/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ export * from "./implode";
export * from "./lcfirst";
export * from "./str_repeat";
export * from "./chr";
export * from "./str_split";
export * from "./str_split";
export * from "./addslashes";
export * from "./count_chars";
export * from "./ltrim";
52 changes: 52 additions & 0 deletions typescript/src/string/ltrim.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ltrim } from "./ltrim";

describe("ltrim with default characters", () => {
it("should return trimmed string", () => {
expect(ltrim(" Hello, World!")).toStrictEqual("Hello, World!");
expect(ltrim("\n \t Hello, World!")).toStrictEqual("Hello, World!");
expect(ltrim("\0 \vHello, World!")).toStrictEqual("Hello, World!");
});

it("should not trim the right side of the string", () => {
expect(ltrim("Hello, World! ")).toStrictEqual("Hello, World! ");
expect(ltrim(" Hello, World! ")).toStrictEqual("Hello, World! ");
});

it("should return the string if there is nothing to be trimmed", () => {
expect(ltrim("Hello, World!")).toStrictEqual("Hello, World!");
});
});

describe("ltrim with different characters", () => {
it("should return trimmed string with different characters", () => {
expect(ltrim("rrrrrrrHello, World!", "r")).toStrictEqual("Hello, World!");
expect(ltrim("rtrtrttrrHello, World!", "rt")).toStrictEqual("Hello, World!");
expect(ltrim("0293174856Hello, World!", "0123456789")).toStrictEqual("Hello, World!");
});

it("should not trim the right side of the string", () => {
expect(ltrim("Hello, World!rrrrrr", "r")).toStrictEqual("Hello, World!rrrrrr");
expect(ltrim("rrrrrHello, World!rrrrr", "r")).toStrictEqual("Hello, World!rrrrr");
});

it("should trim regex special characters", () => {
expect(ltrim(".*+Hello, World!", "*.+")).toStrictEqual("Hello, World!");
expect(ltrim("[$$$*]Hello, World!", "*$[]")).toStrictEqual("Hello, World!");
});
});

describe("ltrim with character range", () => {
it("should return trimmed string for ranged characters", () => {
expect(ltrim("\x09Hello, World!\x0A", "\x00..\x1F")).toStrictEqual("Hello, World!\x0A");
expect(ltrim("0293174856Hello, World!", "0..9")).toStrictEqual("Hello, World!");
});

it("should also work with reverse range", () => {
expect(ltrim("aiaxmioxjHello, World!", "z..a")).toStrictEqual("Hello, World!");
});

it("should also work with multiple ranges", () => {
expect(ltrim("38Hello, World!", "1..57..9")).toStrictEqual("Hello, World!");
expect(ltrim("6Hello, World!", "1..57..9")).toStrictEqual("6Hello, World!");
});
});
37 changes: 37 additions & 0 deletions typescript/src/string/ltrim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* ltrim — Strip whitespace (or other characters) from the beginning of a string.
* @param str - The input string.
* @param characters - You can also specify the characters you want to strip, by means of the characters parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
* @returns string with whitespace stripped from the beginning of string
* Without the second parameter, ltrim() will strip these characters:
* " " (ASCII 32 (0x20)), an ordinary space.
* "\t" (ASCII 9 (0x09)), a tab.
* "\n" (ASCII 10 (0x0A)), a new line (line feed).
* "\r" (ASCII 13 (0x0D)), a carriage return.
* "\0" (ASCII 0 (0x00)), the NUL-byte.
* "\v" (ASCII 11 (0x0B)), a vertical tab.
*/
export const ltrim = (str: string, characters = " \n\r\t\v\x00"): string => {
let charactersEscaped = "";
const ranges = characters.match(/.\.\../g);
if (!ranges) {
charactersEscaped = characters;
} else {
ranges.forEach(range => {
const [start, end] = range.split("..");
let startCode = start.charCodeAt(0);
let endCode = end.charCodeAt(0);

if (startCode > endCode) {
[startCode, endCode] = [endCode, startCode];
}
for (let i = startCode; i <= endCode; i++) {
charactersEscaped += String.fromCharCode(i);
}
});
}

charactersEscaped = charactersEscaped.replace("\\", "\\\\").replace("]", "\\]");
const charactersRegex = new RegExp(`^[${charactersEscaped}]+`);
return str.replace(charactersRegex, "");
};