Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate startOfWeekYear function #2069

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/startOfWeekYear/index.js → src/startOfWeekYear/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getWeekYear from '../getWeekYear/index'
import startOfWeek from '../startOfWeek/index'
import toInteger from '../_lib/toInteger/index'
import requiredArgs from '../_lib/requiredArgs/index'
import { LocaleOptions, FirstWeekContainsDateOptions } from '../types'

/**
* @name startOfWeekYear
Expand Down Expand Up @@ -33,39 +34,42 @@ import requiredArgs from '../_lib/requiredArgs/index'
*
* @example
* // The start of an a week-numbering year for 2 July 2005 with default settings:
* var result = startOfWeekYear(new Date(2005, 6, 2))
* const result = startOfWeekYear(new Date(2005, 6, 2))
* //=> Sun Dec 26 2004 00:00:00
*
* @example
* // The start of a week-numbering year for 2 July 2005
* // if Monday is the first day of week
* // and 4 January is always in the first week of the year:
* var result = startOfWeekYear(new Date(2005, 6, 2), {
* const result = startOfWeekYear(new Date(2005, 6, 2), {
* weekStartsOn: 1,
* firstWeekContainsDate: 4
* })
* //=> Mon Jan 03 2005 00:00:00
*/
export default function startOfWeekYear(dirtyDate, dirtyOptions) {
export default function startOfWeekYear(
dirtyDate: Date | number,
dirtyOptions?: LocaleOptions & FirstWeekContainsDateOptions
): Date {
requiredArgs(1, arguments)

var options = dirtyOptions || {}
var locale = options.locale
var localeFirstWeekContainsDate =
const options = dirtyOptions || {}
const locale = options.locale
const localeFirstWeekContainsDate =
locale && locale.options && locale.options.firstWeekContainsDate
var defaultFirstWeekContainsDate =
const defaultFirstWeekContainsDate =
localeFirstWeekContainsDate == null
? 1
: toInteger(localeFirstWeekContainsDate)
var firstWeekContainsDate =
const firstWeekContainsDate =
options.firstWeekContainsDate == null
? defaultFirstWeekContainsDate
: toInteger(options.firstWeekContainsDate)

var year = getWeekYear(dirtyDate, dirtyOptions)
var firstWeek = new Date(0)
const year = getWeekYear(dirtyDate, dirtyOptions)
const firstWeek = new Date(0)
firstWeek.setFullYear(year, 0, firstWeekContainsDate)
firstWeek.setHours(0, 0, 0, 0)
var date = startOfWeek(firstWeek, dirtyOptions)
const date = startOfWeek(firstWeek, dirtyOptions)
return date
}
83 changes: 0 additions & 83 deletions src/startOfWeekYear/test.js

This file was deleted.

86 changes: 86 additions & 0 deletions src/startOfWeekYear/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-env mocha */

import assert from 'assert'
import startOfWeekYear from '.'

describe('startOfWeekYear', function () {
it('returns the date with the time set to 00:00:00 and the date set to the first day of a week year', function () {
const result = startOfWeekYear(new Date(2005, 6 /* Jul */, 2))
assert.deepStrictEqual(result, new Date(2004, 11 /* Dec */, 26, 0, 0, 0, 0))
})

it('accepts a timestamp', function () {
const result = startOfWeekYear(
new Date(2005, 0 /* Jan */, 1, 6, 0).getTime()
)
assert.deepStrictEqual(result, new Date(2004, 11 /* Dec */, 26, 0, 0, 0, 0))
})

it('does not mutate the original date', function () {
const date = new Date(2014, 6 /* Jul */, 2)
startOfWeekYear(date)
assert.deepStrictEqual(date, new Date(2014, 6 /* Jul */, 2))
})

it('handles dates before 100 AD', function () {
const initialDate = new Date(0)
initialDate.setFullYear(9, 0 /* Jan */, 1)
initialDate.setHours(0, 0, 0, 0)
const expectedResult = new Date(0)
expectedResult.setFullYear(8, 11 /* Dec */, 28)
expectedResult.setHours(0, 0, 0, 0)
const result = startOfWeekYear(initialDate)
assert.deepStrictEqual(result, expectedResult)
})

it('returns `Invalid Date` if the given date is invalid', function () {
const result = startOfWeekYear(new Date(NaN))
//@ts-expect-error
assert(result instanceof Date && isNaN(result))
})

it('allows to specify `weekStartsOn` and `firstWeekContainsDate` in locale', function () {
const date = new Date(2005, 6 /* Jul */, 2)
const result = startOfWeekYear(date, {
// @ts-expect-error
locale: {
options: { weekStartsOn: 1, firstWeekContainsDate: 4 },
},
})
assert.deepStrictEqual(result, new Date(2005, 0 /* Jan */, 3, 0, 0, 0, 0))
})

it('`options.weekStartsOn` overwrites the first day of the week specified in locale', function () {
const date = new Date(2005, 6 /* Jul */, 2)
const result = startOfWeekYear(date, {
weekStartsOn: 1,
firstWeekContainsDate: 4,
// @ts-expect-error
locale: {
options: { weekStartsOn: 0, firstWeekContainsDate: 1 },
},
})
assert.deepStrictEqual(result, new Date(2005, 0 /* Jan */, 3, 0, 0, 0, 0))
})

it('throws `RangeError` if `options.weekStartsOn` is not convertable to 0, 1, ..., 6 or undefined', function () {
// @ts-expect-error
const block = startOfWeekYear.bind(null, new Date(2007, 11 /* Dec */, 31), {
weekStartsOn: NaN,
})
assert.throws(block, RangeError)
})

it('throws `RangeError` if `options.firstWeekContainsDate` is not convertable to 1, 2, ..., 7 or undefined', function () {
// @ts-expect-error
const block = startOfWeekYear.bind(null, new Date(2007, 11 /* Dec */, 31), {
firstWeekContainsDate: NaN,
})
assert.throws(block, RangeError)
})

it('throws TypeError exception if passed less than 1 argument', function () {
//@ts-expect-error
assert.throws(startOfWeekYear.bind(null), TypeError)
})
})
7 changes: 1 addition & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,4 @@ export interface DateValues {
milliseconds?: number
}

export type Unit = 'second'
| 'minute'
| 'hour'
| 'day'
| 'month'
| 'year';
export type Unit = 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'