Skip to content

Commit

Permalink
using byte-data ^5.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rochars committed Apr 26, 2018
1 parent 7e9cc82 commit 5ffeb66
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 1,217 deletions.
49 changes: 25 additions & 24 deletions dist/riff-chunks-min.js

Large diffs are not rendered by default.

350 changes: 163 additions & 187 deletions dist/riff-chunks.js

Large diffs are not rendered by default.

927 changes: 5 additions & 922 deletions docs/global.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<label for="nav-trigger" class="overlay"></label>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#getChunkId">getChunkId</a></li><li><a href="global.html#getChunkSize">getChunkSize</a></li><li><a href="global.html#getSubChunk">getSubChunk</a></li><li><a href="global.html#getSubChunks">getSubChunks</a></li><li><a href="global.html#read">read</a></li><li><a href="global.html#write">write</a></li><li><a href="global.html#writeSubChunks">writeSubChunks</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#read">read</a></li><li><a href="global.html#write">write</a></li></ul>
</nav>

<div id="main">
Expand Down Expand Up @@ -104,7 +104,7 @@ <h2>riffChunks.write()</h2><pre class="prettyprint source lang-javascript"><code
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Apr 25 2018 01:05:40 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Apr 26 2018 10:35:18 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
81 changes: 43 additions & 38 deletions docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<label for="nav-trigger" class="overlay"></label>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#getChunkId">getChunkId</a></li><li><a href="global.html#getChunkSize">getChunkSize</a></li><li><a href="global.html#getSubChunk">getSubChunk</a></li><li><a href="global.html#getSubChunks">getSubChunks</a></li><li><a href="global.html#read">read</a></li><li><a href="global.html#write">write</a></li><li><a href="global.html#writeSubChunks">writeSubChunks</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#read">read</a></li><li><a href="global.html#write">write</a></li></ul>
</nav>

<div id="main">
Expand All @@ -45,64 +45,64 @@ <h1 class="page-title">index.js</h1>
*
*/

/** @private */
const byteData = require("byte-data");
const uInt32 = byteData.uInt32;
/** @private */
const chr = byteData.chr;
/** @private */
let uInt32 = byteData.uInt32;


