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/str split #68

Merged
merged 20 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions typescript/src/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./explode";
export * from "./implode";
export * from "./str_repeat";
export * from "./chr";
export * from "./str_split";
25 changes: 25 additions & 0 deletions typescript/src/string/str_split.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { str_split } from "./str_split";

const str = "Hello World!";

describe("string split", () => {
elianiva marked this conversation as resolved.
Show resolved Hide resolved
it("should be able to return every letter as element of the array", () => {
expect(str_split(str)).toEqual(
["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"]
);
});

it("should be able to return 2 or more letter as element of the array depend on number of len", () => {
expect(str_split(str, 2)).toEqual(["He", "ll", "o ", "Wo", "rl", "d!"]);
expect(str_split(str, 3)).toEqual(["Hel", "lo ", "Wor", "ld!"]);
expect(str_split(str, 4)).toEqual(["Hell", "o Wo", "rld!"]);
});

it("should be able to return false", () => {
expect(str_split(str, -1)).toEqual(false);
});

it("should be able to return the entire string as the only element of the array", () => {
expect(str_split(str, 15)).toEqual(["Hello World!"]);
});
});
31 changes: 31 additions & 0 deletions typescript/src/string/str_split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* str_split — Convert a string to an array
* @param str - The string to be convert.
elianiva marked this conversation as resolved.
Show resolved Hide resolved
* @param len - Maximum length of the chunk.
* @returns If the optional length parameter is specified, the returned array will be broken down into chunks with each being length in length, except the final chunk which may be shorter if the string does not divide evenly. The default length is 1, meaning every chunk will be one byte in size.
*/
export const str_split = (str: string, len?: number): string[] | false => {
if (typeof str === "undefined" || typeof str !== "string") return false;
elianiva marked this conversation as resolved.
Show resolved Hide resolved
if (typeof len !== "undefined" && typeof len === "number") {
elianiva marked this conversation as resolved.
Show resolved Hide resolved
if (len < 0) return false;

const arr: string[] = [];
let result = "";
let counter = 0;
for (let i=0; i<str.length; i++) {
result = result + str[i];
counter++;

if (counter === len || i === str.length - 1) {
arr.push(result);
result = "";
counter = 0;
}
}

return arr;
}

return str.split("");
elianiva marked this conversation as resolved.
Show resolved Hide resolved
};