@@ -692,21 +692,6 @@ function m.sortCallbackOfIndex(arr)
692692 end
693693end
694694
695- --- @param datas any[]
696- --- @param scores integer[]
697- --- @return SortByScoreCallback
698- function m .sortCallbackOfScore (datas , scores )
699- local map = {}
700- for i = 1 , # datas do
701- local data = datas [i ]
702- local score = scores [i ]
703- map [data ] = score
704- end
705- return function (v )
706- return map [v ]
707- end
708- end
709-
710695--- 裁剪字符串
711696--- @param str string
712697--- @param mode ? ' "left"' | ' "right"'
@@ -721,15 +706,20 @@ function m.trim(str, mode)
721706 return (str :match ' ^%s*(.-)%s*$' )
722707end
723708
724- function m .expandPath (path )
709+ --- @param path string
710+ --- @param env ? { [string] : string }
711+ --- @return string
712+ function m .expandPath (path , env )
725713 if path :sub (1 , 1 ) == ' ~' then
726714 local home = getenv (' HOME' )
727715 if not home then -- has to be Windows
728716 home = getenv ' USERPROFILE' or (getenv ' HOMEDRIVE' .. getenv ' HOMEPATH' )
729717 end
730718 return home .. path :sub (2 )
731719 elseif path :sub (1 , 1 ) == ' $' then
732- path = path :gsub (' %$([%w_]+)' , getenv )
720+ path = path :gsub (' %$([%w_]+)' , function (name )
721+ return env and env [name ] or getenv (name ) or ' '
722+ end )
733723 return path
734724 end
735725 return path
@@ -865,13 +855,12 @@ function m.multiTable(count, default)
865855 end })
866856 end
867857 for _ = 3 , count do
868- local tt = current
869858 current = setmetatable ({}, { __index = function (t , k )
870859 if k == nil then
871860 return nil
872861 end
873- t [k ] = tt
874- return tt
862+ t [k ] = current
863+ return current
875864 end })
876865 end
877866 return current
@@ -963,4 +952,92 @@ function m.arrayMerge(a, b)
963952 return a
964953end
965954
955+ --- @generic K
956+ --- @param t { [K] : any }
957+ --- @return K[]
958+ function m .keysOf (t )
959+ local keys = {}
960+ for k in pairs (t ) do
961+ keys [# keys + 1 ] = k
962+ end
963+ return keys
964+ end
965+
966+ --- @generic V
967+ --- @param t { [any] : V }
968+ --- @return V[]
969+ function m .valuesOf (t )
970+ local values = {}
971+ for _ , v in pairs (t ) do
972+ values [# values + 1 ] = v
973+ end
974+ return values
975+ end
976+
977+ --- @param t table
978+ --- @return integer
979+ function m .countTable (t )
980+ local count = 0
981+ for _ in pairs (t ) do
982+ count = count + 1
983+ end
984+ return count
985+ end
986+
987+ --- @param arr any[]
988+ function m .arrayRemoveDuplicate (arr )
989+ local mark = {}
990+ local offset = 0
991+ local len = # arr
992+ for i = 1 , len do
993+ local v = arr [i ]
994+ if mark [v ] then
995+ offset = offset + 1
996+ else
997+ arr [i - offset ] = v
998+ mark [v ] = true
999+ end
1000+ end
1001+ for i = len - offset + 1 , len do
1002+ arr [i ] = nil
1003+ end
1004+ end
1005+
1006+ --- @param ... table
1007+ --- @return table
1008+ function m .mergeStruct (...)
1009+ local result
1010+ local copyed = {}
1011+
1012+ local function merge (a , b )
1013+ if copyed [b ] then
1014+ return copyed [b ]
1015+ end
1016+ if type (b ) ~= ' table' then
1017+ return b
1018+ end
1019+ if not a then
1020+ a = {}
1021+ end
1022+ copyed [b ] = a
1023+ local usedKeys = {}
1024+ for i , v in ipairs (b ) do
1025+ a [# a + 1 ] = v
1026+ usedKeys [i ] = true
1027+ end
1028+ for k , v in pairs (b ) do
1029+ if not usedKeys [k ] then
1030+ a [k ] = merge (a [k ], v )
1031+ end
1032+ end
1033+ return a
1034+ end
1035+
1036+ for _ , t in ipairs { ... } do
1037+ result = merge (result , t )
1038+ end
1039+
1040+ return result
1041+ end
1042+
9661043return m
0 commit comments