structurae
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -7,2 +7,12 @@ # Changelog | ||
## [1.7.0] - 2019-09-09 | ||
### Added | ||
- Support custom types in ObjectView | ||
- Add getValue methods to View classes | ||
- Add toJSON methods and deprecate toObject in View classes | ||
## [1.6.1] - 2019-08-20 | ||
### Fixed | ||
- Add iterators in TypeScript definitions | ||
## [1.6.0] - 2019-08-17 | ||
@@ -9,0 +19,0 @@ ### Added |
@@ -212,10 +212,12 @@ // Type definitions for structurae | ||
declare class ExtendedDataView extends DataView { | ||
getArray(position: number, ctor: typeof ObjectView | typeof ArrayView, size: number): ArrayLike<object>; | ||
getTypedArray(position: number, ctor: typeof TypedArrayView, size: number): ArrayLike<number>; | ||
getObject(schema: ObjectViewSchema, offset: number): object; | ||
setArray(position: number, value: ArrayLike<object>, ctor: ViewType, size: number): void; | ||
setObject(position: number, value: object, ctor: ViewType): void; | ||
setString(position: number, value: string, length: number): void; | ||
setTypedArray(position: number, value: ArrayLike<number>, ctor: typeof TypedArrayView, size: number): void; | ||
setValue(field: string, value: any, schema: ObjectViewSchema, offset: number): this; | ||
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; | ||
} | ||
@@ -227,5 +229,7 @@ | ||
get(index: number): ObjectView; | ||
getValue(index: number): object; | ||
set(index: number, value: object): this; | ||
setView(index: number, value: ObjectView): this; | ||
toObject(): object[]; | ||
toJSON(): object[]; | ||
[Symbol.iterator](): IterableIterator<ObjectView>; | ||
@@ -246,3 +250,3 @@ static from(value: ArrayLike<object>, array?: ArrayView): ArrayView; | ||
type ObjectViewFieldType = PrimitiveFieldType | 'string' | 'array' | 'object' | 'typedarray' | ViewType; | ||
type ObjectViewFieldType = PrimitiveFieldType| string | ViewType; | ||
@@ -255,3 +259,5 @@ interface ObjectViewField { | ||
length?: number; | ||
ctor?: ViewType; | ||
view?: ViewType; | ||
getter?: string; | ||
setter?: string; | ||
} | ||
@@ -270,9 +276,11 @@ | ||
get(field: string): number | View; | ||
private getView(position: number, length: number, ctor: ViewType): View; | ||
getValue(field: string): any; | ||
set(field: string, value: any): this; | ||
setView(field: string, value: View): this; | ||
toObject(): object; | ||
toJSON(): object; | ||
static from(object: object, objectView?: ObjectView): ObjectView; | ||
static getLength(): number; | ||
static initialize(): void; | ||
private static getFieldKind(field: ObjectViewField): string; | ||
} | ||
@@ -298,2 +306,3 @@ | ||
toString(): string; | ||
toJSON(): string; | ||
trim(): StringView; | ||
@@ -311,7 +320,10 @@ static fromString(string: string, size?: number): StringView; | ||
get(index: number): StringView; | ||
set(index: number, value: string|StringView): this; | ||
getValue(index: number): string; | ||
set(index: number, value: string): this; | ||
setView(index: number, value: Uint8Array): this; | ||
toObject(): Array<string>; | ||
toJSON(): Array<string>; | ||
static from(value: ArrayLike<string>, stringLength: number, array?: StringArrayView): StringArrayView; | ||
static of(size: number, stringLength: number): StringArrayView; | ||
static getLength(size: number, stringLength: number): number; | ||
static of(size: number, stringLength: number): StringArrayView; | ||
} | ||
@@ -328,3 +340,4 @@ | ||
set(index: number, value: number): this; | ||
toObject(): object; | ||
toObject(): Array<number>; | ||
toJSON(): Array<number>; | ||
[Symbol.iterator](): IterableIterator<number>; | ||
@@ -344,2 +357,3 @@ static getLength(size: number): number; | ||
toObject(): object[]; | ||
toJSON(): object[]; | ||
[Symbol.iterator](): IterableIterator<View>; | ||
@@ -346,0 +360,0 @@ static from(value: object[], array?: CollectionView): CollectionView; |
@@ -15,3 +15,3 @@ const ExtendedDataView = require('./extended-data-view'); | ||
/** | ||
* Returns an object at a given index. | ||
* Returns an object view at a given index. | ||
* | ||
@@ -29,2 +29,12 @@ * @param {number} index | ||
/** | ||
* Returns an object at a given index. | ||
* | ||
* @param {number} index | ||
* @returns {Object} | ||
*/ | ||
getValue(index) { | ||
return this.getObject(index * this.constructor.objectLength, { view: ObjectViewClass }); | ||
} | ||
/** | ||
* Sets an object at a given index. | ||
@@ -37,3 +47,3 @@ * | ||
set(index, value) { | ||
this.setObject(index * this.constructor.objectLength, value, this.constructor); | ||
this.setObject(index * this.constructor.objectLength, value, { view: ObjectViewClass }); | ||
return this; | ||
@@ -79,2 +89,3 @@ } | ||
/** | ||
* @deprecated use `ArrayView#toJSON()` instead. | ||
* Returns an array representation of the array view. | ||
@@ -85,6 +96,15 @@ * | ||
toObject() { | ||
return this.getArray(0, this.constructor, this.size); | ||
return this.toJSON(); | ||
} | ||
/** | ||
* Returns an array representation of the array view. | ||
* | ||
* @returns {Array<Object>} | ||
*/ | ||
toJSON() { | ||
return this.getArray(0, { view: ObjectViewClass, size: this.size }); | ||
} | ||
/** | ||
* Creates an array view from a given array of objects. | ||
@@ -91,0 +111,0 @@ * |
@@ -49,2 +49,3 @@ /** | ||
/** | ||
* @deprecated | ||
* Returns an array representation of the collection view. | ||
@@ -55,2 +56,11 @@ * | ||
toObject() { | ||
return this.toJSON(); | ||
} | ||
/** | ||
* Returns an array representation of the collection view. | ||
* | ||
* @returns {Array<Object>} | ||
*/ | ||
toJSON() { | ||
const { schema } = this.constructor; | ||
@@ -57,0 +67,0 @@ const array = new Array(schema.length); |
@@ -6,12 +6,13 @@ const StringView = require('./string-view'); | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {Class<ArrayView>|Class<ObjectView>} ctor | ||
* @param {number} size | ||
* @param {ObjectViewField} field | ||
* @returns {Array<Object>} | ||
*/ | ||
getArray(position, ctor, size) { | ||
const { schema, objectLength } = ctor; | ||
getArray(position, field) { | ||
const { view, size } = field; | ||
const { objectLength } = view; | ||
const result = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
result[i] = this.getObject(schema, position + (i * objectLength)); | ||
result[i] = this.getObject(position + (i * objectLength), { view }); | ||
} | ||
@@ -22,13 +23,13 @@ return result; | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {number} size | ||
* @param {number} length | ||
* @param {number} stringLength | ||
* @param {ObjectViewField} field | ||
* @returns {Array<string>} | ||
*/ | ||
getStringArray(position, size, length, stringLength) { | ||
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 * stringLength), stringLength, | ||
this.buffer, this.byteOffset + position + (i * itemLength), itemLength, | ||
); | ||
@@ -41,9 +42,10 @@ array[i] = string.toString(); | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {Class<TypedArrayView>} ctor | ||
* @param {number} size | ||
* @param {ObjectViewField} field | ||
* @returns {Array<number>} | ||
*/ | ||
getTypedArray(position, ctor, size) { | ||
const { typeGetter, offset, littleEndian } = ctor; | ||
getTypedArray(position, field) { | ||
const { view, size } = field; | ||
const { typeGetter, offset, littleEndian } = view; | ||
const result = new Array(size); | ||
@@ -57,7 +59,9 @@ for (let i = 0; i < size; i++) { | ||
/** | ||
* @param {object} schema | ||
* @param {number} offset | ||
* @protected | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {Object} | ||
*/ | ||
getObject(schema, offset) { | ||
getObject(position, field) { | ||
const { schema } = field.view; | ||
const fields = Object.keys(schema); | ||
@@ -67,25 +71,7 @@ const result = {}; | ||
const name = fields[i]; | ||
const { | ||
type, littleEndian, start, ctor: Ctor, length, size, stringLength, | ||
} = schema[name]; | ||
let value; | ||
const position = offset + start; | ||
switch (type) { | ||
case 'int8': value = this.getInt8(position); break; | ||
case 'uint8': value = this.getUint8(position); break; | ||
case 'int16': value = this.getInt16(position, littleEndian); break; | ||
case 'uint16': value = this.getUint16(position, littleEndian); break; | ||
case 'int32': value = this.getInt32(position, littleEndian); break; | ||
case 'uint32': value = this.getUint32(position, littleEndian); break; | ||
case 'float32': value = this.getFloat32(position, littleEndian); break; | ||
case 'float64': value = this.getFloat64(position, littleEndian); break; | ||
case 'bigint64': value = this.getBigInt64(position, littleEndian); break; | ||
case 'biguint64': value = this.getBigUint64(position, littleEndian); break; | ||
case 'string': value = new StringView(this.buffer, this.byteOffset + position, length).toString(); break; | ||
case 'typedarray': value = this.getTypedArray(position, Ctor, size); break; | ||
case 'stringarray': value = this.getStringArray(position, size, length, stringLength); break; | ||
case 'array': value = this.getArray(position, Ctor, size); break; | ||
default: value = this.getObject(Ctor.schema, position); | ||
} | ||
result[name] = value; | ||
const currentField = schema[name]; | ||
const { start, getter, kind } = currentField; | ||
const offset = position + start; | ||
const arg = kind === 'number' ? currentField.littleEndian : currentField; | ||
result[name] = this[getter](offset, arg); | ||
} | ||
@@ -96,10 +82,21 @@ return result; | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {ObjectViewField} field | ||
* @returns {string} | ||
*/ | ||
getString(position, field) { | ||
return new StringView(this.buffer, this.byteOffset + position, field.length).toString(); | ||
} | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {ArrayLike<object>} value | ||
* @param {Class<ArrayView>|Class<ObjectView>} ctor | ||
* @param {number} size | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setArray(position, value, ctor, size) { | ||
const { fields, schema } = ctor; | ||
setArray(position, value, field) { | ||
const { view, size } = field; | ||
const { fields, schema } = view; | ||
const max = (size < value.length ? size : value.length); | ||
@@ -109,3 +106,3 @@ for (let i = 0; i < max; i++) { | ||
const name = fields[j]; | ||
this.setValue(name, value[i][name], schema, position + (i * ctor.objectLength)); | ||
this.setValue(name, value[i][name], schema, position + (i * view.objectLength)); | ||
} | ||
@@ -116,9 +113,10 @@ } | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {Object} value | ||
* @param {Class<ObjectView>} ctor | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setObject(position, value, ctor) { | ||
const { fields, schema } = ctor; | ||
setObject(position, value, field) { | ||
const { fields, schema } = field.view; | ||
for (let i = 0; i < fields.length; i++) { | ||
@@ -131,9 +129,10 @@ const name = fields[i]; | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {string} value | ||
* @param {number} length | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setString(position, value, length) { | ||
new Uint8Array(this.buffer, this.byteOffset + position, length) | ||
setString(position, value, field) { | ||
new Uint8Array(this.buffer, this.byteOffset + position, field.length) | ||
.fill(0) | ||
@@ -144,10 +143,10 @@ .set(StringView.fromString(value)); | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {string} value | ||
* @param {number} size | ||
* @param {number} length | ||
* @param {number} stringLength | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setStringArray(position, value, size, length, stringLength) { | ||
setStringArray(position, value, field) { | ||
const { length, size, itemLength } = field; | ||
const array = new Uint8Array(this.buffer, this.byteOffset + position, length); | ||
@@ -157,4 +156,4 @@ array.fill(0); | ||
for (let i = 0; i < max; i++) { | ||
const string = StringView.fromString(value[i], stringLength); | ||
array.set(string, i * stringLength); | ||
const string = StringView.fromString(value[i], itemLength); | ||
array.set(string, i * itemLength); | ||
} | ||
@@ -164,10 +163,11 @@ } | ||
/** | ||
* @protected | ||
* @param {number} position | ||
* @param {ArrayLike<number>} value | ||
* @param {Class<TypedArrayView>} ctor | ||
* @param {number} size | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
setTypedArray(position, value, ctor, size) { | ||
const { typeSetter, offset, littleEndian } = ctor; | ||
setTypedArray(position, value, field) { | ||
const { view, size } = field; | ||
const { typeSetter, offset, littleEndian } = view; | ||
const max = (size < value.length ? size : value.length); | ||
@@ -180,3 +180,4 @@ for (let i = 0; i < max; i++) { | ||
/** | ||
* @param {string} field | ||
* @protected | ||
* @param {string} name | ||
* @param {*} value | ||
@@ -187,54 +188,10 @@ * @param {*} [schema] | ||
*/ | ||
setValue(field, value, schema = this.constructor.schema, offset = 0) { | ||
setValue(name, value, schema = this.constructor.schema, offset = 0) { | ||
const field = schema[name]; | ||
const { | ||
type, littleEndian, start, ctor, length, size, stringLength, | ||
} = schema[field]; | ||
kind, start, setter, littleEndian, | ||
} = field; | ||
const position = offset + start; | ||
switch (type) { | ||
case 'int8': | ||
this.setInt8(position, value); | ||
break; | ||
case 'uint8': | ||
this.setUint8(position, value); | ||
break; | ||
case 'int16': | ||
this.setInt16(position, value, littleEndian); | ||
break; | ||
case 'uint16': | ||
this.setUint16(position, value, littleEndian); | ||
break; | ||
case 'int32': | ||
this.setInt32(position, value, littleEndian); | ||
break; | ||
case 'uint32': | ||
this.setUint32(position, value, littleEndian); | ||
break; | ||
case 'float32': | ||
this.setFloat32(position, value, littleEndian); | ||
break; | ||
case 'float64': | ||
this.setFloat64(position, value, littleEndian); | ||
break; | ||
case 'bigint64': | ||
this.setBigInt64(position, value, littleEndian); | ||
break; | ||
case 'biguint64': | ||
this.setBigUint64(position, value, littleEndian); | ||
break; | ||
case 'string': | ||
this.setString(position, value, length); | ||
break; | ||
case 'typedarray': | ||
this.setTypedArray(position, value, ctor, size); | ||
break; | ||
case 'array': | ||
this.setArray(position, value, ctor, size); | ||
break; | ||
case 'stringarray': | ||
this.setStringArray(position, value, size, length, stringLength); | ||
break; | ||
default: | ||
this.setObject(position, value, ctor); | ||
break; | ||
} | ||
const arg = kind === 'number' ? littleEndian : field; | ||
this[setter](position, value, arg); | ||
return this; | ||
@@ -241,0 +198,0 @@ } |
@@ -6,2 +6,3 @@ const StringView = require('./string-view'); | ||
const ExtendedDataView = require('./extended-data-view'); | ||
const { typeOffsets, typeGetters, typeSetters } = require('./utilities'); | ||
@@ -20,24 +21,10 @@ /** | ||
* @property {boolean} [littleEndian] | ||
* @property {number} [length] | ||
* @property {number} [start] | ||
* @property {number} [length] | ||
* @property {Class<ArrayView>|Class<ObjectView>|Class<TypedArrayView>} [ctor] | ||
* @property {Class<ArrayView>|Class<ObjectView>|Class<TypedArrayView>} [view] | ||
* @property {string} getter | ||
* @property {string} setter | ||
*/ | ||
/** | ||
* @private | ||
*/ | ||
const fieldSizes = { | ||
int8: 1, | ||
uint8: 1, | ||
int16: 2, | ||
uint16: 2, | ||
int32: 4, | ||
uint32: 4, | ||
float32: 4, | ||
float64: 8, | ||
bigint64: 8, | ||
biguint64: 8, | ||
}; | ||
/** | ||
* @extends DataView | ||
@@ -47,3 +34,3 @@ */ | ||
/** | ||
* Returns the value of a given field. | ||
* Returns the value or view of a given field. | ||
* | ||
@@ -55,30 +42,19 @@ * @param {string} field the name of the field | ||
const { | ||
type, littleEndian, start, ctor, length, stringLength, | ||
getter, start, kind, littleEndian, view: View, length, itemLength, | ||
} = this.constructor.schema[field]; | ||
switch (type) { | ||
case 'int8': return this.getInt8(start); | ||
case 'uint8': return this.getUint8(start); | ||
case 'int16': return this.getInt16(start, littleEndian); | ||
case 'uint16': return this.getUint16(start, littleEndian); | ||
case 'int32': return this.getInt32(start, littleEndian); | ||
case 'uint32': return this.getUint32(start, littleEndian); | ||
case 'float32': return this.getFloat32(start, littleEndian); | ||
case 'float64': return this.getFloat64(start, littleEndian); | ||
case 'bigint64': return this.getBigInt64(start, littleEndian); | ||
case 'biguint64': return this.getBigUint64(start, littleEndian); | ||
case 'stringarray': return new StringArrayView(this.buffer, this.byteOffset + start, length, stringLength); | ||
default: return this.getView(start, length, ctor); | ||
} | ||
if (kind === 'number') return this[getter](start, littleEndian); | ||
return new View(this.buffer, this.byteOffset + start, length, itemLength); | ||
} | ||
/** | ||
* @private | ||
* @param {number} position | ||
* @param {number} length | ||
* @param {Class<ArrayView>|Class<TypedArrayView> | ||
* |Class<ObjectView>|Class<StringView>} [Ctor=StringView] | ||
* @returns {ArrayView|TypedArrayView|ObjectView|StringView} | ||
* Returns the JavaScript value of a given field. | ||
* | ||
* @param {string} field the name of the field | ||
* @returns {*} value of the field | ||
*/ | ||
getView(position, length, Ctor = StringView) { | ||
return new Ctor(this.buffer, this.byteOffset + position, length); | ||
getValue(field) { | ||
const fieldOptions = this.constructor.schema[field]; | ||
const { start, getter, kind } = fieldOptions; | ||
const arg = kind === 'number' ? fieldOptions.littleEndian : fieldOptions; | ||
return this[getter](start, arg); | ||
} | ||
@@ -94,3 +70,4 @@ | ||
set(field, value) { | ||
return this.setValue(field, value); | ||
this.setValue(field, value); | ||
return this; | ||
} | ||
@@ -116,2 +93,3 @@ | ||
/** | ||
* @deprecated use `ObjectView#toJSON()` instead. | ||
* Returns an Object corresponding to the object view. | ||
@@ -122,6 +100,15 @@ * | ||
toObject() { | ||
return this.getObject(this.constructor.schema, 0); | ||
return this.toJSON(); | ||
} | ||
/** | ||
* Returns an Object corresponding to the object view. | ||
* | ||
* @returns {Object} | ||
*/ | ||
toJSON() { | ||
return this.getObject(0, { view: this.constructor }); | ||
} | ||
/** | ||
* Assigns fields of a given object to the provided object view | ||
@@ -136,3 +123,3 @@ * or a new object view. | ||
const objectView = view || new this(new ArrayBuffer(this.getLength())); | ||
objectView.setObject(0, object, objectView.constructor); | ||
objectView.setObject(0, object, { view: objectView.constructor }); | ||
return objectView; | ||
@@ -162,43 +149,9 @@ } | ||
const field = schema[name]; | ||
const { | ||
type, size, littleEndian, length, | ||
} = field; | ||
field.start = lastOffset; | ||
switch (type) { | ||
case 'int8': | ||
case 'uint8': | ||
case 'int16': | ||
case 'uint16': | ||
case 'int32': | ||
case 'uint32': | ||
case 'float32': | ||
case 'float64': | ||
case 'bigint64': | ||
case 'biguint64': | ||
if (size) { | ||
field.ctor = TypedArrayViewMixin(type, littleEndian); | ||
field.type = 'typedarray'; | ||
field.length = field.ctor.getLength(size); | ||
} else { | ||
field.length = fieldSizes[type]; | ||
} | ||
break; | ||
case 'string': | ||
if (size) { | ||
field.type = 'stringarray'; | ||
field.stringLength = length; | ||
field.length = StringArrayView.getLength(size, length); | ||
} | ||
break; | ||
default: | ||
if (typeof type === 'string') throw TypeError(`Type "${type}" is not a valid type.`); | ||
if (size) { | ||
field.type = 'array'; | ||
field.ctor = ArrayViewMixin(type); | ||
field.length = field.ctor.getLength(size); | ||
} else { | ||
field.type = 'object'; | ||
field.ctor = type; | ||
field.length = type.getLength(); | ||
} | ||
const kind = this.getFieldKind(field); | ||
if (Reflect.has(this.types, kind)) { | ||
field.kind = kind; | ||
this.types[kind](field); | ||
} else { | ||
throw TypeError(`Type "${field.type}" is not a valid type.`); | ||
} | ||
@@ -211,3 +164,101 @@ lastOffset += field.length; | ||
} | ||
/** | ||
* @private | ||
* @param {ObjectViewField} field | ||
* @returns {string} | ||
*/ | ||
static getFieldKind(field) { | ||
const { type, size } = field; | ||
switch (type) { | ||
case 'int8': | ||
case 'uint8': | ||
case 'int16': | ||
case 'uint16': | ||
case 'int32': | ||
case 'uint32': | ||
case 'float32': | ||
case 'float64': | ||
case 'bigint64': | ||
case 'biguint64': | ||
if (size) return 'typedarray'; | ||
return 'number'; | ||
case 'string': | ||
if (size) return 'stringarray'; | ||
return 'string'; | ||
default: | ||
if (typeof type === 'string') return type; | ||
if (size) return 'array'; | ||
return 'object'; | ||
} | ||
} | ||
} | ||
ObjectView.types = { | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
number(field) { | ||
const { type } = field; | ||
field.length = 1 << typeOffsets[type]; | ||
field.getter = typeGetters[type]; | ||
field.setter = typeSetters[type]; | ||
}, | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
typedarray(field) { | ||
const { type, littleEndian, size } = field; | ||
field.view = TypedArrayViewMixin(type, littleEndian); | ||
field.length = field.view.getLength(size); | ||
field.getter = 'getTypedArray'; | ||
field.setter = 'setTypedArray'; | ||
}, | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
string(field) { | ||
field.view = StringView; | ||
field.getter = 'getString'; | ||
field.setter = 'setString'; | ||
}, | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
stringarray(field) { | ||
const { length, size } = field; | ||
field.view = StringArrayView; | ||
field.length = StringArrayView.getLength(size, length); | ||
field.getter = 'getStringArray'; | ||
field.setter = 'setStringArray'; | ||
field.itemLength = length; | ||
}, | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
object(field) { | ||
const { type } = field; | ||
field.view = type; | ||
field.length = type.getLength(); | ||
field.getter = 'getObject'; | ||
field.setter = 'setObject'; | ||
}, | ||
/** | ||
* @param {ObjectViewField} field | ||
* @returns {void} | ||
*/ | ||
array(field) { | ||
const { type, size } = field; | ||
field.view = ArrayViewMixin(type); | ||
field.length = field.view.getLength(size); | ||
field.getter = 'getArray'; | ||
field.setter = 'setArray'; | ||
}, | ||
}; | ||
/** | ||
@@ -214,0 +265,0 @@ * @private |
@@ -31,11 +31,20 @@ const StringView = require('./string-view'); | ||
/** | ||
* Sets a string or StringView at a given index. | ||
* Returns a string at a given index. | ||
* | ||
* @param {number} index | ||
* @param {string|StringView} value | ||
* @returns {string} | ||
*/ | ||
getValue(index) { | ||
return this.get(index).toString(); | ||
} | ||
/** | ||
* Sets a string at a given index. | ||
* | ||
* @param {number} index | ||
* @param {string} value | ||
* @returns {StringArrayView} | ||
*/ | ||
set(index, value) { | ||
const view = value instanceof Uint8Array ? value | ||
: StringView.fromString(value, this.stringLength); | ||
const view = StringView.fromString(value, this.stringLength); | ||
this.bytes.set(view, index * this.stringLength); | ||
@@ -46,2 +55,14 @@ return this; | ||
/** | ||
* Sets a StringView at a given index. | ||
* | ||
* @param {number} index | ||
* @param {Uint8Array} value | ||
* @returns {StringArrayView} | ||
*/ | ||
setView(index, value) { | ||
this.bytes.set(value, index * this.stringLength); | ||
return this; | ||
} | ||
/** | ||
* Returns the amount of available strings in the array. | ||
@@ -69,2 +90,3 @@ * | ||
/** | ||
* @deprecated use `StringArrayView#toJSON()` instead. | ||
* Returns an array of strings held inside the array view. | ||
@@ -75,6 +97,15 @@ * | ||
toObject() { | ||
return this.toJSON(); | ||
} | ||
/** | ||
* Returns an array of strings held inside the array view. | ||
* | ||
* @returns {Array<string>} | ||
*/ | ||
toJSON() { | ||
const { size } = this; | ||
const array = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
array[i] = this.get(i).toString(); | ||
array[i] = this.getValue(i); | ||
} | ||
@@ -81,0 +112,0 @@ return array; |
@@ -289,2 +289,9 @@ /** | ||
/** | ||
* @returns {string} | ||
*/ | ||
toJSON() { | ||
return this.toString(); | ||
} | ||
/** | ||
* Returns a StringView without trailing zeros. | ||
@@ -291,0 +298,0 @@ * |
@@ -1,50 +0,4 @@ | ||
/** | ||
* @private | ||
*/ | ||
const getters = { | ||
int8: 'getInt8', | ||
uint8: 'getUint8', | ||
int16: 'getInt16', | ||
uint16: 'getUint16', | ||
int32: 'getInt32', | ||
uint32: 'getUint32', | ||
float32: 'getFloat32', | ||
float64: 'getFloat64', | ||
bigint64: 'getBigInt64', | ||
biguint64: 'getBigUint64', | ||
}; | ||
const { typeGetters, typeSetters, typeOffsets } = require('./utilities'); | ||
/** | ||
* @private | ||
*/ | ||
const setters = { | ||
int8: 'setInt8', | ||
uint8: 'setUint8', | ||
int16: 'setInt16', | ||
uint16: 'setUint16', | ||
int32: 'setInt32', | ||
uint32: 'setUint32', | ||
float32: 'setFloat32', | ||
float64: 'setFloat64', | ||
bigint64: 'setBigInt64', | ||
biguint64: 'setBigUint64', | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
const offsets = { | ||
int8: 0, | ||
uint8: 0, | ||
int16: 1, | ||
uint16: 1, | ||
int32: 2, | ||
uint32: 2, | ||
float32: 2, | ||
float64: 3, | ||
bigint64: 3, | ||
biguint64: 3, | ||
}; | ||
/** | ||
* @param {string} type | ||
@@ -106,2 +60,3 @@ * @param {boolean} [littleEndian] | ||
/** | ||
* @deprecated use `TypedArrayView#toJSON()` instead | ||
* Returns an array representation of the array view. | ||
@@ -112,2 +67,11 @@ * | ||
toObject() { | ||
return this.toJSON(); | ||
} | ||
/** | ||
* Returns an array representation of the array view. | ||
* | ||
* @returns {Array<number>} | ||
*/ | ||
toJSON() { | ||
return [...this]; | ||
@@ -158,3 +122,3 @@ } | ||
*/ | ||
TypedArrayView.typeGetter = getters[type]; | ||
TypedArrayView.typeGetter = typeGetters[type]; | ||
@@ -165,3 +129,3 @@ /** | ||
*/ | ||
TypedArrayView.typeSetter = setters[type]; | ||
TypedArrayView.typeSetter = typeSetters[type]; | ||
@@ -172,3 +136,3 @@ /** | ||
*/ | ||
TypedArrayView.offset = offsets[type]; | ||
TypedArrayView.offset = typeOffsets[type]; | ||
@@ -175,0 +139,0 @@ /** |
@@ -70,2 +70,42 @@ const log2 = { | ||
const typeGetters = { | ||
int8: 'getInt8', | ||
uint8: 'getUint8', | ||
int16: 'getInt16', | ||
uint16: 'getUint16', | ||
int32: 'getInt32', | ||
uint32: 'getUint32', | ||
float32: 'getFloat32', | ||
float64: 'getFloat64', | ||
bigint64: 'getBigInt64', | ||
biguint64: 'getBigUint64', | ||
}; | ||
const typeSetters = { | ||
int8: 'setInt8', | ||
uint8: 'setUint8', | ||
int16: 'setInt16', | ||
uint16: 'setUint16', | ||
int32: 'setInt32', | ||
uint32: 'setUint32', | ||
float32: 'setFloat32', | ||
float64: 'setFloat64', | ||
bigint64: 'setBigInt64', | ||
biguint64: 'setBigUint64', | ||
}; | ||
const typeOffsets = { | ||
int8: 0, | ||
uint8: 0, | ||
int16: 1, | ||
uint16: 1, | ||
int32: 2, | ||
uint32: 2, | ||
float32: 2, | ||
float64: 3, | ||
bigint64: 3, | ||
biguint64: 3, | ||
}; | ||
module.exports = { | ||
@@ -76,2 +116,5 @@ log2, | ||
getGTEMultiple, | ||
typeGetters, | ||
typeSetters, | ||
typeOffsets, | ||
}; |
{ | ||
"name": "structurae", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -51,7 +51,7 @@ "main": "index.js", | ||
"codecov": "^3.5.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb-base": "^13.2.0", | ||
"eslint": "^6.3.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"jest": "^24.9.0", | ||
"jsdoc-to-markdown": "^5.0.0", | ||
"jsdoc-to-markdown": "^5.0.1", | ||
"json-schema-faker": "^0.5.0-rc17" | ||
@@ -73,4 +73,4 @@ }, | ||
"engines": { | ||
"node": ">=10.4.0" | ||
"node": ">=11.0.0" | ||
} | ||
} |
@@ -75,5 +75,7 @@ # Structurae | ||
]); | ||
const arthur = hitchhikers.get(0); | ||
// get a view of the first object | ||
hitchhikers.get(0); | ||
//=> Person [14] | ||
arthur.toObject(); | ||
// get the value of the first object | ||
hitchhikers.getValue(0); | ||
//=> { id: 1, name: 'Arthur' } | ||
@@ -83,6 +85,6 @@ | ||
hitchhikers.set(0, { id: 3, name: 'Trillian' }); | ||
hitchhikers.get(0).toObject(); | ||
hitchhikers.get(0).toJSON(); | ||
//=> { id: 3, name: 'Trillian' } | ||
hitchhikers.toObject(); | ||
hitchhikers.toJSON(); | ||
//=> [{ id: 1, name: 'Arthur' }, { id: 2, name: 'Ford' }] | ||
@@ -162,10 +164,12 @@ ``` | ||
//=> 1 | ||
person.get('name'); | ||
person.get('name') | ||
//=> StringView [10] | ||
person.getValue('name'); | ||
//=> Zaphod | ||
person.get('scores').toObject() | ||
person.getValue('scores') | ||
//=> [1, 2, 3, 0, 0, 0, 0, 0, 0, 0,] | ||
person.set('house', { size: 5 }); | ||
person.get('house').get('size'); | ||
//=> 5 | ||
person.toObject() | ||
person.getValue('house'); | ||
//=> { size: 5 } | ||
person.toJSON() | ||
//=> { name: 'Zaphod', scores: [1, 2, 3, 0, 0, 0, 0, 0, 0, 0,], house: { size: 5 }, pets: [{ type: 'dog' }, { type: 'cat' }, { type: '' }] } | ||
@@ -231,5 +235,5 @@ ``` | ||
//=> StringView []; | ||
list.get(0).toString(); | ||
list.getValue(0); | ||
//=> 'a' | ||
list.toObject(); | ||
list.toJSON(); | ||
//=> ['a', 'bc', 'defg'] | ||
@@ -239,5 +243,5 @@ | ||
const emptyList = StringArrayView.of(3, 5); | ||
emptyList.get(0).toString(); | ||
emptyList.getValue(0); | ||
//=> '' | ||
emptyList.set(0, 'ab').get(0).toString(); | ||
emptyList.set(0, 'ab').getValue(0); | ||
//=> 'ab' | ||
@@ -244,0 +248,0 @@ |
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
198795
5606
879