diff --git a/changelog/next.md b/changelog/next.md index 1018098..b17d553 100644 --- a/changelog/next.md +++ b/changelog/next.md @@ -48,12 +48,17 @@ + Add a new method of `Array` namespace: + `Array.LastIndexOf` -### 2025-07--09 (feature/array-methods → main) +### 2025-07-09 (feature/array-methods → main) + Add two new methods of `Array` namespace: + `Array.Reverse` + `Array.Shift` +### 2025-07-10 (feature/array-methods → main) + ++ Add a new method of `Array` namespace: + + `Array.Unshift` + ## ⚡ Improvements ## 🐦‍🔥 No longer broken \ No newline at end of file diff --git a/lib/Array/index.d.ts b/lib/Array/index.d.ts index ca46c08..940d652 100644 --- a/lib/Array/index.d.ts +++ b/lib/Array/index.d.ts @@ -411,6 +411,20 @@ export type Shift< ? ShiftElement : never; +/** + * This method is like `Array.prototype.unshift`, it returns the new array that + * has been unshifted (which means pushing at the first) the new element. + * + * @param Arr The array to be unshifted. + * @param E The new element to be unshifted into the `Arr`. + * @returns The new array. + * + * @example + * type Unshift1 = Array.Unshift<[1, 2, 3], "1">; // ["1", 1, 2, 3] + * type Unshift2 = Array.Unshift<[], 1>; // [1] + */ +export type Unshift = [E, ...Arr]; + } export default Array; \ No newline at end of file diff --git a/test/lib-Array.test.ts b/test/lib-Array.test.ts index ff9f391..c5e1358 100644 --- a/test/lib-Array.test.ts +++ b/test/lib-Array.test.ts @@ -17,6 +17,17 @@ type CaseLibArray = [ Expect, []>>, Expect, []>>, + // Array.Unshift + Expect, [4,1,2,3]>>, + Expect, [[]]>>, + Expect, [[],1]>>, + Expect, [1]>>, + Expect, [{c:3},{a:1},{b:2}]>>, + Expect, [[5,6],[1,2],[3,4]]>>, + Expect, [4|5,1|2,3]>>, + Expect, [{a:string,b:number}]>>, + Expect, [{y:boolean},{x:number}]>>, + // Array.MultipleConcat Expect, [1,2,3,4,2,3,undefined,true]>>, Expect, [1,2]>>, @@ -51,6 +62,13 @@ type CaseLibArray = [ Expect, -1>>, Expect, 0>>, + // Array.Shift + Expect, [2,3]>>, + Expect, []>>, + Expect, never>>, + Expect, 1>>, + Expect, "1">>, + // Array.IsFlatten Expect, true>>, Expect, true>>, diff --git a/test/script/lib-gen.sh b/test/script/lib-gen.sh index ff664ce..530970b 100644 --- a/test/script/lib-gen.sh +++ b/test/script/lib-gen.sh @@ -36,6 +36,8 @@ declare -A test_cases=( ["Array.Join"]="[1,2,3]∷\"1,2,3\" [-1,\".\",9]‖\"-\"∷\"-1-.-9\" [1,2,3]‖\"\"∷\"123\"" ["Array.Pop"]="[1,2,3]∷[1,2] [1]‖\"get-rest\"∷[] []‖\"get-rest\"∷never [1,2,3]‖\"get-pop-element\"∷3 [1]‖\"get-pop-element\"∷1 []‖\"get-pop-element\"∷never" ["Array.Push"]="[1,2,3]‖\"1\"∷[1,2,3,\"1\"] []‖1∷[1] []‖[]∷[[]]" + ["Array.Shift"]="[1,2,3]∷[2,3] [\"1\"]∷[] []‖\"get-rest\"∷never [1,2,3]‖\"get-shift-element\"∷1 [\"1\"]‖\"get-shift-element\"∷\"1\"" + ["Array.Unshift"]="[1,2,3]‖4∷[4,1,2,3] []‖[]∷[[]] [1]‖[]∷[[],1] []‖1∷[1] [{a:1},{b:2}]‖{c:3}∷[{c:3},{a:1},{b:2}] [[1,2],[3,4]]‖[5,6]∷[[5,6],[1,2],[3,4]] [1|2,3]‖4|5∷[4|5,1|2,3] []‖{a:string,b:number}∷[{a:string,b:number}] [{x:number}]‖{y:boolean}∷[{y:boolean},{x:number}]" ["BigInteger.ToString"]="2n∷\"2\" -2n∷\"-2\" 0n∷\"0\"" ["BigInteger.IsNegative"]="-23n∷true 26n∷false 0n∷false"