Skip to content

Commit

Permalink
Merge pull request #3 from Mike96Angelo/v2-proposal
Browse files Browse the repository at this point in the history
Reorganize package files for npm/deno publish
  • Loading branch information
Mike96Angelo committed Feb 16, 2023
2 parents 1ef2fcb + 728966c commit ff6de51
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npx tsc
- run: ./scripts/publish-package.sh
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

Expand All @@ -48,6 +48,6 @@ jobs:
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npx tsc
- run: ./scripts/publish-package.sh
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port
*.js
*.d.ts
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/*.ts
!**/*.d.ts
tests/**/*
src\ copy/**/*
docs/**/*
.github/**/*
jest.config.js
59 changes: 59 additions & 0 deletions example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Variant, variant, VariantTypeClass } from "./variant.js";

type ABCVariant =
| Variant<"A", [a: string]>
| Variant<"B", [b: number, bool: boolean]>
| Variant<"C">;

class ABC extends VariantTypeClass<ABCVariant> {
// add any useful methods for ABC variants
}

const A = (a: string) => new ABC(variant("A", a));
const B = (b: number, bool: boolean) => new ABC(variant("B", b, bool));
const C = new ABC(variant("C"));

export { A, B, C };

const handleABC = (abc: ABC) =>
console.log(
abc,
abc.match({
A(a) {
return `A: ${a}`;
},
B(b, bool) {
return `B: ${b} | ${bool}`;
},
C() {
return "C";
},
})
);

handleABC(A("string")); // 'A: string'
handleABC(B(123, true)); // 'B: 123 | true'
handleABC(C); // 'C'

const handleA = (abc: ABC) => {
if (abc.variant.kind === "A") {
abc.variant.values;
}
console.log(
abc,
abc.match<string | number | boolean>({
A(a) {
return `A: ${a}`;
},

// catch all
_() {
return "B or C";
},
})
);
};

handleA(A("string")); // 'A: string'
handleA(B(123, true)); // 'B or C'
handleA(C); // 'B or C'
4 changes: 4 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./variant.js";
export * from "./optional.js";
export * from "./optional-pair.js";
export * from "./result.js";
6 changes: 3 additions & 3 deletions src/optional-pair.ts → optional-pair.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Optional, toOptional, None } from "./optional";
import { Func } from "./util.types";
import { Variant, variant, VariantTypeClass } from "./variant";
import { Optional, toOptional, None } from "./optional.js";
import { Func } from "./util.types.js";
import { Variant, variant, VariantTypeClass } from "./variant.js";

type OptionalPairVariants<A, B> =
| Variant<"First", [first: A]>
Expand Down
10 changes: 7 additions & 3 deletions src/optional.ts → optional.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Err, Ok, Result } from "./result";
import { Func } from "./util.types";
import { Variant, variant, VariantTypeClass } from "./variant";
import { Err, Ok, Result } from "./result.js";
import { Func } from "./util.types.js";
import { Variant, variant, VariantTypeClass } from "./variant.js";

type OptionalVariants<T> = Variant<"Some", [T]> | Variant<"None">;

Expand Down Expand Up @@ -65,6 +65,10 @@ class Optional<T> extends VariantTypeClass<OptionalVariants<T>> {
return this.map((a) => b.map((b) => combiner(a, b)));
}

filter(filter: Func<[value: T], boolean>) {
return this.map((value) => (filter(value) ? value : None));
}

/**
* Converts this `Optional<T>` into `Result<T, E>`
*
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "variant-match",
"version": "2.0.0",
"version": "2.0.1",
"description": "Brings variant match pattern to TypeScript.",
"main": "variant.js",
"type": "module",
"main": "mod.js",
"scripts": {
"test": "jest"
},
Expand Down
15 changes: 15 additions & 0 deletions pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Func } from "./util.types";

export type Pipe<T> = {
pipe<R>(func: Func<[value: T], R>): Pipe<R>;
value(): T;
};

export const Pipe = <T>(value: T): Pipe<T> => ({
pipe(func) {
return Pipe(func(value));
},
value() {
return value;
},
});
27 changes: 23 additions & 4 deletions src/result.ts → result.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { None, Optional, toOptional } from "./optional";
import { First, OptionalPair, Second, toOptionalPair } from "./optional-pair";
import { Func } from "./util.types";
import { Variant, variant, VariantTypeClass } from "./variant";
import { None, Optional, toOptional } from "./optional.js";
import {
First,
OptionalPair,
Second,
toOptionalPair,
} from "./optional-pair.js";
import { Func } from "./util.types.js";
import { Variant, variant, VariantTypeClass } from "./variant.js";

type ResultVariants<T, E> = Variant<"Ok", [T]> | Variant<"Err", [E]>;
type ResultOk<T> = T extends Result<infer K, any> ? K : never;
Expand Down Expand Up @@ -119,6 +124,20 @@ class Result<T, E> extends VariantTypeClass<ResultVariants<T, E>> {
});
}

filter<E>(
filter: Func<[value: T], boolean>,
error: Func<[], NonNullable<E>>
): ResultType<T, E> {
return this.match({
Ok(value) {
return filter(value) ? Ok(value!) : Err(error());
},
Err() {
return Err(error());
},
});
}

/**
* Converts this `Result<T, E>` into `Optional<T>`
*
Expand Down
7 changes: 0 additions & 7 deletions scripts/publish-package.sh

This file was deleted.

4 changes: 2 additions & 2 deletions src/optional.test.ts → tests/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
OptionalMapper,
Some,
toOptional,
} from "./optional";
import { Err, Ok } from "./result";
} from "../optional.js";
import { Err, Ok } from "../result.js";

describe("Some", () => {
it.each([undefined, null])(
Expand Down
10 changes: 7 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "ESNext", /* Specify what module code is generated. */
"module": "ES2015", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand All @@ -49,7 +49,7 @@
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand Down Expand Up @@ -99,5 +99,9 @@
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
},
"exclude": [
"./src copy/**/*",
"./tests/**/*"
]
}
File renamed without changes.
15 changes: 10 additions & 5 deletions src/variant.ts → variant.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Func, UnionToIntersection, Exact } from "./util.types";
import { Func, UnionToIntersection, Exact } from "./util.types.js";

type Variant<K extends string, V extends any[] = []> = {
readonly kind: K;
readonly values: V;
readonly values: Readonly<V>;
};

type VariantBranch<V, R> = V extends Variant<any, infer A> ? Func<A, R> : never;
Expand Down Expand Up @@ -49,11 +49,16 @@ const variant = <K extends string, V extends any[]>(
/**
* VariantTypeClass is an abstract class used to define sum type classes which contain variants.
*/
abstract class VariantTypeClass<V extends Variant<any, any>> {
private variant: V;
abstract class VariantTypeClass<V extends Variant<any, any[]>> {
readonly variant: V;

constructor(variant: V) {
assertVariant(variant);
this.variant = variant;
this.variant = { ...variant, values: [...variant.values] };

Object.freeze(this.variant.values);
Object.freeze(this.variant);
Object.freeze(this);
}

/**
Expand Down

0 comments on commit ff6de51

Please sign in to comment.