Skip to content

Commit

Permalink
Do check correctly for undefined atomic values
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Dzialocha committed Mar 9, 2017
1 parent 47e55ac commit 09a2e9c
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 29 deletions.
23 changes: 13 additions & 10 deletions dist/osc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/osc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/osc.min.js

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions lib/osc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function isBlob(n) {
function isDate(n) {
return n instanceof Date;
}
function isUndefined(n) {
return typeof n === 'undefined';
}
function pad(n) {
return n + 3 & ~0x03;
}
Expand Down Expand Up @@ -255,24 +258,24 @@ var Atomic = function () {
throw new Error('OSC Atomic cant\'t be packed without given method or byteLength');
}
var data = new Uint8Array(byteLength);
var dataView = new DataView(data.buffer);
if (!this.value) {
var dataView$$1 = new DataView(data.buffer);
if (isUndefined(this.value)) {
throw new Error('OSC Atomic cant\'t be encoded with empty value');
}
dataView[method](this.offset, this.value, false);
dataView$$1[method](this.offset, this.value, false);
return data;
}
}, {
key: 'unpack',
value: function unpack(dataView, method, byteLength) {
value: function unpack(dataView$$1, method, byteLength) {
var initialOffset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
if (!(dataView && method && byteLength)) {
if (!(dataView$$1 && method && byteLength)) {
throw new Error('OSC Atomic cant\'t be unpacked without given dataView, method or byteLength');
}
if (!(dataView instanceof DataView)) {
if (!(dataView$$1 instanceof DataView)) {
throw new Error('OSC Atomic expects an instance of type DataView');
}
this.value = dataView[method](initialOffset, false);
this.value = dataView$$1[method](initialOffset, false);
this.offset = initialOffset + byteLength;
return this.offset;
}
Expand Down Expand Up @@ -340,7 +343,7 @@ var AtomicString = function (_Atomic) {
createClass(AtomicString, [{
key: 'pack',
value: function pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicString can not be encoded with empty value');
}
var terminated = this.value + '\0';
Expand Down Expand Up @@ -393,7 +396,7 @@ var AtomicBlob = function (_Atomic) {
createClass(AtomicBlob, [{
key: 'pack',
value: function pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicBlob can not be encoded with empty value');
}
var byteLength = pad(this.value.byteLength);
Expand Down Expand Up @@ -575,7 +578,7 @@ var AtomicTimetag = function (_Atomic) {
createClass(AtomicTimetag, [{
key: 'pack',
value: function pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicTimetag can not be encoded with empty value');
}
var _value = this.value,
Expand Down
4 changes: 3 additions & 1 deletion src/atomic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isUndefined } from './common/utils'

/**
* Base class for OSC Atomic Data Types
*/
Expand Down Expand Up @@ -27,7 +29,7 @@ export default class Atomic {
const data = new Uint8Array(byteLength)
const dataView = new DataView(data.buffer)

if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC Atomic cant\'t be encoded with empty value')
}

Expand Down
4 changes: 2 additions & 2 deletions src/atomic/blob.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pad, isBlob } from '../common/utils'
import { pad, isBlob, isUndefined } from '../common/utils'

import Atomic from '../atomic'

Expand All @@ -23,7 +23,7 @@ export default class AtomicBlob extends Atomic {
* @return {Uint8Array} Packed binary data
*/
pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicBlob can not be encoded with empty value')
}

Expand Down
4 changes: 2 additions & 2 deletions src/atomic/string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pad, isString } from '../common/utils'
import { pad, isString, isUndefined } from '../common/utils'

import Atomic from '../atomic'

Expand All @@ -23,7 +23,7 @@ export default class AtomicString extends Atomic {
* @return {Uint8Array} Packed binary data
*/
pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicString can not be encoded with empty value')
}

Expand Down
4 changes: 2 additions & 2 deletions src/atomic/timetag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isInt, isDate } from '../common/utils'
import { isInt, isDate, isUndefined } from '../common/utils'

import Atomic from '../atomic'

Expand Down Expand Up @@ -81,7 +81,7 @@ export default class AtomicTimetag extends Atomic {
* @return {Uint8Array} Packed binary data
*/
pack() {
if (!this.value) {
if (isUndefined(this.value)) {
throw new Error('OSC AtomicTimetag can not be encoded with empty value')
}

Expand Down
9 changes: 9 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ export function isDate(n) {
return n instanceof Date
}

/**
* Check if given object is undefined
* @param {*} n
* @return {boolean}
*/
export function isUndefined(n) {
return typeof n === 'undefined'
}

/**
* Return the next multiple of four
* @param {number} n
Expand Down
2 changes: 2 additions & 0 deletions test/atomic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ describe('Atomic', () => {
atomic = new Atomic(2)

atomicChildren = [
new AtomicInt32(0),
new AtomicInt32(123132132),
new AtomicFloat32(1299389992.342243),
new AtomicString('hello'),
new AtomicString(''),
new AtomicBlob(new Uint8Array([5, 4, 3, 2, 1])),
new AtomicTimetag(new Timetag(SECONDS_70_YEARS + 123, 3312123)),
]
Expand Down

0 comments on commit 09a2e9c

Please sign in to comment.