|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
875 bytes
|
| Line | |
|---|
| 1 | var restArguments = require('./restArguments.js');
|
|---|
| 2 | var isFunction = require('./isFunction.js');
|
|---|
| 3 | var map = require('./map.js');
|
|---|
| 4 | var _deepGet = require('./_deepGet.js');
|
|---|
| 5 | var _toPath = require('./_toPath.js');
|
|---|
| 6 |
|
|---|
| 7 | // Invoke a method (with arguments) on every item in a collection.
|
|---|
| 8 | var invoke = restArguments(function(obj, path, args) {
|
|---|
| 9 | var contextPath, func;
|
|---|
| 10 | if (isFunction(path)) {
|
|---|
| 11 | func = path;
|
|---|
| 12 | } else {
|
|---|
| 13 | path = _toPath(path);
|
|---|
| 14 | contextPath = path.slice(0, -1);
|
|---|
| 15 | path = path[path.length - 1];
|
|---|
| 16 | }
|
|---|
| 17 | return map(obj, function(context) {
|
|---|
| 18 | var method = func;
|
|---|
| 19 | if (!method) {
|
|---|
| 20 | if (contextPath && contextPath.length) {
|
|---|
| 21 | context = _deepGet(context, contextPath);
|
|---|
| 22 | }
|
|---|
| 23 | if (context == null) return void 0;
|
|---|
| 24 | method = context[path];
|
|---|
| 25 | }
|
|---|
| 26 | return method == null ? method : method.apply(context, args);
|
|---|
| 27 | });
|
|---|
| 28 | });
|
|---|
| 29 |
|
|---|
| 30 | module.exports = invoke;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.