Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions babelrc.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
plugins: [
'@babel/plugin-transform-runtime',
"@babel/plugin-transform-runtime",
[
'@babel/plugin-proposal-decorators',
"@babel/plugin-proposal-decorators",
{
legacy: true
}
legacy: true,
},
],
[
'@babel/plugin-proposal-class-properties',
"@babel/plugin-proposal-class-properties",
{
loose: true
}
]
]
}
loose: true,
},
],
],
};
54 changes: 27 additions & 27 deletions src/Binary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Not efficient enough...
// import { nonenumerable } from 'core-decorators';

import Types from './Types'
import Types from "./Types";

/** Class allowing `@binary` members */
class Binary {
Expand All @@ -12,23 +12,23 @@ class Binary {
// Class props
// Slowers down 4x times...
// @nonenumerable
static _size
static _size;
/**
* Static getter for the class binary size
* @return {number} - The class binary size
*/
static get binarySize () {
return this._size
static get binarySize() {
return this._size;
}

// @nonenumerable
static _binaryProps
static _binaryProps;
/**
* Static getter for the class binary props
* @return {array} - The list of binary props
*/
static get binaryProps () {
return this._binaryProps
static get binaryProps() {
return this._binaryProps;
}

/**
Expand All @@ -40,34 +40,34 @@ class Binary {
* @return {array} - The array {@link list} where the objects have been added
*/
// @nonenumerable
static arrayFactory (binOrDV, length, initialOffset = 0, list = []) {
static arrayFactory(binOrDV, length, initialOffset = 0, list = []) {
// Optimize: Generate a single DataView for all elements
const dv = binOrDV instanceof DataView ? binOrDV : new DataView(binOrDV)
const dv = binOrDV instanceof DataView ? binOrDV : new DataView(binOrDV);

for (let i = 0; i < length; i++) {
list.push(new this(dv, initialOffset + this._size * i))
list.push(new this(dv, initialOffset + this._size * i));
}

return list
return list;
}

// Prototype props
// @nonenumerable
_initialOffset
_initialOffset;
// @nonenumerable
_bin
_bin;
// @nonenumerable
__dv
__dv;
/**
* Getter of the DataView containing this object's data
* @return {DataView} - The DataView
*/
// @nonenumerable
get _dv () {
get _dv() {
this.__dv =
this?.__dv ??
new DataView(this._bin, this._initialOffset, this.constructor._size)
return this.__dv
new DataView(this._bin, this._initialOffset, this.constructor._size);
return this.__dv;
}

/**
Expand All @@ -80,25 +80,25 @@ class Binary {
this.constructor._binaryProps.reduce(
(acc, prop) => ({
...acc,
[prop]: this[prop]
[prop]: this[prop],
}),
{}
)
{},
);

/**
* Save own initial offset at binary data
* @param {ArrayBuffer/SharedArrayBuffer/DataView} binOrDv - The buffer where the data lives
* @param {number} initialOffset - Buffer offset before this object data start
* @param {boolean} isLazy - If true and {@link binOrDv} is not a {DataView}, wait until first acces before Instantiating the __dv
*/
constructor (binOrDV, initialOffset = 0, isLazy = true) {
this._initialOffset = initialOffset
constructor(binOrDV, initialOffset = 0, isLazy = true) {
this._initialOffset = initialOffset;
if (binOrDV instanceof DataView) {
this.__dv = binOrDV
this.__dv = binOrDV;
} else {
this._bin = binOrDV
this._bin = binOrDV;
if (!isLazy) {
this._dv // Call getter
this._dv; // Call getter
}
}
}
Expand All @@ -111,7 +111,7 @@ class Binary {
*/
// @nonenumerable
getByteAt = (offset) =>
Types.Uint8.get(this._dv, this._initialOffset + offset)
Types.Uint8.get(this._dv, this._initialOffset + offset);
}

/*
Expand All @@ -134,4 +134,4 @@ Object.defineProperty(Binary, "binaryProps", {
});
*/

export default Binary
export default Binary;
70 changes: 35 additions & 35 deletions src/BinaryArray.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/** Class for returning array members from {@link Binary} objects */
class BinaryArrayBase {
// @member
type
type;
// @member
dv
dv;
// @member
offset
offset;
// @member
length
length;
// @member
bytes
bytes;

/**
* Creates a new customized array
Expand All @@ -18,12 +18,12 @@ class BinaryArrayBase {
* @param {number} offset - The offset of the first member of the array into the buffer
* @param {number} length - The length of the array
*/
constructor (dv, type, offset, length) {
this.type = type
this.dv = dv
this.offset = offset
this.length = length
this.bytes = length * type.bytes
constructor(dv, type, offset, length) {
this.type = type;
this.dv = dv;
this.offset = offset;
this.length = length;
this.bytes = length * type.bytes;
}

/**
Expand All @@ -32,7 +32,7 @@ class BinaryArrayBase {
* @return {array} - The new generated array (not bound to original values)
* @method
*/
map = (fn) => Array.from(this, fn)
map = (fn) => Array.from(this, fn);
// reduce = (...args) => Array.prototype.reduce.call([...this], ...args);

/**
Expand All @@ -50,20 +50,20 @@ class BinaryArrayBase {
* @yield {any} - Each of this array elements of type {@link Types}
* @name iterator
*/
* [Symbol.iterator] () {
*[Symbol.iterator]() {
// Deconstruct to optimize and ease reading
const {
length,
dv,
offset,
type: { get, bytes }
} = this
type: { get, bytes },
} = this;

// Use a new index for each iterator. This makes multiple
// iterations over the iterable safe for non-trivial cases,
// such as use of break or nested looping over the same iterable.
for (let index = 0; index < length; index++) {
yield get(dv, offset + bytes * index)
yield get(dv, offset + bytes * index);
}
}
}
Expand All @@ -79,23 +79,23 @@ const BinaryArrayHandler = {
* @param {string} prop - The property to return (only handled when prop is a string representing a number)
* @return {any} - The element at {@link prop} position, or a reflected value from {@link target}
*/
get (target, prop) {
get(target, prop) {
// Very inefficient way
// Need to:
// - Override Array internals, but are private
// - Override `[]` operator, but it's not possible
if (prop === '0' || (typeof prop === 'string' && Number(prop) > 0)) {
if (prop === "0" || (typeof prop === "string" && Number(prop) > 0)) {
// Destructure to optimize
const {
dv,
offset,
type: { get, bytes }
} = target
return get(dv, offset + bytes * Number(prop))
type: { get, bytes },
} = target;
return get(dv, offset + bytes * Number(prop));
}

// Return original value
return Reflect.get(target, prop)
return Reflect.get(target, prop);
},

/**
Expand All @@ -105,20 +105,20 @@ const BinaryArrayHandler = {
* @param {any} value - The value to assign to the {@link prop}'th element
* @return {boolean} - If {@link prop} is numericalish, true (as needed for JS setters), else the return value from the {@link target} reflected setter
*/
set (target, prop, value) {
if (prop === '0' || (typeof prop === 'string' && Number(prop) > 0)) {
set(target, prop, value) {
if (prop === "0" || (typeof prop === "string" && Number(prop) > 0)) {
// Destructure to optimize
const {
dv,
offset,
type: { set, bytes }
} = target
set(dv, offset + bytes * Number(prop), value)
return true
type: { set, bytes },
} = target;
set(dv, offset + bytes * Number(prop), value);
return true;
}
return Reflect.set(target, prop, value)
}
}
return Reflect.set(target, prop, value);
},
};

// #TODO: BUG: Argument Spread Operator not working
// well when packing with webpack
Expand All @@ -133,8 +133,8 @@ const BinaryArrayHandler = {
const BinaryArray = (dv, type, offset, length) => {
return new Proxy(
new BinaryArrayBase(dv, type, offset, length),
BinaryArrayHandler
)
}
BinaryArrayHandler,
);
};

export default BinaryArray
export default BinaryArray;
10 changes: 5 additions & 5 deletions src/TextEncoder-polyfill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Mock Node missing TextEncoder and TextDecoder APIs from its `util` lib
if (!('TextEncoder' in global)) {
import('util').then((nodeUtil) => {
global.TextEncoder = nodeUtil.TextEncoder
global.TextDecoder = nodeUtil.TextDecoder
})
if (!("TextEncoder" in global)) {
import("util").then((nodeUtil) => {
global.TextEncoder = nodeUtil.TextEncoder;
global.TextDecoder = nodeUtil.TextDecoder;
});
}
Loading