chai-luxon

一個 Chai 外掛,為由 Luxon 驅動的日期和格式化日期字串新增匹配器

NPM Version NPM License NPM Downloads
NPM

使用方式

另請參閱 測試

瀏覽器端

在 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 相容的日期/字串/任何值與另一個,可選擇設定精細度。

當使用精細度時,請使用以下其中之一:yearmonthweekdayhourminutesecond。當使用 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,這是此專案的基礎。