Skip to content

Commit ffdf7e0

Browse files
committed
refactoring
1 parent 6aedf36 commit ffdf7e0

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

src/extract.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const stripStoryPointsAndTaskToken = function (name) {
2+
return name
3+
.replace(/^(\s*\(\d+\))?\s*/, "") // story points, e.g. (3)
4+
.replace(/\s*#\w+\s*$/, ""); // task token, e.g. #orga_5417
5+
};
6+
7+
const extractProjectFromLabels = function (labels) {
8+
const projectLabels = labels
9+
.map((label) => label.name.match(/(?<=#)[a-z0-9]+$/)?.[0])
10+
.filter((prefix) => prefix !== undefined);
11+
if (projectLabels.length === 0) {
12+
throw new Error("Card has no valid project labels.");
13+
}
14+
if (projectLabels.length > 1) {
15+
throw new Error("Card has multiple project labels.");
16+
}
17+
return projectLabels[0];
18+
};
19+
20+
const extractTaskFromLabels = function (labels) {
21+
const tasks = labels
22+
.map((label) => label.name.match(/(?<=!)\w+$/)?.[0])
23+
.filter((prefix) => prefix !== undefined);
24+
if (tasks.length === 0) {
25+
return null;
26+
}
27+
if (tasks.length > 1) {
28+
throw new Error("Card has multiple tasks labels.");
29+
}
30+
return tasks[0];
31+
};
32+
33+
export const extractTrackingData = async function (t) {
34+
const card = await t.card("name", "labels", "idShort", "shortLink");
35+
const project = extractProjectFromLabels(card.labels);
36+
37+
return {
38+
project,
39+
task:
40+
extractTaskFromLabels(card.labels) ??
41+
`${project}_${card.idShort}_${card.shortLink}`,
42+
description: stripStoryPointsAndTaskToken(card.name),
43+
};
44+
};

src/trello.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TogglGateway, TogglService } from "./toggl.js";
2+
import { extractTrackingData } from "./extract.js";
23

34
const withErrorMessage = async function (t, fnc) {
45
try {
@@ -9,53 +10,6 @@ const withErrorMessage = async function (t, fnc) {
910
}
1011
};
1112

12-
const stripStoryPointsAndTaskToken = function (name) {
13-
return name
14-
.replace(/^(\s*\(\d+\))?\s*/, "") // story points, e.g. (3)
15-
.replace(/\s*#\w+\s*$/, ""); // task token, e.g. #orga_5417
16-
};
17-
18-
const extractProjectFromLabels = function (labels) {
19-
const projectLabels = labels
20-
.map((label) => label.name.match(/(?<=#)[a-z0-9]+$/)?.[0])
21-
.filter((prefix) => prefix !== undefined);
22-
if (projectLabels.length === 0) {
23-
throw new Error("Card has no valid project labels.");
24-
}
25-
if (projectLabels.length > 1) {
26-
throw new Error("Card has multiple project labels.");
27-
}
28-
return projectLabels[0];
29-
};
30-
31-
const extractTaskFromLabels = function (labels) {
32-
const tasks = labels
33-
.map((label) => label.name.match(/(?<=!)\w+$/)?.[0])
34-
.filter((prefix) => prefix !== undefined);
35-
if (tasks.length === 0) {
36-
return null;
37-
}
38-
if (tasks.length > 1) {
39-
throw new Error("Card has multiple tasks labels.");
40-
}
41-
return tasks[0];
42-
};
43-
44-
const extractTrackingData = async function (t) {
45-
const card = await t.card("name", "labels", "idShort", "shortLink");
46-
47-
const project = extractProjectFromLabels(card.labels);
48-
const task =
49-
extractTaskFromLabels(card.labels) ??
50-
`${project}_${card.idShort}_${card.shortLink}`;
51-
52-
return {
53-
project,
54-
task,
55-
description: stripStoryPointsAndTaskToken(card.name),
56-
};
57-
};
58-
5913
const setTrackingData = async function (t, tracking) {
6014
await t.set("card", "shared", "tracking", tracking);
6115
};

0 commit comments

Comments
 (0)