forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatePropertyValue.ts
More file actions
29 lines (26 loc) · 871 Bytes
/
updatePropertyValue.ts
File metadata and controls
29 lines (26 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import curryN from './curryN';
import { updatePropertyValue } from '../typings/types';
/**
* A function to change value by property name in object.
*
* @param {String} propertyName property name of target object
* @param {Any} propertyValue new value
* @param {Object} obj a target object
* @return {Object} changed object with new value
* @example
* var fn = () => {};
* var updateName = updatePropertyValue('name', 'newFn');
*
* fn.name //=> 'fn'
*
* var newFn = updateName(fn);
*
* newFn.name //=> 'newFn'
* fn.name //=> 'newFn'
*/
export default curryN(2, (propertyName, propertyValue, obj) => {
const descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
descriptor.value = propertyValue;
Object.defineProperty(obj, propertyName, descriptor);
return obj;
}) as typeof updatePropertyValue;