Skip to content

Commit

Permalink
commitlint: add OptionStatic.OfObj and .None
Browse files Browse the repository at this point in the history
  • Loading branch information
knocte committed Sep 6, 2024
1 parent e5a23bd commit bc751b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
20 changes: 18 additions & 2 deletions commitlint/fpHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export class None {
public IsSome(): boolean {
return false;
}

/**
* @deprecated it is better to use `OptionStatic.None`
**/
constructor(){}
}
export class Some<T> {
value: T;

constructor(val: T) {
constructor(val: NonNullable<T>) {
this.value = val;
}

Expand All @@ -32,4 +37,15 @@ export class Some<T> {
}
}

export type Option<T> = (None | Some<T>) & IOption;
export type Option<T> = (None | Some<NonNullable<T>>) & IOption;

export class OptionStatic {
public static None = new None();
public static OfObj<T>(obj: T | null | undefined): Option<NonNullable<T>> {
if (obj === null || obj === undefined) {
return OptionStatic.None;
} else {
return new Some(obj);
}
}
}
26 changes: 24 additions & 2 deletions commitlint/tests/testHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { None, Some, Option } from "../fpHelpers.js";
import { None, Some, Option, OptionStatic } from "../fpHelpers.js";
const { spawnSync } = require("child_process");
const os = require("os");

Expand All @@ -12,6 +12,16 @@ function typeGuard(option: Option<number>) {
}
}

function ofObj1(option: number | null): Option<number> {
let foo = OptionStatic.OfObj(option);
return foo;
}

function ofObj2(option: number | undefined): Option<number> {
let foo = OptionStatic.OfObj(option);
return foo;
}

test("testing Options", () => {
let foo: Option<number> = new None();
let bar: Option<number> = new Some(2);
Expand All @@ -20,14 +30,26 @@ test("testing Options", () => {
});

test("testing Is methods", () => {
let foo: Option<number> = new None();
let foo: Option<number> = OptionStatic.None;
let bar: Option<number> = new Some(2);
expect(foo.IsNone()).toBe(true);
expect(bar.IsNone()).toBe(false);
expect(foo.IsSome()).toBe(false);
expect(bar.IsSome()).toBe(true);
});

test("testing OfObj", () => {
let two: number | null = 2;
expect(typeGuard(ofObj1(two))).toBe("4");
two = null;
expect(typeGuard(ofObj1(two))).toBe("NAH");

let four : number | undefined = 4;
expect(typeGuard(ofObj2(four))).toBe("16");
four = undefined;
expect(typeGuard(ofObj2(four))).toBe("NAH");
});

export function runCommitLintOnMsg(inputMsg: string) {
// FIXME: should we .lowerCase().startsWith("win") in case it starts
// returning Win64 in the future? thing is, our CI doesn't like this
Expand Down

0 comments on commit bc751b1

Please sign in to comment.