Comparing version 12.0.1 to 12.1.0
(function() { | ||
'use strict'; | ||
var Datom, GUY, MAIN, alert, assign, debug, echo, get_base_types, help, info, inspect, letsfreezethat, letsfreezethat_nofreeze, log, matcher_cache, plain, praise, rpr, urge, warn, whisper; | ||
var Dataclass, Datom, GUY, MAIN, alert, assign, debug, echo, get_base_types, help, info, inspect, letsfreezethat, letsfreezethat_nofreeze, log, matcher_cache, plain, praise, rpr, urge, warn, whisper; | ||
@@ -197,6 +197,65 @@ //########################################################################################################### | ||
Dataclass = (function() { | ||
//=========================================================================================================== | ||
class Dataclass { | ||
//--------------------------------------------------------------------------------------------------------- | ||
static new_datom(x) { | ||
return new Proxy(x, { | ||
//....................................................................................................... | ||
get: function(target, key, receiver) { | ||
if (!Object.isFrozen(target)) { | ||
Object.freeze(target); | ||
} | ||
return Reflect.get(target, key, receiver); | ||
}, | ||
//....................................................................................................... | ||
set: function(target, key, value, receiver) { | ||
if (!Object.isFrozen(target)) { | ||
Object.freeze(target); | ||
} | ||
throw new TypeError(`Cannot assign to read only property ${rpr(key)} of object ${rpr(target)}`); | ||
} | ||
}); | ||
} | ||
//--------------------------------------------------------------------------------------------------------- | ||
constructor(cfg) { | ||
var R, __types, clasz, declaration, freezemode, k, paragon, ref, ref1, v; | ||
clasz = this.constructor; | ||
__types = (ref = clasz.types) != null ? ref : new (require('intertype')).Intertype(); | ||
GUY.props.hide(this, '__types', __types); | ||
declaration = clasz.declaration; | ||
freezemode = (ref1 = declaration != null ? declaration.freeze : void 0) != null ? ref1 : 'deep'; | ||
if (declaration != null) { | ||
this.__types.declare[clasz.name](declaration); | ||
paragon = this.__types.create[clasz.name](cfg); | ||
if (freezemode === 'deep') { | ||
paragon = GUY.lft.freeze(paragon); | ||
} | ||
for (k in paragon) { | ||
v = paragon[k]; | ||
this[k] = v; | ||
} | ||
} | ||
if (freezemode === false) { | ||
return void 0; | ||
} | ||
(R = clasz.new_datom(this))[Symbol('test')]; | ||
return R; | ||
} | ||
}; | ||
//--------------------------------------------------------------------------------------------------------- | ||
Dataclass.declaration = null; | ||
return Dataclass; | ||
}).call(this); | ||
//=========================================================================================================== | ||
module.exports = { | ||
Datom, | ||
DATOM: new Datom() | ||
DATOM: new Datom(), | ||
Dataclass | ||
}; | ||
@@ -203,0 +262,0 @@ |
@@ -43,3 +43,3 @@ (function() { | ||
}, | ||
/* TAINT ??? to be removed ??? */default: { | ||
/* TAINT ??? to be removed ??? */template: { | ||
merge_values: true, | ||
@@ -80,3 +80,3 @@ freeze: true, | ||
}, | ||
default: { | ||
template: { | ||
$key: null, | ||
@@ -83,0 +83,0 @@ $stamped: null, |
{ | ||
"name": "datom", | ||
"version": "12.0.1", | ||
"version": "12.1.0", | ||
"description": "standardized immutable objects in the spirit of datomic, especially suited for use in data pipelines", | ||
@@ -27,3 +27,3 @@ "main": "lib/main.js", | ||
"guy": "^13.5.0", | ||
"intertype": "0.114.0", | ||
"intertype": "0.115.0", | ||
"letsfreezethat": "^3.1.0" | ||
@@ -30,0 +30,0 @@ }, |
@@ -11,2 +11,3 @@ | ||
- [Dataclasses](#dataclasses) | ||
- [Export Bound Methods](#export-bound-methods) | ||
@@ -33,3 +34,35 @@ - [Creation of Bespoke Library Instances](#creation-of-bespoke-library-instances) | ||
## Dataclasses | ||
* Dataclasses allow to marry ES6 classes and Intertype type declarations | ||
* derive your class from `( require 'datom' ).Dataclass` | ||
* declare class property `declaration` as an [Intertype | ||
declaration](https://github.com/loveencounterflow/intertype/blob/main/README-declare.md) | ||
* simple example: | ||
```coffee | ||
class Quantity extends Dataclass | ||
@declaration: | ||
fields: | ||
q: 'float' | ||
u: 'nonempty.text' | ||
template: | ||
q: 0 | ||
u: 'unit' | ||
``` | ||
* now, when you do `q = new Quantity()`, you get a (shallowly) frozen object with properties `{ q: 0, u: | ||
'unit', }` which is the default value for that class (representing the most generic measurement, zero | ||
dimensionless units) | ||
* this is probably not very useful, so pass in values to override defaults, as in `new Quantity { u: 'km', | ||
}` to define a length or `new Quantity { q: 12.5, u: 's', }` to define a time span | ||
* can modify using `quantity = DATOM.lets quantity, ( quantity ) -> quantity.q = 120` | ||
* by default, instances of derivatives of `Dataclass` are deep-frozen, meaning not the instance itself nor | ||
its properties can be mutated | ||
* default can be made explicit by adding `freeze: 'deep'` to type declaration | ||
* can also dow shallow freezing by setting `freeze: true`; in that case, properties like lists and objects | ||
can still be mutated, but properties can not be reassigned, added or deleted | ||
* setting `freeze: false` will result in a fully mutable object | ||
* `DATOM.thaw x` can always be used to obtain a fully mutable copy where that is called for | ||
**NOTE: Documentation is outdated. WIP.** | ||
@@ -394,4 +427,6 @@ | ||
* [ ] implement wildcards for `select()`; cache selectors to avoid re-interpretation of recurrent patterns | ||
* [ ] dataclasses should optionally be mutable | ||
* [ ] make deep-freezing the default for `Dataclass`? | ||
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
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
61713
313
430
+ Addedintertype@0.115.0(transitive)
- Removedintertype@0.114.0(transitive)
Updatedintertype@0.115.0