Skip to content

Commit 1927e79

Browse files
added more functions
1 parent eecfd17 commit 1927e79

11 files changed

+197
-9
lines changed

.yarn/versions/40175b34.yml

Whitespace-only changes.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"name": "@devlander/utils",
33
"description": "Utils shared between projects that are plain javascript",
4-
"version": "0.0.14",
4+
"version": "0.0.15",
55
"main": "dist/cjs/index.js",
6-
"browser": "dist/umd/index.js",
76
"types": "typings/index.d.ts",
87
"module": "dist/esm/index.js",
98
"type": "commonjs",

rollup.config.mjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ const makeExternalPredicate = externalArr => {
2727
export default {
2828
input: "src/index.ts", // Your entry point
2929
output: [
30-
{
31-
file: packageJson.browser,
32-
format: "umd",
33-
name: "DevlanderUtils", // Replace with your library's name
34-
globals: globals,
35-
sourcemap: true,
36-
},
30+
3731
{
3832
file: packageJson.main,
3933
format: "cjs",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { calculatePercentage } from "../calculatePercentage";
2+
3+
describe("calculatePercentage", () => {
4+
it("should calculate the percentage correctly when partial and whole are numbers", () => {
5+
const partial = 25;
6+
const whole = 100;
7+
const expectedPercentage = 25;
8+
9+
const result = calculatePercentage(partial, whole);
10+
11+
expect(result).toBe(expectedPercentage);
12+
});
13+
14+
it("should throw an error when partial or whole is not a number", () => {
15+
const partial = "25";
16+
const whole = 100;
17+
18+
expect(() => {
19+
calculatePercentage(partial as any, whole);
20+
}).toThrow("Both inputs must be numbers and 'whole' cannot be zero.");
21+
});
22+
23+
it("should throw an error when whole is zero", () => {
24+
const partial = 25;
25+
const whole = 0;
26+
27+
expect(() => {
28+
calculatePercentage(partial, whole);
29+
}).toThrow("Both inputs must be numbers and 'whole' cannot be zero.");
30+
});
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { convertVideoTimeStampToSeconds } from "../convertVideoTimeStampToSeconds";
2+
3+
describe("convertVideoTimeStampToSeconds", () => {
4+
it("should convert a timestamp in the format 'HH:MM:SS' to seconds", () => {
5+
// Test cases
6+
expect(convertVideoTimeStampToSeconds("00:00:10")).toBe(10);
7+
expect(convertVideoTimeStampToSeconds("00:01:30")).toBe(90);
8+
expect(convertVideoTimeStampToSeconds("01:00:00")).toBe(3600);
9+
expect(convertVideoTimeStampToSeconds("02:30:45")).toBe(9045);
10+
});
11+
12+
it("should handle timestamps with leading zeros", () => {
13+
expect(convertVideoTimeStampToSeconds("00:00:01")).toBe(1);
14+
expect(convertVideoTimeStampToSeconds("00:01:01")).toBe(61);
15+
expect(convertVideoTimeStampToSeconds("01:01:01")).toBe(3661);
16+
expect(convertVideoTimeStampToSeconds("01:01:10")).toBe(3670);
17+
});
18+
19+
it("should handle timestamps with single-digit hours and minutes", () => {
20+
expect(convertVideoTimeStampToSeconds("0:0:10")).toBe(10);
21+
expect(convertVideoTimeStampToSeconds("0:10:30")).toBe(630);
22+
expect(convertVideoTimeStampToSeconds("1:0:0")).toBe(3600);
23+
expect(convertVideoTimeStampToSeconds("2:30:45")).toBe(9045);
24+
});
25+
26+
it("should handle timestamps with only seconds", () => {
27+
expect(convertVideoTimeStampToSeconds("0:0:1")).toBe(1);
28+
expect(convertVideoTimeStampToSeconds("0:0:30")).toBe(30);
29+
expect(convertVideoTimeStampToSeconds("0:0:59")).toBe(59);
30+
});
31+
});

src/__tests__/getRange.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getRange } from "../getRange";
2+
3+
describe("getRange", () => {
4+
it("should return an array of numbers starting from the specified start value", () => {
5+
const start = 1;
6+
const count = 5;
7+
const result = getRange(start, count);
8+
expect(result).toEqual([1, 2, 3, 4, 5]);
9+
});
10+
11+
it("should return an empty array when count is 0", () => {
12+
const start = 1;
13+
const count = 0;
14+
const result = getRange(start, count);
15+
expect(result).toEqual([]);
16+
});
17+
18+
it("should return an empty array when count is negative", () => {
19+
const start = 1;
20+
const count = -5;
21+
const result = getRange(start, count);
22+
expect(result).toEqual([]);
23+
});
24+
});

src/calculatePercentage.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Calculates the percentage of a partial value compared to a whole value.
3+
*
4+
* @param partial The partial value.
5+
* @param whole The whole value.
6+
* @returns The percentage value.
7+
* @throws Error if either partial or whole is not a number, or if whole is zero.
8+
*/
9+
export function calculatePercentage(
10+
partial: number,
11+
whole: number
12+
) {
13+
if (typeof partial !== 'number' || typeof whole !== 'number' || whole === 0) {
14+
throw new Error("Both inputs must be numbers and 'whole' cannot be zero.");
15+
}
16+
17+
const percentage = (partial / whole) * 100;
18+
return percentage;
19+
}
20+
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Converts a video timestamp string to seconds.
3+
*
4+
* @param timestamp - The video timestamp string in the format "HH:MM:SS".
5+
* @returns The number of seconds represented by the timestamp, or NaN if the timestamp is invalid.
6+
*/
7+
export function convertVideoTimeStampToSeconds(timestamp: string): number {
8+
const parts: string[] = timestamp.split(':');
9+
10+
if (parts.length !== 3) {
11+
throw new Error('Invalid timestamp format. Expected format: "HH:MM:SS"');
12+
}
13+
14+
const hours = parseInt(parts[0], 10);
15+
const minutes = parseInt(parts[1], 10);
16+
const seconds = parseInt(parts[2], 10);
17+
18+
if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) {
19+
throw new Error('Invalid timestamp format. Expected format: "HH:MM:SS"');
20+
}
21+
22+
if (hours < 0 || minutes < 0 || seconds < 0) {
23+
throw new Error('Invalid timestamp. Negative values are not allowed.');
24+
}
25+
26+
return hours * 3600 + minutes * 60 + seconds;
27+
}

src/dashToCamelCase.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { dashToCamelCase } from "./dashToCamelCase";
2+
3+
describe("dashToCamelCase", () => {
4+
it("should convert a dash-case string to camelCase", () => {
5+
const input = "hello-world";
6+
const expectedOutput = "helloWorld";
7+
const result = dashToCamelCase(input);
8+
expect(result).toBe(expectedOutput);
9+
});
10+
11+
it("should convert a single character dash-case string to camelCase", () => {
12+
const input = "a-b-c";
13+
const expectedOutput = "aBC";
14+
const result = dashToCamelCase(input);
15+
expect(result).toBe(expectedOutput);
16+
});
17+
18+
it("should convert a dash-case string with multiple dashes to camelCase", () => {
19+
const input = "this-is-a-test";
20+
const expectedOutput = "thisIsATest";
21+
const result = dashToCamelCase(input);
22+
expect(result).toBe(expectedOutput);
23+
});
24+
25+
it("should convert a dash-case string with leading and trailing dashes to camelCase", () => {
26+
const input = "-hello-world-";
27+
const expectedOutput = "helloWorld";
28+
const result = dashToCamelCase(input);
29+
expect(result).toBe(expectedOutput);
30+
});
31+
32+
it("should convert an empty string to an empty camelCase string", () => {
33+
const input = "";
34+
const expectedOutput = "";
35+
const result = dashToCamelCase(input);
36+
expect(result).toBe(expectedOutput);
37+
});
38+
});

src/dashToCamelCase.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Converts a dash-separated string to camel case.
3+
*
4+
* @param dashName - The dash-separated string to convert.
5+
* @returns The camel case version of the input string.
6+
*/
7+
export function dashToCamelCase(dashName: string): string {
8+
return dashName.replace(/^-+/, '').replace(/-+$/, '').toLowerCase().replace(/-([a-z])/g, function (_match, group1) {
9+
return group1.toUpperCase();
10+
});
11+
}

0 commit comments

Comments
 (0)