Skip to content

Commit

Permalink
Add new set function
Browse files Browse the repository at this point in the history
  • Loading branch information
jmannanc committed Aug 31, 2019
1 parent c3502f6 commit 2098253
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fp/set/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import { set } from 'date-fns/fp'
export default set
8 changes: 8 additions & 0 deletions src/fp/set/index.js
@@ -0,0 +1,8 @@
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it.

import fn from '../../set/index.js'
import convertToFP from '../_lib/convertToFP/index.js'

var set = convertToFP(fn, 1)

export default set
40 changes: 40 additions & 0 deletions src/fp/set/index.js.flow
@@ -0,0 +1,40 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | number,
end: Date | number
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

type CurriedFn1<A, R> = <A>(a: A) => R

declare module.exports: CurriedFn1<Date | number, Date>
4 changes: 4 additions & 0 deletions src/fp/setWithOptions/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import { setWithOptions } from 'date-fns/fp'
export default setWithOptions
8 changes: 8 additions & 0 deletions src/fp/setWithOptions/index.js
@@ -0,0 +1,8 @@
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it.

import fn from '../../set/index.js'
import convertToFP from '../_lib/convertToFP/index.js'

var setWithOptions = convertToFP(fn, 2)

export default setWithOptions
44 changes: 44 additions & 0 deletions src/fp/setWithOptions/index.js.flow
@@ -0,0 +1,44 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | number,
end: Date | number
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

type CurriedFn1<A, R> = <A>(a: A) => R

type CurriedFn2<A, B, R> = <A>(
a: A
) => CurriedFn1<B, R> | (<A, B>(a: A, b: B) => R)

declare module.exports: CurriedFn2<Object, Date | number, Date>
25 changes: 25 additions & 0 deletions src/set/benchmark.js
@@ -0,0 +1,25 @@
// @flow
/* eslint-env mocha */
/* global suite, benchmark */

import set from '.'
import moment from 'moment'

suite(
'set',
function() {
benchmark('date-fns', function() {
return set(this.date, { years: 2014, months: 8 })
})

benchmark('Moment.js', function() {
return this.moment.set({ year: 2014, month: 3 })
})
},
{
setup: function() {
this.date = new Date(2013, 7)
this.moment = moment()
}
}
)
4 changes: 4 additions & 0 deletions src/set/index.d.ts
@@ -0,0 +1,4 @@
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

import { set } from 'date-fns'
export default set
59 changes: 59 additions & 0 deletions src/set/index.js
@@ -0,0 +1,59 @@
import toDate from '../toDate/index.js'
import toInteger from '../_lib/toInteger/index.js'
import setMonth from '../setMonth/index.js'

/**
* @name set
* @category Common Helpers
* @summary Set the object to a given date
*
* @description
* Set the object to a given date
*
* @param {Date|Number} date - the date to be changed
* @param {Object} [options] - an object with options
* @param {Number} [options.years=0] - the number of years to be set
* @param {Number} [options.months=0] - the number of months to be set
* @param {Number} [options.days=0] - the number of days to be set
* @param {Number} [options.hours=0] - the number of hours to be set
* @param {Number} [options.minutes=0] - the number of minutes to be set
* @param {Number} [options.seconds=0] - the number of seconds to be set
* @param {Number} [options.milliseconds=0] - the number of milliseconds to be set
* @returns {Date} the new date with options set
* @throws {TypeError} 2 arguments required
*/

export default function set(dirtyDate, dirtyOptions) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}

var options = dirtyOptions || {}
var years = options.years == null ? 0 : toInteger(options.years)
var months = options.months == null ? 0 : toInteger(options.months)
var days = options.days == null ? 0 : toInteger(options.days)
var hours = options.hours == null ? 0 : toInteger(options.hours)
var minutes = options.minutes == null ? 0 : toInteger(options.minutes)
var seconds = options.seconds == null ? 0 : toInteger(options.seconds)
var milliseconds =
options.milliseconds == null ? 0 : toInteger(options.milliseconds)

var date = toDate(dirtyDate)

// Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
if (isNaN(date)) {
return new Date(NaN)
}

