File tree Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -118,15 +118,37 @@ local function _cloned(o, deep)
118
118
return o2
119
119
end
120
120
121
- function M .is_array (o )
121
+ --- check if object is array-like
122
+ --- object is array-like if it is a non-empty table and all keys are integers
123
+ --- @param o ? any object to check
124
+ --- @param linear ? boolean false to allow gaps between keys
125
+ --- @return boolean is_array true if object is non-empty and is array-like
126
+ function M .is_array (o , linear )
122
127
if type (o ) ~= " table" then
123
128
return false
124
129
end
125
- local max_idx = 0
130
+
131
+ linear = linear ~= false
132
+ if linear then
133
+ local i = 1
134
+ for _ , _ in pairs (o ) do
135
+ if o [i ] == nil then
136
+ return false
137
+ end
138
+ i = i + 1
139
+ end
140
+ return i > 1 -- check for not empty
141
+ end
142
+
143
+ local is_empty = true
126
144
for i , _ in pairs (o ) do
127
- max_idx = i
145
+ if type (i ) ~= " number" or math.floor (i ) ~= i then
146
+ return false
147
+ end
148
+ is_empty = false
128
149
end
129
- return max_idx == # o
150
+
151
+ return not is_empty
130
152
end
131
153
132
154
--- @param items any[] array to read from
You can’t perform that action at this time.
0 commit comments