@leojsandkotdev/korusjs 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.si.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "plugin": "org.smartide.plugin.editor",
3
+ "run": "npx tsc src/index.ts",
4
+ "intelligence": {
5
+ ".json": {
6
+ "enabled": true,
7
+ "run": "vscode-json-language-server --stdio"
8
+ }
9
+ }
10
+ }
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # korusjs 🚀
2
+
3
+ A **korusjs** é uma biblioteca leve e robusta para TypeScript, projetada para lidar com valores nulos, opcionais e transformações de dados de forma segura.
4
+
5
+ Este projeto é a evolução da **koruspy**, minha biblioteca original escrita em Python, agora trazendo a mesma filosofia de segurança para o ecossistema JavaScript/TypeScript.
6
+
7
+ ---
8
+
9
+ ## ✨ Características
10
+
11
+ * **Type Safety:** Evite erros de `null` ou `undefined` em tempo de execução.
12
+ * **Inspirada na koruspy:** Mantém a lógica de tratamento de dados que criei na versão Python.
13
+ * **Leve e Rápida:** Sem dependências externas, perfeita para projetos de qualquer tamanho.
14
+ * **Desenvolvida no Termux:** Criada e testada inteiramente em ambiente mobile (Android).
15
+
16
+ ---
17
+
18
+ ## 📦 Instalação
19
+
20
+ ```bash
21
+ npm install korusjs
22
+ ```
23
+ ## 🛠️ Como Usar
24
+ Usando o tipo Option
25
+ O Option permite que você lide com valores que podem não existir sem quebrar o seu código.
26
+ ```typescript
27
+ import { OptionOf } from 'korusjs';
28
+
29
+ // Criando um valor seguro
30
+ const nome = OptionOf("Usuário").unwrapOr("Desconhecido");
31
+
32
+ // Lidando com valores nulos
33
+ const nulo = OptionOf(null).unwrapOr("Valor Padrão");
34
+ console.log(nulo); // "Valor Padrão"
35
+
36
+ // Conversão segura de tipos
37
+ const numero = OptionOf("42").toInt();
38
+ console.log(numero.isSome()); // true
39
+ ```
40
+
41
+ ## Funções Úteis (println)
42
+ A biblioteca também inclui utilitários práticos para saída de dados:
43
+ ```typescript
44
+ import { println } from 'korusjs';
45
+
46
+ println("Korusjs está pronta!", true);
47
+ // Imprime com quebra de linha
48
+ ```
49
+
50
+ ## 🧑‍💻 Sobre o Projeto
51
+ Olá! Eu tenho 12 anos e sou o criador da koruspy e agora da korusjs. Desenvolvi esta biblioteca para aprender mais sobre TypeScript e para facilitar o meu próprio fluxo de trabalho no Android usando o Termux.
52
+ Este projeto não busca ser "ultra-profissional", mas sim funcional, educativo e seguro. Foi feito de um estudante para outros desenvolvedores que gostam de simplicidade.
53
+ 📄 Licença
54
+ Este projeto está sob a licença MIT.
@@ -0,0 +1,3 @@
1
+ export * from "./option.ts";
2
+ export { println, advancedPrintln } from "./println.ts";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./option.ts";
2
+ export { println, advancedPrintln } from "./println.ts";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,35 @@
1
+ export type Option<T> = Some<T> | Nothing;
2
+ export declare class Nothing {
3
+ readonly kind: "nothing";
4
+ toString(): string;
5
+ map<U>(_fn: (v: any) => U): Option<U>;
6
+ getValue<T>(): null;
7
+ filter(_fn: (v: any) => boolean): Option<any>;
8
+ finalize<T>(): never;
9
+ unwrapOr<T>(defaultValue: T): T;
10
+ unwrapOrElse<T>(_fn: () => T): T;
11
+ isPresent(): boolean;
12
+ ifPresent(_fn: (v: any) => void): void;
13
+ andThen<U>(_fn: (v: never) => Option<U>): Option<U>;
14
+ toInt(): Option<number>;
15
+ }
16
+ export declare const _nothing: Nothing;
17
+ export declare class Some<T> {
18
+ private readonly value;
19
+ readonly kind: "some";
20
+ constructor(value: T);
21
+ toString(): string;
22
+ unwrapOr(defaultValue: T): T;
23
+ toInt(): Option<number>;
24
+ finalize(): T;
25
+ filter(fn: (v: T) => boolean): Option<T>;
26
+ map<U>(fn: (v: T) => U): Option<U>;
27
+ unwrapOrElse(fn: () => T): T;
28
+ andThen<U>(fn: (v: T) => Option<U>): Option<U>;
29
+ ifPresent(fn: (v: T) => void): void;
30
+ isPresent(): boolean;
31
+ getValue(): Some<T>;
32
+ }
33
+ export declare function OptionOf<T>(value: T | null | undefined, defaultValue?: T): Option<T>;
34
+ export { _nothing as nothing };
35
+ //# sourceMappingURL=option.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;AAE1C,qBAAa,OAAO;IAChB,QAAQ,CAAC,IAAI,EAAG,SAAS,CAAS;IAClC,QAAQ,IAAI,MAAM;IAGlB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IAGrC,QAAQ,CAAC,CAAC,KAAK,IAAI;IAGnB,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC;IAG7C,QAAQ,CAAC,CAAC,KAAK,KAAK;IAGpB,QAAQ,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC;IAG/B,YAAY,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC;IAGhC,SAAS,IAAG,OAAO;IA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AAG,MAAM,CAAC,CAAC,CAAC,CAQpF;AACD,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,CAAC"}
package/dist/option.js ADDED
@@ -0,0 +1,94 @@
1
+ export class Nothing {
2
+ kind = "nothing";
3
+ toString() {
4
+ return "Nothing";
5
+ }
6
+ map(_fn) {
7
+ return this;
8
+ }
9
+ getValue() {
10
+ return null;
11
+ }
12
+ filter(_fn) {
13
+ return this;
14
+ }
15
+ finalize() {
16
+ throw new Error("tried to finalize value from Nothing type");
17
+ }
18
+ unwrapOr(defaultValue) {
19
+ return defaultValue;
20
+ }
21
+ unwrapOrElse(_fn) {
22
+ return _fn();
23
+ }
24
+ isPresent() {
25
+ return false;
26
+ }
27
+ ifPresent(_fn) {
28
+ return;
29
+ }
30
+ andThen(_fn) {
31
+ return this;
32
+ }
33
+ toInt() {
34
+ return this;
35
+ }
36
+ }
37
+ export const _nothing = new Nothing();
38
+ export class Some {
39
+ value;
40
+ kind = "some";
41
+ constructor(value) {
42
+ this.value = value;
43
+ }
44
+ toString() {
45
+ return `Some(${JSON.stringify(this.value)})`;
46
+ }
47
+ unwrapOr(defaultValue) {
48
+ return this.value;
49
+ }
50
+ toInt() {
51
+ const n = Number(this.value);
52
+ return isNaN(n) ? _nothing : new Some(n);
53
+ }
54
+ finalize() {
55
+ return this.value;
56
+ }
57
+ filter(fn) {
58
+ return fn(this.value) ? this : _nothing;
59
+ }
60
+ map(fn) {
61
+ return new Some(fn(this.value));
62
+ }
63
+ unwrapOrElse(fn) {
64
+ return this.value;
65
+ }
66
+ andThen(fn) {
67
+ try {
68
+ return fn(this.value);
69
+ }
70
+ catch (e) {
71
+ return _nothing;
72
+ }
73
+ }
74
+ ifPresent(fn) {
75
+ fn(this.value);
76
+ }
77
+ isPresent() {
78
+ return true;
79
+ }
80
+ getValue() {
81
+ return new Some(this.value);
82
+ }
83
+ }
84
+ export function OptionOf(value, defaultValue) {
85
+ if (value != null) {
86
+ return new Some(value);
87
+ }
88
+ if (defaultValue != null) {
89
+ return new Some(defaultValue);
90
+ }
91
+ return _nothing;
92
+ }
93
+ export { _nothing as nothing };
94
+ //# sourceMappingURL=option.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.js","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,OAAO;IACP,IAAI,GAAG,SAAkB,CAAA;IAClC,QAAQ;QACJ,OAAO,SAAS,CAAA;IACpB,CAAC;IACD,GAAG,CAAI,GAAkB;QACrB,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,QAAQ;QACJ,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,MAAM,CAAC,GAAwB;QAC3B,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,QAAQ;QACJ,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IACjE,CAAC;IACD,QAAQ,CAAI,YAAe;QACvB,OAAO,YAAY,CAAC;IACxB,CAAC;IACD,YAAY,CAAI,GAAY;QACxB,OAAO,GAAG,EAAE,CAAC;IACjB,CAAC;IACD,SAAS;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,SAAS,CAAC,GAAqB;QAC3B,OAAO;IACX,CAAC;IACD,OAAO,CAAI,GAA4B;QACnC,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,KAAK;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AACD,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAE,CAAC;AAEtC,MAAM,OAAO,IAAI;IAEgB;IADpB,IAAI,GAAG,MAAe,CAAA;IAC/B,YAA6B,KAAQ;QAAR,UAAK,GAAL,KAAK,CAAG;IAAE,CAAC;IACxC,QAAQ;QACJ,OAAO,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAA;IAChD,CAAC;IACD,QAAQ,CAAC,YAAe;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IACD,KAAK;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ClC,CAAC;IACD,OAAO,QAAQ,CAAC;AACpB,CAAC;AACD,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=option.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.test.d.ts","sourceRoot":"","sources":["../src/option.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,30 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { Some, nothing, OptionOf } from './option.ts'; // ajuste o caminho
3
+ describe('Option (korusjs)', () => {
4
+ it('deve converter string para int com sucesso', () => {
5
+ const s = new Some("10");
6
+ const result = s.toInt();
7
+ expect(result.finalize()).toBe(10);
8
+ });
9
+ it('deve retornar nothing ao falhar no toInt', () => {
10
+ const s = new Some("abc");
11
+ const result = s.toInt();
12
+ expect(result.kind).toBe("nothing");
13
+ });
14
+ it('deve lançar erro ao dar finalize num Nothing', () => {
15
+ expect(() => {
16
+ nothing.finalize();
17
+ }).toThrow("tried to finalize value from Nothing type");
18
+ });
19
+ it('deve funcionar o unwrapOr', () => {
20
+ expect(nothing.unwrapOr(50)).toBe(50);
21
+ expect(new Some(10).unwrapOr(50)).toBe(10);
22
+ });
23
+ it('deve manter o fluxo no andThen', () => {
24
+ const res = new Some(5)
25
+ .andThen(v => new Some(v * 2))
26
+ .finalize();
27
+ expect(res).toBe(10);
28
+ });
29
+ });
30
+ //# sourceMappingURL=option.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"option.test.js","sourceRoot":"","sources":["../src/option.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC,CAAC,mBAAmB;AAE1E,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAE9B,EAAE,CAAC,4CAA4C,EA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C,IAAI,CAAC,EAAE,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ export declare function println(...args: unknown[]): void;
2
+ export declare function advancedPrintln(options?: {
3
+ newLine?: boolean;
4
+ decimals?: number;
5
+ }, ...args: unknown[]): void;
6
+ //# sourceMappingURL=println.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"println.d.ts","sourceRoot":"","sources":["../src/println.ts"],"names":[],"mappings":"AACA,wBAAgB,OAAO,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,QAqCzC;AACD,wBAAgB,eAAe,CAAC,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAC,MAAM,CAAA;CAAO,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,QAmCxG"}
@@ -0,0 +1,77 @@
1
+ import { Some, Nothing } from "./option.ts";
2
+ export function println(...args) {
3
+ let outputs = [];
4
+ args.forEach(arg => {
5
+ switch (typeof (arg)) {
6
+ case "string":
7
+ case "boolean":
8
+ outputs.push(String(arg));
9
+ break;
10
+ case "number":
11
+ outputs.push(String(Number.isInteger(arg) ? arg : arg.toFixed(4)));
12
+ break;
13
+ case "object":
14
+ if (arg === null) {
15
+ outputs.push("null");
16
+ break;
17
+ }
18
+ else if (Array.isArray(arg)) {
19
+ outputs.push("[" + arg.map(x => String(x)).join(", ") + "]");
20
+ break;
21
+ }
22
+ else if (arg instanceof Some) {
23
+ outputs.push(String(arg));
24
+ }
25
+ else if (arg instanceof Nothing) {
26
+ outputs.push("Nothing");
27
+ }
28
+ else {
29
+ outputs.push(JSON.stringify(arg, null, 2));
30
+ break;
31
+ }
32
+ default:
33
+ outputs.push(String(arg));
34
+ }
35
+ });
36
+ outputs.forEach(output => {
37
+ process.stdout.write(JSON.stringify(output, null, 2));
38
+ process.stdout.write("\n");
39
+ });
40
+ }
41
+ export function advancedPrintln(options = {}, ...args) {
42
+ let outputs = [];
43
+ const newLine = options.newLine ?? true;
44
+ const decimals = options.decimals ?? 2;
45
+ args.forEach(arg => {
46
+ switch (typeof (arg)) {
47
+ case "string":
48
+ case "boolean":
49
+ outputs.push(String(arg));
50
+ break;
51
+ case "number":
52
+ outputs.push(String(Number.isInteger(arg) ? arg : arg.toFixed(Number(decimals))));
53
+ break;
54
+ case "object":
55
+ if (arg === null) {
56
+ outputs.push("null");
57
+ break;
58
+ }
59
+ else if (Array.isArray(arg)) {
60
+ outputs.push("[" + arg.map(x => String(x)).join(", ") + "]");
61
+ break;
62
+ }
63
+ else {
64
+ outputs.push(JSON.stringify(arg, null, 2));
65
+ break;
66
+ }
67
+ default:
68
+ outputs.push(String(arg));
69
+ }
70
+ });
71
+ const separador = newLine ? "\n" : " ";
72
+ const outputFinal = outputs.join(separador);
73
+ console.log(outputFinal);
74
+ if (newLine)
75
+ process.stdout.write("\n");
76
+ }
77
+ //# sourceMappingURL=println.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"println.js","sourceRoot":"","sources":["../src/println.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC5C,MAAM,UAAU,OAAO,CAAC,GAAG,IAAe;IACtC,IAAI,OAAO,GAAc,EAAE,CAAC;IAE5B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACf,QAAO,OAAM,CAAC,GAAG,CAAC,EAAC,CAAC;YAChB,KAAK,QAAQ,CAAC;YACd,KAAK,SAAS;gBACV,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;gBAC1B,MAAM;YACV,KAAK,QAAQ;gBACT,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;g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,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;IACvC,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAE5C,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACzB,IAAG,OAAO;QAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAC3C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@leojsandkotdev/korusjs",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.1.0",
7
+ "description": "Option / Result handling inspired and made by the author of koruspy library",
8
+ "main": "index.js",
9
+ "type": "module",
10
+ "types": "dist/index.d.ts",
11
+ "scripts": {
12
+ "debug": "npx tsx src/index.ts",
13
+ "runjscode": "node dist/index.js",
14
+ "build": "tsc --outDir dist",
15
+ "vitest": "vitest run",
16
+ "vitest:watch": "vitest"
17
+ },
18
+ "keywords": [],
19
+ "author": "leonardo",
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "@types/node": "^25.0.3",
23
+ "tsx": "latest",
24
+ "typescript": "latest",
25
+ "vitest": "^4.0.16"
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./option.ts";
2
+ export { println, advancedPrintln } from "./println.ts"
@@ -0,0 +1,35 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { Some, nothing, OptionOf } from './option.ts'; // ajuste o caminho
3
+
4
+ describe('Option (korusjs)', () => {
5
+
6
+ it('deve converter string para int com sucesso', () => {
7
+ const s = new Some("10");
8
+ const result = s.toInt();
9
+ expect(result.finalize()).toBe(10);
10
+ });
11
+
12
+ it('deve retornar nothing ao falhar no toInt', () => {
13
+ const s = new Some("abc");
14
+ const result = s.toInt();
15
+ expect(result.kind).toBe("nothing");
16
+ });
17
+
18
+ it('deve lançar erro ao dar finalize num Nothing', () => {
19
+ expect(() => {
20
+ nothing.finalize();
21
+ }).toThrow("tried to finalize value from Nothing type");
22
+ });
23
+
24
+ it('deve funcionar o unwrapOr', () => {
25
+ expect(nothing.unwrapOr(50)).toBe(50);
26
+ expect(new Some(10).unwrapOr(50)).toBe(10);
27
+ });
28
+
29
+ it('deve manter o fluxo no andThen', () => {
30
+ const res = new Some(5)
31
+ .andThen(v => new Some(v * 2))
32
+ .finalize();
33
+ expect(res).toBe(10);
34
+ });
35
+ });
package/src/option.ts ADDED
@@ -0,0 +1,93 @@
1
+ export type Option<T> = Some<T> | Nothing;
2
+
3
+ export class Nothing{
4
+ readonly kind = "nothing" as const
5
+ toString(): string{
6
+ return "Nothing"
7
+ }
8
+ map<U>(_fn: (v: any) => U): Option<U>{
9
+ return this;
10
+ }
11
+ getValue<T>(): null{
12
+ return null;
13
+ }
14
+ filter(_fn: (v: any) => boolean): Option<any>{
15
+ return this;
16
+ }
17
+ finalize<T>(): never{
18
+ throw new Error("tried to finalize value from Nothing type");
19
+ }
20
+ unwrapOr<T>(defaultValue: T): T{
21
+ return defaultValue;
22
+ }
23
+ unwrapOrElse<T>(_fn: () => T): T {
24
+ return _fn();
25
+ }
26
+ isPresent():boolean{
27
+ return false;
28
+ }
29
+ ifPresent(_fn: (v: any) => void): void{
30
+ return;
31
+ }
32
+ andThen<U>(_fn: (v: never) => Option<U>): Option<U>{
33
+ return this;
34
+ }
35
+ toInt(): Option<number>{
36
+ return this;
37
+ }
38
+ }
39
+ export const _nothing = new Nothing();
40
+
41
+ export class Some<T> {
42
+ readonly kind = "some" as const
43
+ constructor(private readonly value: T){}
44
+ toString(): string{
45
+ return `Some(${JSON.stringify(this.value)})`
46
+ }
47
+ unwrapOr(defaultValue: T): T {
48
+ return this.value;
49
+ }
50
+ toInt(): Option<number>{
51
+ const n = Number(this.value);
52
+ return isNaN(n) ? _nothing : new Some(n);
53
+ }
54
+ finalize(): T{
55
+ return this.value;
56
+ }
57
+ filter(fn: (v: T) => boolean): Option<T>{
58
+ return fn(this.value) ? this : _nothing
59
+ }
60
+ map<U>(fn: (v:T) => U): Option<U>{
61
+ return new Some(fn(this.value));
62
+ }
63
+ unwrapOrElse(fn: () => T): T{
64
+ return this.value;
65
+ }
66
+ andThen<U>(fn: (v: T) => Option<U>): Option<U> {
67
+ try{
68
+ return fn(this.value);
69
+ } catch(e){
70
+ return _nothing;
71
+ }
72
+ }
73
+ ifPresent(fn: (v: T) => void): void {
74
+ fn(this.value);
75
+ }
76
+ isPresent(): boolean{
77
+ return true;
78
+ }
79
+ getValue():Some<T>{
80
+ return new Some(this.value)
81
+ }
82
+ }
83
+
84
+ export function OptionOf<T>(value: T | null | undefined, defaultValue?: T): Option<T>{
85
+ if(value != null){
86
+ return new Some(value);
87
+ }
88
+ if(defaultValue != null){
89
+ return new Some(defaultValue);
90
+ }
91
+ return _nothing;
92
+ }
93
+ export { _nothing as nothing };
package/src/println.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { Some, Nothing } from "./option.ts";
2
+ export function println(...args: unknown[]){
3
+ let outputs: unknown[] = [];
4
+
5
+ args.forEach(arg => {
6
+ switch(typeof(arg)){
7
+ case "string":
8
+ case "boolean":
9
+ outputs.push(String(arg));
10
+ break;
11
+ case "number":
12
+ outputs.push(String(Number.isInteger(arg) ? arg : arg.toFixed(4)));
13
+ break;
14
+ case "object":
15
+ if(arg === null){
16
+ outputs.push("null");
17
+ break;
18
+ }
19
+ else if(Array.isArray(arg)){
20
+ outputs.push("[" + arg.map(x => String(x)).join(", ")+"]");
21
+ break;
22
+ }else if(arg instanceof Some){
23
+ outputs.push(String(arg))
24
+ }else if(arg instanceof Nothing){
25
+ outputs.push("Nothing")
26
+ }else{
27
+ outputs.push(JSON.stringify(arg,null,2));
28
+ break;
29
+ }
30
+ default:
31
+ outputs.push(String(arg));
32
+ }
33
+
34
+ });
35
+ outputs.forEach(output => {
36
+ process.stdout.write(JSON.stringify(output,null,2));
37
+ process.stdout.write("\n")
38
+ });
39
+ }
40
+ export function advancedPrintln(options: { newLine?: boolean; decimals?:number } = {} ,...args: unknown[]){
41
+ let outputs: unknown[] = [];
42
+ const newLine = options.newLine?? true;
43
+ const decimals = options.decimals?? 2;
44
+ args.forEach(arg => {
45
+ switch(typeof(arg)){
46
+ case "string":
47
+ case "boolean":
48
+ outputs.push(String(arg));
49
+ break;
50
+ case "number":
51
+ outputs.push(String(Number.isInteger(arg) ? arg : arg.toFixed(Number(decimals))));
52
+ break;
53
+ case "object":
54
+ if(arg === null){
55
+ outputs.push("null");
56
+ break;
57
+ }
58
+ else if(Array.isArray(arg)){
59
+ outputs.push("[" + arg.map(x => String(x)).join(", ")+"]");
60
+ break;
61
+ }else{
62
+ outputs.push(JSON.stringify(arg,null,2));
63
+ break;
64
+ }
65
+ default:
66
+ outputs.push(String(arg));
67
+ }
68
+
69
+ });
70
+ const separador = newLine ? "\n" : " ";
71
+ const outputFinal = outputs.join(separador);
72
+
73
+ console.log(outputFinal);
74
+ if(newLine) process.stdout.write("\n");
75
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "compilerOptions": {
3
+
4
+ "rootDir": "./src",
5
+ "outDir": "./dist",
6
+ "target": "ESNext",
7
+ "module": "NodeNext",
8
+ "moduleResolution": "NodeNext",
9
+ "lib": ["ESNext"],
10
+ "types": ["node"],
11
+ "allowImportingTsExtensions": true,
12
+ "rewriteRelativeImportExtensions": true,
13
+ "noEmit": true
14
+ },
15
+
16
+
17
+ "declaration": true,
18
+ "declarationMap": true,
19
+ "sourceMap": true,
20
+
21
+
22
+ "strict": true,
23
+ "noUncheckedIndexedAccess": true,
24
+ "exactOptionalPropertyTypes": true,
25
+ "skipLibCheck": true,
26
+ "verbatimModuleSyntax": true,
27
+ "isolatedModules": true,
28
+ "moduleDetection": "force",
29
+
30
+ "jsx": "react-jsx",
31
+
32
+ "include": ["src/**/*"],
33
+ "exclude": ["node_modules", "dist", "output"]
34
+ }