simple-common-utils
Advanced tools
Comparing version 1.7.1 to 2.0.0
import BaseFormat from "./formats/BaseFormat"; | ||
export default class Format { | ||
static zx55v1 = new BaseFormat(0x55, 1, 1, 1, 2); | ||
static zx55v2 = new BaseFormat(0x55, 1, 2, 1, 2); | ||
} | ||
export default class Format {} |
import StaticUtils from "../StaticUtils"; | ||
export default class Packet { | ||
/** | ||
* Creates a packet of the specified format. Valid invocation variants: | ||
* 1) new Packet(format[, <command number>[, byte1, ..., byteN]]); | ||
* 2) new Packet(format, buffer, start, end); | ||
* | ||
**/ | ||
constructor() { | ||
this._format = arguments[0]; | ||
constructor(format, commandNumber, params = []) { | ||
this._format = format; | ||
if (arguments.length > 1) { | ||
if (Array.isArray(arguments[1])) { | ||
this._buf = arguments[1].slice(arguments[2], arguments[3]); | ||
StaticUtils.verify(this._format.isValid(this._buf), `[ ${this._buf.join(", ")} ] is not a valid packet.`); | ||
} else { | ||
const params = Array.from(arguments); | ||
params.shift(); | ||
this._buf = []; | ||
this._format.setStartMarker(this._buf); | ||
this._format.setCommandNumber(this._buf, params.shift()); | ||
this.setParams(params); | ||
} | ||
this._buf = []; | ||
this._format.setStartMarker(this._buf); | ||
this._format.setCommandNumber(this._buf, commandNumber); | ||
this.setParams(params); | ||
} | ||
} | ||
wrap(buffer, start = 0, end = buffer.length) { | ||
this._buf = buffer.slice(start, end); | ||
StaticUtils.verify(this._format.isValid(this._buf), `[ ${this._buf.join(", ")} ] is not a valid packet.`); | ||
return this; | ||
} | ||
setParams(params) { | ||
@@ -33,0 +26,0 @@ this._format.setParams(this._buf, params); |
@@ -51,3 +51,3 @@ import Packet from "./Packet"; | ||
try { | ||
const packet = new (handler ? handler._type : this._defaultPacketType)(this._format, this._buf, offset, offset + packetSize); | ||
const packet = new (handler ? handler._type : this._defaultPacketType)(this._format).wrap(this._buf, offset, offset + packetSize); | ||
@@ -54,0 +54,0 @@ handler && handler._handler ? handler._handler(packet) : packets.push(packet); |
{ | ||
"name": "simple-common-utils", | ||
"version": "1.7.1", | ||
"version": "2.0.0", | ||
"description": "A collection of utility classes.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -251,3 +251,4 @@ This is a collection of utility classes used for JS development. | ||
-|- | ||
v1.7.1|Autobinding removed from the `data-transmission-protocol`-classes. | ||
v2.0.0|Backwards-incompatible changes in `data-transmission-protocol` classes:<br>1. `Packet.constructor()` changed and `Packet.wrap()` added.<br>2. `Format` is filled dynamically: it's necessary to import relevant format-defining classes. | ||
v1.7.1|Autobinding removed from the `data-transmission-protocol` classes. | ||
v1.7.0|`data-transmission-protocol`-classes added. These deprecate `zx55`. | ||
@@ -254,0 +255,0 @@ v1.6.0|`zx55` added. |
31258
16
263
582