Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Strings #862

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleFileExtensions: ["ts", "js", "json", "node"],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"start:watch": "nodemon --exec 'yarn start'",
"utils:cspell": "cspell --config .cspell.json 'src/**/*.{js,ts,json,md,yml}'",
"start": "probot run ./lib/index.js",
"prepare": "husky install"
"prepare": "husky install",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.10.0",
Expand Down
11 changes: 11 additions & 0 deletions src/configs/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ export const GLOBAL_STRINGS = {
"Please select a child issue from the specification checklist to work on. The `/start` command is disabled on parent issues.",
autopayComment: "Automatic payment for this issue is enabled:",
};

// Typescript equivalent of Python's f-string

type Variables = Record<string, string | number>;

export const formatFString = <T extends Variables>(template: string, variables: T): string => {
return template.replace(/{(.*?)}/g, (_match, key) => {
if (!(key.trim() in variables)) throw new Error(`Missing value for variable: ${key.trim()}`);
return String(variables[key.trim()]);
});
};
59 changes: 59 additions & 0 deletions src/tests/strings-test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { formatFString } from "../configs";

Check warning on line 1 in src/tests/strings-test.test.ts

View workflow job for this annotation

GitHub Actions / check-filenames

This file is not in kebab-case or snake_case

const varStrings = {
askUpdate: "Do you have any updates {username}?",
assignNotice: "The `/assign` command is disabled for this repository due to {reason}.",
askPricing: "Using the /ask command, will cost you {price}.",
manyVars: "{repo} is {status} as {reason}, it's {repoDesc}.",
};

describe("f-string utilities", () => {
describe("formatFString", () => {
it("should correctly format f-string with a valid input value", () => {
const values1 = {
username: "Keyrxng",
};
const expectedString1 = "Do you have any updates Keyrxng?";
const result1 = formatFString(varStrings.askUpdate, values1);

console.log(`Expected: ${expectedString1}\n Result: ${result1}`);
expect(result1).toBe(expectedString1);

const values2 = {
reason: "maintenance",
};
const expectedString2 = "The `/assign` command is disabled for this repository due to maintenance.";
const result2 = formatFString(varStrings.assignNotice, values2);

console.log(`Expected: ${expectedString2}\n Result: ${result2}`);
expect(result2).toBe(expectedString2);

const values3 = {
price: "$10",
};
const expectedString3 = "Using the /ask command, will cost you $10.";
const result3 = formatFString(varStrings.askPricing, values3);

console.log(`Expected: ${expectedString3}\n Result: ${result3}`);
expect(result3).toBe(expectedString3);
});

it("should correctly format f-string with multiple input values", () => {
const values = {
repo: "Block#Builder",
status: "no longer maintained",
reason: "it's too boring",
repoDesc: "used for building blocks",
};
const expectedString = "Block#Builder is no longer maintained as it's too boring, it's used for building blocks.";
const result = formatFString(varStrings.manyVars, values);

console.log(`Expected: ${expectedString}\n Result: ${result}`);
expect(result).toBe(expectedString);
});

it("should throw error for missing input values using varStrings", () => {
expect(() => formatFString(varStrings.askUpdate, {})).toThrowError("Missing value for variable: username");
});
});
});
Loading