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

chai-luxon

v0.2.1

Published

DateTime assertions for Chai, powered by Luxon

Downloads

1,052

Readme

chai-luxon

A Chai plugin that adds matchers for dates and formatted date strings powered by Luxon

NPM Version NPM License NPM Downloads
NPM

Using

Also see the tests

browser-side

include chai luxon after chai and luxon:

<script src="luxon.js"></script>
<script src="chai.js"></script>
<script src="chai-luxon.js"></script>

server-side

have chai use chai-luxon:

var chai = require('chai');
chai.use(require('chai-luxon'));

Assertions

Compare any Luxon compatible date/string/whatever with another, with optional granularity.

When using granularity, please use one of the following: year, month, week, day, hour, minute, second. When using tdd-style assertions, if you do not use one of the listed granularities, the argument will be interpreted as a custom error message.

sameDateTime

var dateString = '2020-04-21',
  date = new Date(2020, 3, 21),
  milliseconds = 1461222000000, // assumes system has PDT timezone
  obj = { year: 2020, month: 3, day: 21 },
  luxonDateTime = DateTime.fromISO('2020-04-21'),
  oneDayLater = DateTime.fromISO('2020-04-22');

// using should-style assertions
dateString.should.be.sameDateTime(date);
dateString.should.be.sameDateTime(oneDayLater, 'month');

// using expect-style assertions
expect(milliseconds).to.be.sameDateTime(obj);
expect(dateString).to.be.sameDateTime(oneDayLater, 'month');

// using tdd assertions
assert.sameDateTime(luxonDateTime, luxonDateTime);
assert.sameDateTime(luxonDateTime, oneDayLater, 'month');
assert.sameDateTime(luxonDateTime, oneDayLater, 'month', 'custom error message');
assert.sameDateTime(luxonDateTime, oneDayLater, 'custom error message'); // fails

beforeDateTime

var dateString = '2020-04-21',
  oneDayLater = '2020-04-22';

// using should-style assertions
dateString.should.be.beforeDateTime(oneDayLater);
dateString.should.be.beforeDateTime(oneDayLater, 'month'); // fails

// using expect-style assertions
expect(dateString).to.be.beforeDateTime(oneDayLater);
expect(dateString).to.be.beforeDateTime(oneDayLater, 'month'); // fails

// using tdd assertions
assert.beforeDateTime(luxonDateTime, oneDayLater);
assert.beforeDateTime(luxonDateTime, oneDayLater, 'month'); // fails
assert.beforeDateTime(luxonDateTime, oneDayLater, 'month', 'custom error message'); // fails
assert.beforeDateTime(luxonDateTime, oneDayLater, 'custom error message');

afterDateTime

var dateString = '2020-04-21',
  oneDayLater = '2020-04-22';

// using should-style assertions
oneDayLater.should.be.afterDateTime(luxonDateTime);
oneDayLater.should.be.afterDateTime(luxonDateTime, 'month'); // fails

// using expect-style assertions
expect(oneDayLater).to.be.afterDateTime(luxonDateTime);
expect(oneDayLater).to.be.afterDateTime(luxonDateTime, 'month'); // fails

// using tdd assertions
assert.afterDateTime(oneDayLater, luxonDateTime);
assert.afterDateTime(oneDayLater, luxonDateTime, 'month'); // fails
assert.afterDateTime(oneDayLater, luxonDateTime, 'month', 'custom error message'); // fails
assert.afterDateTime(oneDayLater, luxonDateTime, 'custom error message');

Date-part only

The library includes convenience methods for comparing only the date portion of DateTime values. These convenience methods are aliases for using a granularity of 'day' with the above matchers. All the same date formats (object, JS Date, string, etc) are supported in the same way.

sameDate, beforeDate, afterdate

const date = DateTime.fromISO('2020-04-21T12:00:00Z');
const oneHourLater = date.plus({ hour: 1 });
const oneHourEarlier = date.minus({ hour: 1 });
const oneDayLater = date.plus({ day: 1 });
const oneDayEarlier = date.minus({ day: 1 });

// using should-style assertions
date.should.be.sameDate(oneHourLater);
date.should.be.beforeDate(oneHourLater); // fails
date.should.be.beforeDate(oneDayLater);
date.should.be.afterDate(oneHourEarlier); // fails
date.should.be.afterDate(oneDayLater);

// using expect-style assertions
expect(date).to.be.sameDate(oneHourLater);
expect(date).to.be.beforeDate(oneHourLater); // fails
expect(date).to.be.beforeDate(oneDayLater);
expect(date).to.be.afterDate(oneHourEarlier); // fails
expect(date).to.be.afterDate(oneDayLater);

// using tdd assertions
assert.sameDate(date, oneHourLater);
assert.beforeDate(date, oneDayLater);
assert.beforeDate(date, oneHourLater); // fails
assert.afterDate(oneDayLater, date);
assert.afterDate(oneDayLater, oneHourLater); // fails

Limitations

Strings are limited to ISO-8601 strings only. Other string Date/Time formats aren't guaranteed to work (and probably won't).

Thanks

Thanks to picardy for chai-moment, which was the basis for this.