source: imaps-frontend/node_modules/es-abstract/helpers/integerToNBytes.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.1 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $Number = GetIntrinsic('%Number%');
6var $BigInt = GetIntrinsic('%BigInt%', true);
7
8module.exports = function integerToNBytes(intValue, n, isLittleEndian) {
9 var Z = typeof intValue === 'bigint' ? $BigInt : $Number;
10 /*
11 if (intValue >= 0) { // step 3.d
12 // Let rawBytes be a List containing the n-byte binary encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
13 } else { // step 3.e
14 // Let rawBytes be a List containing the n-byte binary 2's complement encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
15 }
16 */
17 if (intValue < 0) {
18 intValue >>>= 0; // eslint-disable-line no-param-reassign
19 }
20
21 var rawBytes = [];
22 for (var i = 0; i < n; i++) {
23 rawBytes[isLittleEndian ? i : n - 1 - i] = $Number(intValue & Z(0xFF));
24 intValue >>= Z(8); // eslint-disable-line no-param-reassign
25 }
26
27 return rawBytes; // step 4
28};
Note: See TracBrowser for help on using the repository browser.