Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.
Merged
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"rollup": "rollup -c rollup.config.js",
"lint": "eslint src/",
"lintfix": "eslint src/ --fix"
"lintfix": "eslint src/ --fix",
"test": "rollup -c rollup.config.js && jasmine"
},
"dependencies": {
"debug": "^3.1.0",
Expand All @@ -19,6 +20,7 @@
"eslint": "^4.16.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"jasmine": "^2.8.0",
"rollup": "^0.55.0",
"rollup-plugin-buble": "^0.18.0",
"rollup-plugin-eslint": "^4.0.0"
Expand Down
10 changes: 10 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,15 @@ export default [
eslint(),
buble(),
]
},
{
input: 'src/split-descriptors.js',
output: [
{ file: 'dist/split-descriptors.cjs.js', format: 'cjs', sourcemap: true },
],
plugins: [
eslint(),
buble(),
]
}
];
71 changes: 71 additions & 0 deletions spec/split-descriptors-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2010 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

const splitDescriptors = require('../dist/split-descriptors.cjs.js');

describe('splitDescriptors', () => {
it('should return empty array for undefined input', () => {
expect(splitDescriptors()).toEqual([]);
});

it('should return empty array for null input', () => {
expect(splitDescriptors(null)).toEqual([]);
});

it('should return empty array for non-Uint8Array', () => {
expect(splitDescriptors([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([]);
});

it('should split input to subarrays', () => {
const bytes = new Uint8Array([
5, 36, 0, 16, 1, 5, 36, 1, 3, 1, 4, 36, 2, 6, 5, 36, 6, 0, 1,
]);
expect(splitDescriptors(bytes))
.toEqual([
new Uint8Array([5, 36, 0, 16, 1]),
new Uint8Array([5, 36, 1, 3, 1]),
new Uint8Array([4, 36, 2, 6]),
new Uint8Array([5, 36, 6, 0, 1]),
]);
});

it('should silently ignore insufficient data', () => {
const bytes = new Uint8Array([15, 36, 0, 16, 1]);
expect(splitDescriptors(bytes)).toEqual([bytes]);
});

it('should throw exception on 0 length descriptor', () => {
const bytes = new Uint8Array([0, 36, 0, 16, 1]);
expect(() => {
splitDescriptors(bytes);
}).toThrowError();
});
});
11 changes: 11 additions & 0 deletions spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
22 changes: 5 additions & 17 deletions src/split-descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
// should be splitted into
// 5 X X X X | 4 X X X | 9 X X X X X X X X


// Given a Uint8Array, returns an Array of Uint8Array
// Each element of the resulting array is a subarray of the original Uint8Array.
export default function splitDescriptors(bytes) {
Expand All @@ -49,26 +48,15 @@ export default function splitDescriptors(bytes) {

while (len > 0) {
const descLen = bytes[pointer];
if (descLen < 1) {
throw new Error('invalid descriptor length');
}
descs.push(bytes.subarray(pointer, pointer + descLen));
len -= descLen;
pointer += descLen;
}

// TODO: Consider handling if len !== 0 at this point.

return descs;
}


/*
let test = Uint8Array.from([5, 36, 0, 16, 1, 5, 36, 1, 3, 1, 4, 36, 2, 6, 5, 36, 6, 0, 1]);

console.log(splitDescriptors(test));
*/

/*
The previous code should output:

[ Uint8Array [ 5, 36, 0, 16, 1 ],
Uint8Array [ 5, 36, 1, 3, 1 ],
Uint8Array [ 4, 36, 2, 6 ],
Uint8Array [ 5, 36, 6, 0, 1 ] ]
*/