Skip to content

Commit

Permalink
Merge pull request #315 from emibcn/deepsource-transform-37d5c4e5
Browse files Browse the repository at this point in the history
style: format code with Prettier and StandardJS
  • Loading branch information
emibcn authored Mar 8, 2024
2 parents 0296dde + 78cbb85 commit 4107516
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MyBinaryClass extends Binary {
Object.defineProperty(
MyBinaryClass.prototype,
"someMember",
binary(Types.Float32)
binary(Types.Float32),
);
```

Expand Down
72 changes: 36 additions & 36 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 { nonenumerable } from 'core-decorators';

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

/** Class allowing `@binary` members */
class Binary {
Expand All @@ -11,24 +11,24 @@ class Binary {

// Class props
// Slowers down 4x times...
//@nonenumerable
static _size;
// @nonenumerable
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;
// @nonenumerable
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 @@ -39,66 +39,66 @@ class Binary {
* @param {array} list - The array where new objects will be added
* @return {array} - The array {@link list} where the objects have been added
*/
//@nonenumerable
static arrayFactory(binOrDV, length, initialOffset = 0, list = []) {
// @nonenumerable
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;
//@nonenumerable
_bin;
//@nonenumerable
__dv;
// @nonenumerable
_initialOffset
// @nonenumerable
_bin
// @nonenumerable
__dv
/**
* Getter of the DataView containing this object's data
* @return {DataView} - The DataView
*/
//@nonenumerable
get _dv() {
// @nonenumerable
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
}

/**
* Transform this object into a JSON string containing all the binary members
* @return {string} - The JSON string
* @method
*/
//@nonenumerable
// @nonenumerable
toJSON = () =>
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 @@ -109,9 +109,9 @@ class Binary {
* @return {number} - The unsigned numerical number at the specified position
* @method
*/
//@nonenumerable
// @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,8 +32,8 @@ class BinaryArrayBase {
* @return {array} - The new generated array (not bound to original values)
* @method
*/
map = (fn) => Array.from(this, fn);
//reduce = (...args) => Array.prototype.reduce.call([...this], ...args);
map = (fn) => Array.from(this, fn)
// reduce = (...args) => Array.prototype.reduce.call([...this], ...args);

/**
* Transform this array into a JSON string
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 @@ -134,7 +134,7 @@ const BinaryArray = (dv, type, offset, length) => {
return new Proxy(
new BinaryArrayBase(dv, type, offset, length),
BinaryArrayHandler
);
};
)
}

export default BinaryArray;
export default BinaryArray
22 changes: 11 additions & 11 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ test("Instantiate an object with a binary data using an initial offset > 0", ()

const expectedValues = [1, 2, 3];
const values = expectedValues.map((_, index) =>
initialsDV.getInt32(index * 4)
initialsDV.getInt32(index * 4),
);
expect(values).toEqual(expectedValues);
});
Expand Down Expand Up @@ -497,7 +497,7 @@ test("Profile a natural object against a binary object", async () => {

await testProfile(`${name} modification array element 2nd phase`, () => {
objList.forEach(
({ testFloatArray }, id) => (testFloatArray[0] = id * 2)
({ testFloatArray }, id) => (testFloatArray[0] = id * 2),
);
});
}
Expand Down Expand Up @@ -543,13 +543,13 @@ test("Profile a natural object against a binary object", async () => {
() => {
binTest3 = new ArrayBuffer(BinaryTest.binarySize * iterations);
dv3 = new DataView(binTest3);
}
},
);
await testProfile(
"Binary Object with pre-created DataView instantation",
() => {
bDvObjList = BinaryTest.arrayFactory(dv3, iterations);
}
},
);

await testObjList("Binary Object with pre-created DataView", bDvObjList);
Expand Down Expand Up @@ -591,7 +591,7 @@ test("Profile a natural object against a binary object", async () => {
}

const binTest2 = new ArrayBuffer(
BinaryWithoutArrayTest.binarySize * iterations
BinaryWithoutArrayTest.binarySize * iterations,
);
bwoaObjList = BinaryWithoutArrayTest.arrayFactory(binTest2, iterations);
});
Expand All @@ -616,7 +616,7 @@ test("Profile a natural object against a binary object", async () => {
}

const binTest2 = new ArrayBuffer(
BinaryObjectWithDecorator.binarySize * iterations
BinaryObjectWithDecorator.binarySize * iterations,
);
bdObjList = BinaryObjectWithDecorator.arrayFactory(binTest2, iterations);
});
Expand All @@ -643,15 +643,15 @@ test("Profile a natural object against a binary object", async () => {
}

const binTest2 = new ArrayBuffer(
BinaryObjectWithDecorator.binarySize * iterations
BinaryObjectWithDecorator.binarySize * iterations,
);
bdpObjList = BinaryObjectWithDecorator.arrayFactory(binTest2, iterations);
}
},
);

await testObjList(
"Binary Object with class decorator and padded array",
bdpObjList
bdpObjList,
);

// Show collected metrics in a table
Expand All @@ -672,15 +672,15 @@ test("Profile a natural object against a binary object", async () => {
...acc,
[key]: numberWithCommas(value),
}),
{}
{},
),
}))
.reduce(
(acc, { name, ...rest }) => ({
...acc,
[name]: rest,
}),
{}
{},
);
console.table(memoryDiffTable);
}, 6e4);

0 comments on commit 4107516

Please sign in to comment.