letsfreezethat
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -116,17 +116,25 @@ (function() { | ||
//----------------------------------------------------------------------------------------------------------- | ||
lets_compute = function(original, name, get, set = null) { | ||
var draft, type; | ||
if ((type = typeof get) !== 'function') { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
lets_compute = function(original, name, get = null, set = null) { | ||
var descriptor, draft, type; | ||
draft = thaw(original); | ||
descriptor = { | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
if (get != null) { | ||
if ((type = typeof get) !== 'function') { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
} | ||
descriptor.get = get; | ||
} | ||
if (!(((!set) != null) || (type = typeof set) === 'function')) { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
if (set != null) { | ||
if (!(((!set) != null) || (type = typeof set) === 'function')) { | ||
throw new Error(`µ77631 expected a function, got a ${type}`); | ||
} | ||
descriptor.set = set; | ||
} | ||
draft = thaw(original); | ||
Object.defineProperty(draft, name, { | ||
enumerable: true, | ||
configurable: false, | ||
get, | ||
set | ||
}); | ||
if ((get == null) && (set == null)) { | ||
throw new Error("µ79825 must define getter or setter"); | ||
} | ||
Object.defineProperty(draft, name, descriptor); | ||
return freeze(draft); | ||
@@ -133,0 +141,0 @@ }; |
@@ -273,2 +273,45 @@ (function() { | ||
//----------------------------------------------------------------------------------------------------------- | ||
this["may pass in null as getter, setter"] = function() { | ||
var counter, d, lets, lets_compute; | ||
({lets, lets_compute} = (require('..')).partial); | ||
// log '^!!!!!!!!!!!!!!!!!!!!!!!!!!^'; return | ||
//......................................................................................................... | ||
counter = 0; | ||
d = lets({ | ||
foo: 'bar' | ||
}); | ||
d = lets_compute(d, 'count', (function() { | ||
return ++counter; | ||
})); | ||
assert.ok(d.count === 1, '^lft@148^'); | ||
assert.ok(d.count === 2, '^lft@149^'); | ||
//......................................................................................................... | ||
counter = 0; | ||
d = lets({ | ||
foo: 'bar' | ||
}); | ||
d = lets_compute(d, 'count', (function() { | ||
return ++counter; | ||
}), null); | ||
assert.ok(d.count === 1, '^lft@150^'); | ||
assert.ok(d.count === 2, '^lft@151^'); | ||
//......................................................................................................... | ||
counter = 0; | ||
d = lets({ | ||
foo: 'bar' | ||
}); | ||
d = lets_compute(d, 'count', null, (function() { | ||
return ++counter; | ||
})); | ||
//......................................................................................................... | ||
counter = 0; | ||
d = lets({ | ||
foo: 'bar' | ||
}); | ||
return assert.throws((function() { | ||
return lets_compute(d, 'count', null, null); | ||
}), /must define getter or setter/, '^lft@152^'); | ||
}; | ||
//########################################################################################################### | ||
@@ -275,0 +318,0 @@ if (require.main === module) { |
{ | ||
"name": "letsfreezethat", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "An utterly minimal immutability library in the spirit of immer", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -173,10 +173,12 @@ | ||
It is sometimes desirable to freeze as many properties of a given object and still keep some properties in a | ||
mutable state; this is often the case when custom objects wrap objects from libraries one has no control | ||
over. For example, I recently ran into that conundrum when writing a library that accepts an object | ||
representing a database and some configuration in order to read from and write to the DB. That library will | ||
construct an object `{ foo: 42, bar: [...], db, }` to represent both the configuration and the DB instance; | ||
naturally, I would very much like to freeze the configurational part of that object, but I can't do that | ||
with that 3rd-party DB object. | ||
It is sometimes desirable to freeze as many properties of a given object as possible and still keep some | ||
properties in a mutable state; this is often the case when a custom object contains other objects from | ||
libraries one has no control over. | ||
For example, I recently ran into that conundrum when writing a library that accepts an object representing a | ||
database and some configuration in order to read from and write to the DB. That library will construct an | ||
object `{ foo: 42, bar: [...], db, }` to represent both the configuration and the DB instance; naturally, I | ||
would very much like to freeze the configurational part of that object, but I can't do that with that | ||
3rd-party DB instance which might rely on being mutable. | ||
This is where `(require 'letsfreezethat' ).partial` comes in. It offers the same methods as the standard | ||
@@ -229,3 +231,3 @@ version of LetsFreezeThat, but they are implemented (with `Object.seal()`) in such a way that *dynamic | ||
Here is | ||
Here is how one would typically use partial freezing and `lets_compute()`: | ||
@@ -232,0 +234,0 @@ ```coffee |
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
60914
790
249