setImmutable: Example 2

node v6.17.1
version: 0.1.0
endpointsharetweet
require('object.assign').shim() const set = require('setimmutable') const clone = require('setimmutable/clone')
// Complex Constructor class Person { constructor (firstName, lastName, age) { if (!firstName) throw new TypeError('Argument 1: firstName is requried.') if (!lastName) throw new TypeError('Argument 2: lastName is requried.') if (!age) throw new TypeError('Argument 3: age is requried.') this.firstName = firstName this.lastName = lastName this.age = age } static clone (personObj) { return new this(personObj.firstName, personObj.lastName, personObj.age) } }
const object = [{people:[new Person('santiago', 'Gomez', 30), new Person('Rayan', 'Comand', 10)]}]
function customClone (objValue, srcValue) { switch (objValue.constructor) { case Person: return Person.clone(objValue) default: return clone(objValue) } }
set(object, '[0].people.[1].firstName', 'Lucky', customClone)
Loading…

no comments

    sign in to comment