chai-luxon
指定された日時およびフォーマット済みの日付文字列を使用したマッチャーを追加するChaiプラグイン。実現にはLuxonを使用。
使用
また、テストも参照してください。
ブラウザ側
chai と luxon の後に chai luxon を含める
<script src="luxon.js"></script>
<script src="chai.js"></script>
<script src="chai-luxon.js"></script>
サーバ側
chai に chai-luxon を使用させる
var chai = require('chai');
chai.use(require('chai-luxon'));
アサーション
Luxon と互換性のある任意の日付/文字列/その他を相互に比較する。オプションで詳細レベルを指定できる。
詳細レベルを使用する場合は、次のいずれかを使用してください。 year
、month
、week
、day
、hour
、minute
、second
。Tdd スタイルのアサーションを使用する場合に、これらの詳細レベルを使用しなかった場合は、引数はカスタムエラーメッセージとして解釈されます。
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');
日付部分のみ
このライブラリには、DateTime 値の日付部分のみを比較するための便利なメソッドが含まれています。これらの便利なメソッドは、上記のマッチャーで、「day」の詳細レベルを使用するためのエイリアスです。同じ日付形式(オブジェクト、JS Date、文字列など)がすべて、同じようにサポートされています。
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
制約事項
文字列は、ISO-8601 文字列のみに限定されています。その他の日付/時刻文字列形式は正常に機能する保証はありません(おそらく機能しません)。
感謝
このライブラリを作成する土台となった、picardy による chai-moment に感謝します。