mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 22:49:18 +00:00
refactor: simplify parsing logic
This commit is contained in:
committed by
Carlos Valente
parent
ce910c9cc6
commit
7cbb2112ed
@@ -1,5 +1,5 @@
|
|||||||
import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND } from "./conversionUtils";
|
import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND } from './conversionUtils';
|
||||||
import { parseUserTime } from "./parseUserTime";
|
import { parseUserTime } from './parseUserTime';
|
||||||
|
|
||||||
describe('test parseUserTime()', () => {
|
describe('test parseUserTime()', () => {
|
||||||
describe('function handles time with no separators', () => {
|
describe('function handles time with no separators', () => {
|
||||||
@@ -330,4 +330,4 @@ describe('test parseUserTime()', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND } from './conversionUtils.js';
|
import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND } from './conversionUtils.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Parses a time string to millis, auto-filling to the left
|
* @description Parses a user given time string to a value in milliseconds
|
||||||
* @param {string} value - time string
|
* @param {string} value - time string
|
||||||
* @returns {number} - time string in millis
|
* @returns {number} - value in milliseconds
|
||||||
*/
|
*/
|
||||||
export function parseUserTime(value: string): number {
|
export function parseUserTime(value: string): number {
|
||||||
value = value.toLowerCase();
|
const { isAM, isPM, value: parsingValue } = checkAmPm(value.toLowerCase());
|
||||||
|
|
||||||
const { isAM, isPM, value: parsingValue } = checkAmPm(value);
|
|
||||||
const maybeMillisFromMatchers = checkMatchers(parsingValue);
|
const maybeMillisFromMatchers = checkMatchers(parsingValue);
|
||||||
if (maybeMillisFromMatchers !== null) {
|
if (maybeMillisFromMatchers !== null) {
|
||||||
return maybeMillisFromMatchers;
|
return maybeMillisFromMatchers;
|
||||||
@@ -18,9 +16,8 @@ export function parseUserTime(value: string): number {
|
|||||||
const separatorRegex = /[\s,:.]+/;
|
const separatorRegex = /[\s,:.]+/;
|
||||||
const [first, second, third] = parsingValue.split(separatorRegex);
|
const [first, second, third] = parsingValue.split(separatorRegex);
|
||||||
|
|
||||||
let addTwelve = isPM;
|
|
||||||
|
|
||||||
let millis = 0;
|
let millis = 0;
|
||||||
|
let addTwelve = isPM;
|
||||||
|
|
||||||
if (first != null && second != null && third != null) {
|
if (first != null && second != null && third != null) {
|
||||||
// if string has three sections, treat as [hours] [minutes] [seconds]
|
// if string has three sections, treat as [hours] [minutes] [seconds]
|
||||||
@@ -31,7 +28,6 @@ export function parseUserTime(value: string): number {
|
|||||||
}
|
}
|
||||||
addTwelve = false;
|
addTwelve = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
millis = hours * MILLIS_PER_HOUR;
|
millis = hours * MILLIS_PER_HOUR;
|
||||||
millis += parse(second) * MILLIS_PER_MINUTE;
|
millis += parse(second) * MILLIS_PER_MINUTE;
|
||||||
millis += parse(third) * MILLIS_PER_SECOND;
|
millis += parse(third) * MILLIS_PER_SECOND;
|
||||||
@@ -40,8 +36,7 @@ export function parseUserTime(value: string): number {
|
|||||||
const { inferredMillis, addAM } = inferSeparators(first, isAM, isPM);
|
const { inferredMillis, addAM } = inferSeparators(first, isAM, isPM);
|
||||||
millis = inferredMillis;
|
millis = inferredMillis;
|
||||||
addTwelve = addAM < 12 && isPM;
|
addTwelve = addAM < 12 && isPM;
|
||||||
}
|
} else if (first != null && second != null && third == null) {
|
||||||
if (first != null && second != null && third == null) {
|
|
||||||
// if string has two sections, treat as [hours] [minutes]
|
// if string has two sections, treat as [hours] [minutes]
|
||||||
let hours = parse(first);
|
let hours = parse(first);
|
||||||
if (hours === 12) {
|
if (hours === 12) {
|
||||||
@@ -50,12 +45,10 @@ export function parseUserTime(value: string): number {
|
|||||||
}
|
}
|
||||||
addTwelve = false;
|
addTwelve = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
millis = hours * MILLIS_PER_HOUR;
|
millis = hours * MILLIS_PER_HOUR;
|
||||||
millis += parse(second) * MILLIS_PER_MINUTE;
|
millis += parse(second) * MILLIS_PER_MINUTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add 12 hours if needed
|
|
||||||
if (addTwelve) {
|
if (addTwelve) {
|
||||||
millis += 12 * MILLIS_PER_HOUR;
|
millis += 12 * MILLIS_PER_HOUR;
|
||||||
}
|
}
|
||||||
@@ -73,6 +66,7 @@ function inferSeparators(value: string, isAM: boolean, isPM: boolean) {
|
|||||||
const length = value.length;
|
const length = value.length;
|
||||||
let inferredMillis = 0;
|
let inferredMillis = 0;
|
||||||
let addAM = 0;
|
let addAM = 0;
|
||||||
|
|
||||||
if (length === 1) {
|
if (length === 1) {
|
||||||
if (isPM || isAM) {
|
if (isPM || isAM) {
|
||||||
inferredMillis = parse(value) * MILLIS_PER_HOUR;
|
inferredMillis = parse(value) * MILLIS_PER_HOUR;
|
||||||
@@ -135,29 +129,31 @@ function checkMatchers(value: string): number | null {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Utility function to check if a string contain am/pm indicators
|
* @description Utility function to check if a string contain am/pm indicators
|
||||||
|
* @description expects the value to be lowercased
|
||||||
* @param {string} value
|
* @param {string} value
|
||||||
|
* @returns {Object}
|
||||||
|
* @returns {boolean} result.isAM - Whether we parsed an AM indicator
|
||||||
|
* @returns {boolean} result.isPM - Whether we parsed a PM indicator
|
||||||
|
* @returns {string} result.value - The original string with AM/PM indicator removed
|
||||||
*/
|
*/
|
||||||
function checkAmPm(value: string) {
|
function checkAmPm(value: string): { isAM: boolean; isPM: boolean; value: string } {
|
||||||
let isPM = false;
|
if (value.endsWith('pm')) {
|
||||||
let isAM = false;
|
return { isAM: false, isPM: true, value: value.slice(0, -2) };
|
||||||
if (value.toLowerCase().includes('pm')) {
|
|
||||||
isPM = true;
|
|
||||||
value = value.replace(/pm/i, '');
|
|
||||||
} else if (value.toLowerCase().includes('p')) {
|
|
||||||
isPM = true;
|
|
||||||
value = value.replace(/p/i, '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to remove am, but it doesn't actually change anything
|
if (value.endsWith('p')) {
|
||||||
if (value.toLowerCase().includes('am')) {
|
return { isAM: false, isPM: true, value: value.slice(0, -1) };
|
||||||
isAM = true;
|
|
||||||
value = value.replace(/am/i, '');
|
|
||||||
} else if (value.toLowerCase().includes('a')) {
|
|
||||||
isAM = true;
|
|
||||||
value = value.replace(/a/i, '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { isAM, isPM, value };
|
if (value.endsWith('am')) {
|
||||||
|
return { isAM: true, isPM: false, value: value.slice(0, -2) };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.endsWith('a')) {
|
||||||
|
return { isAM: true, isPM: false, value: value.slice(0, -1) };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { isAM: false, isPM: false, value };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,7 +162,7 @@ function checkAmPm(value: string) {
|
|||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
function parse(valueAsString: string): number {
|
function parse(valueAsString: string): number {
|
||||||
const parsed = parseInt(valueAsString, 10);
|
const parsed = Number(valueAsString);
|
||||||
if (isNaN(parsed)) {
|
if (isNaN(parsed)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user