Skip to content

Commit

Permalink
feat: use branded IsoDate instead of non-branded IsoDateString
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Nov 9, 2024
1 parent 40befe6 commit a03ee2b
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 216 deletions.
6 changes: 4 additions & 2 deletions scripts/dateParseBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ yarn tsn dateParseBench

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

const strings = localDate.range('2022-01-03', '2023-01-05').map(String)
const strings = localDate
.range('2022-01-03' as IsoDate, '2023-01-05' as IsoDate)
.map(d => d.toString())
const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/

// dayjs x 4,733 ops/sec ±0.31% (90 runs sampled)
Expand Down
4 changes: 2 additions & 2 deletions scripts/lazyLocalDateBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ yarn tsn lazyLocalDateBench

import { runBenchScript } from '@naturalcycles/bench-lib'
// import { LazyLocalDate } from '../src/__exclude/lazyLocalDate'
import { localDate } from '../src'
import { IsoDate, localDate } from '../src'

const str = '1984-06-21'
const str = '1984-06-21' as IsoDate

runBenchScript({
fns: {
Expand Down
4 changes: 2 additions & 2 deletions scripts/localDateBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ yarn tsn localDateBench

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

const str = '1984-06-21'
const str = '1984-06-21' as IsoDate

runBenchScript({
fns: {
Expand Down
10 changes: 5 additions & 5 deletions scripts/todayIsoDateBench.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ yarn tsn todayIsoDateBench
*/

import { runBenchScript } from '@naturalcycles/bench-lib'
import { IsoDateString } from '../src'
import { IsoDate } from '../src'

runBenchScript({
fns: {
Expand All @@ -20,17 +20,17 @@ runBenchScript({
},
})

function fn1(): IsoDateString {
function fn1(): IsoDate {
const d = new Date()
return [
d.getFullYear(),
String(d.getMonth() + 1).padStart(2, '0'),
String(d.getDate()).padStart(2, '0'),
].join('-')
].join('-') as IsoDate
}

function fn2(): IsoDateString {
return new Date().toISOString().slice(0, 10)
function fn2(): IsoDate {
return new Date().toISOString().slice(0, 10) as IsoDate
}

// function fn1(): IsoDateString {
Expand Down
32 changes: 18 additions & 14 deletions src/datetime/dateInterval.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IsoDate } from '../types'
import { DateInterval } from './dateInterval'
import { localDate } from './localDate'

Expand All @@ -8,11 +9,14 @@ test('basic', () => {
const int1 = DateInterval.parse(str1)
expect(int1.toString()).toBe(str1)
expect(JSON.stringify(int1)).toBe(`"${str1}"`)
expect(int1.start.isSame('2022-02-24'))
expect(int1.end.isSame('2022-03-30'))
expect(int1.start.isSame('2022-02-24' as IsoDate))
expect(int1.end.isSame('2022-03-30' as IsoDate))

const int2 = DateInterval.of('2022-02-24', '2022-03-30')
const int3 = DateInterval.of(localDate('2022-02-24'), localDate('2022-03-30'))
const int2 = DateInterval.of('2022-02-24' as IsoDate, '2022-03-30' as IsoDate)
const int3 = DateInterval.of(
localDate('2022-02-24' as IsoDate),
localDate('2022-03-30' as IsoDate),
)

expect(int1.isSame(int2)).toBe(true)
expect(int1.cmp(int2)).toBe(0)
Expand All @@ -38,16 +42,16 @@ test('basic', () => {

test('includes', () => {
const int = DateInterval.parse('2022-02-24/2022-03-30')
expect(int.includes('2022-02-23')).toBe(false)
expect(int.includes('2022-02-24')).toBe(true)
expect(int.includes('2022-02-25')).toBe(true)
expect(int.includes('2022-02-28')).toBe(true)
expect(int.includes('2022-03-01')).toBe(true)
expect(int.includes('2022-03-11')).toBe(true)
expect(int.includes('2022-03-29')).toBe(true)
expect(int.includes('2022-03-30')).toBe(true)
expect(int.includes('2022-03-31')).toBe(false)
expect(int.includes('2022-04-01')).toBe(false)
expect(int.includes('2022-02-23' as IsoDate)).toBe(false)
expect(int.includes('2022-02-24' as IsoDate)).toBe(true)
expect(int.includes('2022-02-25' as IsoDate)).toBe(true)
expect(int.includes('2022-02-28' as IsoDate)).toBe(true)
expect(int.includes('2022-03-01' as IsoDate)).toBe(true)
expect(int.includes('2022-03-11' as IsoDate)).toBe(true)
expect(int.includes('2022-03-29' as IsoDate)).toBe(true)
expect(int.includes('2022-03-30' as IsoDate)).toBe(true)
expect(int.includes('2022-03-31' as IsoDate)).toBe(false)
expect(int.includes('2022-04-01' as IsoDate)).toBe(false)
})

test('getDays', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/datetime/dateInterval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inclusiveness } from '../types'
import { Inclusiveness, IsoDate } from '../types'
import { LocalDate, localDate, LocalDateInput, LocalDateUnit } from './localDate'

export type DateIntervalConfig = DateInterval | DateIntervalString
Expand All @@ -25,7 +25,7 @@ export class DateInterval {
static parse(d: DateIntervalConfig): DateInterval {
if (d instanceof DateInterval) return d

const [start, end] = d.split('/')
const [start, end] = d.split('/') as IsoDate[]

if (!end || !start) {
throw new Error(`Cannot parse "${d}" into DateInterval`)
Expand Down
Loading

0 comments on commit a03ee2b

Please sign in to comment.