mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 09:23:51 +00:00
template testing
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { stringFromMillis } from '../../src/utils/time';
|
||||
|
||||
const t1 = { val: null, result: '...' };
|
||||
test('test stringFromMillis() on null values', () => {
|
||||
expect(stringFromMillis(t1.val)).toBe(t1.result);
|
||||
});
|
||||
|
||||
const t2 = { val: 3600000, result: '01:00:00' };
|
||||
test('test stringFromMillis() on valid millis', () => {
|
||||
expect(stringFromMillis(t2.val)).toBe(t2.result);
|
||||
});
|
||||
|
||||
const t3 = { val: -3600000, result: '-01:00:00' };
|
||||
test('test stringFromMillis() on negative millis', () => {
|
||||
expect(stringFromMillis(t3.val)).toBe(t3.result);
|
||||
});
|
||||
+5
-1
@@ -16,6 +16,10 @@
|
||||
"socket.io": "^4.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node src/app.js"
|
||||
"start": "node src/app.js",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^27.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
/**
|
||||
* @description Converts milliseconds to string representing time
|
||||
* @param {number} ms - time in milliseconds
|
||||
* @param {boolean} showSeconds - wether to show the seconds
|
||||
* @param {string} delim - character between HH MM SS
|
||||
* @param {string} ifNull - what to return if value is null
|
||||
* @returns {string} String representing time 00:12:02
|
||||
*/
|
||||
|
||||
export const stringFromMillis = (
|
||||
ms,
|
||||
showSeconds = true,
|
||||
@@ -5,14 +14,17 @@ export const stringFromMillis = (
|
||||
ifNull = '...'
|
||||
) => {
|
||||
if (ms === null || isNaN(ms)) return ifNull;
|
||||
const isNegative = ms < 0 ? '-' : '';
|
||||
const millis = Math.abs(ms);
|
||||
|
||||
const showWith0 = (value) => (value < 10 ? `0${value}` : value);
|
||||
const hours = showWith0(Math.floor(((ms / (1000 * 60 * 60)) % 60) % 24));
|
||||
const minutes = showWith0(Math.floor((ms / (1000 * 60)) % 60));
|
||||
const seconds = showWith0(Math.floor((ms / 1000) % 60));
|
||||
const hours = showWith0(Math.floor(((millis / (1000 * 60 * 60)) % 60) % 24));
|
||||
const minutes = showWith0(Math.floor((millis / (1000 * 60)) % 60));
|
||||
const seconds = showWith0(Math.floor((millis / 1000) % 60));
|
||||
|
||||
return showSeconds
|
||||
? `${
|
||||
? `${isNegative}${
|
||||
parseInt(hours) ? `${hours}${delim}` : `00${delim}`
|
||||
}${minutes}${delim}${seconds}`
|
||||
: `${parseInt(hours) ? `${hours}` : '00'}${delim}${minutes}`;
|
||||
: `${isNegative}${parseInt(hours) ? `${hours}` : '00'}${delim}${minutes}`;
|
||||
};
|
||||
|
||||
+2460
-6
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user