'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
 
var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');
 
var IsArray = require('./IsArray');
var IsConstructor = require('./IsConstructor');
var ValidateTypedArray = require('./ValidateTypedArray');
 
var availableTypedArrays = require('available-typed-arrays')();
var typedArrayLength = require('typed-array-length');
 
// https://262.ecma-international.org/7.0/#typedarray-create
 
module.exports = function TypedArrayCreate(constructor, argumentList) {
    if (!IsConstructor(constructor)) {
        throw new $TypeError('Assertion failed: `constructor` must be a constructor');
    }
    if (!IsArray(argumentList)) {
        throw new $TypeError('Assertion failed: `argumentList` must be a List');
    }
    if (availableTypedArrays.length === 0) {
        throw new $SyntaxError('Assertion failed: Typed Arrays are not supported in this environment');
    }
 
    // var newTypedArray = Construct(constructor, argumentList); // step 1
    var newTypedArray;
    if (argumentList.length === 0) {
        newTypedArray = new constructor();
    } else if (argumentList.length === 1) {
        newTypedArray = new constructor(argumentList[0]);
    } else if (argumentList.length === 2) {
        newTypedArray = new constructor(argumentList[0], argumentList[1]);
    } else {
        newTypedArray = new constructor(argumentList[0], argumentList[1], argumentList[2]);
    }
 
    ValidateTypedArray(newTypedArray); // step 2
 
    if (argumentList.length === 1 && typeof argumentList[0] === 'number') { // step 3
        if (typedArrayLength(newTypedArray) < argumentList[0]) {
            throw new $TypeError('Assertion failed: `argumentList[0]` must be <= `newTypedArray.length`'); // step 3.a
        }
    }
 
    return newTypedArray; // step 4
};