Skip to content

Instantly share code, notes, and snippets.

@aboqasem
Created April 1, 2024 04:52
Show Gist options
  • Save aboqasem/4eba3c977e9593d1c060d739ef9c773c to your computer and use it in GitHub Desktop.
Save aboqasem/4eba3c977e9593d1c060d739ef9c773c to your computer and use it in GitHub Desktop.
Gitgraph.js utils
import { describe, expect, test } from "bun:test";
import { m } from "./utils";
describe("utils", () => {
describe("m", () => {
test("message", () => {
const commit = m`Hello, world!`;
expect(commit.subject).toBe("Hello, world!");
expect(commit.body).toBeUndefined();
});
test("message with body", () => {
const commit = m`
Hello, world!
This is a body.
`;
expect(commit.subject).toBe("Hello, world!");
expect(commit.body).toBe("This is a body.");
});
test("tagged", () => {
const commit = m`Hello, world!`.tagged("v1.0.0");
expect(commit.subject).toBe("Hello, world!");
expect(commit.tag).toBe("v1.0.0");
expect(commit.hash).toBe("v1.0.0");
});
test("dimmed", () => {
const commit = m`Hello, world!`.dimmed();
expect(commit.style?.message?.color).toBeTruthy();
});
test("message with body, tagged, and dimmed", () => {
const commit = m`
Hello, world!
This is a body.
`
.tagged("v1.0.0")
.dimmed();
expect(commit.subject).toBe("Hello, world!");
expect(commit.body).toBe("This is a body.");
expect(commit.tag).toBe("v1.0.0");
expect(commit.hash).toBe("v1.0.0");
expect(commit.style?.message?.color).toBeTruthy();
});
});
});
import type { GitgraphCommitOptions } from "@gitgraph/core";
import dedent from "dedent";
type CommitOptions = GitgraphCommitOptions<SVGElement> & {
tagged(tag: string): CommitOptions;
dimmed(): CommitOptions;
};
export function m(message: string): CommitOptions;
export function m(strings: TemplateStringsArray, ...values: unknown[]): CommitOptions;
export function m(v: string | TemplateStringsArray, ...values: unknown[]): CommitOptions {
const message = typeof v === "string" ? dedent(v) : dedent(v, values);
const newlineIndex = message.indexOf("\n");
const hasBody = newlineIndex !== -1;
const subject = hasBody ? message.slice(0, newlineIndex) : message;
const body = hasBody ? message.slice(newlineIndex + 1).trimStart() : undefined;
return {
subject,
body,
tagged(tag: string) {
this.tag = tag;
// to give the ability to checkout a tag
// https://github.com/nicoespeon/gitgraph.js/issues/248#issuecomment-495420899
this.hash = tag;
return this;
},
dimmed() {
this.style ??= {};
this.style.message ??= {};
this.style.message.color = "#b0b0b0";
return this;
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment