Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
+ `Array.Pop<Arr, Mode>`
+ `Array.Push<Arr, Element>`

### 2025-07-01 (feature/array-methods → main)

+ Add a new method of `Array` namespace:
+ `Array.LastIndexOf<Arr, T>`

## ⚡ Improvements

## 🐦‍🔥 No longer broken
25 changes: 25 additions & 0 deletions lib/Array/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ export type IndexOf<
: IndexOf<Rest, T, [...Count, 0]>
: -1;

/**
* This method returns the last encounter index of element `T` in array `Arr`.
*
* @param Arr The array to be checked.
* @param T The element to check the last encounter index.
* @param Count Middle variable to store the scanning state.
* @returns The last encounter index of element `T` in array `Arr`.
*
* @example
* type LastIndex1 = Array.LastIndexOf<[1, 2, 1, 22, 3], 1>; // 2
* type LastIndex2 = Array.LastIndexOf<[], 1>; // -1
* type LastIndex3 = Array.LastIndexOf<[1], 1>; // 0
*/
export type LastIndexOf<
Arr extends unknown[],
T extends unknown,
Count extends 0[] = CreateArrayFromLength<Arr["length"], 0>
> = Arr extends [...infer Rest, infer L]
? Equal<L, T> extends true
? Integer.Dec<Count["length"]>
: Count extends [...infer RestCount extends 0[], 0]
? LastIndexOf<Rest, T, RestCount>
: -1
: -1;

