UNPKG

1 kBJavaScriptView Raw
1/**
2 * This method enhances a base object which would normally contain data, with
3 * methods from another object that might work on manipulating that data.
4 * All the added methods are set as non enumerable, non configurable, and non
5 * writable properties. This ensures that if we try to clone or stringify the
6 * base object, we don't have to worry about these additional methods.
7 * @private
8 * @param {object} baseObject - Base object with data
9 * @param {object} methodsObject - Object with methods as properties. The key
10 * values used here will be the same that will be defined on the baseObject.
11 */
12export default function enhanceWithMethods(baseObject, methodsObject) {
13 // @ts-expect-error
14 return Object.keys(methodsObject).reduce((enhancedObject, methodName) => {
15 Object.defineProperty(enhancedObject, methodName, {
16 enumerable: false,
17 configurable: true,
18 writable: false,
19 value: methodsObject[methodName]
20 });
21 return enhancedObject;
22 }, baseObject);
23}
\No newline at end of file