Skip to content

Commit

Permalink
arr.removeを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
salano-ym committed May 9, 2024
1 parent 86fe815 commit f6993b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 未リリース分
- `Date:year`系の関数に0を渡すと現在時刻になる問題を修正
- シンタックスエラーなどの位置情報を修正
- `arr.insert`を追加
- `arr.insert`,`arr.remove`を追加

# 0.18.0
- `Core:abort`でプログラムを緊急停止できるように
Expand Down
6 changes: 6 additions & 0 deletions docs/primitive-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ _times_ には0以上の整数値を指定します。それ以外ではエラ
_index_ が負の場合は末尾から数えます。\
_index_ が最後の要素より後の場合は末尾に追加します。

### @(_v_: arr).remove(_index_: num): value | null
**【この操作は配列を書き換えます】**
配列から _index_ の位置の要素を取り除き、その要素を返します。\
_index_ が負の場合は末尾から数えます。\
_index_ が最後の要素より後の場合は取り除かず、`null`を返します。

### @(_v_: arr).every(_func_: @(_item_: value, _index_: num) { bool }): bool
配列の全ての要素に対して _func_ が true を返す時のみ true 返します。空配列には常に true を返します。

Expand Down
8 changes: 8 additions & 0 deletions src/interpreter/primitive-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ const PRIMITIVE_PROPS: {

return NULL;
}),

remove: (target: VArr): VFn => FN_NATIVE(async ([index], opts) => {
assertNumber(index);

const removed = target.value.splice(index.value, 1);

return removed[0] ?? NULL;
}),
},

error: {
Expand Down
19 changes: 19 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2939,6 +2939,25 @@ describe('primitive props', () => {
ARR([NUM(30), NUM(0), NUM(1), NUM(50), NUM(20), NUM(2), NUM(40), NUM(10), NUM(60)])
]));
});

test.concurrent('remove', async () => {
const res = await exe(`
let arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let res = []
res.push(arr1.remove(9)) // 9 [0, 1, 2, 3, 4, 5, 6, 7, 8]
res.push(arr1.remove(3)) // 3 [0, 1, 2, 4, 5, 6, 7, 8]
res.push(arr1.remove(0)) // 0 [1, 2, 4, 5, 6, 7, 8]
res.push(arr1.remove(-1)) // 8 [1, 2, 4, 5, 6, 7]
res.push(arr1.remove(-5)) // 2 [1, 4, 5, 6, 7]
res.push(arr1.remove(100)) // null [1, 4, 5, 6, 7]
res.push(arr1)
<: res
`);
eq(res, ARR([
NUM(9), NUM(3), NUM(0), NUM(8), NUM(2), NULL,
ARR([NUM(1), NUM(4), NUM(5), NUM(6), NUM(7)])
]));
});
});
});

Expand Down

0 comments on commit f6993b2

Please sign in to comment.