Skip to content

Commit 2d6e0c6

Browse files
committedFeb 4, 2024
chore(devops): updated var name
/spend 30m
1 parent 214482a commit 2d6e0c6

File tree

9 files changed

+44
-15
lines changed

9 files changed

+44
-15
lines changed
 

‎.github/workflows/npm-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
- run: npm ci
3131
- run: npm publish
3232
env:
33-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
33+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

‎.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no -- commitlint --edit $1

‎.husky/pre-push

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pnpm test

‎index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateTimeUnit } from "./src/types/TimeUnit";
1+
const { validateTimeUnit } = require("./src/types/TimeUnit");
22

33
/**
44
* The GitLab spend directives.

‎package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
"type": "git",
2222
"url": "https://github.com/nico-i/commitlint-plugin-spend.git"
2323
},
24+
"bugs": {
25+
"url": "https://github.com/nico-i/commitlint-plugin-spend/issues"
26+
},
2427
"scripts": {
25-
"test": "vitest"
28+
"test": "vitest run",
29+
"prepare": "husky"
2630
},
2731
"peerDependencies": {
2832
"@commitlint/lint": ">=7.6.0"
@@ -31,6 +35,7 @@
3135
"@commitlint/core": "^18.6.0",
3236
"@types/node": "^20.11.16",
3337
"commitlint-plugin-spend": "link:",
38+
"husky": "^9.0.10",
3439
"vitest": "^1.2.2"
3540
}
3641
}

‎pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/types/TimeUnit.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,37 @@ const maxValueByUnit = {
2323
* @param timeValues - The time values to validate sorted from largest to smallest time unit (e.g., ["3m", "2h", "1d"])
2424
* @returns - True if the time values are valid and in the correct order, false otherwise
2525
*/
26-
export function validateTimeUnit(timeValues) {
26+
exports.validateTimeUnit = (timeValues) => {
2727
let lastUnitIndex = -1;
2828
const unitCount = new Array(orderedTimeUnits.length).fill(0);
2929
for (const timeValue of timeValues) {
30-
const unitIndex = orderedTimeUnits.findIndex((u) => {
31-
const lastChar = timeValue[timeValue.length - 1];
32-
return u === lastChar;
33-
});
30+
const regex = /^(\d+)(\D+)$/; // Match numbers followed by a non-digit characters (e.g., "2234234abcdef")
31+
const match = timeValue.match(regex);
32+
33+
if (!match) {
34+
return [
35+
false,
36+
`The time value "${timeValue}" cannot be parsed. Please provide a valid time value with no whitespace in between (e.g., "2${orderedTimeUnits[0]}")`,
37+
];
38+
}
39+
40+
// match[1] will contain the numeric part, match[2] will contain the char part
41+
const numberString = match[1];
42+
const charString = match[2];
43+
44+
const unitIndex = orderedTimeUnits.findIndex((u) => charString === u);
45+
const unit = orderedTimeUnits[unitIndex];
3446

3547
if (unitIndex === -1) {
3648
return [false, `No valid time unit found in "${timeValue}"`];
3749
}
38-
50+
// Increment the count for the unit
3951
unitCount[unitIndex] += 1;
4052
if (unitCount[unitIndex] > 1) {
4153
return [false, `Duplicate time unit found in "${timeValue}"`];
4254
}
4355

44-
const unit = orderedTimeUnits[unitIndex];
45-
46-
const timeValueNumberString = timeValue.replace(unit, "");
47-
const timeValueNumber = parseInt(timeValueNumberString, 10);
48-
56+
const timeValueNumber = parseInt(numberString, 10);
4957
if (isNaN(timeValueNumber)) {
5058
return [
5159
false,
@@ -75,4 +83,4 @@ export function validateTimeUnit(timeValues) {
7583
}
7684

7785
return [true, undefined];
78-
}
86+
};

‎src/types/TimeUnit.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ describe("validateTimeUnit", () => {
1414
const [isValid, error] = validateTimeUnit(timeValues);
1515
expect(isValid).toBe(false);
1616
expect(error).toBe('No valid time unit found in "30x"');
17+
18+
const timeValues2 = ["2d", "3h", "30asdfasdfasdfm"];
19+
const [isValid2, error2] = validateTimeUnit(timeValues2);
20+
expect(isValid2).toBe(false);
21+
expect(error2).toBe('No valid time unit found in "30asdfasdfasdfm"');
1722
});
1823

1924
test("should return false and an error message for time values that cannot be parsed", () => {

0 commit comments

Comments
 (0)