1.1.1 • Published 2 years ago

date-chunk v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

date-chunk

Split date range into multiple chunks. You can divide date range either into specified number of chunks or via size of chunk.

Install

yarn add date-chunk

npm install date-chunk

pnpm install date-chunk

Use case

import { chunkDate } from 'date-chunk';

const chunksWithSpecifiedQuantity = chunkDate({
  start: new Date('2021-10-17T00:00:00.000Z'),
  end: new Date('2022-10-16T00:00:00.000Z'),
  chunks: 2,
  strictSizedChunks: true, // default false
});

/*
[
  { start: 2021-10-17T00:00:00.000Z, end: 2022-04-17T00:00:00.000Z },
  { start: 2022-04-17T00:00:00.000Z, end: 2022-10-16T00:00:00.000Z }
]
*/

const chunksWithSpeicfiedSize = chunkDate({
  start: new Date('2021-10-17T00:00:00.000Z'),
  end: new Date('2022-10-16T00:00:00.000Z'),
  maxLimitPerChunk: [180, 'd'], // split date into chunks of 180 day max
});

/*
[
  { start: 2021-10-17T00:00:00.000Z, end: 2022-04-15T00:00:00.000Z },
  { start: 2022-04-15T00:00:00.000Z, end: 2022-10-12T00:00:00.000Z },
  { start: 2022-10-12T00:00:00.000Z, end: 2022-10-16T00:00:00.000Z }
]
*/

const chunksWithSpeicfiedSizeAndTimestamp = chunkDate({
  start: 1634428800000,
  end: new Date('2022-10-16T00:00:00.000Z'),
  maxLimitPerChunk: [180, 'd'], // split date into chunks of 180 day max
});

/*
[
  { start: 1634428800000, end: 2022-04-15T00:00:00.000Z },
  { start: 1649980800000, end: 2022-10-12T00:00:00.000Z },
  { start: 1665532800000, end: 2022-10-16T00:00:00.000Z }
]
*/

API Reference

Full Typescript documentation -> docs/modules.md

dateChunk(options)

option paramDescriptionType
startStart dateDate or milliseconds as number
endEnd dateDate or milliseconds as number
maxLimitPerChunkSet max limit for individual chunkTuple of number of time units, time unit (see supported units below)
chunksHow many chunks should be generated from start and endnumber
strictSizeChunksForce flag to make chunks same size when there is remainder after divisionboolean

maxLimitPerChunk units

UnitDescription
msMillisecond
sSecond
mMinute
hHour
dDay

ReturnType

Array Of | Attribute | Description | Type | |-----------|-----------------------|-------------------------------------------------------------------------------------| | start | Start date of a chunk | Date or milliseconds as number (returns type that was provided in params for start) | | end | End date of a chunk | Date or milliseconds as number (returns type that was provided in params for start) |

Changelog

The changelog can be found on the Releases page.

Contributing

Everyone is welcome to contribute. Please take a moment to review the contributing guidelines.

Authors and license

Michal Honc and contributors.

MIT License, see the included License.md file.

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.0-alpha

2 years ago