(self: any, { include, exclude }: AutoBindOptions = {})
| 153 | `; |
| 154 | |
| 155 | function 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 | |
| 185 | function lerp(p1: number, p2: number, t: number): number { |
| 186 | return p1 + (p2 - p1) * t; |
no test coverage detected