Skip to content

Commit

Permalink
more DRY withExtended
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Mar 27, 2024
1 parent fe37479 commit 09dc3f0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 63 deletions.
3 changes: 3 additions & 0 deletions packages/temporal-polyfill/src/internal/calendarNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export function computeNativeYearOfWeek(
// String Parsing
// -----------------------------------------------------------------------------

/*
TODO: rename. used for other purposes
*/
export interface NativeYearMonthParseOps {
day: DayOp
}
Expand Down
154 changes: 91 additions & 63 deletions packages/temporal-polyfill/src/internal/withExtended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,53 @@ WIP. Ultimately for funcApi
import {
createNativeDayOfYearOps,
createNativeWeekOps,
createNativeYearMonthParseOps,
} from './calendarNativeQuery'
import * as errorMessages from './errorMessages'
import { moveByIsoDays, moveToDayOfWeek, moveToDayOfYear } from './move'
import { IsoDateFields } from './isoFields'
import {
PlainDateTimeSlots,
moveByIsoDays,
moveToDayOfMonth,
moveToDayOfWeek,
moveToDayOfYear,
} from './move'
import {
DateSlots,
ZonedDateTimeSlots,
createPlainDateTimeSlots,
createZonedDateTimeSlots,
} from './slots'
import { queryNativeTimeZone } from './timeZoneNative'
import { getSingleInstantFor, zonedEpochSlotsToIso } from './timeZoneOps'
import { bindArgs } from './utils'

// Generic
// -----------------------------------------------------------------------------

function zonedSlotsWithTransform(
transformIso: (isoSlots: DateSlots<string>, num: number) => IsoDateFields,
slots: ZonedDateTimeSlots<string, string>,
num: number,
): ZonedDateTimeSlots<string, string> {
const { timeZone, calendar } = slots
const timeZoneOps = queryNativeTimeZone(timeZone)
const isoSlots = zonedEpochSlotsToIso(slots, timeZoneOps)

// PlainDateTime
const transformedIsoSlots = {
...isoSlots,
...transformIso(isoSlots, num),
}

export function pdt_withWeekOfYear(
slots: PlainDateTimeSlots<string>,
return createZonedDateTimeSlots(
getSingleInstantFor(timeZoneOps, transformedIsoSlots),
timeZone,
calendar,
)
}

function slotsWithWeekOfYear<S extends DateSlots<string>>(
slots: S,
weekOfYear: number,
): PlainDateTimeSlots<string> {
): S {
const { calendar } = slots
const calendarOps = createNativeWeekOps(calendar)

Expand All @@ -30,79 +59,78 @@ export function pdt_withWeekOfYear(
throw new RangeError(errorMessages.unsupportedWeekNumbers)
}

const movedIsoFields = {
...slots, // for time
return {
...slots, // for possible time
...moveByIsoDays(slots, (weekOfYear - currentWeekOfYear) * 7),
}

return createPlainDateTimeSlots(movedIsoFields, calendar)
}

// ZonedDateTime
// -----------------------------------------------------------------------------

export function zdt_withWeekOfYear(
export function zdt_withDayOfYear(
slots: ZonedDateTimeSlots<string, string>,
weekOfYear: number,
dayOfYear: number,
): ZonedDateTimeSlots<string, string> {
const { timeZone, calendar } = slots
const calendarOps = createNativeWeekOps(calendar)
const timeZoneOps = queryNativeTimeZone(timeZone)
const isoFields = zonedEpochSlotsToIso(slots, timeZoneOps)

const currentWeekOfYear = calendarOps.weekOfYear(isoFields)
if (currentWeekOfYear === undefined) {
throw new RangeError(errorMessages.unsupportedWeekNumbers)
}

const movedIsoFields = {
...isoFields, // for time
...moveByIsoDays(isoFields, (weekOfYear - currentWeekOfYear) * 7),
}

return createZonedDateTimeSlots(
getSingleInstantFor(timeZoneOps, movedIsoFields),
timeZone,
calendar,
const calendarOps = createNativeDayOfYearOps(slots.calendar)
return zonedSlotsWithTransform(
bindArgs(moveToDayOfYear, calendarOps),
slots,
dayOfYear,
)
}

export function zdt_withDayOfWeek(
export function zdt_withDayOfMonth(
slots: ZonedDateTimeSlots<string, string>,
dayOfWeek: number,
dayOfMonth: number,
): ZonedDateTimeSlots<string, string> {
const { timeZone, calendar } = slots
const timeZoneOps = queryNativeTimeZone(timeZone)
const isoFields = zonedEpochSlotsToIso(slots, timeZoneOps)

const movedIsoFields = {
...isoFields, // for time
...moveToDayOfWeek(isoFields, dayOfWeek),
}

return createZonedDateTimeSlots(
getSingleInstantFor(timeZoneOps, movedIsoFields),
timeZone,
calendar,
const calendarOps = createNativeYearMonthParseOps(slots.calendar)
return zonedSlotsWithTransform(
bindArgs(moveToDayOfMonth, calendarOps),
slots,
dayOfMonth,
)
}

export function zdt_withDayOfYear(
slots: ZonedDateTimeSlots<string, string>,
export const zdt_withDayOfWeek = bindArgs(
zonedSlotsWithTransform,
moveToDayOfWeek,
)

export const zdt_withWeekOfYear = bindArgs(
zonedSlotsWithTransform,
slotsWithWeekOfYear,
)

export function pd_or_pdt_withDayOfYear<S extends DateSlots<string>>(
slots: S,
dayOfYear: number,
): ZonedDateTimeSlots<string, string> {
const { timeZone, calendar } = slots
const calendarOps = createNativeDayOfYearOps(calendar)
const timeZoneOps = queryNativeTimeZone(timeZone)
const isoFields = zonedEpochSlotsToIso(slots, timeZoneOps)
): S {
const calendarOps = createNativeDayOfYearOps(slots.calendar)
return {
...slots,
...moveToDayOfYear(calendarOps, slots, dayOfYear),
}
}

const movedIsoFields = {
...isoFields, // for time
...moveToDayOfYear(calendarOps, isoFields, dayOfYear),
export function pd_or_pdt_withDayOfMonth<S extends DateSlots<string>>(
slots: S,
dayOfMonth: number,
): S {
const calendarOps = createNativeYearMonthParseOps(slots.calendar)
return {
...slots,
...moveToDayOfMonth(calendarOps, slots, dayOfMonth),
}
}

return createZonedDateTimeSlots(
getSingleInstantFor(timeZoneOps, movedIsoFields),
timeZone,
calendar,
)
export function pd_or_pdt_withDayOfWeek<S extends DateSlots<string>>(
slots: S,
dayOfWeek: number,
): S {
return {
...slots,
...moveToDayOfWeek(slots, dayOfWeek),
}
}

export const pd_or_pdt_withWeekOfYear = slotsWithWeekOfYear

0 comments on commit 09dc3f0

Please sign in to comment.