Skip to content

Commit

Permalink
chore(devops): updated var name
Browse files Browse the repository at this point in the history
/spend 30m
  • Loading branch information
nico-i committed Feb 4, 2024
1 parent 214482a commit 2d6e0c6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
File renamed without changes.
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no -- commitlint --edit $1
1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm test
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateTimeUnit } from "./src/types/TimeUnit";
const { validateTimeUnit } = require("./src/types/TimeUnit");

/**
* The GitLab spend directives.
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
"type": "git",
"url": "https://github.com/nico-i/commitlint-plugin-spend.git"
},
"bugs": {
"url": "https://github.com/nico-i/commitlint-plugin-spend/issues"
},
"scripts": {
"test": "vitest"
"test": "vitest run",
"prepare": "husky"
},
"peerDependencies": {
"@commitlint/lint": ">=7.6.0"
Expand All @@ -31,6 +35,7 @@
"@commitlint/core": "^18.6.0",
"@types/node": "^20.11.16",
"commitlint-plugin-spend": "link:",
"husky": "^9.0.10",
"vitest": "^1.2.2"
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions src/types/TimeUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,37 @@ const maxValueByUnit = {
* @param timeValues - The time values to validate sorted from largest to smallest time unit (e.g., ["3m", "2h", "1d"])
* @returns - True if the time values are valid and in the correct order, false otherwise
*/
export function validateTimeUnit(timeValues) {
exports.validateTimeUnit = (timeValues) => {
let lastUnitIndex = -1;
const unitCount = new Array(orderedTimeUnits.length).fill(0);
for (const timeValue of timeValues) {
const unitIndex = orderedTimeUnits.findIndex((u) => {
const lastChar = timeValue[timeValue.length - 1];
return u === lastChar;
});
const regex = /^(\d+)(\D+)$/; // Match numbers followed by a non-digit characters (e.g., "2234234abcdef")
const match = timeValue.match(regex);

if (!match) {
return [
false,
`The time value "${timeValue}" cannot be parsed. Please provide a valid time value with no whitespace in between (e.g., "2${orderedTimeUnits[0]}")`,
];
}

// match[1] will contain the numeric part, match[2] will contain the char part
const numberString = match[1];
const charString = match[2];

const unitIndex = orderedTimeUnits.findIndex((u) => charString === u);
const unit = orderedTimeUnits[unitIndex];

if (unitIndex === -1) {
return [false, `No valid time unit found in "${timeValue}"`];
}

// Increment the count for the unit
unitCount[unitIndex] += 1;
if (unitCount[unitIndex] > 1) {
return [false, `Duplicate time unit found in "${timeValue}"`];
}

const unit = orderedTimeUnits[unitIndex];

const timeValueNumberString = timeValue.replace(unit, "");
const timeValueNumber = parseInt(timeValueNumberString, 10);

const timeValueNumber = parseInt(numberString, 10);
if (isNaN(timeValueNumber)) {
return [
false,
Expand Down Expand Up @@ -75,4 +83,4 @@ export function validateTimeUnit(timeValues) {
}

return [true, undefined];
}
};
5 changes: 5 additions & 0 deletions src/types/TimeUnit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe("validateTimeUnit", () => {
const [isValid, error] = validateTimeUnit(timeValues);
expect(isValid).toBe(false);
expect(error).toBe('No valid time unit found in "30x"');

const timeValues2 = ["2d", "3h", "30asdfasdfasdfm"];
const [isValid2, error2] = validateTimeUnit(timeValues2);
expect(isValid2).toBe(false);
expect(error2).toBe('No valid time unit found in "30asdfasdfasdfm"');
});

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

0 comments on commit 2d6e0c6

Please sign in to comment.