/**
* This method is like `Array.prototype.join`, it creates and returns a new string by
* concatenating all of the elements in this array, separated by commas or a specified
Expand Down
8 changes: 8 additions & 0 deletions test/lib-Array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ type CaseLibArray = [
Expect<Equal<Array.At<[], -1>, never>>,
Expect<Equal<Array.At<[], 0>, never>>,

// Array.LastIndexOf
Expect<Equal<Array.LastIndexOf<[1,2,1,2,1,3,4], 1>, 4>>,
Expect<Equal<Array.LastIndexOf<[], 1>, -1>>,
Expect<Equal<Array.LastIndexOf<[1,2,3], 0>, -1>>,
Expect<Equal<Array.LastIndexOf<[[],[[]],[],[]], [[]]>, 1>>,
Comment on lines +93 to +96
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Missing tests for edge cases with complex types and repeated non-primitive values.

Add tests for arrays with objects, symbols, functions, and cases like different object references or NaN to ensure correct LastIndexOf behavior with complex types and tricky equality scenarios.

Expect<Equal<Array.LastIndexOf<[()=>{},(a:string)=>{},(b:string)=>{},()=>{}], (c:string)=>{}>, 2>>,
Expect<Equal<Array.LastIndexOf<[{a:string},{},{b:string}], {}>, 1>>,

]
113 changes: 57 additions & 56 deletions test/script/lib-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,61 @@ ROOT_DIR=$(dirname "$(realpath "$0")")/../..
TEST_DIR="${ROOT_DIR}/test"

declare -A test_cases=(
["Integer.IsNegative"]="-2:true 0:false 9:false"
["Integer.IsPositive"]="-2:false 0:false 9:true"
["Integer.IsZero"]="-2:false 0:true 9:false"
["Integer.Opposite"]="-2:2 0:0 20:-20"
["Integer.Lower"]="2|1:false 1|2:true 2|-1:false -1|2:true -3|-2:true -2|-3:false -1|0:true 2|0:false 0|3:true 0|-3:false 0|0:false 2|2:false -2|-2:false"
["Integer.Greater"]="2|1:true 1|2:false 2|-1:true -1|2:false -3|-2:false -2|-3:true -1|0:false 2|0:true 0|3:false 0|-3:true 0|0:false 2|2:false -2|-2:false"
["Integer.Eq"]="2|1:false 1|2:false 2|-1:false -1|2:false -3|-2:false -2|-3:false -1|0:false 2|0:false 0|3:false 0|-3:false 0|0:true 2|2:true -2|-2:true"
["Integer.Neq"]="2|1:true 1|2:true 2|-1:true -1|2:true -3|-2:true -2|-3:true -1|0:true 2|0:true 0|3:true 0|-3:true 0|0:false 2|2:false -2|-2:false"
["Integer.LowerEq"]="2|1:false 1|2:true 2|-1:false -1|2:true -3|-2:true -2|-3:false -1|0:true 2|0:false 0|3:true 0|-3:false 0|0:true 2|2:true -2|-2:true"
["Integer.GreaterEq"]="2|1:true 1|2:false 2|-1:true -1|2:false -3|-2:false -2|-3:true -1|0:false 2|0:true 0|3:false 0|-3:true 0|0:true 2|2:true -2|-2:true"
["Integer.Add"]="2|3:5 -2|2:0 -2|-6:-8 3|-3:0 3|-2:1 -2|1:-1 0|0:0 -2|0:-2 2|0:2 0|20:20 0|-30:-30"
["Integer.Inc"]="2:3 0:1 -1:0 -30:-29"
["Integer.Sub"]="2|3:-1 3|2:1 -2|3:-5 -2|-3:1 -9|-2:-7 2|0:2 -3|0:-3 0|2:-2 0|-3:3 0|0:0 2|2:0 -3|-3:0"
["Integer.Dec"]="2:1 0:-1 1:0 -3:-4"
["Integer.Divide"]="2|3:0 4|2:2 9|-3:-3 9|-2:-4 9|0:never -2|-3:0 -4|2:-2 4|4:1 4|-4:-1 -4|-4:1"
["Integer.Mod"]="2|2:0 3|2:1 -3|-2:1 -3|2:-1 9|5:4"
["Integer.IsOdd"]="1:true 2:false 0:false -3:true -19:true 20:false"
["Integer.IsEven"]="1:false 0:true -22:true 19:false"

["Array.CreateArrayFromLength"]="2:[undefined,undefined] 3|string:[string,string,string] 0:[] -1:[]"
["Array.At"]="[1,2,3]|2:3 [1,2,3,4]|0|:1 []|1:never []|-1:never []|0:never"
["Array.Concat"]="[1,2,3]|[2,3,1]:[1,2,3,2,3,1]"
["Array.MultipleConcat"]="[[1,2,3],[4,2,3],[undefined,true]]:[1,2,3,4,2,3,undefined,true] [[],[1],[],[2]]:[1,2] [[[2]],[1]]:[[2],1]"
["Array.Fill"]="[1,2,3]|\"3\":[\"3\",\"3\",\"3\"] [0,9,1]|\"1\"|1|2:[0,\"1\",1] [\"head\",9,1]|true|1:[\"head\",true,true] [1,2,3]|\"3\"|2|1:[1,2,3] [1,2,3]|\"3\"|-4|2:[\"3\",\"3\",3] [1,2,3]|\"3\"|-2|3:[1,\"3\",\"3\"] [1,2,3]|\"3\"|7|2:[1,2,3] [1,2,3]|\"3\"|1|-1:[1,\"3\",3]"
["Array.IsFlatten"]="[1,2,3]:true []:true [[]]:false [1,[2],3]:false [[],1,2]:false [1,[1,2,[]],undefined]:false"
["Array.Flat"]="[1,2,3]:[1,2,3] [1,2,3,[]]:[1,2,3] [1,2,[3]]:[1,2,3] [1,2,[3,[0]]]:[1,2,3,0] []:[] [[]]:[] [[[1]]]:[1]"
["Array.Includes"]="[1,2,3]|1:true [1,2,3]|-1:false []|\"\":false [\"\"]|\"\":true [[]]|[]:true [[1]]|1:false"
["Array.IndexOf"]="[1,2,3]|1:0 [1,[2],3]|[2]:1 []|0:-1 [1,2,3]|4:-1 [1,2,1]|1:0"
["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] []|[]:[[]]"

["BigInteger.ToString"]="2n:\"2\" -2n:\"-2\" 0n:\"0\""
["BigInteger.IsNegative"]="-23n:true 26n:false 0n:false"
["BigInteger.IsPositive"]="90n:true 0n:false -8n:false"
["BigInteger.IsZero"]="0n:true 29n:false -2n:false"
["BigInteger.Opposite"]="0n:0n 23n:-23n -12n:12n"
["BigInteger.Eq"]="0n|0n:true -12n|-12n:true 23n|22n:false 23n|-23n:false"
["BigInteger.Lower"]="23n|123n:true 23n|23n:false 1234n|1235n:true -123n|90n:true 123n|-90n:false 0n|0n:false -2n|-2n:false 12345n|12345n:false"

["String.Length"]="\"abs\":3 \"\":0 \"2222_2222_2222_2222_2222\":24"
["String.At"]="\"123456789\"|0:\"1\" \"123456789\"|2:\"3\" \"123456789\"|-3:\"7\" \"123456789\"|100:never"
["String.Concat"]="[\"12\",\"ab\"]:\"12ab\" [\"love\"]:\"love\" []:\"\""
["String.SubString"]="\"LoveYouHuaier\":\"LoveYouHuaier\" \"LoveYouHuaier\"|4:\"YouHuaier\" \"LoveYouHuaier\"|4|9:\"YouHu\" \"LoveYouHuaier\"|3|-2:\"eYouHuai\" \"LoveYouHuaier\"|-7|-2:\"uHuai\" \"LoveYouHuaier\"|0|0:\"\" \"LoveYouHuaier\"|-1|-1:\"\" \"LoveYouHuaier\"|8|7:\"\""
["String.EndsWith"]="\"What?\"|\"?\":true \"What?\"|\"What?\":true \"What?\"|\"SoWhat?\":false \"What?\"|\"t?\":true \"What?\"|\"W?\":false \"What?\"|\"\":true \"\"|\"test\":false \"\"|\"\":true \"What?\"|\"?\"|5:true \"What?\"|\"t?\"|3:false \"\"|\"\"|2:true"
["String.Includes"]="\"AshGrey\"|\"Ash\":true \"AshGrey\"|\"A\":true \"AshGrey\"|\"a\":false \"AshGrey\"|\"\":true \"AshGrey\"|\"Ash\"|0:true \"AshGrey\"|\"Ash\"|1:false \"AshGrey\"|\"Ash\"|-1:true"

["Boolean.And"]="false|false:false false|true:false true|false:false true|true:true"
["Boolean.Or"]="false|false:false false|true:true true|false:true true|true:true"
["Boolean.Nor"]="false:true true:false"
["Boolean.Nand"]="false|false:true false|true:true true|false:true true|true:false"
["Boolean.Nor"]="false|false:true false|true:false true|false:false true|true:false"
["Boolean.MultipleAnd"]="[true,true,true]:true [true,false,true]:false [false,false,false]:false"
["Boolean.MultipleOr"]="[true,true,true]:true [true,false,false]:true [false,false,false]:false"
["Integer.IsNegative"]="-2∷true 0∷false 9∷false"
["Integer.IsPositive"]="-2∷false 0∷false 9∷true"
["Integer.IsZero"]="-2∷false 0∷true 9∷false"
["Integer.Opposite"]="-2∷2 0∷0 20∷-20"
["Integer.Lower"]="2‖1∷false 1‖2∷true 2‖-1∷false -1‖2∷true -3‖-2∷true -2‖-3∷false -1‖0∷true 2‖0∷false 0‖3∷true 0‖-3∷false 0‖0∷false 2‖2∷false -2‖-2∷false"
["Integer.Greater"]="2‖1∷true 1‖2∷false 2‖-1∷true -1‖2∷false -3‖-2∷false -2‖-3∷true -1‖0∷false 2‖0∷true 0‖3∷false 0‖-3∷true 0‖0∷false 2‖2∷false -2‖-2∷false"
["Integer.Eq"]="2‖1∷false 1‖2∷false 2‖-1∷false -1‖2∷false -3‖-2∷false -2‖-3∷false -1‖0∷false 2‖0∷false 0‖3∷false 0‖-3∷false 0‖0∷true 2‖2∷true -2‖-2∷true"
["Integer.Neq"]="2‖1∷true 1‖2∷true 2‖-1∷true -1‖2∷true -3‖-2∷true -2‖-3∷true -1‖0∷true 2‖0∷true 0‖3∷true 0‖-3∷true 0‖0∷false 2‖2∷false -2‖-2∷false"
["Integer.LowerEq"]="2‖1∷false 1‖2∷true 2‖-1∷false -1‖2∷true -3‖-2∷true -2‖-3∷false -1‖0∷true 2‖0∷false 0‖3∷true 0‖-3∷false 0‖0∷true 2‖2∷true -2‖-2∷true"
["Integer.GreaterEq"]="2‖1∷true 1‖2∷false 2‖-1∷true -1‖2∷false -3‖-2∷false -2‖-3∷true -1‖0∷false 2‖0∷true 0‖3∷false 0‖-3∷true 0‖0∷true 2‖2∷true -2‖-2∷true"
["Integer.Add"]="2‖3∷5 -2‖2∷0 -2‖-6∷-8 3‖-3∷0 3‖-2∷1 -2‖1∷-1 0‖0∷0 -2‖0∷-2 2‖0∷2 0‖20∷20 0‖-30∷-30"
["Integer.Inc"]="2∷3 0∷1 -1∷0 -30∷-29"
["Integer.Sub"]="2‖3∷-1 3‖2∷1 -2‖3∷-5 -2‖-3∷1 -9‖-2∷-7 2‖0∷2 -3‖0∷-3 0‖2∷-2 0‖-3∷3 0‖0∷0 2‖2∷0 -3‖-3∷0"
["Integer.Dec"]="2∷1 0∷-1 1∷0 -3∷-4"
["Integer.Divide"]="2‖3∷0 4‖2∷2 9‖-3∷-3 9‖-2∷-4 9‖0∷never -2‖-3∷0 -4‖2∷-2 4‖4∷1 4‖-4∷-1 -4‖-4∷1"
["Integer.Mod"]="2‖2∷0 3‖2∷1 -3‖-2∷1 -3‖2∷-1 9‖5∷4"
["Integer.IsOdd"]="1∷true 2∷false 0∷false -3∷true -19∷true 20∷false"
["Integer.IsEven"]="1∷false 0∷true -22∷true 19∷false"

["Array.CreateArrayFromLength"]="2∷[undefined,undefined] 3‖string∷[string,string,string] 0∷[] -1∷[]"
["Array.At"]="[1,2,3]‖2∷3 [1,2,3,4]‖0‖∷1 []‖1∷never []‖-1∷never []‖0∷never"
["Array.Concat"]="[1,2,3]‖[2,3,1]∷[1,2,3,2,3,1]"
["Array.MultipleConcat"]="[[1,2,3],[4,2,3],[undefined,true]]∷[1,2,3,4,2,3,undefined,true] [[],[1],[],[2]]∷[1,2] [[[2]],[1]]∷[[2],1]"
["Array.Fill"]="[1,2,3]‖\"3\"∷[\"3\",\"3\",\"3\"] [0,9,1]‖\"1\"‖1‖2∷[0,\"1\",1] [\"head\",9,1]‖true‖1∷[\"head\",true,true] [1,2,3]‖\"3\"‖2‖1∷[1,2,3] [1,2,3]‖\"3\"‖-4‖2∷[\"3\",\"3\",3] [1,2,3]‖\"3\"‖-2‖3∷[1,\"3\",\"3\"] [1,2,3]‖\"3\"‖7‖2∷[1,2,3] [1,2,3]‖\"3\"‖1‖-1∷[1,\"3\",3]"
["Array.IsFlatten"]="[1,2,3]∷true []∷true [[]]∷false [1,[2],3]∷false [[],1,2]∷false [1,[1,2,[]],undefined]∷false"
["Array.Flat"]="[1,2,3]∷[1,2,3] [1,2,3,[]]∷[1,2,3] [1,2,[3]]∷[1,2,3] [1,2,[3,[0]]]∷[1,2,3,0] []∷[] [[]]∷[] [[[1]]]∷[1]"
["Array.Includes"]="[1,2,3]‖1∷true [1,2,3]‖-1∷false []‖\"\"∷false [\"\"]‖\"\"∷true [[]]‖[]∷true [[1]]‖1∷false"
["Array.IndexOf"]="[1,2,3]‖1∷0 [1,[2],3]‖[2]∷1 []‖0∷-1 [1,2,3]‖4∷-1 [1,2,1]‖1∷0"
["Array.LastIndexOf"]="[1,2,1,2,1,3,4]‖1∷4 []‖1∷-1 [1,2,3]‖0∷-1 [[],[[]],[],[]]‖[[]]∷1 [()=>{},(a:string)=>{},(b:string)=>{},()=>{}]‖(c:string)=>{}∷2 [{a:string},{},{b:string}]‖{}∷1"
["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] []‖[]∷[[]]"

["BigInteger.ToString"]="2n∷\"2\" -2n∷\"-2\" 0n∷\"0\""
["BigInteger.IsNegative"]="-23n∷true 26n∷false 0n∷false"
["BigInteger.IsPositive"]="90n∷true 0n∷false -8n∷false"
["BigInteger.IsZero"]="0n∷true 29n∷false -2n∷false"
["BigInteger.Opposite"]="0n∷0n 23n∷-23n -12n∷12n"
["BigInteger.Eq"]="0n‖0n∷true -12n‖-12n∷true 23n‖22n∷false 23n‖-23n∷false"
["BigInteger.Lower"]="23n‖123n∷true 23n‖23n∷false 1234n‖1235n∷true -123n‖90n∷true 123n‖-90n∷false 0n‖0n∷false -2n‖-2n∷false 12345n‖12345n∷false"

["String.Length"]="\"abs\"∷3 \"\"∷0 \"2222_2222_2222_2222_2222\"∷24"
["String.At"]="\"123456789\"‖0∷\"1\" \"123456789\"‖2∷\"3\" \"123456789\"‖-3∷\"7\" \"123456789\"‖100∷never"
["String.Concat"]="[\"12\",\"ab\"]∷\"12ab\" [\"love\"]∷\"love\" []∷\"\""
["String.SubString"]="\"LoveYouHuaier\"∷\"LoveYouHuaier\" \"LoveYouHuaier\"‖4∷\"YouHuaier\" \"LoveYouHuaier\"‖4‖9∷\"YouHu\" \"LoveYouHuaier\"‖3‖-2∷\"eYouHuai\" \"LoveYouHuaier\"‖-7‖-2∷\"uHuai\" \"LoveYouHuaier\"‖0‖0∷\"\" \"LoveYouHuaier\"‖-1‖-1∷\"\" \"LoveYouHuaier\"‖8‖7∷\"\""
["String.EndsWith"]="\"What?\"‖\"?\"∷true \"What?\"‖\"What?\"∷true \"What?\"‖\"SoWhat?\"∷false \"What?\"‖\"t?\"∷true \"What?\"‖\"W?\"∷false \"What?\"‖\"\"∷true \"\"‖\"test\"∷false \"\"‖\"\"∷true \"What?\"‖\"?\"‖5∷true \"What?\"‖\"t?\"‖3∷false \"\"‖\"\"‖2∷true"
["String.Includes"]="\"AshGrey\"‖\"Ash\"∷true \"AshGrey\"‖\"A\"∷true \"AshGrey\"‖\"a\"∷false \"AshGrey\"‖\"\"∷true \"AshGrey\"‖\"Ash\"‖0∷true \"AshGrey\"‖\"Ash\"‖1∷false \"AshGrey\"‖\"Ash\"‖-1∷true"

["Boolean.And"]="false‖false∷false false‖true∷false true‖false∷false true‖true∷true"
["Boolean.Or"]="false‖false∷false false‖true∷true true‖false∷true true‖true∷true"
["Boolean.Nor"]="false∷true true∷false"
["Boolean.Nand"]="false‖false∷true false‖true∷true true‖false∷true true‖true∷false"
["Boolean.Nor"]="false‖false∷true false‖true∷false true‖false∷false true‖true∷false"
["Boolean.MultipleAnd"]="[true,true,true]∷true [true,false,true]∷false [false,false,false]∷false"
["Boolean.MultipleOr"]="[true,true,true]∷true [true,false,false]∷true [false,false,false]∷false"
)

function repeat {
Expand Down Expand Up @@ -96,8 +97,8 @@ for test_name in "${!test_cases[@]}"; do

echo " // ${test_name}" >> "$output_file"
for pair in ${test_cases[$test_name]}; do
IFS=":" read -r params expect <<< "$pair"
IFS="|" read -ra args <<< "$params"
IFS="" read -r params expect <<< "$pair"
IFS="" read -ra args <<< "$params"

# echo "🐵 Check Middle Args ${args[@]}"

Expand Down