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