if (years > 0) date.setFullYear(years)
if (months > 0) date = setMonth(date, months)
if (days > 0) date.setDate(days)
if (hours > 0) date.setHours(hours)
if (minutes > 0) date.setMinutes(minutes)
if (seconds > 0) date.setSeconds(seconds)
if (milliseconds > 0) date.setMilliseconds(milliseconds)

return date
}
49 changes: 49 additions & 0 deletions src/set/index.js.flow
@@ -0,0 +1,49 @@
// @flow
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

type Interval = {
start: Date | number,
end: Date | number
}

type Locale = {
formatDistance: Function,
formatRelative: Function,
localize: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
formatLong: Object,
date: Function,
time: Function,
dateTime: Function,
match: {
ordinalNumber: Function,
era: Function,
quarter: Function,
month: Function,
day: Function,
dayPeriod: Function
},
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
}
}

declare module.exports: (
date: Date | number,
options?: {
years?: number,
months?: number,
days?: number,
hours?: number,
minutes?: number,
seconds?: number,
milliseconds?: number
}
) => Date
84 changes: 84 additions & 0 deletions src/set/test.js
@@ -0,0 +1,84 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import set from '.'

describe('set', function() {
it('set the date with every option', function() {
var result = set(new Date(2013), {
years: 2014,
months: 8,
days: 20,
hours: 12,
minutes: 12,
seconds: 12,
milliseconds: 12
})
assert.deepEqual(result, new Date(2014, 8, 20, 12, 12, 12, 12))
})
it('test only years', function() {
var result = set(new Date(2013, 8), { years: 2014 })
assert.deepEqual(result, new Date(2014, 8))
})
it('test only months', function() {
var result = set(new Date(2014, 8 /* Sep */), { months: 9 })
assert.deepEqual(result, new Date(2014, 9 /* Oct */))
})
it('test only days', function() {
var result = set(new Date(2014, 8), { days: 20 })
assert.deepEqual(result, new Date(2014, 8, 20))
})
it('test only hours', function() {
var result = set(new Date(2014, 8, 1), { hours: 12 })
assert.deepEqual(result, new Date(2014, 8, 1, 12))
})
it('test only minutes', function() {
var result = set(new Date(2014, 8, 1, 1), { minutes: 12 })
assert.deepEqual(result, new Date(2014, 8, 1, 1, 12))
})
it('test only seconds', function() {
var result = set(new Date(2014, 8, 1, 1, 1), { seconds: 12 })
assert.deepEqual(result, new Date(2014, 8, 1, 1, 1, 12))
})
it('test only milliseconds', function() {
var result = set(new Date(2014, 8, 1, 1, 1, 1), { milliseconds: 500 })
assert.deepEqual(result, new Date(2014, 8, 1, 1, 1, 1, 500))
})
it('months turns into years', function() {
var result = set(new Date(2014, 8), { months: 13 })
assert.deepEqual(result, new Date(2015, 1))
})
it('days turns into months', function() {
var result = set(new Date(2014, 8), { days: 31 })
assert.deepEqual(result, new Date(2014, 9))
})
it('hours turns into days', function() {
var result = set(new Date(2014, 8, 19), { hours: 24 })
assert.deepEqual(result, new Date(2014, 8, 20))
})
it('minutes turns into hours', function() {
var result = set(new Date(2014, 8, 20, 11), { minutes: 60 })
assert.deepEqual(result, new Date(2014, 8, 20, 12))
})
it('seconds turns into minutes', function() {
var result = set(new Date(2014, 8, 20, 12, 58), { seconds: 60 })
assert.deepEqual(result, new Date(2014, 8, 20, 12, 59))
})
it('milliseconds turns into seconds', function() {
var result = set(new Date(2014, 8, 20, 12, 58, 30), { milliseconds: 1000 })
assert.deepEqual(result, new Date(2014, 8, 20, 12, 58, 31))
})
it('NaN does not affect', function() {
var result = set(new Date(2014, 8), { years: NaN, months: NaN })
assert.deepEqual(result, new Date(2014, 8))
})
it('boolean does not affect', function() {
var result = set(new Date(2014, 8), { years: false, months: true })
assert.deepEqual(result, new Date(2014, 8))
})
it('undefined does not affect', function() {
var result = set(new Date(2014, 8), { years: undefined, months: undefined })
assert.deepEqual(result, new Date(2014, 8))
})
})

0 comments on commit 2098253

Please sign in to comment.