|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.3 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const isFunction = require('../../predicate/isFunction.js');
|
|---|
| 6 | const isArray = require('../predicate/isArray.js');
|
|---|
| 7 | const isObject = require('../predicate/isObject.js');
|
|---|
| 8 | const toString = require('./toString.js');
|
|---|
| 9 |
|
|---|
| 10 | function bindAll(object, ...methodNames) {
|
|---|
| 11 | if (object == null) {
|
|---|
| 12 | return object;
|
|---|
| 13 | }
|
|---|
| 14 | if (!isObject.isObject(object)) {
|
|---|
| 15 | return object;
|
|---|
| 16 | }
|
|---|
| 17 | if (isArray.isArray(object) && methodNames.length === 0) {
|
|---|
| 18 | return object;
|
|---|
| 19 | }
|
|---|
| 20 | const methods = [];
|
|---|
| 21 | for (let i = 0; i < methodNames.length; i++) {
|
|---|
| 22 | const name = methodNames[i];
|
|---|
| 23 | if (isArray.isArray(name)) {
|
|---|
| 24 | methods.push(...name);
|
|---|
| 25 | }
|
|---|
| 26 | else if (name && typeof name === 'object' && 'length' in name) {
|
|---|
| 27 | methods.push(...Array.from(name));
|
|---|
| 28 | }
|
|---|
| 29 | else {
|
|---|
| 30 | methods.push(name);
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | if (methods.length === 0) {
|
|---|
| 34 | return object;
|
|---|
| 35 | }
|
|---|
| 36 | for (let i = 0; i < methods.length; i++) {
|
|---|
| 37 | const key = methods[i];
|
|---|
| 38 | const stringKey = toString.toString(key);
|
|---|
| 39 | const func = object[stringKey];
|
|---|
| 40 | if (isFunction.isFunction(func)) {
|
|---|
| 41 | object[stringKey] = func.bind(object);
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | return object;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | exports.bindAll = bindAll;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.