不可变数组原型方法的集合(每个方法包)

您是否曾经考虑过Array原型方法的不可变版本?

例如)Array.prototype.push返回一个新的Array。

不可变数组原型代表了这一点。

azu /不可变数组原型

immutable-array-prototype –不可变数组原型方法的集合(每个方法包)。

github.com

为什么?

ECMAScript Array有一些可变方法。
提供每种方法的不变版本。

可变与不可变

@immutable-array/prototype包含这些方法的不可变版本。

 npm install @immutable-array/prototype 

或者,如果要使用单个方法,则可以将一个方法作为程序包使用。

 npm install @immutable-array/pop npm install @immutable-array/push npm install @immutable-array/shift npm install @immutable-array/unshift npm install @immutable-array/sort npm install @immutable-array/reverse npm install @immutable-array/fill npm install @immutable-array/splice npm install @immutable-array/copy-within 

用法很简单。 该库提供相同的API和行为,而无需第一个参数。

  导入{ 
      分类, 
      保持不变 
      推, 
      填, 
      拼接, 
      流行 
      相反, 
      copyWithin, 
      转移 
  }来自“ @ immutable-array / prototype”; 
  describe(“ prototype”,()=> { 
      it(“ shift”,()=> { 
          assert.deepStrictEqual(shift([“ a”,“ b”,“ c”,“ d”,“ e”])),[“ b”,“ c”,“ d”,“ e”])); 
      }); 
      it(“ unshift”,()=> { 
          assert.deepStrictEqual(unshift([[“ a”,“ b”,“ c”,“ d”,“ e”],“ x”),[ 
              “X”, 
              “一种”, 
              “ b”, 
              “C”, 
              “ d”, 
              “ e” 
          ]); 
      }); 
      it(“ pop”,()=> { 
          assert.deepStrictEqual(pop([“ a”,“ b”,“ c”,“ d”,“ e”]),[“ a”,“ b”,“ c”,“ d”])); 
      }); 
      it(“ push”,()=> { 
          assert.deepStrictEqual(push([“ a”,“ b”,“ c”,“ d”,“ e”],“ x”),[ 
              “一种”, 
              “ b”, 
              “C”, 
              “ d”, 
              “ e”, 
              “X” 
          ]); 
      }); 
      it(“ splice”,()=> { 
          assert.deepStrictEqual(splice([“ a”,“ b”,“ c”,“ d”,“ e”],0,1,“ x”),[ 
              “X”, 
              “ b”, 
              “C”, 
              “ d”, 
              “ e” 
          ]); 
      }); 
      it(“ sort”,()=> { 
          assert.deepStrictEqual(sort([“ e”,“ a”,“ c”,“ b”,“ d”]),[“ a”,“ b”,“ c”,“ d”,“ e”] ); 
      }); 
      it(“ reverse”,()=> { 
          assert.deepStrictEqual(reverse([“ a”,“ b”,“ c”,“ d”,“ e”])),[“ e”,“ d”,“ c”,“ b”,“ a”] ); 
      }); 
      it(“ fill”,()=> { 
          assert.deepStrictEqual(fill(new Array(5),“ x”),[“ x”,“ x”,“ x”,“ x”,“ x”])); 
      }); 
      it(“ copyWithin”,()=> { 
          assert.deepStrictEqual(copyWithin([“ a”,“ b”,“ c”,“ d”,“ e”],0,3,4),[“ d”,“ b”,“ c”,“ d “,” e“]); 
      }); 
  }); 

拉取请求和星总是欢迎的。

https://github.com/azu/immutable-array-prototype