structurae
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -7,2 +7,6 @@ # Changelog | ||
## [1.7.1] - 2019-11-09 | ||
### Added | ||
- Support custom types in nested ObjectViews | ||
## [1.7.0] - 2019-09-09 | ||
@@ -9,0 +13,0 @@ ### Added |
@@ -211,16 +211,3 @@ // Type definitions for structurae | ||
declare class ExtendedDataView extends DataView { | ||
protected getArray(position: number, field: ObjectViewField): ArrayLike<object>; | ||
protected getTypedArray(position: number, field: ObjectViewField): ArrayLike<number>; | ||
protected getObject(position: number, field: ObjectViewField): object; | ||
protected getString(position: number, field: ObjectViewField): object; | ||
protected setArray(position: number, value: ArrayLike<object>, field: ObjectViewField): void; | ||
protected setObject(position: number, value: object, field: ObjectViewField): void; | ||
protected setString(position: number, value: string, field: ObjectViewField): void; | ||
protected setStringArray(position: number, value: string[], field: ObjectViewField): void; | ||
protected setTypedArray(position: number, value: ArrayLike<number>, field: ObjectViewField): void; | ||
protected setValue(field: string, value: any, schema: ObjectViewSchema, offset: number): this; | ||
} | ||
declare class ArrayView extends ExtendedDataView { | ||
declare class ArrayView extends DataView { | ||
size: number; | ||
@@ -257,5 +244,6 @@ | ||
length?: number; | ||
view?: ViewType; | ||
View?: ViewType; | ||
getter?: string; | ||
setter?: string; | ||
itemLength?: number; | ||
} | ||
@@ -267,3 +255,3 @@ | ||
export declare class ObjectView extends ExtendedDataView { | ||
export declare class ObjectView extends DataView { | ||
static fields: string[]; | ||
@@ -275,4 +263,14 @@ static schema: ObjectViewSchema; | ||
get(field: string): number | View; | ||
private getArray(position: number, field: ObjectViewField): ArrayLike<object>; | ||
private getObject(position: number, field: ObjectViewField): object; | ||
private getString(position: number, field: ObjectViewField): string; | ||
private getStringArray(position: number, field: ObjectViewField): string[]; | ||
private getTypedArray(position: number, field: ObjectViewField): ArrayLike<number>; | ||
getValue(field: string): any; | ||
set(field: string, value: any): this; | ||
private setArray(position: number, value: ArrayLike<object>, field: ObjectViewField): void; | ||
private setObject(position: number, value: object, field: ObjectViewField): void; | ||
private setString(position: number, value: string, field: ObjectViewField): void; | ||
private setStringArray(position: number, value: string[], field: ObjectViewField): void; | ||
private setTypedArray(position: number, value: ArrayLike<number>, field: ObjectViewField): void; | ||
setView(field: string, value: View): this; | ||
@@ -279,0 +277,0 @@ toObject(): object; |
@@ -1,3 +0,1 @@ | ||
const ExtendedDataView = require('./extended-data-view'); | ||
/** | ||
@@ -13,3 +11,3 @@ * @param {Class<ObjectView>} ObjectViewClass | ||
*/ | ||
class ArrayView extends ExtendedDataView { | ||
class ArrayView extends DataView { | ||
/** | ||
@@ -35,3 +33,3 @@ * Returns an object view at a given index. | ||
getValue(index) { | ||
return this.getObject(index * this.constructor.objectLength, { view: ObjectViewClass }); | ||
return this.get(index).toJSON(); | ||
} | ||
@@ -47,3 +45,3 @@ | ||
set(index, value) { | ||
this.setObject(index * this.constructor.objectLength, value, { view: ObjectViewClass }); | ||
ObjectViewClass.from(value, this.get(index)); | ||
return this; | ||
@@ -104,3 +102,8 @@ } | ||
toJSON() { | ||
return this.getArray(0, { view: ObjectViewClass, size: this.size }); | ||
const { size } = this; | ||
const json = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
json[i] = this.get(i).toJSON(); | ||
} | ||
return json; | ||
} | ||
@@ -148,14 +151,3 @@ | ||
* @private | ||
* @type {Array<ObjectViewField>} | ||
*/ | ||
ArrayView.fields = ObjectViewClass.fields; | ||
/** | ||
* @private | ||
*/ | ||
ArrayView.schema = ObjectViewClass.schema; | ||
/** | ||
* @private | ||
*/ | ||
ArrayView.objectLength = ObjectViewClass.objectLength; | ||
@@ -162,0 +154,0 @@ |
@@ -39,3 +39,3 @@ /** | ||
* | ||
* @returns {Iterable<number>} | ||
* @returns {Iterable<View>} | ||
*/ | ||
@@ -42,0 +42,0 @@ * [Symbol.iterator]() { |
@@ -5,10 +5,6 @@ const StringView = require('./string-view'); | ||
const TypedArrayViewMixin = require('./typed-array-view'); | ||
const ExtendedDataView = require('./extended-data-view'); | ||
const { typeOffsets, typeGetters, typeSetters } = require('./utilities'); | ||
/** | ||
* @typedef {('int8' | 'uint8' | 'int16' | 'uint16' | ||
* | 'int32' | 'uint32' | 'float32' | 'float64' | ||
* | 'bigint64' | 'biguint64' | 'string' | 'array' | 'object' | 'typedarray' | ||
* | Class<ArrayView>|Class<ObjectView>|Class<TypedArrayView>)} ObjectViewFieldType | ||
* @typedef {(string|Class<ObjectView>)} ObjectViewFieldType | ||
*/ | ||
@@ -23,5 +19,7 @@ | ||
* @property {number} [start] | ||
* @property {Class<ArrayView>|Class<ObjectView>|Class<TypedArrayView>} [view] | ||
* @property {string} getter | ||
* @property {string} setter | ||
* @property {Class<ArrayView>|Class<ObjectView>|Class<TypedArrayView> | ||
* |Class<StringView>|Class<StringArrayView>} [View] | ||
* @property {string} [getter] | ||
* @property {string} [setter] | ||
* @property {number} [itemLength] | ||
*/ | ||
@@ -32,3 +30,3 @@ | ||
*/ | ||
class ObjectView extends ExtendedDataView { | ||
class ObjectView extends DataView { | ||
/** | ||
@@ -42,3 +40,3 @@ * Returns the value or view of a given field. | ||
const { | ||
getter, start, kind, littleEndian, view: View, length, itemLength, | ||
getter, start, kind, littleEndian, View, length, itemLength, | ||
} = this.constructor.schema[field]; | ||
@@ -50,2 +48,74 @@ if (kind === 'number') return this[getter](start, littleEndian); | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {Array<Object>} | ||
*/ | ||
getArray(position, field) { | ||
const { size, type: View, itemLength } = field; | ||
const json = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
const view = new View(this.buffer, this.byteOffset + position + (i * itemLength), itemLength); | ||
json[i] = view.toJSON(); | ||
} | ||
return json; | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {Object} | ||
*/ | ||
getObject(position, field) { | ||
const { View, length } = field; | ||
const view = new View(this.buffer, this.byteOffset + position, length); | ||
return view.toJSON(); | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {string} | ||
*/ | ||
getString(position, field) { | ||
return new StringView(this.buffer, this.byteOffset + position, field.length).toString(); | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {Array<string>} | ||
*/ | ||
getStringArray(position, field) { | ||
const { size, itemLength } = field; | ||
const array = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
const string = new StringView( | ||
this.buffer, this.byteOffset + position + (i * itemLength), itemLength, | ||
); | ||
array[i] = string.toString(); | ||
} | ||
return array; | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {Array<number>} | ||
*/ | ||
getTypedArray(position, field) { | ||
const { View, size } = field; | ||
const { typeGetter, offset, littleEndian } = View; | ||
const result = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
result[i] = this[typeGetter](position + (i << offset), littleEndian); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Returns the JavaScript value of a given field. | ||
@@ -71,3 +141,8 @@ * | ||
set(field, value) { | ||
this.setValue(field, value); | ||
const fieldOptions = this.constructor.schema[field]; | ||
const { | ||
kind, start, setter, littleEndian, | ||
} = fieldOptions; | ||
const arg = kind === 'number' ? littleEndian : fieldOptions; | ||
this[setter](start, value, arg); | ||
return this; | ||
@@ -77,2 +152,78 @@ } | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ArrayLike<object>} value | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setArray(position, value, field) { | ||
const { size, type: View, itemLength } = field; | ||
const max = (size < value.length ? size : value.length); | ||
for (let i = 0; i < max; i++) { | ||
const view = new View(this.buffer, this.byteOffset + position + (i * itemLength), itemLength); | ||
View.from(value[i], view); | ||
} | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {Object} value | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setObject(position, value, field) { | ||
const { View, length } = field; | ||
const view = new View(this.buffer, this.byteOffset + position, length); | ||
View.from(value, view); | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {string} value | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setString(position, value, field) { | ||
new Uint8Array(this.buffer, this.byteOffset + position, field.length) | ||
.fill(0) | ||
.set(StringView.fromString(value)); | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {string} value | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setStringArray(position, value, field) { | ||
const { length, size, itemLength } = field; | ||
const array = new Uint8Array(this.buffer, this.byteOffset + position, length); | ||
array.fill(0); | ||
const max = (size < value.length ? size : value.length); | ||
for (let i = 0; i < max; i++) { | ||
const string = StringView.fromString(value[i], itemLength); | ||
array.set(string, i * itemLength); | ||
} | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {ArrayLike<number>} value | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setTypedArray(position, value, field) { | ||
const { View, size } = field; | ||
const { typeSetter, offset, littleEndian } = View; | ||
const max = (size < value.length ? size : value.length); | ||
for (let i = 0; i < max; i++) { | ||
this[typeSetter](position + (i << offset), value[i], littleEndian); | ||
} | ||
} | ||
/** | ||
* Sets an ObjectView value to a field. | ||
@@ -110,3 +261,9 @@ * | ||
toJSON() { | ||
return this.getObject(0, { view: this.constructor }); | ||
const { fields } = this.constructor; | ||
const result = {}; | ||
for (let i = 0; i < fields.length; i++) { | ||
const name = fields[i]; | ||
result[name] = this.getValue(name); | ||
} | ||
return result; | ||
} | ||
@@ -124,3 +281,7 @@ | ||
const objectView = view || new this(new ArrayBuffer(this.getLength())); | ||
objectView.setObject(0, object, { view: objectView.constructor }); | ||
const { fields } = objectView.constructor; | ||
for (let i = 0; i < fields.length; i++) { | ||
const name = fields[i]; | ||
if (Reflect.has(object, name)) objectView.set(name, object[name]); | ||
} | ||
return objectView; | ||
@@ -140,3 +301,2 @@ } | ||
/** | ||
* @private | ||
* @returns {void} | ||
@@ -214,4 +374,4 @@ */ | ||
const { type, littleEndian, size } = field; | ||
field.view = TypedArrayViewMixin(type, littleEndian); | ||
field.length = field.view.getLength(size); | ||
field.View = TypedArrayViewMixin(type, littleEndian); | ||
field.length = field.View.getLength(size); | ||
field.getter = 'getTypedArray'; | ||
@@ -225,3 +385,3 @@ field.setter = 'setTypedArray'; | ||
string(field) { | ||
field.view = StringView; | ||
field.View = StringView; | ||
field.getter = 'getString'; | ||
@@ -236,3 +396,3 @@ field.setter = 'setString'; | ||
const { length, size } = field; | ||
field.view = StringArrayView; | ||
field.View = StringArrayView; | ||
field.length = StringArrayView.getLength(size, length); | ||
@@ -249,3 +409,3 @@ field.getter = 'getStringArray'; | ||
const { type } = field; | ||
field.view = type; | ||
field.View = type; | ||
field.length = type.getLength(); | ||
@@ -261,6 +421,7 @@ field.getter = 'getObject'; | ||
const { type, size } = field; | ||
field.view = ArrayViewMixin(type); | ||
field.length = field.view.getLength(size); | ||
field.View = ArrayViewMixin(type); | ||
field.length = field.View.getLength(size); | ||
field.getter = 'getArray'; | ||
field.setter = 'setArray'; | ||
field.itemLength = type.objectLength; | ||
}, | ||
@@ -267,0 +428,0 @@ }; |
{ | ||
"name": "structurae", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
197780
29
5576