structurae
Advanced tools
Comparing version 3.0.3 to 3.0.4
@@ -7,2 +7,7 @@ # Changelog | ||
## [3.0.4] - 2020-04-05 | ||
### Added | ||
- Export TypeView class | ||
- Add *View.Array field to allow using custom ArrayView classes | ||
## [3.0.3] - 2020-04-03 | ||
@@ -9,0 +14,0 @@ ### Added |
@@ -207,6 +207,8 @@ // Type definitions for structurae | ||
declare class TypeView extends DataView { | ||
export declare class TypeView extends DataView { | ||
static offset: number; | ||
static littleEndian: true; | ||
static objectLength: number; | ||
static Views: Map<string, typeof TypeView>; | ||
static Array: typeof ArrayView; | ||
@@ -227,3 +229,4 @@ get(): number; | ||
export declare function TypeViewMixin(type: PrimitiveFieldType, littleEndian?: boolean): typeof TypeView; | ||
export declare function TypeViewMixin(type: PrimitiveFieldType, littleEndian?: boolean, | ||
TypeViewClass?: typeof TypeView): typeof TypeView; | ||
@@ -234,2 +237,3 @@ export declare class ArrayView extends DataView { | ||
static View: ViewType; | ||
static Array: typeof ArrayView; | ||
@@ -288,2 +292,3 @@ get(index: number): any; | ||
static Views: ViewTypes; | ||
static Array: typeof ArrayView; | ||
static types: ObjectViewTypeDefs; | ||
@@ -315,2 +320,3 @@ static objectLength: number; | ||
static decoder: TextDecoder; | ||
static Array: typeof ArrayView; | ||
@@ -317,0 +323,0 @@ characters(): Iterable<string>; |
@@ -22,3 +22,3 @@ const BitField = require('./lib/bit-field'); | ||
const BinaryProtocol = require('./lib/binary-protocol'); | ||
const TypeViewMixin = require('./lib/type-view'); | ||
const { TypeView, TypeViewMixin } = require('./lib/type-view'); | ||
const BooleanView = require('./lib/boolean-view'); | ||
@@ -65,4 +65,5 @@ | ||
BinaryProtocol, | ||
TypeView, | ||
TypeViewMixin, | ||
BooleanView, | ||
}; |
const ArrayView = require('./array-view'); | ||
const TypedArrayView = require('./typed-array-view'); | ||
const TypeViewMixin = require('./type-view'); | ||
const { TypeViewMixin } = require('./type-view'); | ||
/** | ||
* @private | ||
* @type {WeakMap<Class<View>, Class<ArrayView>>} | ||
*/ | ||
const ArrayViews = new WeakMap(); | ||
/** | ||
* Creates an ArrayView class for a given ObjectView class. | ||
@@ -19,2 +12,3 @@ * | ||
function ArrayViewMixin(ObjectViewClass, itemLength) { | ||
const ArrayViews = ArrayView.Views; | ||
let ViewClass = ObjectViewClass; | ||
@@ -29,7 +23,6 @@ if (typeof ObjectViewClass === 'string') { | ||
} | ||
const ArrayViewClass = Reflect.has(ViewClass, 'offset') ? TypedArrayView : ArrayView; | ||
const View = class extends ArrayViewClass {}; | ||
const View = class extends ViewClass.Array {}; | ||
View.View = ViewClass; | ||
View.itemLength = ViewClass.objectLength || itemLength; | ||
ArrayViews.set(ViewClass, View); | ||
if (typeof itemLength !== 'number') ArrayViews.set(ViewClass, View); | ||
return View; | ||
@@ -36,0 +29,0 @@ } |
@@ -175,2 +175,12 @@ /** | ||
/** | ||
* @type {WeakMap<Class<View>, Class<ArrayView>>} | ||
*/ | ||
ArrayView.Views = new WeakMap(); | ||
/** | ||
* @type {Class<ArrayView>} | ||
*/ | ||
ArrayView.Array = ArrayView; | ||
module.exports = ArrayView; |
@@ -1,2 +0,2 @@ | ||
const TypeViewMixin = require('./type-view'); | ||
const { TypeViewMixin } = require('./type-view'); | ||
/** | ||
@@ -3,0 +3,0 @@ * @extends {TypeView} |
const StringView = require('./string-view'); | ||
const ArrayView = require('./array-view'); | ||
const ArrayViewMixin = require('./array-view-mixin'); | ||
const TypeViewMixin = require('./type-view'); | ||
const { TypeViewMixin } = require('./type-view'); | ||
const BooleanView = require('./boolean-view'); | ||
@@ -368,2 +369,7 @@ | ||
/** | ||
* @type {Class<ArrayView>} | ||
*/ | ||
ObjectView.Array = ArrayView; | ||
/** | ||
* Creates an ObjectView class with a given schema. | ||
@@ -370,0 +376,0 @@ * |
@@ -0,1 +1,3 @@ | ||
const ArrayView = require('./array-view'); | ||
/** | ||
@@ -386,2 +388,7 @@ * @private | ||
/** | ||
* @type {Class<ArrayView>} | ||
*/ | ||
StringView.Array = ArrayView; | ||
/* istanbul ignore next */ | ||
@@ -388,0 +395,0 @@ if (!StringView.encoder.encodeInto) { |
const { typeGetters, typeSetters, typeOffsets } = require('./utilities'); | ||
const TypedArrayView = require('./typed-array-view'); | ||
/** | ||
* @private | ||
* @type {Map<string, Class<TypeView>>} | ||
* @extends {DataView} | ||
*/ | ||
const TypeViews = new Map(); | ||
class TypeView extends DataView { | ||
/** | ||
* Returns the numerical value of the view. | ||
* | ||
* @returns {number} | ||
*/ | ||
get() { | ||
return this.constructor.toJSON(this); | ||
} | ||
/** | ||
* @param {PrimitiveFieldType} type | ||
* @param {boolean} [littleEndian] | ||
* @returns {Class<TypeView>} | ||
*/ | ||
function TypeViewMixin(type, littleEndian = true) { | ||
const classId = type + +!!littleEndian; | ||
if (TypeViews.has(classId)) return TypeViews.get(classId); | ||
/** | ||
* Sets the numerical value of the view. | ||
* | ||
* @param {number} value | ||
* @returns {TypeView} | ||
*/ | ||
set(value) { | ||
this.constructor.from(value, this); | ||
return this; | ||
} | ||
const getter = typeGetters[type]; | ||
const setter = typeSetters[type]; | ||
const offset = typeOffsets[type]; | ||
/** | ||
* Returns the numerical value of the view. | ||
* | ||
* @returns {number} | ||
*/ | ||
toJSON() { | ||
return this.constructor.toJSON(this); | ||
} | ||
/** | ||
* @extends {DataView} | ||
* Returns the length of a view. | ||
* | ||
* @returns {number} | ||
*/ | ||
class TypeView extends DataView { | ||
/** | ||
* Returns the numerical value of the view. | ||
* | ||
* @returns {number} | ||
*/ | ||
get() { | ||
return this.constructor.toJSON(this); | ||
} | ||
/** | ||
* Sets the numerical value of the view. | ||
* | ||
* @param {number} value | ||
* @returns {TypeView} | ||
*/ | ||
set(value) { | ||
this.constructor.from(value, this); | ||
return this; | ||
} | ||
/** | ||
* Returns the numerical value of the view. | ||
* | ||
* @returns {number} | ||
*/ | ||
toJSON() { | ||
return this.constructor.toJSON(this); | ||
} | ||
/** | ||
* Returns the length of a view. | ||
* | ||
* @returns {number} | ||
*/ | ||
static getLength() { | ||
return this.objectLength; | ||
} | ||
/** | ||
* Creates a view with a given value. | ||
* | ||
* @param {number} value | ||
* @param {View} [view] | ||
* @param {number} [start=0] | ||
* @returns {View} | ||
*/ | ||
static from(value, view, start = 0) { | ||
const typeView = view || this.of(); | ||
setter.call(typeView, start, value, this.littleEndian); | ||
return typeView; | ||
} | ||
/** | ||
* Creates an empty view. | ||
* | ||
* @returns {TypeView} | ||
*/ | ||
static of() { | ||
return new this(new ArrayBuffer(this.objectLength)); | ||
} | ||
/** | ||
* Returns the numerical value of a given view. | ||
* | ||
* @param {View} view | ||
* @param {number} [start=0] | ||
* @returns {number} | ||
* | ||
*/ | ||
static toJSON(view, start = 0) { | ||
return getter.call(view, start, this.littleEndian); | ||
} | ||
static getLength() { | ||
return this.objectLength; | ||
} | ||
/** | ||
* @type {number} | ||
* Creates a view with a given value. | ||
* | ||
* @param {number} value | ||
* @param {View} [view] | ||
* @param {number} [start=0] | ||
* @returns {View} | ||
*/ | ||
TypeView.offset = offset; | ||
static from(value, view, start = 0) { | ||
const typeView = view || this.of(); | ||
this.setter.call(typeView, start, value, this.littleEndian); | ||
return typeView; | ||
} | ||
/** | ||
* @type {boolean} | ||
* Creates an empty view. | ||
* | ||
* @returns {TypeView} | ||
*/ | ||
TypeView.littleEndian = !!littleEndian; | ||
static of() { | ||
return new this(new ArrayBuffer(this.objectLength)); | ||
} | ||
/** | ||
* @type {number} | ||
* Returns the numerical value of a given view. | ||
* | ||
* @param {View} view | ||
* @param {number} [start=0] | ||
* @returns {number} | ||
* | ||
*/ | ||
TypeView.objectLength = 1 << TypeView.offset; | ||
static toJSON(view, start = 0) { | ||
return this.getter.call(view, start, this.littleEndian); | ||
} | ||
} | ||
TypeViews.set(classId, TypeView); | ||
return TypeView; | ||
TypeView.getter = DataView.prototype.getUint8; | ||
TypeView.setter = DataView.prototype.setUint8; | ||
/** | ||
* @type {number} | ||
*/ | ||
TypeView.offset = 0; | ||
/** | ||
* @type {boolean} | ||
*/ | ||
TypeView.littleEndian = true; | ||
/** | ||
* @type {number} | ||
*/ | ||
TypeView.objectLength = 1; | ||
/** | ||
* @type {Map<string, Class<TypeView>>} | ||
*/ | ||
TypeView.Views = new Map(); | ||
/** | ||
* @type {Class<ArrayView>} | ||
*/ | ||
TypeView.Array = TypedArrayView; | ||
/** | ||
* @param {PrimitiveFieldType} type | ||
* @param {boolean} [littleEndian] | ||
* @param {Class<TypeView>} [TypeViewClass] | ||
* @returns {Class<TypeView>} | ||
*/ | ||
function TypeViewMixin(type, littleEndian = true, TypeViewClass = TypeView) { | ||
const classId = type + +!!littleEndian; | ||
const TypeViews = TypeView.Views; | ||
if (TypeViews.has(classId)) return TypeViews.get(classId); | ||
const View = class extends TypeViewClass {}; | ||
View.getter = typeGetters[type]; | ||
View.setter = typeSetters[type]; | ||
View.offset = typeOffsets[type]; | ||
View.littleEndian = !!littleEndian; | ||
View.objectLength = 1 << View.offset; | ||
TypeViews.set(classId, View); | ||
return View; | ||
} | ||
module.exports = TypeViewMixin; | ||
module.exports = { | ||
TypeView, | ||
TypeViewMixin, | ||
}; |
{ | ||
"name": "structurae", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -48,3 +48,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/jest": "^25.1.4", | ||
"@types/jest": "^25.2.1", | ||
"benchmark": "^2.1.4", | ||
@@ -54,3 +54,3 @@ "eslint": "^6.8.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"jest": "^25.2.4", | ||
"jest": "^25.2.7", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
@@ -57,0 +57,0 @@ "json-schema-faker": "^0.5.0-rcv.24" |
216598
5806