Skip to content

Commit

Permalink
Merge pull request #12 from SaekiTominaga/wareki
Browse files Browse the repository at this point in the history
和暦取得プログラムを作成
  • Loading branch information
SaekiTominaga committed Apr 13, 2024
2 parents 41965d9 + d9f0509 commit cc489a7
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
},
"overrides": [
{
"files": ["packages/console-locale-timestamp/src/*.ts"],
"files": ["packages/console-locale-timestamp/src/ConsoleLocaleTimestamp.ts"],
"rules": {
"class-methods-use-this": "off",
"no-console": "off"
}
},
{
"files": ["packages/html-escape/__tests__/*.test.js", "packages/html-escape/src/*.ts"],
"files": ["packages/html-escape/__tests__/HtmlEscape.test.js", "packages/html-escape/src/HtmlEscape.ts"],
"rules": {
"no-underscore-dangle": [
"error",
Expand All @@ -27,6 +27,12 @@
}
]
}
},
{
"files": ["packages/wareki/src/Wareki.ts"],
"rules": {
"class-methods-use-this": "off"
}
}
]
}
26 changes: 26 additions & 0 deletions .github/workflows/wareki-lint.yml
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'
30 changes: 30 additions & 0 deletions .github/workflows/wareki-test.yml
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 }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- [Parsing a PA-API 5.0 Item Image URL](packages/paapi-item-image-url-parser)
- [Converts newlines in a string, `trim()`, half-width / full-width conversion, etc](packages/string-convert)
- [Support characters other than "&" as URL query separator](packages/urlsearchparams-custom-separator)
- [Obtain Japanese calendar from Western calendar data(西暦データから和暦を取得)](packages/wareki)
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions packages/wareki/README.md
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&lt;FormatOption&gt;): string | undefined</code></dt>
<dd>和暦年の文字列を取得する</dd>
<dt><code>getYearParts(options?: Readonly&lt;FormatOption&gt;): 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` オプションと同等)
}
```

0 comments on commit cc489a7

Please sign in to comment.