Skip to content

Commit

Permalink
chore: fix types and lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
3846masa committed May 1, 2024
1 parent 4a44e06 commit 09a23f3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
11 changes: 7 additions & 4 deletions examples/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ const now = new Date();

// With function
{
/** @param {import('lit-date').DateProxy} param */
const monthName = ({ month }) =>
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][month - 1];
/** @param {import('lit-date').DateProxy} param */
const dayWithSuffix = ({ day }) => {
if (Math.floor(day / 10) !== 1) {
switch (day % 10) {
case 1:
return `${day}st`;
return `${day.toString(10)}st`;
case 2:
return `${day}nd`;
return `${day.toString(10)}nd`;
case 3:
return `${day}rd`;
return `${day.toString(10)}rd`;
}
}
return `${day}th`;
return `${day.toString(10)}th`;
};
const format = litdate`${monthName} ${dayWithSuffix}, ${'YYYY'}`;
console.log(format(now));
}

// Intl
{
/** @param {import('lit-date').DateProxy} param */
const dayOfWeekName = ({ dayOfWeek }) => ['日', '月', '火', '水', '木', '金', '土'][dayOfWeek];
const format = litdate`${'M'}${'D'}${dayOfWeekName}曜日`;
console.log(format(now));
Expand Down
2 changes: 1 addition & 1 deletion src/DateProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pad = (num: number, count = 2) => `${num}`.padStart(count, '0');
const pad = (num: number, count = 2) => num.toString(10).padStart(count, '0');

class DateProxy {
private _date: Date;
Expand Down
37 changes: 18 additions & 19 deletions src/__tests__/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { expect, test } from '@jest/globals';
import { afterEach, expect, jest, test } from '@jest/globals';

import type { DateProxy } from '../';
import fdate from '../';

afterEach(() => {
jest.restoreAllMocks();
});

test('year', () => {
const date = new Date('2019-05-07T00:00:00.000');
expect(fdate`${'year'}`(date)).toBe('2019');
Expand Down Expand Up @@ -100,30 +104,25 @@ test('milliSecond', () => {
});

test('TimeZone', () => {
const getTimezoneOffset = Date.prototype.getTimezoneOffset;
try {
const date = new Date();
Date.prototype.getTimezoneOffset = () => -540;
expect(fdate`${'Z'}`(date)).toBe('+09:00');
expect(fdate`${'ZZ'}`(date)).toBe('+0900');

Date.prototype.getTimezoneOffset = () => +660;
expect(fdate`${'Z'}`(date)).toBe('-11:00');
expect(fdate`${'ZZ'}`(date)).toBe('-1100');

Date.prototype.getTimezoneOffset = () => 0;
expect(fdate`${'Z'}`(date)).toBe('Z');
expect(fdate`${'ZZ'}`(date)).toBe('Z');
} finally {
Date.prototype.getTimezoneOffset = getTimezoneOffset;
}
const date = new Date();
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-540);
expect(fdate`${'Z'}`(date)).toBe('+09:00');
expect(fdate`${'ZZ'}`(date)).toBe('+0900');

jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(+660);
expect(fdate`${'Z'}`(date)).toBe('-11:00');
expect(fdate`${'ZZ'}`(date)).toBe('-1100');

jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(0);
expect(fdate`${'Z'}`(date)).toBe('Z');
expect(fdate`${'ZZ'}`(date)).toBe('Z');
});

test('i18n', () => {
const date = new Date('2019-02-25T00:00:00.000');
const era = ({ year }: DateProxy) => {
const eraYear = year - 1988;
return eraYear === 1 ? '平成元年' : `平成${year - 1988}`;
return eraYear === 1 ? '平成元年' : `平成${(year - 1988).toString(10)}`;
};
const dayOfWeek = ({ dayOfWeek }: DateProxy) => '日月火水木金土'[dayOfWeek];
expect(fdate`${era}${'M'}${'D'}日(${dayOfWeek})`(date)).toBe('平成31年2月25日(月)');
Expand Down
2 changes: 2 additions & 0 deletions src/string-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const loosePolyfill = (callSite: TemplateStringsArray, ...substitutions: any[])
const raw = callSite.raw;
const length = raw.length - 1;
for (let idx = 0; idx < length; idx++) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
t += raw[idx] + substitutions[idx];
}
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
return t + raw[length];
};

Expand Down

0 comments on commit 09a23f3

Please sign in to comment.