Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date:parseの失敗時にエラー型の値を返すように #492

Closed
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 実行時エラーの発生位置が表示されるように。
- **Breaking Change** パースの都合によりmatch文の構文を変更。パターンの前に`case`キーワードが必要となり、`*`は`default`に変更。
- **Breaking Change** 多くの予約語を追加。これまで変数名等に使えていた名前に影響が出る可能性があります。
- `Date:parse`がパース失敗時にエラー型の値を返すように
- **Breaking Change** 配列及び関数の引数において、空白区切りが使用できなくなりました。`,`または改行が必要です。
- **Breaking Change** 関数同士の比較の実装
- **Breaking Change** `+`や`!`などの演算子の優先順位に変更があります。新しい順序は[syntax.md](docs/syntax.md#%E6%BC%94%E7%AE%97%E5%AD%90)を参照して下さい。
Expand Down
4 changes: 4 additions & 0 deletions docs/std.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ _date_ を渡した場合、_date_に対応するミリ秒、
渡していない場合は現在時刻のミリ秒が返されます。

### @Date:parse(_date_: str): num
日付として解釈可能な文字列から日時を表す数値を生成します。
解釈は[JavaScriptのDateコンストラクター](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)に依存します。
引数が日付として解釈可能でない場合、エラー型の値(`name`=`'not_date'`)を返します。


### @Date:to_iso_str(_date_?: num, _time_offset_?: num): str
_date_ を拡張表記のISO形式にした文字列を返します。
Expand Down
4 changes: 3 additions & 1 deletion src/interpreter/lib/std.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ export const std: Record<string, Value> = {

'Date:parse': FN_NATIVE(([v]) => {
assertString(v);
return NUM(new Date(v.value).getTime());
const res = new Date(v.value).getTime();
// NaN doesn't equal to itself
return (res === res) ? NUM(res) : ERROR('not_date');
}),

'Date:to_iso_str': FN_NATIVE(([v, ofs]) => {
Expand Down
18 changes: 18 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3328,6 +3328,24 @@ describe('std', () => {
});
});

describe('Date', () => {
test.concurrent('parse', async () => {
eq(await exe(`<: [
'01 Jan 1970 00:00:00 GMT'
'1970-01-01'
'1970-01-01T00:00:00.000Z'
'1970-01-01T00:00:00.000+00:00'
'hoge'
].map(Date:parse)`), ARR([
NUM(0),
NUM(0),
NUM(0),
NUM(0),
ERROR('not_date')
]));
});
});

describe('Math', () => {
test.concurrent('trig', async () => {
eq(await exe("<: Math:sin(Math:PI / 2)"), NUM(1));
Expand Down
Loading