BLOGサブスレッドの日常

2021.11.21

Google日本語入力で日付入力 by.Deno

s.kono

むおおおお!
以前とまた同じネタです。

Google日本語入力用の日付辞書は便利

過去に Google日本語入力で日付入力 という記事を書きました。

このプログラムで出力した辞書を使うと「0101」→「2021年11月1日(月)」のような変換を一瞬でできます。

この辞書、まあ便利で。
毎年のように作っては入れ替えをしています。

で、久々に自分のブログ記事を懐かしい思いで見てみたところ、シュッっと動かすが面倒でした。
nodeだとごちゃつくのもあり、過去に弊社代表がPythonでリライトしたことも。
(参考: Google日本語入力で日付入力 by.Python )

Denoで動くようにしてみた

セットアップはできるだけ楽にしてシュッと使いたい。
それなら、Denoを使えば、ランタイムさえインストールしてあればローカルにファイル作るのすら不要なのでは?
って思えてきたので、Denoで動くように書き直してみました。

import { format } from "https://deno.land/std@0.115.1/datetime/mod.ts";

const main = function() {
    // expect format: "yyyy-MM-dd"
    const datetimeArg = Deno.args[0]

    // set period
    const from = new Date(datetimeArg)
    if (isNaN(from.getTime())) {
        console.error(`The specified time is invalid.`)
        return
    }
    const until = new Date(from)
    until.setFullYear(from.getFullYear() + 1);

    // display result
    const currentDate = new Date(from)
    const weeks = ['日', '月', '火', '水', '木', '金', '土']
    do {
        const read = format(currentDate, 'MMdd')
        console.log(`${read}\t${format(currentDate, 'yyyy年M月d日')}(${weeks[currentDate.getDay()]})\t名詞\t`)
        console.log(`${read}\t${format(currentDate, 'M月d日')}(${weeks[currentDate.getDay()]})\t名詞\t`)

        // increment day
        currentDate.setDate(currentDate.getDate() + 1)
    } while (currentDate.getTime() < until.getTime())
}

main();

実行

上記プログラムは私のGitHubのgistに保存しておきました。
なので、↓をターミナルに入力するだけで、すぐに辞書を作成できます。

## denoのインストール(macOS + Homebrewの場合)
brew install deno

## 辞書作成
deno run https://gist.githubusercontent.com/esperia-subthread/d8280a3a9fc276fa81952b05d11ddd75/raw/5cc17d686b634379d5e4b7e5332080dd10d91d6b/index.ts \
    2021-08-01 > dict202108.txt

コマンドライン引数として指定している日付(上記例だと 2022-08-01 の部分)を変更すると、その日付から一年間の辞書を作成できます。

さすがDeno。"web browser for command-line scripts"と呼ぶだけのことはありますね!

(2021-11-23 年なしの表記がなかったので更新)

この記事を書いた人

s.kono