structurae
Advanced tools
Comparing version 1.7.5 to 1.8.0
@@ -7,2 +7,10 @@ # Changelog | ||
## [1.8.0] - 2019-09-24 | ||
### Added | ||
- Support default field values in ObjectView | ||
### Changed | ||
- (potentially breaking) `ObjectView.from` no longer initializes ObjectView upon the first call. | ||
Call `ObjectView.intialize()` upon setting the schema (`ObjectView.schema`) for the extending class, or use ObjectViewMixin | ||
## [1.7.5] - 2019-09-22 | ||
@@ -9,0 +17,0 @@ ### Added |
@@ -206,3 +206,3 @@ const StringView = require('./string-view'); | ||
static from(object, view) { | ||
const objectView = view || new this(new ArrayBuffer(this.getLength())); | ||
const objectView = view || new this(this.defaultBuffer.slice()); | ||
if (view) { | ||
@@ -222,2 +222,22 @@ // zero out existing view | ||
/** | ||
* @private | ||
* @returns {void} | ||
*/ | ||
static setDefaultBuffer() { | ||
const { objectLength, fields, schema } = this; | ||
const buffer = new ArrayBuffer(objectLength); | ||
const view = new this(buffer); | ||
for (let i = 0; i < fields.length; i++) { | ||
const name = fields[i]; | ||
const field = schema[name]; | ||
if (Reflect.has(field, 'default')) { | ||
view.set(name, field.default); | ||
} else if (field.View.defaultBuffer) { | ||
new Uint8Array(buffer).set(new Uint8Array(field.View.defaultBuffer), field.start); | ||
} | ||
} | ||
this.defaultBuffer = buffer; | ||
} | ||
/** | ||
* Returns the byte length of an object view. | ||
@@ -228,3 +248,2 @@ * | ||
static getLength() { | ||
if (!this.isInitialized) this.initialize(); | ||
return this.objectLength; | ||
@@ -258,2 +277,3 @@ } | ||
this.isInitialized = true; | ||
this.setDefaultBuffer(); | ||
} | ||
@@ -365,2 +385,8 @@ | ||
/** | ||
* @private | ||
* @type {ArrayBuffer} | ||
*/ | ||
ObjectView.defaultBuffer = undefined; | ||
/** @type {boolean} */ | ||
@@ -367,0 +393,0 @@ ObjectView.isInitialized = false; |
@@ -350,3 +350,4 @@ /** | ||
mapFn.fill(0); | ||
this.encoder.encodeInto(arrayLike, mapFn); | ||
// fix for Node.js 12.11, todo remove when encodeInto fixed in Node.js | ||
if (arrayLike) this.encoder.encodeInto(arrayLike, mapFn); | ||
return mapFn; | ||
@@ -396,2 +397,3 @@ } | ||
/* istanbul ignore next */ | ||
if (!StringView.encoder.encodeInto) { | ||
@@ -398,0 +400,0 @@ /** |
{ | ||
"name": "structurae", | ||
"version": "1.7.5", | ||
"version": "1.8.0", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,3 +65,3 @@ # Structurae | ||
const House = ObjectViewMixin({ | ||
size: { type: 'uint32' }, // a primitive type | ||
size: { type: 'uint32', default: 100 }, // a primitive type (unsigned 32-bit integer) that defaults to 100 | ||
}); | ||
@@ -110,2 +110,27 @@ | ||
ObjectView supports setting default values of fields. Default values are applied upon creation of a view: | ||
```javascript | ||
const House = ObjectViewMixin({ | ||
size: { type: 'uint32', default: 100 } | ||
}); | ||
const house = House.from({}); | ||
house.get('size'); | ||
//=> 100 | ||
``` | ||
Default values of a ObjectView can be overridden when the view is used as a field inside other views: | ||
```javascript | ||
const Neighborhood = ObjectViewMixin({ | ||
house: { type: House }, | ||
biggerHouse: { type: House, default: { size: 200 } }, | ||
}); | ||
const neighborhood = Neighborhood.from({}); | ||
neighborhood.get('house') | ||
//=> { size: 100 } | ||
neighborhood.get('biggerHouse') | ||
//=> { size: 200 } | ||
``` | ||
You can add your own field types to ObjectView, for example an ObjectView that supports booleans: | ||
@@ -112,0 +137,0 @@ ```javascript |
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
201542
5642
915