diff --git a/index.js b/index.js index 797c3b1..ce507da 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var whichTypedArray = require('which-typed-array'); var taSlice = require('typedarray.prototype.slice'); +var gopd = require('gopd'); // TODO: use call-bind, is-date, is-regex, is-string, is-boolean-object, is-number-object function toS(obj) { return Object.prototype.toString.call(obj); } @@ -57,6 +58,14 @@ var hasOwnProperty = Object.prototype.hasOwnProperty || function (obj, key) { return key in obj; }; +function isWritable(object, key) { + if (typeof gopd !== 'function') { + return true; + } + + return !gopd(object, key).writable; +} + function copy(src) { if (typeof src === 'object' && src !== null) { var dst; @@ -200,7 +209,11 @@ function walk(root, cb) { if (modifiers.pre) { modifiers.pre.call(state, state.node[key], key); } var child = walker(state.node[key]); - if (immutable && hasOwnProperty.call(state.node, key)) { + if ( + immutable + && hasOwnProperty.call(state.node, key) + && !isWritable(state.node, key) + ) { state.node[key] = child.node; } diff --git a/package.json b/package.json index 88305b1..223335e 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ ] }, "dependencies": { + "gopd": "^1.0.1", "typedarray.prototype.slice": "^1.0.2", "which-typed-array": "^1.1.15" },