Skip to content

Commit

Permalink
Update bundled auto-bind
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 26, 2018
1 parent c219535 commit a394fce
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions auto-bind.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// From https://github.com/sindresorhus/auto-bind/blob/master/index.js
// Bundled because of Create React App…
'use strict';

// Gets all non-builtin properties up the prototype chain
const getAllProperties = object => {
const props = new Set();

do {
for (const key of Reflect.ownKeys(object)) {
props.add([object, key]);
}
} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);

return props;
};

module.exports = (self, options) => {
options = Object.assign({}, options);

Expand All @@ -18,11 +32,14 @@ module.exports = (self, options) => {
return true;
};

for (const key of Object.getOwnPropertyNames(self.constructor.prototype)) {
const val = self[key];
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
if (key === 'constructor' || !filter(key)) {
continue;
}

if (key !== 'constructor' && typeof val === 'function' && filter(key)) {
self[key] = val.bind(self);
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
if (descriptor && typeof descriptor.value === 'function') {
self[key] = self[key].bind(self);
}
}

Expand Down

0 comments on commit a394fce

Please sign in to comment.