npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@simonja/moment-date-range

v0.1.2

Published

Utility to create a moment date range iterator

Downloads

403

Readme

moment-date-range Build Status

Creates an generator to lazily loop over a date range using moment.

Requirements

  • Node >= 8
  • moment (Peer dependency)

If you use Typescript, Typescript 3.6+ is required since this lib uses the global generic Generator type.

Install

$ npm install @simonja/moment-date-range --save

Usage

With only start passed

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-21'));

for (const day of generator) {
    console.log(day.format('YYYY-MM-DD')) // 2020-01-21 -> Infinity
}

With start and end passed using the default configuration options passed

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-21'), moment('2020-01-31'));

for (const day of generator) {
    console.log(day.format('YYYY-MM-DD')) // 2020-01-21 -> 2020-01-31
}

With a start, end and configuration options

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-01'), moment('2020-01-05'), {step: 2, unit: 'd'});

for (const day of generator) {
    console.log(day.format('YYYY-MM-DD')) // 2020-01-01 -> 2020-01-03 -> 2020-01-05
}

Without lazy computation

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-01'), moment('2020-01-10'));

const dates = Array
    .from(generator)
    .map(item => item.format('YYYY-MM-DD'));

console.log(dates)

// ['2020-01-01',
// '2020-01-02',
// '2020-01-03',
// '2020-01-04',
// '2020-01-05',
// '2020-01-06',
// '2020-01-07',
// '2020-01-08',
// '2020-01-09',
// '2020-01-10']

With start and end with a negative step configuration

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-31'), moment('2020-01-29'), {step: -1});

for (const day of generator) {
    console.log(day.format('YYYY-MM-DD')) // 2020-01-31 -> 2020-01-30 -> 2020-01-29
}

With start and end with a defined unit

import {range} from '@simonja/moment-date-range';

const generator = range(moment('2020-01-01'), moment('2020-01-18'), {unit: 'w'});

for (const day of generator) {
    console.log(day.format('YYYY-MM-DD')) // 2020-01-01 -> 2020-01-08 -> 2020-01-15
}

API

range(start, [end], [options]) -> Generator

start

Type: moment object

Start of the date range

end

Type: moment object

End of the date range

options

Configuration options

options.step

Number of units between each iteration. Default 1

options.unit

Unit used during iteration for comparison and cycling. Default d

Generator

Result of the range function. Returns a generator that lazily creates moment objects.