letsfreezethat
Advanced tools
Comparing version 2.2.2 to 2.2.3
(function() { | ||
'use strict'; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.type_of = function(x) { | ||
@@ -11,2 +12,74 @@ var R; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.is_computed = function(d, key) { | ||
return this.is_descriptor_of_computed_value(Object.getOwnPropertyDescriptor(d, key)); | ||
}; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.is_descriptor_of_computed_value = function(descriptor) { | ||
var keys; | ||
return ((keys = Object.keys(descriptor)).includes('set')) || (keys.includes('get')); | ||
}; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.set_writable_value = function(d, key, value) { | ||
/* Acc to MDN `defineProperty`, a 'writable data descriptor'. */ | ||
return Object.defineProperty(d, key, { | ||
enumerable: true, | ||
writable: true, | ||
configurable: true, | ||
value | ||
}); | ||
}; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.set_readonly_value = function(d, key, value) { | ||
/* Acc to MDN `defineProperty`, a 'readonly data descriptor'. */ | ||
return Object.defineProperty(d, key, { | ||
enumerable: true, | ||
writable: false, | ||
configurable: false, | ||
value | ||
}); | ||
}; | ||
//----------------------------------------------------------------------------------------------------------- | ||
this.set_computed_value = function(d, key, get = null, set = null) { | ||
/* Acc to MDN `defineProperty`, an 'accessor descriptor'. */ | ||
var descriptor, type_of_get, type_of_set; | ||
descriptor = { | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
type_of_get = get != null ? this.type_of(get) : null; | ||
type_of_set = set != null ? this.type_of(set) : null; | ||
if (!((type_of_get != null) || (type_of_set != null))) { | ||
throw new Error("^lft@h1^ must define getter or setter"); | ||
} | ||
if (type_of_get != null) { | ||
if (type_of_get !== 'function') { | ||
throw new Error(`^lft@h2^ expected a function, got a ${type}`); | ||
} | ||
descriptor.get = get; | ||
} | ||
if (type_of_set != null) { | ||
if (type_of_set !== 'function') { | ||
throw new Error(`^lft@h3^ expected a function, got a ${type}`); | ||
} | ||
descriptor.set = set; | ||
} | ||
return Object.defineProperty(d, key, descriptor); | ||
}; | ||
(() => { //########################################################################################################### | ||
var k, ref, results, v; | ||
ref = this; | ||
results = []; | ||
for (k in ref) { | ||
v = ref[k]; | ||
results.push(this[k] = v.bind(this)); | ||
} | ||
return results; | ||
})(); | ||
}).call(this); |
(function() { | ||
'use strict'; | ||
var _freeze, _is_computed, _thaw, fix, freeze, lets, lets_compute, thaw, type_of; | ||
var _freeze, _thaw, fix, freeze, is_descriptor_of_computed_value, lets, lets_compute, set_computed_value, set_readonly_value, set_writable_value, thaw, type_of; | ||
//----------------------------------------------------------------------------------------------------------- | ||
({type_of} = require('./helpers')); | ||
({type_of, is_descriptor_of_computed_value, set_writable_value, set_readonly_value, set_computed_value} = require('./helpers')); | ||
@@ -37,41 +37,34 @@ //----------------------------------------------------------------------------------------------------------- | ||
//----------------------------------------------------------------------------------------------------------- | ||
_is_computed = function(descriptor) { | ||
var keys; | ||
return ((keys = Object.keys(descriptor)).includes('set')) || (keys.includes('get')); | ||
}; | ||
//----------------------------------------------------------------------------------------------------------- | ||
_freeze = function(x) { | ||
var R, descriptor, key, ref, value; | ||
//......................................................................................................... | ||
if (Array.isArray(x)) { | ||
return Object.seal((function() { | ||
var i, len, results; | ||
results = []; | ||
for (i = 0, len = x.length; i < len; i++) { | ||
value = x[i]; | ||
results.push(_freeze(value)); | ||
} | ||
return results; | ||
})()); | ||
} | ||
//......................................................................................................... | ||
if ((type_of(x)) === 'object') { | ||
R = {}; | ||
ref = Object.getOwnPropertyDescriptors(x); | ||
for (key in ref) { | ||
descriptor = ref[key]; | ||
if (_is_computed(descriptor)) { | ||
Object.defineProperty(R, key, descriptor); | ||
} else { | ||
/* TAINT must recurse into object */ | ||
if (Array.isArray(descriptor.value)) { | ||
descriptor.value = _freeze(descriptor.value); | ||
var R, descriptor, key, ref, ref1, type, value; | ||
switch (type = type_of(x)) { | ||
//....................................................................................................... | ||
case 'array': | ||
return Object.seal((function() { | ||
var i, len, results; | ||
results = []; | ||
for (i = 0, len = x.length; i < len; i++) { | ||
value = x[i]; | ||
results.push(_freeze(value)); | ||
} | ||
descriptor.configurable = false; | ||
descriptor.writable = false; | ||
Object.defineProperty(R, key, descriptor); | ||
return results; | ||
})()); | ||
//....................................................................................................... | ||
case 'object': | ||
R = {}; | ||
ref = Object.getOwnPropertyDescriptors(x); | ||
for (key in ref) { | ||
descriptor = ref[key]; | ||
if (is_descriptor_of_computed_value(descriptor)) { | ||
Object.defineProperty(R, key, descriptor); | ||
} else { | ||
if ((ref1 = type_of(descriptor.value)) === 'object' || ref1 === 'array') { | ||
descriptor.value = _freeze(descriptor.value); | ||
} | ||
descriptor.configurable = false; | ||
descriptor.writable = false; | ||
Object.defineProperty(R, key, descriptor); | ||
} | ||
} | ||
} | ||
return Object.seal(R); | ||
return Object.seal(R); | ||
} | ||
@@ -84,33 +77,33 @@ //......................................................................................................... | ||
_thaw = function(x) { | ||
var R, descriptor, key, ref, value; | ||
//......................................................................................................... | ||
if (Array.isArray(x)) { | ||
return (function() { | ||
var i, len, results; | ||
results = []; | ||
for (i = 0, len = x.length; i < len; i++) { | ||
value = x[i]; | ||
results.push(_thaw(value)); | ||
} | ||
return results; | ||
})(); | ||
} | ||
//......................................................................................................... | ||
if ((type_of(x)) === 'object') { | ||
R = {}; | ||
ref = Object.getOwnPropertyDescriptors(x); | ||
for (key in ref) { | ||
descriptor = ref[key]; | ||
if (_is_computed(descriptor)) { | ||
Object.defineProperty(R, key, descriptor); | ||
} else { | ||
if (Array.isArray(descriptor.value)) { | ||
descriptor.value = _thaw(descriptor.value); | ||
var R, descriptor, key, ref, ref1, type, value; | ||
switch (type = type_of(x)) { | ||
//....................................................................................................... | ||
case 'array': | ||
return (function() { | ||
var i, len, results; | ||
results = []; | ||
for (i = 0, len = x.length; i < len; i++) { | ||
value = x[i]; | ||
results.push(_thaw(value)); | ||
} | ||
descriptor.configurable = true; | ||
descriptor.writable = true; | ||
Object.defineProperty(R, key, descriptor); | ||
return results; | ||
})(); | ||
//....................................................................................................... | ||
case 'object': | ||
R = {}; | ||
ref = Object.getOwnPropertyDescriptors(x); | ||
for (key in ref) { | ||
descriptor = ref[key]; | ||
if (is_descriptor_of_computed_value(descriptor)) { | ||
Object.defineProperty(R, key, descriptor); | ||
} else { | ||
if ((ref1 = type_of(descriptor.value)) === 'object' || ref1 === 'array') { | ||
descriptor.value = _thaw(descriptor.value); | ||
} | ||
descriptor.configurable = true; | ||
descriptor.writable = true; | ||
Object.defineProperty(R, key, descriptor); | ||
} | ||
} | ||
} | ||
return R; | ||
return R; | ||
} | ||
@@ -132,25 +125,6 @@ //......................................................................................................... | ||
//----------------------------------------------------------------------------------------------------------- | ||
lets_compute = function(original, name, get = null, set = null) { | ||
var descriptor, draft, type; | ||
lets_compute = function(original, key, get = null, set = null) { | ||
var draft; | ||
draft = thaw(original); | ||
descriptor = { | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
if (get != null) { | ||
if ((type = type_of(get)) !== 'function') { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
} | ||
descriptor.get = get; | ||
} | ||
if (set != null) { | ||
if (!(((!set) != null) || (type = type_of(set)) === 'function')) { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
} | ||
descriptor.set = set; | ||
} | ||
if ((get == null) && (set == null)) { | ||
throw new Error("µ79825 must define getter or setter"); | ||
} | ||
Object.defineProperty(draft, name, descriptor); | ||
set_computed_value(draft, key, get, set); | ||
return freeze(draft); | ||
@@ -160,9 +134,4 @@ }; | ||
//----------------------------------------------------------------------------------------------------------- | ||
fix = function(target, name, value) { | ||
Object.defineProperty(target, name, { | ||
enumerable: true, | ||
writable: false, | ||
configurable: false, | ||
value: freeze(value) | ||
}); | ||
fix = function(target, key, value) { | ||
set_readonly_value(target, key, freeze(value)); | ||
return target; | ||
@@ -169,0 +138,0 @@ }; |
336
lib/test.js
@@ -35,3 +35,3 @@ (function() { | ||
} | ||
}, '^lft@100^'); | ||
}, '^lft@1^'); | ||
assert.deepEqual(e, { | ||
@@ -45,14 +45,14 @@ foo: 'bar', | ||
} | ||
}, '^lft@101^'); | ||
assert.ok(d !== e, '^lft@102^'); | ||
assert.ok(Object.isFrozen(d), '^lft@103^'); | ||
assert.ok(Object.isFrozen(d.nested), '^lft@104^'); | ||
assert.ok(Object.isFrozen(d.u), '^lft@105^'); | ||
assert.ok(Object.isFrozen(d.u.v), '^lft@106^'); | ||
assert.ok(Object.isFrozen(d.u.v.w), '^lft@107^'); | ||
assert.ok(Object.isFrozen(e), '^lft@108^'); | ||
assert.ok(Object.isFrozen(e.nested), '^lft@109^'); | ||
assert.ok(Object.isFrozen(e.u), '^lft@110^'); | ||
assert.ok(Object.isFrozen(e.u.v), '^lft@111^'); | ||
assert.ok(Object.isFrozen(e.u.v.w), '^lft@112^'); | ||
}, '^lft@2^'); | ||
assert.ok(d !== e, '^lft@3^'); | ||
assert.ok(Object.isFrozen(d), '^lft@4^'); | ||
assert.ok(Object.isFrozen(d.nested), '^lft@5^'); | ||
assert.ok(Object.isFrozen(d.u), '^lft@6^'); | ||
assert.ok(Object.isFrozen(d.u.v), '^lft@7^'); | ||
assert.ok(Object.isFrozen(d.u.v.w), '^lft@8^'); | ||
assert.ok(Object.isFrozen(e), '^lft@9^'); | ||
assert.ok(Object.isFrozen(e.nested), '^lft@10^'); | ||
assert.ok(Object.isFrozen(e.u), '^lft@11^'); | ||
assert.ok(Object.isFrozen(e.u.v), '^lft@12^'); | ||
assert.ok(Object.isFrozen(e.u.v.w), '^lft@13^'); | ||
return null; | ||
@@ -75,12 +75,12 @@ }; | ||
nested: [2, 3, 5, 7] | ||
}, '^lft@113^'); | ||
}, '^lft@14^'); | ||
assert.deepEqual(e, { | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7, 11] | ||
}, '^lft@114^'); | ||
assert.ok(d !== e, '^lft@115^'); | ||
assert.ok(!Object.isFrozen(d), '^lft@116^'); | ||
assert.ok(!Object.isFrozen(d.nested), '^lft@117^'); | ||
assert.ok(!Object.isFrozen(e), '^lft@118^'); | ||
assert.ok(!Object.isFrozen(e.nested), '^lft@119^'); | ||
}, '^lft@15^'); | ||
assert.ok(d !== e, '^lft@16^'); | ||
assert.ok(!Object.isFrozen(d), '^lft@17^'); | ||
assert.ok(!Object.isFrozen(d.nested), '^lft@18^'); | ||
assert.ok(!Object.isFrozen(e), '^lft@19^'); | ||
assert.ok(!Object.isFrozen(e.nested), '^lft@20^'); | ||
return null; | ||
@@ -125,5 +125,5 @@ }; | ||
}); | ||
assert.ok(d === e, '^lft@120^'); | ||
assert.ok(!Object.isFrozen(d), '^lft@121^'); | ||
assert.deepEqual(Object.keys(d), ['foo', 'sql'], '^lft@122^'); | ||
assert.ok(d === e, '^lft@21^'); | ||
assert.ok(!Object.isFrozen(d), '^lft@22^'); | ||
assert.deepEqual(Object.keys(d), ['foo', 'sql'], '^lft@23^'); | ||
assert.deepEqual(d, { | ||
@@ -134,3 +134,3 @@ foo: 'bar', | ||
} | ||
}, '^lft@123^'); | ||
}, '^lft@24^'); | ||
assert.throws((function() { | ||
@@ -140,3 +140,3 @@ return d.sql = 'other'; | ||
message: /Cannot assign to read only property/ | ||
}, '^lft@124^'); | ||
}, '^lft@25^'); | ||
assert.throws((function() { | ||
@@ -146,3 +146,3 @@ return d.sql.query = 'other'; | ||
message: /Cannot assign to read only property/ | ||
}, '^lft@125^'); | ||
}, '^lft@26^'); | ||
return null; | ||
@@ -154,14 +154,58 @@ }; | ||
/* Pretest: Ensure invariant behavior for non-special attributes (copy of first test, above): */ | ||
var d, e, fix, freeze, lets, thaw; | ||
var d, d2, d_thawed, e, fix, freeze, is_readonly, lets, matcher_a, matcher_b, matcher_c, thaw; | ||
({lets, freeze, thaw, fix} = (require('..')).partial); | ||
is_readonly = function(d, key) { | ||
var descriptor; | ||
descriptor = Object.getOwnPropertyDescriptor(d, key); | ||
return (!descriptor.writable) && (!descriptor.configurable); | ||
}; | ||
//......................................................................................................... | ||
d = lets({ | ||
matcher_a = { | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7] | ||
}); | ||
nested: [2, 3, 5, 7], | ||
u: { | ||
v: { | ||
w: 'x' | ||
} | ||
} | ||
}; | ||
matcher_b = { | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7, 11], | ||
u: { | ||
v: { | ||
w: 'x' | ||
} | ||
} | ||
}; | ||
matcher_c = { | ||
foo: 'other', | ||
nested: [2, 3, 5, 7, 'other'], | ||
u: { | ||
v: { | ||
w: 'other' | ||
} | ||
}, | ||
blah: 'other' | ||
}; | ||
d = lets(matcher_a); | ||
e = lets(d, function(d) { | ||
return d.nested.push(11); | ||
}); | ||
// log '^7763^', Object.getOwnPropertyDescriptor d, 'nested' | ||
// log '^7763^', Object.getOwnPropertyDescriptor e, 'nested' | ||
assert.ok(d !== e, '^lft@27^'); | ||
assert.ok(d !== matcher_a, '^lft@28^'); | ||
assert.deepEqual(d, matcher_a, '^lft@29^'); | ||
assert.deepEqual(e, matcher_b, '^lft@30^'); | ||
assert.ok(is_readonly(d, 'nested'), '^lft@31^'); | ||
assert.ok(is_readonly(d, 'u'), '^lft@32^'); | ||
assert.ok(is_readonly(d.u, 'v'), '^lft@33^'); | ||
assert.ok(is_readonly(d.u.v, 'w'), '^lft@34^'); | ||
assert.ok(Object.isSealed(d), '^lft@35^'); | ||
assert.ok(Object.isSealed(d.nested), '^lft@36^'); | ||
assert.ok(Object.isSealed(d.u), '^lft@37^'); | ||
assert.ok(Object.isSealed(d.u.v), '^lft@38^'); | ||
assert.ok(Object.isSealed(e), '^lft@39^'); | ||
assert.ok(Object.isSealed(e.nested), '^lft@40^'); | ||
assert.ok(Object.isSealed(e.u), '^lft@41^'); | ||
assert.ok(Object.isSealed(e.u.v), '^lft@42^'); | ||
assert.throws((function() { | ||
@@ -171,3 +215,3 @@ return d.nested.push('other'); | ||
message: /Cannot add property/ | ||
}, '^lft@126^'); | ||
}, '^lft@43^'); | ||
assert.throws((function() { | ||
@@ -177,3 +221,3 @@ return d.foo = 'other'; | ||
message: /Cannot assign to read only property/ | ||
}, '^lft@127^'); | ||
}, '^lft@44^'); | ||
assert.throws((function() { | ||
@@ -183,18 +227,80 @@ return d.blah = 'other'; | ||
message: /Cannot add property/ | ||
}, '^lft@128^'); | ||
log('^5589^', d); | ||
log('^5589^', e); | ||
assert.deepEqual(d, { | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7] | ||
}, '^lft@129^'); | ||
assert.deepEqual(e, { | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7, 11] | ||
}, '^lft@130^'); | ||
assert.ok(d !== e, '^lft@131^'); | ||
assert.ok(Object.isSealed(d), '^lft@132^'); | ||
assert.ok(Object.isSealed(e), '^lft@133^'); | ||
assert.ok(Object.isSealed(d.nested), '^lft@134^'); | ||
assert.ok(Object.isSealed(e.nested), '^lft@135^'); | ||
}, '^lft@45^'); | ||
//......................................................................................................... | ||
d2 = lets(d, function(d_copy) { | ||
assert.ok(d !== d_copy, '^lft@46^'); | ||
assert.ok(!is_readonly(d_copy, 'nested'), '^lft@47^'); | ||
assert.ok(!is_readonly(d_copy, 'u'), '^lft@48^'); | ||
assert.ok(!is_readonly(d_copy.u, 'v'), '^lft@49^'); | ||
assert.ok(!is_readonly(d_copy.u.v, 'w'), '^lft@50^'); | ||
assert.ok(!Object.isSealed(d_copy), '^lft@51^'); | ||
assert.ok(!Object.isSealed(d_copy.nested), '^lft@52^'); | ||
assert.ok(!Object.isSealed(d_copy.u), '^lft@53^'); | ||
assert.ok(!Object.isSealed(d_copy.u.v), '^lft@54^'); | ||
try { | ||
d_copy.nested.push('other'); | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@55^ ' + e.message); | ||
} | ||
try { | ||
d_copy.foo = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@56^ ' + e.message); | ||
} | ||
try { | ||
d_copy.blah = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@57^ ' + e.message); | ||
} | ||
try { | ||
return d_copy.u.v.w = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@58^ ' + e.message); | ||
} | ||
}); | ||
assert.ok(d2 !== d, '^lft@59^'); | ||
assert.deepEqual(d, matcher_a, '^lft@60^'); | ||
assert.deepEqual(d2, matcher_c, '^lft@61^'); | ||
//......................................................................................................... | ||
d_thawed = thaw(d); | ||
assert.deepEqual(d_thawed, d, '^lft@62^'); | ||
assert.ok(d !== d_thawed, '^lft@63^'); | ||
assert.ok(!is_readonly(d_thawed, 'nested'), '^lft@64^'); | ||
assert.ok(!is_readonly(d_thawed, 'u'), '^lft@65^'); | ||
assert.ok(!is_readonly(d_thawed.u, 'v'), '^lft@66^'); | ||
assert.ok(!is_readonly(d_thawed.u.v, 'w'), '^lft@67^'); | ||
assert.ok(!Object.isSealed(d_thawed), '^lft@68^'); | ||
assert.ok(!Object.isSealed(d_thawed.nested), '^lft@69^'); | ||
assert.ok(!Object.isSealed(d_thawed.u), '^lft@70^'); | ||
assert.ok(!Object.isSealed(d_thawed.u.v), '^lft@71^'); | ||
try { | ||
d_thawed.nested.push('other'); | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@72^ ' + e.message); | ||
} | ||
try { | ||
d_thawed.foo = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@73^ ' + e.message); | ||
} | ||
try { | ||
d_thawed.blah = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@74^ ' + e.message); | ||
} | ||
try { | ||
d_thawed.u.v.w = 'other'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@75^ ' + e.message); | ||
} | ||
assert.deepEqual(d_thawed, matcher_c, '^lft@76^'); | ||
//......................................................................................................... | ||
return null; | ||
@@ -207,3 +313,3 @@ }; | ||
sealing and selective `fix()`ing of attributes: */ | ||
var counter, d, e, fix, freeze, lets, thaw; | ||
var counter, d, e, fix, freeze, lets, open_vz, thaw; | ||
({lets, freeze, thaw, fix} = (require('..')).partial); | ||
@@ -214,5 +320,13 @@ //......................................................................................................... | ||
foo: 'bar', | ||
nested: [2, 3, 5, 7] | ||
nested: [2, 3, 5, 7], | ||
u: { | ||
v: { | ||
w: 'x' | ||
} | ||
} | ||
}; | ||
e = d.nested.push(11); | ||
open_vz = { | ||
a: 123 | ||
}; | ||
Object.defineProperty(d, 'foo', { | ||
@@ -240,12 +354,19 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(d, 'open_vz', { | ||
enumerable: true, | ||
configurable: false, | ||
get: function() { | ||
return open_vz; | ||
} | ||
}); | ||
// log Object.getOwnPropertyDescriptors d | ||
Object.seal(d); | ||
//......................................................................................................... | ||
assert.ok((type_of((Object.getOwnPropertyDescriptor(d, 'count')).set)) === 'function', '^lft@136^'); | ||
assert.ok(Object.isSealed(d), '^lft@137^'); | ||
assert.deepEqual(Object.keys(d), ['foo', 'nested', 'count'], '^lft@138^'); | ||
assert.ok(d.count === 1, '^lft@139^'); | ||
assert.ok(d.count === 2, '^lft@140^'); | ||
assert.ok((d.count = 42) === 42, '^lft@141^'); | ||
assert.ok(d.count === 43, '^lft@142^'); | ||
assert.ok((type_of((Object.getOwnPropertyDescriptor(d, 'count')).set)) === 'function', '^lft@77^'); | ||
assert.ok(Object.isSealed(d), '^lft@78^'); | ||
assert.deepEqual(Object.keys(d), ['foo', 'nested', 'u', 'count', 'open_vz'], '^lft@79^'); | ||
assert.ok(d.count === 1, '^lft@80^'); | ||
assert.ok(d.count === 2, '^lft@81^'); | ||
assert.ok((d.count = 42) === 42, '^lft@82^'); | ||
assert.ok(d.count === 43, '^lft@83^'); | ||
assert.throws((function() { | ||
@@ -255,3 +376,3 @@ return d.blah = 'other'; | ||
message: /Cannot add property blah, object is not extensible/ | ||
}, '^lft@143^'); | ||
}, '^lft@84^'); | ||
assert.throws((function() { | ||
@@ -261,3 +382,10 @@ return d.foo = 'other'; | ||
message: /Cannot assign to read only property/ | ||
}, '^lft@144^'); | ||
}, '^lft@85^'); | ||
try { | ||
d.open_vz.new_property = 42; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@86^ ' + e.message); | ||
} | ||
assert.deepEqual(d.open_vz.new_property, 42, '^lft@87^'); | ||
return null; | ||
@@ -268,7 +396,9 @@ }; | ||
this["use partial freezing (3/3)"] = function() { | ||
var counter, d, e, fix, freeze, lets, lets_compute, thaw; | ||
var counter, d, e, fix, freeze, lets, lets_compute, open_vz, thaw; | ||
({lets, freeze, thaw, fix, lets_compute} = (require('..')).partial); | ||
// log '^!!!!!!!!!!!!!!!!!!!!!!!!!!^'; return | ||
//......................................................................................................... | ||
counter = 0; | ||
open_vz = { | ||
a: 123 | ||
}; | ||
d = lets({ | ||
@@ -291,24 +421,22 @@ foo: 'bar', | ||
})); | ||
// d = lets d, ( d ) -> Object.defineProperty d, 'count', | ||
// enumerable: true | ||
// configurable: false | ||
// get: -> ++counter | ||
// set: ( value ) -> counter = value | ||
assert.ok((type_of((Object.getOwnPropertyDescriptor(d, 'count')).set)) === 'function', '^lft@145^'); | ||
log('^3341^', d); | ||
log('^3341^', Object.getOwnPropertyDescriptor(d, 'u')); | ||
log('^3341^', Object.getOwnPropertyDescriptor(d.u, 'v')); | ||
log('^3341^', Object.getOwnPropertyDescriptor(d.u.v, 'w')); | ||
assert.deepEqual(d.u, { | ||
v: { | ||
w: 'x' | ||
} | ||
}, '^lft@146^'); | ||
assert.ok(Object.isSealed(d), '^lft@147^'); | ||
assert.ok(Object.isSealed(d.u), '^lft@148^'); | ||
assert.deepEqual(Object.keys(d), ['foo', 'nested', 'count'], '^lft@149^'); | ||
assert.ok(d.count === 1, '^lft@150^'); | ||
assert.ok(d.count === 2, '^lft@151^'); | ||
assert.ok((d.count = 42) === 42, '^lft@152^'); | ||
assert.ok(d.count === 43, '^lft@153^'); | ||
d = lets_compute(d, 'open_vz', (function() { | ||
return open_vz; | ||
})); | ||
assert.ok((type_of((Object.getOwnPropertyDescriptor(d, 'count')).set)) === 'function', '^lft@88^'); | ||
assert.ok(d.count === 1, '^lft@89^'); | ||
assert.ok(d.count === 2, '^lft@90^'); | ||
assert.ok((d.count = 42) === 42, '^lft@91^'); | ||
assert.ok(d.count === 43, '^lft@92^'); | ||
assert.ok(d.open_vz === open_vz, '^lft@93^'); | ||
try { | ||
d.open_vz.new_property = 'new value'; | ||
} catch (error1) { | ||
e = error1; | ||
throw new Error('^lft@94^ ' + e.message); | ||
} | ||
assert.ok(d.open_vz === open_vz, '^lft@93^'); | ||
assert.deepEqual(open_vz, { | ||
a: 123, | ||
new_property: 'new value' | ||
}, '^lft@93^'); | ||
assert.throws((function() { | ||
@@ -318,3 +446,3 @@ return d.blah = 'other'; | ||
message: /Cannot add property blah, object is not extensible/ | ||
}, '^lft@154^'); | ||
}, '^lft@84^'); | ||
assert.throws((function() { | ||
@@ -324,3 +452,3 @@ return d.foo = 'other'; | ||
message: /Cannot assign to read only property/ | ||
}, '^lft@155^'); | ||
}, '^lft@85^'); | ||
return null; | ||
@@ -342,4 +470,4 @@ }; | ||
})); | ||
assert.ok(d.count === 1, '^lft@156^'); | ||
assert.ok(d.count === 2, '^lft@157^'); | ||
assert.ok(d.count === 1, '^lft@95^'); | ||
assert.ok(d.count === 2, '^lft@96^'); | ||
//......................................................................................................... | ||
@@ -353,4 +481,4 @@ counter = 0; | ||
}), null); | ||
assert.ok(d.count === 1, '^lft@158^'); | ||
assert.ok(d.count === 2, '^lft@159^'); | ||
assert.ok(d.count === 1, '^lft@97^'); | ||
assert.ok(d.count === 2, '^lft@98^'); | ||
//......................................................................................................... | ||
@@ -371,3 +499,3 @@ counter = 0; | ||
return lets_compute(d, 'count', null, null); | ||
}), /must define getter or setter/, '^lft@160^'); | ||
}), /must define getter or setter/, '^lft@99^'); | ||
}; | ||
@@ -418,3 +546,3 @@ | ||
return assert.ok(((require('util')).inspect(x)).startsWith('Someclass'), | ||
'^lft@161^' + `(#${n})`); | ||
'^lft@100^' + `(#${n})`); | ||
}, | ||
@@ -425,19 +553,19 @@ function() { | ||
'this_is_someclass'], | ||
'^lft@162^' + `(#${n})`); | ||
'^lft@101^' + `(#${n})`); | ||
}, | ||
function() { | ||
return assert.ok(x.hasOwnProperty('this_is_otherclass', | ||
'^lft@163^' + `(#${n})`)); | ||
'^lft@102^' + `(#${n})`)); | ||
}, | ||
function() { | ||
return assert.ok(x.hasOwnProperty('this_is_someclass', | ||
'^lft@164^' + `(#${n})`)); | ||
'^lft@103^' + `(#${n})`)); | ||
}, | ||
function() { | ||
return assert.ok(!x.hasOwnProperty('f', | ||
'^lft@165^' + `(#${n})`)); | ||
'^lft@104^' + `(#${n})`)); | ||
}, | ||
function() { | ||
return assert.ok(!x.hasOwnProperty('g', | ||
'^lft@166^' + `(#${n})`)); | ||
'^lft@105^' + `(#${n})`)); | ||
}, | ||
@@ -448,3 +576,3 @@ function() { | ||
'Otherclass.this_is_someclass'], | ||
'^lft@167^' + `(#${n})`); | ||
'^lft@106^' + `(#${n})`); | ||
}, | ||
@@ -455,3 +583,3 @@ function() { | ||
'Someclass.this_is_someclass'], | ||
'^lft@168^' + `(#${n})`); | ||
'^lft@107^' + `(#${n})`); | ||
} | ||
@@ -468,7 +596,7 @@ ]; | ||
error_count++; | ||
log('^lft@169^', "ERROR:", error.message); | ||
log('^lft@108^', "ERROR:", error.message); | ||
} | ||
} | ||
if (error_count > 0) { | ||
assert.ok(false, `^lft@162^(#${n}) ${error_count} tests failed`); | ||
assert.ok(false, `^lft@86^(#${n}) ${error_count} tests failed`); | ||
} | ||
@@ -554,7 +682,7 @@ return null; | ||
error_count++; | ||
log('^lft@170^', "ERROR:", error.message); | ||
log('^lft@109^', "ERROR:", error.message); | ||
} | ||
} | ||
if (error_count > 0) { | ||
assert.ok(false, `^lft@162^ ${error_count} tests failed`); | ||
assert.ok(false, `^lft@88^ ${error_count} tests failed`); | ||
} | ||
@@ -561,0 +689,0 @@ return null; |
{ | ||
"name": "letsfreezethat", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "An utterly minimal immutability library in the spirit of immer", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84770
1196