-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from SaekiTominaga/wareki
和暦取得プログラムを作成
- Loading branch information
Showing
10 changed files
with
547 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: wareki-lint | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- 'packages/wareki/__tests__/*.test.js' | ||
- 'packages/wareki/src/*.ts' | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'packages/wareki/__tests__/*.test.js' | ||
- 'packages/wareki/src/*.ts' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
- run: npm ci | ||
- run: npm run lint -w '@w0s/wareki' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: wareki-test | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- 'packages/wareki/__tests__/*.test.js' | ||
- 'packages/wareki/src/*.ts' | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'packages/wareki/__tests__/*.test.js' | ||
- 'packages/wareki/src/*.ts' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
- run: npm ci | ||
- run: npm test -w '@w0s/wareki' | ||
|
||
- uses: coverallsapp/github-action@v2 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 西暦データから和暦を取得 | ||
|
||
[![npm version](https://badge.fury.io/js/%40w0s%2Fwareki.svg)](https://www.npmjs.com/package/@w0s/wareki) | ||
[![test status](https://github.com/SaekiTominaga/npm/actions/workflows/wareki-test.yml/badge.svg)](https://github.com/SaekiTominaga/npm/actions/workflows/wareki-test.yml) | ||
|
||
西暦データから和暦を取得します。正確な日付の場合はもちろん、「2000年1月」など曖昧なデータの場合も極力特定を試みます。 | ||
|
||
内部的には [`Intl.DateTimeFormat`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) を使用しており、将来的に新しい元号に切り替わっても(ブラウザや Node.js のアップデートにより)自動的に対応されます。また「明治」より前の元号(「慶応」以前)にも対応しています。 | ||
|
||
## 例 | ||
|
||
```JavaScript | ||
import Wareki from '@w0s/wareki'; | ||
|
||
const wareki1 = new Wareki(new Date(2000, 0, 1)); | ||
wareki1.getYear(); // 平成12 | ||
wareki1.getYearParts(); // [ { type: 'era', value: '平成' }, { type: 'year', value: '12' }, { type: 'literal', value: '年' } ] | ||
|
||
const wareki2 = new Wareki('2000'); // 年月または年のみのデータでも極力特定を試みます | ||
wareki2.getYear({ era: 'narrow' }); // H12 | ||
wareki3.getYearParts({ era: 'narrow' }); // [ { type: 'era', value: 'H' }, { type: 'year', value: '12' }, { type: 'literal', value: '年' } ] | ||
|
||
const wareki3 = new Wareki('1989-01'); // 1989年1月は昭和64年と平成元年の両方が考えられるため、特定が不可能です | ||
wareki3.getYear(); // undefined | ||
wareki3.getYearParts(); // undefined | ||
|
||
try { | ||
new Wareki('2000/01/01'); // 指定外のフォーマットは `Error` がスローされます | ||
} catch { | ||
} | ||
``` | ||
|
||
## コンストラクター | ||
|
||
```TypeScript | ||
new Wareki(date: Date | string) | ||
``` | ||
|
||
### パラメーター | ||
|
||
<dl> | ||
<dt><code>date</code> [必須]</dt> | ||
<dd>西暦による日付データ</dd> | ||
</dl> | ||
|
||
### 日付データの注意点 | ||
|
||
- 第一引数の日付データは `Date` オブジェクトまたは `String` 型で指定することができます。ごくわずかですが `Date` オブジェクトの方が処理効率は高いです。 | ||
- `String` 型の場合、指定できるフォーマットは YYYY-MM-DD, YYYY-MM, YYYY のいずれかとなります。 | ||
- `String` 型で指定外のフォーマットを指定した場合は `Error` がスローされます。 | ||
|
||
## メソッド | ||
|
||
<dl> | ||
<dt><code>getYear(options?: Readonly<FormatOption>): string | undefined</code></dt> | ||
<dd>和暦年の文字列を取得する</dd> | ||
<dt><code>getYearParts(options?: Readonly<FormatOption>): Intl.DateTimeFormatPart[] | undefined</code></dt> | ||
<dd>和暦年をパートごとに分解されたデータを取得する(<a href="https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts"><code>Intl.DateTimeFormat.prototype.formatToParts()</code></a> の返値をそのまま利用)</dd> | ||
</dl> | ||
|
||
### Option | ||
|
||
```TypeScript | ||
interface FormatOption { | ||
era?: 'long' | 'short' | 'narrow'; // 元号の表現方法(`Intl.DateTimeFormat()` コンストラクターの `era` オプションと同等) | ||
} | ||
``` |
Oops, something went wrong.