Home Reference Source Repository

ReducePrototype

This package contains two little helper functions to work with the prototype chain of an object or a class.

Usage

Just use the functions below as if you would reduce any other iterable, with the only exception, that a fourh parameter sets a constructor which stops the iteration if it's prototype occurs.

import { reducePrototypeChain, reduceInheritanceChain } from "reduce-prototype"

class A {
    test() {}
}

class B {
    test2() {}
}

class C extends A {
    test3() {}
}

const instance = new C();

// now we have the methods test, test2 and test3 in our array
const allMethods = reducePrototypeChain(instance, (dest, proto) => dest.concat(Object.getOwnPropertyNames(proto)), [])
    .filter(key => instance[key] instanceof Function)
    .map(key => instance[key]);

// now we have the methods test2 and test3 in our array
const bAndCMethods = reduceInheritanceChain(C, (dest, proto) => dest.concat(Object.getOwnPropertyNames(proto)), [], A)
    .filter(key => instance[key] instanceof Function)
    .map(key => instance[key])

Testing

You can test reducePrototype with mocha by executing make test in the root directory of the project.

Contributing

If you want to contribute to this repository, please ensure ...

All contributors are listed in the AUTHORS file, sorted by the time of their first contribution.