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-iso8601

v1.0.0

Published

Chai assertions to check dates in the ISO-8601 format.

Downloads

3,580

Readme

chai-iso8601

Chai assertion to check dates in the ISO-8601 format.

npm version Dependency Status Build Status Coverage Status License

Developed at the Media Engineering Institute (HEIG-VD).

Installation

$> npm install --save-dev chai-iso8601

Usage

Basic usage allows you to check that a string is a valid ISO-8601 date and represents the expected time.

const chai = require('chai');

// Note the extra call (chai-iso8601 returns a factory function).
chai.use(require('chai-iso8601')());

// Simple usage
const expectedDateString = '2001-01-01T00:00:00Z';
chai.expect('2001-01-01T00:00:00Z').to.be.iso8601(expectedDateString);

// With a date object
const expectedDate = new Date(2001, 0, 1, 0, 0, 0, 0);
chai.expect('2001-01-01T00:00:00Z').to.be.iso8601(expectedDate);

// With a moment object
const expectedMoment = require('moment')('2001-01-01T00:00:00Z');
chai.expect('2001-01-01T00:00:00Z').to.be.iso8601(expectedMoment);

Operators

chai-iso8601 includes operators to perform more complex assertions on dates. All the following assertions will pass:

// Check that a string is an ISO-8601 date that is equal to another date (this is the default operator used above).
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('eq', '2000-01-01T00:00:00Z');

// Check that a string is an ISO-8601 date that is after another date (and not the same).
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:00Z');

// Check that a string is an ISO-8601 date that is after or the same as another date.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gte', '2000-01-01T00:00:00Z');
chai.expect('2001-01-01T00:00:00Z').to.be.iso8601('gte', '2000-01-01T00:00:00Z');

// Check that a string is an ISO-8601 date that is before another date (and not the same).
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('lt', '2001-01-01T00:00:00Z');

// Check that a string is an ISO-8601 date that is before or the same as another date.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('lte', '2000-01-01T00:00:00Z');
chai.expect('1999-01-01T00:00:00Z').to.be.iso8601('lte', '2000-01-01T00:00:00Z');

Margin of error

You may pass an additional number representing a margin of error that the date must be within. This can be useful in tests if you know the approximate value of a date but not its exact value.

Using a margin changes the behavior of the operators. All the following assertions will pass:

// Check that a string is an ISO-8601 date that is equal to another date with a margin of one second.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('eq', '2000-01-01T00:00:00Z', 1000);
chai.expect('1999-12-31T23:59:59Z').to.be.iso8601('eq', '2000-01-01T00:00:00Z', 1000);
chai.expect('2000-01-01T00:00:01Z').to.be.iso8601('eq', '2000-01-01T00:00:00Z', 1000);

// Check that a string is an ISO-8601 date that is after another date but no more than one second.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z', 1000);
chai.expect('1999-12-31T23:59:59.666Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z', 1000);

// Check that a string is an ISO-8601 date that is after or the same as another date but no more than one second.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gte', '2000-01-01T00:00:00Z', 1000);
chai.expect('2000-01-01T00:00:01Z').to.be.iso8601('gte', '2000-01-01T00:00:00Z', 1000);

// Check that a string is an ISO-8601 date that is before another date but no more than one second.
chai.expect('1999-12-31T29:59:59Z').to.be.iso8601('lt', '2000-01-01T00:00:00Z');
chai.expect('1999-12-31T29:59:59.666Z').to.be.iso8601('lt', '2000-01-01T00:00:00Z');

// Check that a string is an ISO-8601 date that is before or the same as another date but no more than one second.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('lte', '2000-01-01T00:00:00Z', 1000);
chai.expect('1999-12-31T23:59:59.666Z').to.be.iso8601('lte', '2000-01-01T00:00:00Z', 1000);

Note that the following assertions WILL NOT PASS although they would without a margin of error:

// The date is after the specified one, but it exceeds the margin of error of 500 milliseconds.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z', 500);
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gte', '1999-12-31T23:59:59Z', 500);

// The date is before the specified one, but it exceeds the margin of error of 500 milliseconds.
chai.expect('1999-12-31T29:59:59Z').to.be.iso8601('lt', '2000-01-01T00:00:00Z', 500);
chai.expect('1999-12-31T29:59:59Z').to.be.iso8601('lte', '2000-01-01T00:00:00Z', 500);

If you are using a margin of error in your tests, you might want to make it mandatory so that it throws an error if you forget to specify one.

const chai = require('chai');

// Note the extra call (chai-iso8601 returns a factory function).
chai.use(require('chai-iso8601')({
  marginRequired: true
}));

// Error thrown! (margin is missing)
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z');

// No error thrown
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z', 1000);

// Use a margin of zero if you don't need a margin for that particular test.
chai.expect('2000-01-01T00:00:00Z').to.be.iso8601('gt', '1999-12-31T23:59:59Z', 0);