From 67f393e1e0a6d25a3c8612e2163af6224d61895d Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:02:07 +0000 Subject: [PATCH 1/9] arr.splice --- CHANGELOG.md | 1 + docs/primitive-props.md | 8 +++++ src/interpreter/primitive-props.ts | 16 ++++++++++ test/index.ts | 48 ++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89464f88..5df721e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `Date:millisecond`を追加 - `arr.fill`, `arr.repeat`, `Arr:create`を追加 - JavaScriptのように分割代入ができるように(現段階では機能は最小限) +- `arr.splice`を追加 # 0.17.0 - `package.json`を修正 diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 0c9c2919..b0c98249 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -184,6 +184,14 @@ _fromIndex_ および _toIndex_ に関する挙動は`arr.slice`に準拠しま `arr.copy`同様シャローコピーであり、配列やオブジェクトの参照は維持されます。 _times_ には0以上の整数値を指定します。それ以外ではエラーになります。 +### @(_v_: arr).splice(_index_: num, _remove_count_: num, _items_: arr\): arr\ +**【この操作は配列を書き換えます】** +配列の _index_ から _remove_count_ 個の要素を取り除き、_items_ の要素を挿入します。 +返り値は取り除いた要素の配列を返します。\ +_index_ が負の場合、末尾から数えます。_index_ が最後の要素より後の場合、要素を取り除きません。\ +_remove_count_ を省略した場合、末尾まで取り除きます。\ +_items_ を省略した場合、何も挿入しません。 + ## エラー型 ### #(_v_: error).name 型: `str` diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index 911313ac..5d05794d 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -279,6 +279,22 @@ const PRIMITIVE_PROPS: { throw e; } }), + + splice: (target: VArr): VFn => FN_NATIVE(async ([idx, rc, vs], opts) => { + assertNumber(idx) + const index = (idx.value < -target.value.length) ? 0 + : (idx.value < 0) ? target.value.length + idx.value + : (idx.value >= target.value.length) ? target.value.length + : idx.value; + + const remove_count = (rc != null) ? (assertNumber(rc), rc.value) + : target.value.length - index; + + const items = (vs != null) ? (assertArray(vs), vs.value) : []; + + const result = target.value.splice(index, remove_count, ...items); + return ARR(result); + }), }, error: { diff --git a/test/index.ts b/test/index.ts index 5d4634e9..a2cdf9fa 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2849,6 +2849,54 @@ describe('primitive props', () => { ARR([]), ])); }); + + test.concurrent('splice (full)', async () => { + const res = await exe(` + let arr1 = [0, 1, 2, 3] + let arr2 = arr1.splice(1, 2, [10]) + <: [arr1, arr2] + `); + eq(res, ARR([ + ARR([NUM(0), NUM(10), NUM(3)]), + ARR([NUM(1), NUM(2)]), + ])); + }); + + test.concurrent('splice (negative-index)', async () => { + const res = await exe(` + let arr1 = [0, 1, 2, 3] + let arr2 = arr1.splice(-1, 0, [10, 20]) + <: [arr1, arr2] + `); + eq(res, ARR([ + ARR([NUM(0), NUM(1), NUM(2), NUM(10), NUM(20), NUM(3)]), + ARR([]), + ])); + }); + + test.concurrent('splice (larger-index)', async () => { + const res = await exe(` + let arr1 = [0, 1, 2, 3] + let arr2 = arr1.splice(4, 100, [10, 20]) + <: [arr1, arr2] + `); + eq(res, ARR([ + ARR([NUM(0), NUM(1), NUM(2), NUM(3), NUM(10), NUM(20)]), + ARR([]), + ])); + }); + + test.concurrent('splice (single argument)', async () => { + const res = await exe(` + let arr1 = [0, 1, 2, 3] + let arr2 = arr1.splice(1) + <: [arr1, arr2] + `); + eq(res, ARR([ + ARR([NUM(0)]), + ARR([NUM(1), NUM(2), NUM(3)]), + ])); + }); }); }); From 3761bf1c895ca6fff7a98246991b0517ff12e383 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:07:16 +0000 Subject: [PATCH 2/9] =?UTF-8?q?doc=E4=BF=AE=E6=AD=A3(arr.splice)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/primitive-props.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/primitive-props.md b/docs/primitive-props.md index b0c98249..f6c54bdb 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -186,9 +186,9 @@ _times_ には0以上の整数値を指定します。それ以外ではエラ ### @(_v_: arr).splice(_index_: num, _remove_count_: num, _items_: arr\): arr\ **【この操作は配列を書き換えます】** -配列の _index_ から _remove_count_ 個の要素を取り除き、_items_ の要素を挿入します。 -返り値は取り除いた要素の配列を返します。\ -_index_ が負の場合、末尾から数えます。_index_ が最後の要素より後の場合、要素を取り除きません。\ +配列の _index_ から _remove_count_ 個の要素を取り除き、その位置に _items_ の要素を挿入します。 +返り値として、取り除いた要素の配列を返します。\ +_index_ が負の場合は末尾から数えます。_index_ が最後の要素より後の場合は要素を取り除きません。\ _remove_count_ を省略した場合、末尾まで取り除きます。\ _items_ を省略した場合、何も挿入しません。 From 3bd6c25a2f508cfb082bb7916b339f43d6c6124e Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:16:59 +0000 Subject: [PATCH 3/9] =?UTF-8?q?doc=E4=BF=AE=E6=AD=A3=20(arr.splice)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/primitive-props.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/primitive-props.md b/docs/primitive-props.md index f6c54bdb..345d2296 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -188,7 +188,8 @@ _times_ には0以上の整数値を指定します。それ以外ではエラ **【この操作は配列を書き換えます】** 配列の _index_ から _remove_count_ 個の要素を取り除き、その位置に _items_ の要素を挿入します。 返り値として、取り除いた要素の配列を返します。\ -_index_ が負の場合は末尾から数えます。_index_ が最後の要素より後の場合は要素を取り除きません。\ +_index_ が負の場合は末尾から数えます。\ +_index_ が最後の要素より後の場合は要素を取り除かず、挿入は末尾に追加します。\ _remove_count_ を省略した場合、末尾まで取り除きます。\ _items_ を省略した場合、何も挿入しません。 From a1e98cff0b7c0ef5d6ba536fd411103b46698fc0 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:23:23 +0000 Subject: [PATCH 4/9] fix lint --- src/interpreter/primitive-props.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index 5d05794d..c8af6182 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -281,9 +281,9 @@ const PRIMITIVE_PROPS: { }), splice: (target: VArr): VFn => FN_NATIVE(async ([idx, rc, vs], opts) => { - assertNumber(idx) + assertNumber(idx); const index = (idx.value < -target.value.length) ? 0 - : (idx.value < 0) ? target.value.length + idx.value + : (idx.value < 0) ? target.value.length + idx.value : (idx.value >= target.value.length) ? target.value.length : idx.value; From de79a9540adc87d27123057337333a32d96ee205 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 11 Apr 2024 18:18:11 +0000 Subject: [PATCH 5/9] =?UTF-8?q?doc=E4=BF=AE=E6=AD=A3(arr.splice)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/primitive-props.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 345d2296..a5a20b8d 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -184,7 +184,7 @@ _fromIndex_ および _toIndex_ に関する挙動は`arr.slice`に準拠しま `arr.copy`同様シャローコピーであり、配列やオブジェクトの参照は維持されます。 _times_ には0以上の整数値を指定します。それ以外ではエラーになります。 -### @(_v_: arr).splice(_index_: num, _remove_count_: num, _items_: arr\): arr\ +### @(_v_: arr).splice(_index_: num, _remove_count_?: num, _items_?: arr\): arr\ **【この操作は配列を書き換えます】** 配列の _index_ から _remove_count_ 個の要素を取り除き、その位置に _items_ の要素を挿入します。 返り値として、取り除いた要素の配列を返します。\ From c7a5a046b66a58169b4626e9850ea09061e53e25 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:50:00 +0000 Subject: [PATCH 6/9] fix indent --- src/interpreter/primitive-props.ts | 2 +- test/index.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index 5f3a38d6..524eb2ac 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -294,7 +294,7 @@ const PRIMITIVE_PROPS: { const result = target.value.splice(index, remove_count, ...items); return ARR(result); - }), + }), every: (target: VArr): VFn => FN_NATIVE(async ([fn], opts) => { assertFunction(fn); diff --git a/test/index.ts b/test/index.ts index db7f3cd6..eb9e3310 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2922,10 +2922,10 @@ describe('primitive props', () => { eq(res, ARR([ ARR([NUM(0)]), ARR([NUM(1), NUM(2), NUM(3)]), - ])); - }); + ])); + }); - test.concurrent('every', async () => { + test.concurrent('every', async () => { const res = await exe(` let arr1 = [0, 1, 2, 3] let res1 = arr1.every(@(v,i){v==0 || i > 0}) From 492f6b81695d6391b244c11677e0d8fedffda49a Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:52:00 +0000 Subject: [PATCH 7/9] fix indent --- test/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.ts b/test/index.ts index eb9e3310..d951dfae 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2925,7 +2925,7 @@ describe('primitive props', () => { ])); }); - test.concurrent('every', async () => { + test.concurrent('every', async () => { const res = await exe(` let arr1 = [0, 1, 2, 3] let res1 = arr1.every(@(v,i){v==0 || i > 0}) From c48c3ed27daf9d362420b5e714dad72a84354266 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:59:24 +0000 Subject: [PATCH 8/9] fix CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a654335e..b52626fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ [Read translated version (en)](./translations/en/CHANGELOG.md) # 未リリース分 +- `arr.splice`を追加 # 0.18.0 - `Core:abort`でプログラムを緊急停止できるように @@ -14,7 +15,6 @@ - ネストされた名前空間下の変数を参照できるように - `arr.every`, `arr.some`を追加 - `Date:to_iso_str`を追加 -- `arr.splice`を追加 # 0.17.0 - `package.json`を修正 From 324fdddae2de004afe2bd52f597f7294b74fd758 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Mon, 13 May 2024 01:06:52 +0900 Subject: [PATCH 9/9] remove spaces index.ts --- test/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.ts b/test/index.ts index bbadfa16..18e215b9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -3105,7 +3105,7 @@ describe('primitive props', () => { ]), ])); }); - + test.concurrent('every', async () => { const res = await exe(` let arr1 = [0, 1, 2, 3]