1.0.17 • Published 12 months ago
ts-methods v1.0.17
ts-methods
ts-methods is a typescript scripting library with an array of utility functions; including error and file handling, terminal executions, data structures, REPLs, utility types, github clone scripts, and more. The following is a list of helpers and how to use them:
- Error Helpers
- File Helpers
- Structs Helpers (data structures)
- Type Helpers
- OS Helpers (terminal execution)
- REPL Helpers
- Net Helpers
- Github Helpers
- Global Helpers
Installation
npm i ts-methods
Error Helpers
import { UtilsError, catchError } from "ts-methods/dist/error";
const addFunc = (a: number, b: number) => a + b;
const throwUtilsError = (msg?: string): void => {
const message = msg ?? "sample error";
throw new UtilsError(message);
};
const main = async () => {
// Catch Thrown Error
await catchError<ReturnType<typeof throwUtilsError>>(() =>
throwUtilsError("error1")
);
const sum = await catchError<ReturnType<typeof addFunc>>(
() => addFunc(5, 2) // Output: 7
);
};
main().then((val) => console.log(val));
File Helpers
import {
CONSTANTS,
createIfNotExist,
root,
writeJson2D,
readJson2D,
} from "ts-methods/dist/fs";
const main = async () => {
// Create File / Path
await createIfNotExist(await root(CONSTANTS));
// 2D JSON
const written2 = await writeJson2D(
await root(CONSTANTS),
"key1",
"key2",
"value"
);
if (!written2) {
console.log("Not Written 2D json");
} else {
console.log(await readJson2D(await root(CONSTANTS), "key1", "key2"));
}
};
main().then((val) => console.log(val));
Structs Helpers
import structs from "ts-methods/dist/structs";
const main = async () => {
// Priority Queue
const pq = new structs.PriorityQueue<string>();
pq.enqueue("item1", 2);
pq.enqueue("item2", 1);
pq.enqueue("item3", 3);
console.log(pq.dequeue()); // Output: item2
console.log(pq.dequeue()); // Output: item1
console.log(pq.dequeue()); // Output: item3
// Doubly Linked List
const list = new structs.DoublyLinkedList<number>();
// HashMap
const map = new structs.HashMap<string, number>();
};
main().then((val) => console.log(val));
Type Helpers
import {
Expect,
Equal,
NotEqual,
Debug,
MergeInsertions,
Alike,
ExpectExtends,
doNotExecute,
} from "../src/types";
export const main = async () => {
doNotExecute(async () => {
type Person = { name: string; age: number };
type Person1 = { name: string; age: number };
type Address = { name: string; age: number; street: string };
type test2 = [Expect<Equal<1, 1>>];
type test3 = [Expect<NotEqual<1, 2>>];
const test6: Debug<Person> = { name: "John Doe", age: 8 };
const test7: MergeInsertions<Person & Address> = {
name: "John Doe",
age: 9,
street: "Cortney Terrace",
};
type test8 = [Expect<Alike<Person, Person1>>];
type test9 = [Expect<ExpectExtends<Person, Address>>];
});
};
main().then((val) => console.log(val));
OS Helpers
import {
execute,
catchExecute,
safeExecute,
uname,
unameExecute,
} from "ts-methods/dist/os";
const main = async () => {
// Execute
console.log(await execute("echo me")); // Output: { stdout: "me", stderr: "" }
console.log(await catchExecute("echo me")); // Output: { stdout: "me", stderr: "" }
console.log(await safeExecute("echo me")); // Output: "me"
// Uname
console.log(await uname()); // Output: "Mac"
console.log(await unameExecute({ Mac: "ls" })());
};
main().then((val) => console.log(val));
REPL Helpers
import { readLine, readLineSelect, REPL } from "ts-methods/dist/repl";
class Chat extends REPL {
constructor() {
super("Enter Text: ", true);
}
override default(cmds: string[]): void {
console.log(cmds);
}
edit(cmds: string[]): void {
if (cmds.length < 3) return;
const second = cmds[1];
const third = cmds[2];
if (second != "question") return;
this.question = third;
}
}
export const main = async () => {
console.log(await readLine("Question 1: "));
console.log(await readLineSelect("Question 2: ", ["boy", "girl"]));
new Chat();
};
main().then((val) => console.log(val));
Github Helpers
import { GithubREPL, gitRepoUrlAdd, cloneAll } from "ts-methods/dist/github";
const main = async () => {
await gitRepoUrlAdd();
await cloneAll();
new GithubREPL();
};
main().then((val) => console.log(val));
Global Helpers
# Add "types/global.d.ts" to the (include array in tsconfig.json)
rm -rf types && mkdir types
cp -r node_modules/ts-methods/dist/global.d.ts types/global.d.ts
1.0.17
12 months ago
1.0.16
12 months ago
1.0.15
12 months ago
1.0.14
12 months ago
1.0.13
12 months ago
1.0.12
12 months ago
1.0.11
12 months ago
1.0.10
12 months ago
1.0.9
12 months ago
1.0.8
12 months ago
1.0.7
12 months ago
1.0.6
12 months ago
1.0.5
12 months ago
1.0.4
12 months ago
1.0.3
12 months ago
1.0.2
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago