Skip to content

Commit

Permalink
fix(micro-dash): functions() no longer calls getters
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Nov 14, 2020
1 parent f29d102 commit 145f301
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions projects/micro-dash/src/lib/object/functions.spec.ts
Expand Up @@ -13,6 +13,17 @@ describe('functions()', () => {
expect(functions(MyClass.prototype)).toEqual(['b']);
});

it("doesn't call getters", () => {
class Gotcha {
get error(): never {
throw new Error('called getter');
}
}
expect(() => {
functions(Gotcha.prototype);
}).not.toThrowError();
});

//
// stolen from https://github.com/lodash/lodash
//
Expand Down
6 changes: 4 additions & 2 deletions projects/micro-dash/src/lib/object/functions.ts
Expand Up @@ -6,10 +6,12 @@ import { StringifiedKey } from '../interfaces';
*
* Contribution to minified bundle size, when it is the only function imported:
* - Lodash: 3,504 bytes
* - Micro-dash: 225 bytes
* - Micro-dash: 310 bytes
*/
export function functions<T extends object>(obj: T): Array<StringifiedKey<T>> {
return keys(obj).filter(
(key) => key !== 'constructor' && isFunction(obj[key as keyof T]),
(key) =>
key !== 'constructor' &&
isFunction(Object.getOwnPropertyDescriptor(obj, key)?.value),
);
}

0 comments on commit 145f301

Please sign in to comment.