endianness
Advanced tools
Comparing version 7.0.2 to 7.0.3
# CHANGELOG | ||
## version 7.0.3 (2018-07-09) | ||
- Fix: UMD dist transpiled to ES5 | ||
## version 7.0.2 (2018-07-06) | ||
@@ -4,0 +7,0 @@ - Fix: make 'offset' param not optional in index.d.ts |
@@ -33,6 +33,2 @@ 'use strict'; | ||
/** | ||
* @module endianness | ||
*/ | ||
/** | ||
* Swap the byte ordering in a buffer. The buffer is modified in place. | ||
@@ -42,15 +38,12 @@ * @param {!Array<number|string>|!Uint8Array} bytes The bytes. | ||
* @param {number=} index The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @param {number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
function endianness(bytes, offset, index=0, end=null) { | ||
let len = end || bytes.length; | ||
let limit = parseInt(offset / 2, 10); | ||
if (len % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
while (index < len) { | ||
swap(bytes, offset, index, limit); | ||
index += offset; | ||
} | ||
function endianness(bytes, offset, index=0, end=bytes.length) { | ||
if (end % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
for (; index < end; index += offset) { | ||
swap(bytes, offset, index); | ||
} | ||
} | ||
@@ -65,12 +58,11 @@ | ||
*/ | ||
function swap(bytes, offset, index, limit) { | ||
let x = 0; | ||
let y = offset - 1; | ||
while(x < limit) { | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + y]; | ||
bytes[index + y] = theByte; | ||
x++; | ||
y--; | ||
} | ||
function swap(bytes, offset, index) { | ||
offset--; | ||
for(let x = 0; x < offset; x++) { | ||
/** @type {number|string} */ | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + offset]; | ||
bytes[index + offset] = theByte; | ||
offset--; | ||
} | ||
} | ||
@@ -77,0 +69,0 @@ |
@@ -31,6 +31,2 @@ /* | ||
/** | ||
* @module endianness | ||
*/ | ||
/** | ||
* Swap the byte ordering in a buffer. The buffer is modified in place. | ||
@@ -40,15 +36,12 @@ * @param {!Array<number|string>|!Uint8Array} bytes The bytes. | ||
* @param {number=} index The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @param {number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
function endianness(bytes, offset, index=0, end=null) { | ||
let len = end || bytes.length; | ||
let limit = parseInt(offset / 2, 10); | ||
if (len % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
while (index < len) { | ||
swap(bytes, offset, index, limit); | ||
index += offset; | ||
} | ||
function endianness(bytes, offset, index=0, end=bytes.length) { | ||
if (end % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
for (; index < end; index += offset) { | ||
swap(bytes, offset, index); | ||
} | ||
} | ||
@@ -63,14 +56,13 @@ | ||
*/ | ||
function swap(bytes, offset, index, limit) { | ||
let x = 0; | ||
let y = offset - 1; | ||
while(x < limit) { | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + y]; | ||
bytes[index + y] = theByte; | ||
x++; | ||
y--; | ||
} | ||
function swap(bytes, offset, index) { | ||
offset--; | ||
for(let x = 0; x < offset; x++) { | ||
/** @type {number|string} */ | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + offset]; | ||
bytes[index + offset] = theByte; | ||
offset--; | ||
} | ||
} | ||
export default endianness; |
/* | ||
endianness Copyright (c) 2017-2018 Rafael da Silva Rocha. | ||
*/ | ||
window.endianness=function(h,c,a,b){a=void 0===a?0:a;b=(void 0===b?null:b)||h.length;var k=parseInt(c/2,10);if(b%c)throw Error("Bad buffer length.");for(;a<b;){for(var d=h,e=a,l=k,f=0,g=c-1;f<l;){var m=d[e+f];d[e+f]=d[e+g];d[e+g]=m;f++;g--}a+=c}}; | ||
window.endianness=function(h,g,a,b){a=void 0===a?0:a;b=void 0===b?h.length:b;if(b%g)throw Error("Bad buffer length.");for(;a<b;a+=g){var d=h,c=g,e=a;c--;for(var f=0;f<c;f++){var k=d[e+f];d[e+f]=d[e+c];d[e+c]=k;c--}}}; |
@@ -1,81 +0,14 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.endianness = factory()); | ||
}(this, (function () { 'use strict'; | ||
/* | ||
* Copyright (c) 2017-2018 Rafael da Silva Rocha. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
/** | ||
* @fileoverview A function to swap endianness in byte buffers. | ||
* @see https://github.com/rochars/endianness | ||
*/ | ||
/** | ||
* @module endianness | ||
*/ | ||
/** | ||
* Swap the byte ordering in a buffer. The buffer is modified in place. | ||
* @param {!Array<number|string>|!Uint8Array} bytes The bytes. | ||
* @param {number} offset The byte offset. | ||
* @param {number=} index The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
function endianness(bytes, offset, index=0, end=null) { | ||
let len = end || bytes.length; | ||
let limit = parseInt(offset / 2, 10); | ||
if (len % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
while (index < len) { | ||
swap(bytes, offset, index, limit); | ||
index += offset; | ||
} | ||
} | ||
/** | ||
* Swap the byte order of a value in a buffer. The buffer is modified in place. | ||
* @param {!Array<number|string>|!Uint8Array} bytes The bytes. | ||
* @param {number} offset The byte offset. | ||
* @param {number} index The start index. | ||
* @private | ||
*/ | ||
function swap(bytes, offset, index, limit) { | ||
let x = 0; | ||
let y = offset - 1; | ||
while(x < limit) { | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + y]; | ||
bytes[index + y] = theByte; | ||
x++; | ||
y--; | ||
} | ||
} | ||
return endianness; | ||
}))); | ||
(function (global, factory) {typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :typeof define === 'function' && define.amd ? define(factory) :(global.endianness = factory());}(this, (function () { 'use strict';var endianness=function(){/** | ||
@param {(!Array<(number|string)>|!Uint8Array)} bytes | ||
@param {number} offset | ||
@param {number=} index | ||
@param {number=} end | ||
@throws {Error} | ||
*/ | ||
function endianness(bytes,offset,index,end){index=index===undefined?0:index;end=end===undefined?bytes.length:end;if(end%offset)throw new Error("Bad buffer length.");for(;index<end;index+=offset)swap(bytes,offset,index)}/** | ||
@private | ||
@param {(!Array<(number|string)>|!Uint8Array)} bytes | ||
@param {number} offset | ||
@param {number} index | ||
*/ | ||
function swap(bytes,offset,index){offset--;for(var x=0;x<offset;x++){/** @type {(number|string)} */ var theByte=bytes[index+x];bytes[index+x]=bytes[index+offset];bytes[index+offset]=theByte;offset--}}return endianness}();return endianness; }))); |
@@ -22,7 +22,7 @@ # Distribution | ||
- The **CommonJS** is the dist file used by Node. It is served in the "main" field of package.json. This is the source you are running when you **npm install endianness**. | ||
- The **CommonJS** dist is **./dist/endianness.cjs.js**. It is the dist file used by Node. It is served in the "main" field of package.json and is the source you are running when you **npm install endianness**. It is not compiled or minified. | ||
- The **UMD** module is compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. This file is not compiled/minified as it may be used by module bundlers. Compilation/minification should be up to the bundler consuming this file. | ||
- The **UMD** module is **./dist/endianness.umd.js**. It is transpiled to ES5 and compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. | ||
- The **compiled dist** is browser-only and should be the one served by CDNs. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
- The **browser-only** dist is **./dist/endianness.min.js**. It is transpiled to ES5 and compiled. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
@@ -29,0 +29,0 @@ - The **ES6 dist** is **./dist/endianness.js**, served as "es2015" in package.json. It is not compiled/minified. |
@@ -13,5 +13,5 @@ /** | ||
* @param {number=} start The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @param {number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
function endianness(bytes, offset, start=0, end=null) {} |
42
index.js
@@ -31,6 +31,2 @@ /* | ||
/** | ||
* @module endianness | ||
*/ | ||
/** | ||
* Swap the byte ordering in a buffer. The buffer is modified in place. | ||
@@ -40,15 +36,12 @@ * @param {!Array<number|string>|!Uint8Array} bytes The bytes. | ||
* @param {number=} index The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @param {number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
export default function endianness(bytes, offset, index=0, end=null) { | ||
let len = end || bytes.length; | ||
let limit = parseInt(offset / 2, 10); | ||
if (len % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
while (index < len) { | ||
swap(bytes, offset, index, limit); | ||
index += offset; | ||
} | ||
export default function endianness(bytes, offset, index=0, end=bytes.length) { | ||
if (end % offset) { | ||
throw new Error("Bad buffer length."); | ||
} | ||
for (; index < end; index += offset) { | ||
swap(bytes, offset, index); | ||
} | ||
} | ||
@@ -63,12 +56,11 @@ | ||
*/ | ||
function swap(bytes, offset, index, limit) { | ||
let x = 0; | ||
let y = offset - 1; | ||
while(x < limit) { | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + y]; | ||
bytes[index + y] = theByte; | ||
x++; | ||
y--; | ||
} | ||
function swap(bytes, offset, index) { | ||
offset--; | ||
for(let x = 0; x < offset; x++) { | ||
/** @type {number|string} */ | ||
let theByte = bytes[index + x]; | ||
bytes[index + x] = bytes[index + offset]; | ||
bytes[index + offset] = theByte; | ||
offset--; | ||
} | ||
} |
{ | ||
"name": "endianness", | ||
"version": "7.0.2", | ||
"version": "7.0.3", | ||
"description": "Swap endianness in byte arrays.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rochars/endianness", |
@@ -21,12 +21,2 @@ # endianness | ||
### ES6 | ||
```import``` endianness from **./dist/endianness.js**: | ||
```javascript | ||
import endianness from './dist/endianness.js'; | ||
// Swap endianness of one 64-bit value: | ||
let bytes = [64, 9, 33, 251, 84, 68, 45, 24]; | ||
endianness(bytes, 8); | ||
``` | ||
### Node | ||
@@ -42,2 +32,12 @@ ```require``` **endianness**: | ||
### ES module | ||
```import``` endianness from **./dist/endianness.js**: | ||
```javascript | ||
import endianness from './dist/endianness.js'; | ||
// Swap endianness of one 64-bit value: | ||
let bytes = [64, 9, 33, 251, 84, 68, 45, 24]; | ||
endianness(bytes, 8); | ||
``` | ||
### Browser | ||
@@ -77,7 +77,7 @@ Use **endianness.min.js** from the **dist/** folder: | ||
* @param {number} offset The byte offset. | ||
* @param {number=} start The start index. Assumes 0. | ||
* @param {?number=} end The end index. Assumes the buffer length. | ||
* @param {number=} index The start index. Assumes 0. | ||
* @param {number=} end The end index. Assumes the buffer length. | ||
* @throws {Error} If the buffer length is not valid. | ||
*/ | ||
function endianness(bytes, offset, start=0, end=null) {} | ||
function endianness(bytes, offset, index=0, end=bytes.length) {} | ||
``` | ||
@@ -106,7 +106,7 @@ | ||
- The **CommonJS** is the dist file used by Node. It is served in the "main" field of package.json. This is the source you are running when you **npm install endianness**. | ||
- The **CommonJS** dist is **./dist/endianness.cjs.js**. It is the dist file used by Node. It is served in the "main" field of package.json and is the source you are running when you **npm install endianness**. It is not compiled or minified. | ||
- The **UMD** module is compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. This file is not compiled/minified as it may be used by module bundlers. Compilation/minification should be up to the bundler consuming this file. | ||
- The **UMD** module is **./dist/endianness.umd.js**. It is transpiled to ES5 and compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. | ||
- The **compiled dist** is browser-only and should be the one served by CDNs. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
- The **browser-only** dist is **./dist/endianness.min.js**. It is transpiled to ES5 and compiled. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
@@ -113,0 +113,0 @@ - The **ES6 dist** is **./dist/endianness.js**, served as "es2015" in package.json. It is not compiled/minified. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22623
222