Skip to content

Commit

Permalink
fix: separate localDateRange for better tree-shakeability
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Oct 21, 2023
1 parent a702b89 commit 6d2fa91
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 61 deletions.
4 changes: 2 additions & 2 deletions scripts/dateParseBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ yarn tsn dateParseBench

import { runBenchScript } from '@naturalcycles/bench-lib'
import { dayjs } from '@naturalcycles/time-lib'
import { localDate, LocalDate } from '../src'
import { localDate, localDateRange } from '../src'

const strings = LocalDate.range('2022-01-03', '2023-01-05').map(String)
const strings = localDateRange('2022-01-03', '2023-01-05').map(String)
const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/

// dayjs x 4,733 ops/sec ±0.31% (90 runs sampled)
Expand Down
6 changes: 3 additions & 3 deletions src/datetime/dateInterval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Inclusiveness, LocalDateInput, LocalDateUnit } from './localDate'
import { LocalDate } from './localDate'
import { LocalDate, localDateRange } from './localDate'

export type DateIntervalConfig = DateInterval | DateIntervalString
export type DateIntervalString = string
Expand Down Expand Up @@ -81,14 +81,14 @@ export class DateInterval {
}

getDays(incl: Inclusiveness = '[]'): LocalDate[] {
return LocalDate.range(this.start, this.end, incl, 1, 'day')
return localDateRange(this.start, this.end, incl, 1, 'day')
}

/**
* Returns an array of LocalDates that are included in the interval.
*/
range(incl: Inclusiveness = '[]', step = 1, stepUnit: LocalDateUnit = 'day'): LocalDate[] {
return LocalDate.range(this.start, this.end, incl, step, stepUnit)
return localDateRange(this.start, this.end, incl, step, stepUnit)
}

toString(): DateIntervalString {
Expand Down
12 changes: 7 additions & 5 deletions src/datetime/localDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
LocalDate,
localDateOrToday,
localDateOrUndefined,
localDateRange,
localDateRangeIt,
localDateToday,
} from './localDate'

Expand Down Expand Up @@ -212,30 +214,30 @@ test('toDate', () => {
})

test('range', () => {
expect(LocalDate.range('2021-12-24', '2021-12-26')).toMatchInlineSnapshot(`
expect(localDateRange('2021-12-24', '2021-12-26')).toMatchInlineSnapshot(`
[
"2021-12-24",
"2021-12-25",
]
`)

expect(LocalDate.range('2021-12-24', '2021-12-26', '[]')).toMatchInlineSnapshot(`
expect(localDateRange('2021-12-24', '2021-12-26', '[]')).toMatchInlineSnapshot(`
[
"2021-12-24",
"2021-12-25",
"2021-12-26",
]
`)

expect(LocalDate.range('2021-12-24', '2021-12-30', '[)', 2)).toMatchInlineSnapshot(`
expect(localDateRange('2021-12-24', '2021-12-30', '[)', 2)).toMatchInlineSnapshot(`
[
"2021-12-24",
"2021-12-26",
"2021-12-28",
]
`)

expect(LocalDate.range('2021-12-24', '2021-12-30', '[]', 2)).toMatchInlineSnapshot(`
expect(localDateRange('2021-12-24', '2021-12-30', '[]', 2)).toMatchInlineSnapshot(`
[
"2021-12-24",
"2021-12-26",
Expand All @@ -246,7 +248,7 @@ test('range', () => {
})

test('rangeIterable', () => {
expect([...LocalDate.rangeIt('2021-12-24', '2021-12-26')]).toMatchInlineSnapshot(`
expect([...localDateRangeIt('2021-12-24', '2021-12-26')]).toMatchInlineSnapshot(`
[
"2021-12-24",
"2021-12-25",
Expand Down
102 changes: 51 additions & 51 deletions src/datetime/localDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,57 +143,6 @@ export class LocalDate {
.reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
}

static range(
min: LocalDateInput,
max: LocalDateInput,
incl: Inclusiveness = '[)',
step = 1,
stepUnit: LocalDateUnit = 'day',
): LocalDate[] {
return this.rangeIt(min, max, incl, step, stepUnit).toArray()
}

/**
* Experimental, returns the range as Iterable2.
*/
static rangeIt(
min: LocalDateInput,
max: LocalDateInput,
incl: Inclusiveness = '[)',
step = 1,
stepUnit: LocalDateUnit = 'day',
): Iterable2<LocalDate> {
if (stepUnit === 'week') {
step *= 7
stepUnit = 'day'
}

const $min = LocalDate.of(min).startOf(stepUnit)
const $max = LocalDate.of(max).startOf(stepUnit)

let value = $min
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
if (value.isAfter($min, incl[0] === '[')) {
// ok
} else {
value.plus(1, stepUnit, true)
}

const rightInclusive = incl[1] === ']'

return Iterable2.of({
*[Symbol.iterator]() {
while (value.isBefore($max, rightInclusive)) {
yield value

// We don't mutate, because we already returned `current`
// in the previous iteration
value = value.plus(step, stepUnit)
}
},
})
}

get(unit: LocalDateUnitStrict): number {
return unit === 'year' ? this.$year : unit === 'month' ? this.$month : this.$day
}
Expand Down Expand Up @@ -583,6 +532,57 @@ export class LocalDate {
}
}

export function localDateRange(
min: LocalDateInput,
max: LocalDateInput,
incl: Inclusiveness = '[)',
step = 1,
stepUnit: LocalDateUnit = 'day',
): LocalDate[] {
return localDateRangeIt(min, max, incl, step, stepUnit).toArray()
}

/**
* Experimental, returns the range as Iterable2.
*/
export function localDateRangeIt(
min: LocalDateInput,
max: LocalDateInput,
incl: Inclusiveness = '[)',
step = 1,
stepUnit: LocalDateUnit = 'day',
): Iterable2<LocalDate> {
if (stepUnit === 'week') {
step *= 7
stepUnit = 'day'
}

const $min = LocalDate.of(min).startOf(stepUnit)
const $max = LocalDate.of(max).startOf(stepUnit)

let value = $min
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
if (value.isAfter($min, incl[0] === '[')) {
// ok
} else {
value.plus(1, stepUnit, true)
}

const rightInclusive = incl[1] === ']'

return Iterable2.of({
*[Symbol.iterator]() {
while (value.isBefore($max, rightInclusive)) {
yield value

// We don't mutate, because we already returned `current`
// in the previous iteration
value = value.plus(step, stepUnit)
}
},
})
}

/**
* Convenience wrapper around `LocalDate.of`
*/
Expand Down

0 comments on commit 6d2fa91

Please sign in to comment.