backk-frontend-utils
Advanced tools
Comparing version 1.0.99 to 1.0.100
@@ -5,4 +5,43 @@ import { registerDecorator } from 'cv-pksilen'; | ||
import isoWeek from 'dayjs/plugin/isoWeek'; | ||
import isLeapYear from 'dayjs/plugin/isLeapYear'; | ||
dayjs.extend(isBetween); | ||
dayjs.extend(isoWeek); | ||
dayjs.extend(isLeapYear); | ||
const minValues = { | ||
year: 1970, | ||
month: 0, | ||
date: 1, | ||
hour: 0, | ||
minute: 0, | ||
}; | ||
const maxValues = { | ||
year: 9999, | ||
month: 11, | ||
hour: 23, | ||
minute: 59, | ||
}; | ||
function getMaxDate(timestamp) { | ||
switch (timestamp.month()) { | ||
case 0: | ||
case 2: | ||
case 4: | ||
case 6: | ||
case 7: | ||
case 9: | ||
case 11: | ||
return 31; | ||
case 3: | ||
case 5: | ||
case 8: | ||
case 10: | ||
return 30; | ||
case 1: | ||
if (timestamp.isLeapYear()) { | ||
return 29; | ||
} | ||
return 28; | ||
default: | ||
throw new Error('Invalid month'); | ||
} | ||
} | ||
export default function IsTimestampBetween(unit, startValue, endValue, validationOptions) { | ||
@@ -23,4 +62,21 @@ return function (object, propertyName) { | ||
} | ||
return date.isBetween(startValue, endValue, unit); | ||
let startTimestamp = dayjs(); | ||
let endTimestamp = dayjs(); | ||
startTimestamp = startTimestamp.set(unit, startValue); | ||
endTimestamp = endTimestamp.set(unit, endValue); | ||
['year', 'month', 'date', 'hour', 'minute'].forEach((u) => { | ||
if (u !== unit) { | ||
startTimestamp = startTimestamp.set(unit, minValues[unit]); | ||
endTimestamp = endTimestamp.set(unit, unit === 'date' ? getMaxDate(endTimestamp) : maxValues[unit]); | ||
} | ||
}); | ||
return date.isBetween(startTimestamp, endTimestamp, unit); | ||
}, | ||
defaultMessage: () => propertyName + | ||
' must be a timestamp where ' + | ||
unit + | ||
' is between ' + | ||
startValue + | ||
' and ' + | ||
endValue, | ||
}, | ||
@@ -27,0 +83,0 @@ }); |
{ | ||
"name": "backk-frontend-utils", | ||
"version": "1.0.99", | ||
"version": "1.0.100", | ||
"description": "Web frontend utils for Backk microservices", | ||
@@ -5,0 +5,0 @@ "author": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
325273
3275