Skip to content

Commit

Permalink
Add isIsoString in time.js and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
snowteamer committed Aug 26, 2023
1 parent aa52565 commit 2a40bdd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions frontend/model/contracts/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { paymentStatusType, paymentType, PAYMENT_COMPLETED } from './shared/payments/index.js'
import { createPaymentInfo, paymentHashesFromPaymentPeriod } from './shared/functions.js'
import { merge, deepEqualJSONType, omit, cloneDeep } from './shared/giLodash.js'
import { addTimeToDate, dateToPeriodStamp, compareISOTimestamps, dateFromPeriodStamp, isPeriodStamp, comparePeriodStamps, dateIsWithinPeriod, DAYS_MILLIS } from './shared/time.js'
import { addTimeToDate, dateToPeriodStamp, compareISOTimestamps, dateFromPeriodStamp, isIsoString, isPeriodStamp, comparePeriodStamps, dateIsWithinPeriod, DAYS_MILLIS } from './shared/time.js'
import { unadjustedDistribution, adjustedDistribution } from './shared/distribution/distribution.js'
import currencies from './shared/currencies.js'
import { inviteType, chatRoomAttributesType } from './shared/types.js'
Expand Down Expand Up @@ -324,7 +324,7 @@ sbp('chelonia/defineContract', {
periodStampGivenDate (state, getters) {
return (recentDate: string | Date) => {
if (typeof recentDate !== 'string') recentDate = recentDate.toISOString()
if (!isPeriodStamp(recentDate)) throw new TypeError('must be date or isostring')
if (!isIsoString(recentDate)) throw new TypeError('must be date or isostring')
const { distributionDate, distributionPeriodLength } = getters.groupSettings
if (!distributionDate) return
const sortedPeriodKeys = getters.groupSortedPeriodKeys
Expand All @@ -349,7 +349,7 @@ sbp('chelonia/defineContract', {
const waitingPeriodStamp = dateToPeriodStamp(
addTimeToDate(distributionDate, -distributionPeriodLength)
)
return recentDate >= waitingPeriodStamp ? waitingPeriodStamp : undefined
return recentDate >= dateFromPeriodStamp(waitingPeriodStamp).toISOString() ? waitingPeriodStamp : undefined
}
const oldestKnownStamp = sortedPeriodKeys[0]
const latestKnownStamp = sortedPeriodKeys[sortedPeriodKeys.length - 1]
Expand All @@ -375,7 +375,7 @@ sbp('chelonia/defineContract', {
if (!distributionDate) return
// This is not always the current period stamp.
const distributionDateStamp = dateToPeriodStamp(distributionDate)
const sortedPeriodKeys = getters.groupSortedPeriodKeys
const sortedPeriodKeys = getters.groupSortedPeriodKseys
const latestKnownStamp = sortedPeriodKeys[sortedPeriodKeys.length - 1]
// Maybe the distribution date has just been updated,
// but the corresponding period has not started or is not stored yet.
Expand Down
2 changes: 1 addition & 1 deletion frontend/model/contracts/manifests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifests": {
"gi.contracts/chatroom": "21XWnNS9zeT2tws6KkJqkibLGfTivPdiGYbSjjqFEkDjL5Q775",
"gi.contracts/group": "21XWnNK3pmNAdgXLe7rkjVjEQ1z3Lccp3o7YpZ64bAr18VNPAW",
"gi.contracts/group": "21XWnNSBv6MFCMx3eAgsuPBzow4abDmeSaYFC8xWXG3qJSSkG6",
"gi.contracts/identity": "21XWnNN7wGNxzFZmiS4ft2TumavYYAgemSuFrGavTVJpxqtnyg",
"gi.contracts/mailbox": "21XWnNHJ7MALCinR7vnu4GM82nq5DLhe6nhEDzruTuTw9CTnXP"
}
Expand Down
6 changes: 5 additions & 1 deletion frontend/model/contracts/shared/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,17 @@ export function humanDate (
}

export function isPeriodStamp (arg: string): boolean {
return /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(arg)
return isIsoString(arg)
}

export function isFullMonthstamp (arg: string): boolean {
return /^\d{4}-(0[1-9]|1[0-2])$/.test(arg)
}

export function isIsoString (arg: string): boolean {
return typeof arg === 'string' && /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(arg)
}

export function isMonthstamp (arg: string): boolean {
return isShortMonthstamp(arg) || isFullMonthstamp(arg)
}
Expand Down

0 comments on commit 2a40bdd

Please sign in to comment.