1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var hasOwnProperty = require('./HasOwnProperty');
|
---|
6 | var ToBigInt64 = require('./ToBigInt64');
|
---|
7 | var ToBigUint64 = require('./ToBigUint64');
|
---|
8 | var ToInt16 = require('./ToInt16');
|
---|
9 | var ToInt32 = require('./ToInt32');
|
---|
10 | var ToInt8 = require('./ToInt8');
|
---|
11 | var ToUint16 = require('./ToUint16');
|
---|
12 | var ToUint32 = require('./ToUint32');
|
---|
13 | var ToUint8 = require('./ToUint8');
|
---|
14 | var ToUint8Clamp = require('./ToUint8Clamp');
|
---|
15 |
|
---|
16 | var valueToFloat32Bytes = require('../helpers/valueToFloat32Bytes');
|
---|
17 | var valueToFloat64Bytes = require('../helpers/valueToFloat64Bytes');
|
---|
18 | var integerToNBytes = require('../helpers/integerToNBytes');
|
---|
19 |
|
---|
20 | var keys = require('object-keys');
|
---|
21 |
|
---|
22 | // https://262.ecma-international.org/15.0/#table-the-typedarray-constructors
|
---|
23 | var TypeToSizes = {
|
---|
24 | __proto__: null,
|
---|
25 | INT8: 1,
|
---|
26 | UINT8: 1,
|
---|
27 | UINT8C: 1,
|
---|
28 | INT16: 2,
|
---|
29 | UINT16: 2,
|
---|
30 | INT32: 4,
|
---|
31 | UINT32: 4,
|
---|
32 | BIGINT64: 8,
|
---|
33 | BIGUINT64: 8,
|
---|
34 | FLOAT32: 4,
|
---|
35 | FLOAT64: 8
|
---|
36 | };
|
---|
37 |
|
---|
38 | var TypeToAO = {
|
---|
39 | __proto__: null,
|
---|
40 | INT8: ToInt8,
|
---|
41 | UINT8: ToUint8,
|
---|
42 | UINT8C: ToUint8Clamp,
|
---|
43 | INT16: ToInt16,
|
---|
44 | UINT16: ToUint16,
|
---|
45 | INT32: ToInt32,
|
---|
46 | UINT32: ToUint32,
|
---|
47 | BIGINT64: ToBigInt64,
|
---|
48 | BIGUINT64: ToBigUint64
|
---|
49 | };
|
---|
50 |
|
---|
51 | // https://262.ecma-international.org/15.0/#sec-numerictorawbytes
|
---|
52 |
|
---|
53 | module.exports = function NumericToRawBytes(type, value, isLittleEndian) {
|
---|
54 | if (typeof type !== 'string' || !hasOwnProperty(TypeToSizes, type)) {
|
---|
55 | throw new $TypeError('Assertion failed: `type` must be a TypedArray element type: ' + keys(TypeToSizes));
|
---|
56 | }
|
---|
57 | if (typeof value !== 'number' && typeof value !== 'bigint') {
|
---|
58 | throw new $TypeError('Assertion failed: `value` must be a Number or a BigInt');
|
---|
59 | }
|
---|
60 | if (typeof isLittleEndian !== 'boolean') {
|
---|
61 | throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (type === 'FLOAT32') { // step 1
|
---|
65 | return valueToFloat32Bytes(value, isLittleEndian);
|
---|
66 | } else if (type === 'FLOAT64') { // step 2
|
---|
67 | return valueToFloat64Bytes(value, isLittleEndian);
|
---|
68 | } // step 3
|
---|
69 |
|
---|
70 | var n = TypeToSizes[type]; // step 3.a
|
---|
71 |
|
---|
72 | var convOp = TypeToAO[type]; // step 3.b
|
---|
73 |
|
---|
74 | var intValue = convOp(value); // step 3.c
|
---|
75 |
|
---|
76 | return integerToNBytes(intValue, n, isLittleEndian); // step 3.d, 3.e, 4
|
---|
77 | };
|
---|