Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

trim-dates

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trim-dates - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

CHANGELOG

2

package.json
{
"name": "trim-dates",
"version": "0.0.1",
"version": "0.1.0",
"description": "Trim (intersect) a date range whith a specific day (or custom range).",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -88,3 +88,3 @@ # trim-dates

**Note: this offset is equivalent to `Date.prototype.getTimezoneOffset()`**.
Which means `2017-01-01T10:00:00+02:00` for a french timezone should be given a **-120** offset.
Which means `2017-01-01T10:00:00+02:00` for a french timezone should be given a **`-120`** offset.

@@ -170,2 +170,3 @@ Finaly, by defaults the returned range is in local timezone, so the above date would gives `10` when calling `getHours()`.

* `opts.revertZone` - default: `false`. In case you prefer the result to keep it's original value.
* `dayTimezoneOffset` - default: `null`. Similat to timezoneOffset, but adapt the day if in another timezone.

@@ -172,0 +173,0 @@

@@ -6,88 +6,111 @@ var trimDates = require('../')

test('check Date.prototype.toLocaleTimeString works as expected', function (t) {
var day = new Date('2017-01-01T02:00:00')
t.equal(day.toLocaleTimeString(), '02:00:00')
t.end()
})
//test('check Date.prototype.toLocaleTimeString works as expected', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//t.equal(day.toLocaleTimeString(), '02:00:00')
//t.end()
//})
test('intersecting inside selected day gives the whole day range', function (t) {
var day = new Date('2017-01-01T02:00:00')
var start = new Date('2017-01-01T09:00:00')
var end = new Date('2017-01-01T16:00:00')
var inter = trimDates(day, [start, end])
//test('intersecting inside selected day gives the whole day range', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//var start = new Date('2017-01-01T09:00:00')
//var end = new Date('2017-01-01T16:00:00')
//var inter = trimDates(day, [start, end])
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
t.equal(inter[0].toLocaleTimeString(), start.toLocaleTimeString())
t.equal(inter[1].toLocaleTimeString(), end.toLocaleTimeString())
t.end()
})
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
//t.equal(inter[0].toLocaleTimeString(), start.toLocaleTimeString())
//t.equal(inter[1].toLocaleTimeString(), end.toLocaleTimeString())
//t.end()
//})
test('dates could be timestamps', function (t) {
var day = new Date('2017-01-01T02:00:00').getTime()
var start = new Date('2017-01-01T09:00:00').getTime()
var end = new Date('2017-01-01T16:00:00').getTime()
var inter = trimDates(day, [start, end])
//test('dates could be timestamps', function (t) {
//var day = new Date('2017-01-01T02:00:00').getTime()
//var start = new Date('2017-01-01T09:00:00').getTime()
//var end = new Date('2017-01-01T16:00:00').getTime()
//var inter = trimDates(day, [start, end])
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
t.equal(inter[0].getTime(), start)
t.equal(inter[1].getTime(), end)
t.end()
})
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
//t.equal(inter[0].getTime(), start)
//t.equal(inter[1].getTime(), end)
//t.end()
//})
test('intersecting outside selected day gives trimmed range, end finish after day', function (t) {
var day = new Date('2017-01-01T02:00:00')
var start = new Date('2017-01-01T09:00:00')
var end = new Date('2017-01-02T16:00:00')
var inter = trimDates(day, [start, end])
//test('intersecting outside selected day gives trimmed range, end finish after day', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//var start = new Date('2017-01-01T23:00:00')
//var end = new Date('2017-01-02T01:00:00')
//var inter = trimDates(day, [start, end])
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 23:59:59')
t.equal(inter[1].getDate(), day.getDate())
t.end()
})
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '23:00:00 - 23:59:59')
//t.equal(inter[1].getDate(), day.getDate())
//t.end()
//})
test('intersecting outside selected day gives inclusive trimmed range if specified', function (t) {
var day = new Date('2017-01-01T02:00:00')
var start = new Date('2017-01-01T09:00:00')
var end = new Date('2017-01-02T16:00:00')
var inter = trimDates(day, [start, end], { inclusive: true })
//test('intersecting outside selected day gives inclusive trimmed range if specified', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//var start = new Date('2017-01-01T09:00:00')
//var end = new Date('2017-01-02T16:00:00')
//var inter = trimDates(day, [start, end], { inclusive: true })
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 00:00:00')
t.equal(inter[1].getDate(), 2)
t.end()
})
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 00:00:00')
//t.equal(inter[1].getDate(), 2)
//t.end()
//})
test('intersecting outside selected day gives trimmed range, start begins before day', function (t) {
//test('intersecting outside selected day gives trimmed range, start begins before day', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//var start = new Date('2016-12-31T23:00:00')
//var end = new Date('2017-01-01T02:00:00')
//var inter = trimDates(day, [start, end])
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '00:00:00 - 02:00:00')
//t.equal(inter[0].getDate(), day.getDate())
//t.end()
//})
//test('intersecting outside selected day gives trimmed range, start begins before day, end finish after day', function (t) {
//var day = new Date('2017-01-01T02:00:00')
//var start = new Date('2016-12-31T09:00:00')
//var end = new Date('2017-02-01T16:00:00')
//var inter = trimDates(day, [start, end])
//t.ok(Array.isArray(inter))
//t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '00:00:00 - 23:59:59')
//t.equal(inter[0].getDate(), day.getDate())
//t.equal(inter[1].getDate(), day.getDate())
//t.end()
//})
test('intersecting inside same day is working in another timezone', function (t) {
var day = new Date('2017-01-01T02:00:00')
var start = new Date('2016-12-31T09:00:00')
var end = new Date('2017-01-01T16:00:00')
var inter = trimDates(day, [start, end])
var start = new Date('2017-01-01T09:00:00-05:00')
var end = new Date('2017-01-01T16:00:00-05:00')
var inter = trimDatesTz(day, [start, end], { timezoneOffset: 5 *60})
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '00:00:00 - 16:00:00')
t.equal(inter[0].getDate(), day.getDate())
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
t.end()
})
test('intersecting outside selected day gives trimmed range, start begins before day, end finish after day', function (t) {
var day = new Date('2017-01-01T02:00:00')
var start = new Date('2016-12-31T09:00:00')
var end = new Date('2017-02-01T16:00:00')
var inter = trimDates(day, [start, end])
test('intersecting in another timezone, and reverting to original timezone', function (t) {
var day = new Date('2017-01-01T22:00:00') // current time zone, all is good
var start = new Date('2017-01-01T09:00:00-10:00')
var end = new Date('2017-01-01T16:00:00-10:00')
var inter = trimDatesTz(day, [start, end], { timezoneOffset: 10 *60, revertZone: true })
t.ok(Array.isArray(inter))
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '00:00:00 - 23:59:59')
t.equal(inter[0].getDate(), day.getDate())
t.equal(inter[1].getDate(), day.getDate())
t.equal(inter[0].toISOString(), start.toISOString())
t.equal(inter[1].toISOString(), end.toISOString())
t.end()
})
test('intersecting inside same day is working in another timezone', function (t) {
test('intersecting inside same day is working in another timezone for day and range', function (t) {
var day = new Date('2017-01-01T02:00:00-05:00')
var start = new Date('2017-01-01T09:00:00-05:00')
var end = new Date('2017-01-01T16:00:00-05:00')
var inter = trimDatesTz(day, [start, end], { tzOffset: 5 *60})
var inter = trimDatesTz(day, [start, end], { timezoneOffset: 5 *60, dayTimezoneOffset: 5 *60 })

@@ -99,12 +122,14 @@ t.ok(Array.isArray(inter))

test('intersecting in another timezone, and reverting to original timezone', function (t) {
var day = new Date('2017-01-01T02:00:00-05:00')
var start = new Date('2017-01-01T09:00:00-05:00')
var end = new Date('2017-01-01T16:00:00-05:00')
var inter = trimDatesTz(day, [start, end], { tzOffset: 5 *60, revertZone: true })
test('intersecting in another timezone for day and another again range', function (t) {
var day = new Date('2017-01-01T22:00:00-05:00')
var start = new Date('2017-01-01T09:00:00+02:00')
var end = new Date('2017-01-01T16:00:00+02:00')
var inter = trimDatesTz(day, [start, end], {
timezoneOffset: -2 *60,
dayTimezoneOffset: 5*60
})
t.ok(Array.isArray(inter))
t.equal(inter[0].toISOString(), start.toISOString())
t.equal(inter[1].toISOString(), end.toISOString())
t.equal(`${inter[0].toLocaleTimeString()} - ${inter[1].toLocaleTimeString()}`, '09:00:00 - 16:00:00')
t.end()
})
var trimDay = require('./')
var localOffset = new Date().getTimezoneOffset()
var defaults = { tzOffset: 0, revertZone: false }
var defaults = {
timezoneOffset: 0,
dayTimezoneOffset: null,
revertZone: false
}

@@ -8,7 +12,9 @@

options = options || defaults
var fullOffset = options.tzOffset - localOffset
var toLocal = changeOffset(fullOffset)
var tzDay = Array.isArray(day) ? day.map(toLocal) : toLocal(day)
var tzRange = range.map(toLocal)
var offset = options.timezoneOffset - localOffset
var dayOffset = options.dayTimezoneOffset ? options.dayTimezoneOffset - localOffset : null
var tzRange = updateZone(range, offset)
var tzDay = dayOffset ? updateZone(day, dayOffset) : day
var inter = trimDay(tzDay, tzRange, options)

@@ -19,9 +25,10 @@

return options.revertZone ? inter.map(changeOffset(-fullOffset)) : inter
return options.revertZone ? updateZone(inter, -offset) : inter
}
function changeOffset(tzOffset) {
return function utc(date) {
return new Date(date.getTime() - toMs(tzOffset))
function updateZone(day, offset) {
var toLocal = function utc(date) {
return new Date(date.getTime() - toMs(offset))
}
return Array.isArray(day) ? day.map(toLocal) : toLocal(day)
}

@@ -28,0 +35,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc