methods method
Implementation
@override
Map<String, Function> methods() {
return {
'init': (
[dynamic first,
dynamic second = _missingArrayItem,
dynamic third = _missingArrayItem,
dynamic fourth = _missingArrayItem,
dynamic fifth = _missingArrayItem]) {
final values = [first, second, third, fourth, fifth]
.where((value) => !identical(value, _missingArrayItem))
.toList();
if (values.length == 1 && values.first is num) {
final length = (values.first as num).toInt();
_List._checkArrayLength(length);
return List<dynamic>.filled(length, jsArrayHole);
}
return values;
},
'isArray': (dynamic value) => value is List,
'from': (dynamic value, [Function? mapFn]) {
List<dynamic> list;
if (value is String) {
list = value.split('');
} else if (value is List) {
list = value.map(_List._visibleValue).toList();
} else if (value is Iterable) {
list = value.toList();
} else if (value is Invokable) {
final methods = value.methods();
final isMapLike =
methods.containsKey('get') && methods.containsKey('set');
final iteratorMethod =
isMapLike ? methods['entries'] : methods['values'];
final result = iteratorMethod == null
? null
: Function.apply(iteratorMethod, const []);
list = result is List ? List<dynamic>.from(result) : <dynamic>[];
} else if (value is Map && value['length'] is num) {
final length = (value['length'] as num).toInt();
_List._checkArrayLength(length);
list = List<dynamic>.generate(length, (index) {
if (InvokableController.hasProperty(value, index)) {
return InvokableController.getProperty(value, index);
}
return InvokableController.getProperty(value, index.toString());
});
} else {
list = [];
}
if (mapFn != null) {
return list
.asMap()
.entries
.map((entry) => mapFn([entry.value, entry.key]))
.toList();
}
return list;
},
'of': (
[dynamic first = _missingArrayItem,
dynamic second = _missingArrayItem,
dynamic third = _missingArrayItem,
dynamic fourth = _missingArrayItem,
dynamic fifth = _missingArrayItem,
dynamic sixth = _missingArrayItem,
dynamic seventh = _missingArrayItem,
dynamic eighth = _missingArrayItem]) =>
[first, second, third, fourth, fifth, sixth, seventh, eighth]
.where((value) => !identical(value, _missingArrayItem))
.toList(),
};
}