MCPcopy Create free account
hub / github.com/dop251/goja / flattenIntoArray

Method flattenIntoArray

builtin_array.go:1205–1237  ·  view source on GitHub ↗
(target, source *Object, sourceLen, start, depth int64, mapperFunction func(FunctionCall) Value, thisArg Value)

Source from the content-addressed store, hash-verified

1203}
1204
1205func (r *Runtime) flattenIntoArray(target, source *Object, sourceLen, start, depth int64, mapperFunction func(FunctionCall) Value, thisArg Value) int64 {
1206 targetIndex, sourceIndex := start, int64(0)
1207 for sourceIndex < sourceLen {
1208 p := intToValue(sourceIndex)
1209 if source.hasProperty(p.toString()) {
1210 element := nilSafe(source.get(p, source))
1211 if mapperFunction != nil {
1212 element = mapperFunction(FunctionCall{
1213 This: thisArg,
1214 Arguments: []Value{element, p, source},
1215 })
1216 }
1217 var elementArray *Object
1218 if depth > 0 {
1219 if elementObj, ok := element.(*Object); ok && isArray(elementObj) {
1220 elementArray = elementObj
1221 }
1222 }
1223 if elementArray != nil {
1224 elementLen := toLength(elementArray.self.getStr("length", nil))
1225 targetIndex = r.flattenIntoArray(target, elementArray, elementLen, targetIndex, depth-1, nil, nil)
1226 } else {
1227 if targetIndex >= maxInt-1 {
1228 panic(r.NewTypeError("Invalid array length"))
1229 }
1230 createDataPropertyOrThrow(target, intToValue(targetIndex), element)
1231 targetIndex++
1232 }
1233 }
1234 sourceIndex++
1235 }
1236 return targetIndex
1237}
1238
1239func (r *Runtime) arrayproto_flatMap(call FunctionCall) Value {
1240 o := call.This.ToObject(r)

Callers 2

arrayproto_flatMethod · 0.95
arrayproto_flatMapMethod · 0.95

Calls 10

NewTypeErrorMethod · 0.95
intToValueFunction · 0.85
nilSafeFunction · 0.85
isArrayFunction · 0.85
toLengthFunction · 0.85
hasPropertyMethod · 0.80
toStringMethod · 0.65
getMethod · 0.65
getStrMethod · 0.65

Tested by

no test coverage detected