/**
* Write the bytes of a RIFF/RIFX file.
* @param {Object} chunks A structure like the return of riffChunks.read().
* @param {boolean} bigEndian if the bytes should be big endian.
* "RIFX" chunkId will always set bigEndian to true.
* @return {Array&lt;number>|Uint8Array} The file bytes as Uint8Array when
* @return {Uint8Array} The file bytes as Uint8Array when
* chunkId is "RIFF" or "RIFX" or the chunk bytes as Array&lt;number>
* when chunkId is "LIST".
*/
function write(chunks, bigEndian=false) {
if (!bigEndian) {
uInt32["be"] = chunks["chunkId"] == "RIFX";
}
function write(chunks) {
uInt32.be = chunks["chunkId"] === "RIFX";
let bytes =
byteData.packArray(chunks["chunkId"], chr).concat(
byteData.pack(chunks["chunkSize"], uInt32),
byteData.packArray(chunks["format"], chr),
writeSubChunks(chunks["subChunks"], uInt32.be)
writeSubChunks_(chunks["subChunks"])
);
if (chunks["chunkId"] == "RIFF" || chunks["chunkId"] == "RIFX" ) {
if (chunks["chunkId"] === "RIFF" || chunks["chunkId"] === "RIFX" ) {
bytes = new Uint8Array(bytes);
}
return bytes;
}

/**
* Get the chunks of a RIFF/RIFX file.
* @param {Uint8Array|!Array&lt;number>} buffer The file bytes.
* @param {Uint8Array|Array&lt;number>} buffer The file bytes.
* @return {Object} The chunk.
*/
function read(buffer) {
buffer = [].slice.call(buffer);
let chunkId = getChunkId(buffer, 0);
uInt32["be"] = chunkId == "RIFX";
let chunkId = getChunkId_(buffer, 0);
uInt32.be = chunkId === "RIFX";
return {
"chunkId": chunkId,
"chunkSize": getChunkSize(buffer, 0),
"chunkSize": getChunkSize_(buffer, 0),
"format": byteData.unpackArray(buffer.slice(8, 12), chr),
"subChunks": getSubChunks(buffer)
"subChunks": getSubChunks_(buffer)
};
}

/**
* Write the sub chunks of a RIFF file.
* @param {Array&lt;Object>} chunks The chunks.
* @param {boolean} bigEndian true if its RIFX.
* @return {Array&lt;number>} The chunk bytes.
* @private
*/
function writeSubChunks(chunks, bigEndian) {
function writeSubChunks_(chunks) {
let subChunks = [];
let i = 0;
while (i &lt; chunks.length) {
if (chunks[i]["chunkId"] == "LIST") {
subChunks = subChunks.concat(write(chunks[i], bigEndian));
if (chunks[i]["chunkId"] === "LIST") {
subChunks = subChunks.concat(write(chunks[i]));
} else {
subChunks = subChunks.concat(
byteData.packArray(chunks[i]["chunkId"], chr),
Expand All @@ -117,57 +117,62 @@ <h1 class="page-title">index.js</h1>

/**
* Get the sub chunks of a RIFF file.
* @param {Uint8Array|!Array&lt;number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array&lt;number>} buffer the RIFF file bytes.
* @return {Object} The subchunks of a RIFF/RIFX or LIST chunk.
* @private
*/
function getSubChunks(buffer) {
function getSubChunks_(buffer) {
let chunks = [];
let i = 12;
while(i &lt; buffer.length) {
chunks.push(getSubChunk(buffer, i));
i += 8 + chunks[chunks.length - 1].chunkSize;
chunks.push(getSubChunk_(buffer, i));
i += 8 + chunks[chunks.length - 1]["chunkSize"];
}
return chunks;
}

/**
* Get a sub chunk from a RIFF file.
* @param {Uint8Array|!Array&lt;number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array&lt;number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {Object} A subchunk of a RIFF/RIFX or LIST chunk.
* @private
*/
function getSubChunk(buffer, index) {
function getSubChunk_(buffer, index) {
let chunk = {
"chunkId": getChunkId(buffer, index),
"chunkSize": getChunkSize(buffer, index)
"chunkId": getChunkId_(buffer, index),
"chunkSize": getChunkSize_(buffer, index)
};
if (chunk.chunkId == "LIST") {
chunk.format = byteData.unpackArray(buffer.slice(8, 12), chr);
chunk.subChunks = getSubChunks(
buffer.slice(index, index + chunk.chunkSize));
if (chunk["chunkId"] === "LIST") {
chunk["format"] = byteData.unpackArray(buffer.slice(8, 12), chr);
chunk["subChunks"] = getSubChunks_(
buffer.slice(index, index + chunk["chunkSize"]));
} else {
chunk.chunkData = buffer.slice(index + 8, index + 8 + chunk.chunkSize);
chunk["chunkData"] = buffer.slice(
index + 8, index + 8 + chunk["chunkSize"]);
}
return chunk;
}

/**
* Return the FourCC of a chunk.
* @param {Uint8Array|!Array&lt;number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array&lt;number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {string} The id of the chunk.
* @private
*/
function getChunkId(buffer, index) {
function getChunkId_(buffer, index) {
return byteData.unpackArray(buffer.slice(index, index + 4), chr);
}

/**
* Return the size of a chunk.
* @param {Uint8Array|!Array&lt;number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array&lt;number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {number} The size of the chunk without the id and size fields.
* @private
*/
function getChunkSize(buffer, index) {
function getChunkSize_(buffer, index) {
return byteData.unpack(buffer.slice(index + 4, index + 8), uInt32);
}

Expand All @@ -185,7 +190,7 @@ <h1 class="page-title">index.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Apr 25 2018 01:05:40 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Apr 26 2018 10:35:18 GMT-0300 (Hora oficial do Brasil) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
77 changes: 41 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,64 @@
*
*/

/** @private */
const byteData = require("byte-data");
const uInt32 = byteData.uInt32;
/** @private */
const chr = byteData.chr;
/** @private */
let uInt32 = byteData.uInt32;


/**
* Write the bytes of a RIFF/RIFX file.
* @param {Object} chunks A structure like the return of riffChunks.read().
* @param {boolean} bigEndian if the bytes should be big endian.
* "RIFX" chunkId will always set bigEndian to true.
* @return {Array<number>|Uint8Array} The file bytes as Uint8Array when
* @return {Uint8Array} The file bytes as Uint8Array when
* chunkId is "RIFF" or "RIFX" or the chunk bytes as Array<number>
* when chunkId is "LIST".
*/
function write(chunks, bigEndian=false) {
if (!bigEndian) {
uInt32["be"] = chunks["chunkId"] == "RIFX";
}
function write(chunks) {
uInt32.be = chunks["chunkId"] === "RIFX";
let bytes =
byteData.packArray(chunks["chunkId"], chr).concat(
byteData.pack(chunks["chunkSize"], uInt32),
byteData.packArray(chunks["format"], chr),
writeSubChunks(chunks["subChunks"], uInt32.be)
writeSubChunks_(chunks["subChunks"])
);
if (chunks["chunkId"] == "RIFF" || chunks["chunkId"] == "RIFX" ) {
if (chunks["chunkId"] === "RIFF" || chunks["chunkId"] === "RIFX" ) {
bytes = new Uint8Array(bytes);
}
return bytes;
}

/**
* Get the chunks of a RIFF/RIFX file.
* @param {Uint8Array|!Array<number>} buffer The file bytes.
* @param {Uint8Array|Array<number>} buffer The file bytes.
* @return {Object} The chunk.
*/
function read(buffer) {
buffer = [].slice.call(buffer);
let chunkId = getChunkId(buffer, 0);
uInt32["be"] = chunkId == "RIFX";
let chunkId = getChunkId_(buffer, 0);
uInt32.be = chunkId === "RIFX";
return {
"chunkId": chunkId,
"chunkSize": getChunkSize(buffer, 0),
"chunkSize": getChunkSize_(buffer, 0),
"format": byteData.unpackArray(buffer.slice(8, 12), chr),
"subChunks": getSubChunks(buffer)
"subChunks": getSubChunks_(buffer)
};
}

/**
* Write the sub chunks of a RIFF file.
* @param {Array<Object>} chunks The chunks.
* @param {boolean} bigEndian true if its RIFX.
* @return {Array<number>} The chunk bytes.
* @private
*/
function writeSubChunks(chunks, bigEndian) {
function writeSubChunks_(chunks) {
let subChunks = [];
let i = 0;
while (i < chunks.length) {
if (chunks[i]["chunkId"] == "LIST") {
subChunks = subChunks.concat(write(chunks[i], bigEndian));
if (chunks[i]["chunkId"] === "LIST") {
subChunks = subChunks.concat(write(chunks[i]));
} else {
subChunks = subChunks.concat(
byteData.packArray(chunks[i]["chunkId"], chr),
Expand All @@ -78,57 +78,62 @@ function writeSubChunks(chunks, bigEndian) {

/**
* Get the sub chunks of a RIFF file.
* @param {Uint8Array|!Array<number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array<number>} buffer the RIFF file bytes.
* @return {Object} The subchunks of a RIFF/RIFX or LIST chunk.
* @private
*/
function getSubChunks(buffer) {
function getSubChunks_(buffer) {
let chunks = [];
let i = 12;
while(i < buffer.length) {
chunks.push(getSubChunk(buffer, i));
i += 8 + chunks[chunks.length - 1].chunkSize;
chunks.push(getSubChunk_(buffer, i));
i += 8 + chunks[chunks.length - 1]["chunkSize"];
}
return chunks;
}

/**
* Get a sub chunk from a RIFF file.
* @param {Uint8Array|!Array<number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array<number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {Object} A subchunk of a RIFF/RIFX or LIST chunk.
* @private
*/
function getSubChunk(buffer, index) {
function getSubChunk_(buffer, index) {
let chunk = {
"chunkId": getChunkId(buffer, index),
"chunkSize": getChunkSize(buffer, index)
"chunkId": getChunkId_(buffer, index),
"chunkSize": getChunkSize_(buffer, index)
};
if (chunk.chunkId == "LIST") {
chunk.format = byteData.unpackArray(buffer.slice(8, 12), chr);
chunk.subChunks = getSubChunks(
buffer.slice(index, index + chunk.chunkSize));
if (chunk["chunkId"] === "LIST") {
chunk["format"] = byteData.unpackArray(buffer.slice(8, 12), chr);
chunk["subChunks"] = getSubChunks_(
buffer.slice(index, index + chunk["chunkSize"]));
} else {
chunk.chunkData = buffer.slice(index + 8, index + 8 + chunk.chunkSize);
chunk["chunkData"] = buffer.slice(
index + 8, index + 8 + chunk["chunkSize"]);
}
return chunk;
}

/**
* Return the FourCC of a chunk.
* @param {Uint8Array|!Array<number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array<number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {string} The id of the chunk.
* @private
*/
function getChunkId(buffer, index) {
function getChunkId_(buffer, index) {
return byteData.unpackArray(buffer.slice(index, index + 4), chr);
}

/**
* Return the size of a chunk.
* @param {Uint8Array|!Array<number>} buffer the RIFF file bytes.
* @param {Uint8Array|Array<number>} buffer the RIFF file bytes.
* @param {number} index The start index of the chunk.
* @return {number} The size of the chunk without the id and size fields.
* @private
*/
function getChunkSize(buffer, index) {
function getChunkSize_(buffer, index) {
return byteData.unpack(buffer.slice(index + 4, index + 8), uInt32);
}

Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "riff-chunks",
"version": "3.1.0",
"version": "4.0.0",
"description": "Read and write the chunks of RIFF and RIFX files.",
"homepage": "https://github.com/rochars/riff-chunks",
"author": "Rafael da Silva Rocha <[email protected]>",
Expand All @@ -25,12 +25,10 @@
"test": "nyc ./node_modules/mocha/bin/_mocha test --src --recursive",
"test-dist": "nyc ./node_modules/mocha/bin/_mocha test --dist --recursive",
"coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
"bundle": "webpack",
"compile": "google-closure-compiler-js dist/riff-chunks.js > dist/riff-chunks-min.js",
"compile": "google-closure-compiler-js dist/riff-chunks.js > dist/riff-chunks-min.js --compilationLevel=ADVANCED",
"doc": "./node_modules/.bin/jsdoc index.js -d docs -r README.md -t node_modules/docdash",
"qa": "npm run lint && npm test",
"pack": "npm run bundle && npm run compile && npm run doc",
"build": "npm run qa && npm run pack && npm run test-dist"
"pack": "webpack && npm run compile && npm run doc",
"build": "npm run lint && npm test && npm run pack && npm run test-dist"
},
"devDependencies": {
"browser-env": "^3.2.4",
Expand All @@ -47,6 +45,6 @@
},
"main": "index.js",
"dependencies": {
"byte-data": "^4.0.2"
"byte-data": "^5.0.1"
}
}
Loading

0 comments on commit 5ffeb66

Please sign in to comment.