MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / AutoBind

Function AutoBind

src/ts-default/Components/FlyingPosters/FlyingPosters.tsx:155–183  ·  view source on GitHub ↗
(self: any, { include, exclude }: AutoBindOptions = {})

Source from the content-addressed store, hash-verified

153`;
154
155function AutoBind(self: any, { include, exclude }: AutoBindOptions = {}) {
156 const getAllProperties = (object: any): Set<[any, string | symbol]> => {
157 const properties = new Set<[any, string | symbol]>();
158 do {
159 for (const key of Reflect.ownKeys(object)) {
160 properties.add([object, key]);
161 }
162 } while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
163 return properties;
164 };
165
166 const filter = (key: string | symbol) => {
167 const match = (pattern: string | RegExp) =>
168 typeof pattern === 'string' ? key === pattern : (pattern as RegExp).test(key.toString());
169
170 if (include) return include.some(match);
171 if (exclude) return !exclude.some(match);
172 return true;
173 };
174
175 for (const [object, key] of getAllProperties(self.constructor.prototype)) {
176 if (key === 'constructor' || !filter(key)) continue;
177 const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
178 if (descriptor && typeof descriptor.value === 'function') {
179 self[key] = self[key].bind(self);
180 }
181 }
182 return self;
183}
184
185function lerp(p1: number, p2: number, t: number): number {
186 return p1 + (p2 - p1) * t;

Callers 1

constructorMethod · 0.70

Calls 3

getAllPropertiesFunction · 0.70
filterFunction · 0.70
bindMethod · 0.45

Tested by

no test coverage detected