mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
 
var test = require('tape');
var typedArrayByteLength = require('../');
var isCallable = require('is-callable');
var generators = require('make-generator-function')();
var arrowFn = require('make-arrow-function')();
var forEach = require('for-each');
var inspect = require('object-inspect');
var availableTypedArrays = require('available-typed-arrays')();
 
test('not arrays', function (t) {
    t.test('non-number/string primitives', function (st) {
        // @ts-expect-error
        st.equal(false, typedArrayByteLength(), 'undefined is not typed array');
        st.equal(false, typedArrayByteLength(null), 'null is not typed array');
        st.equal(false, typedArrayByteLength(false), 'false is not typed array');
        st.equal(false, typedArrayByteLength(true), 'true is not typed array');
        st.end();
    });
 
    t.equal(false, typedArrayByteLength({}), 'object is not typed array');
    t.equal(false, typedArrayByteLength(/a/g), 'regex literal is not typed array');
    t.equal(false, typedArrayByteLength(new RegExp('a', 'g')), 'regex object is not typed array');
    t.equal(false, typedArrayByteLength(new Date()), 'new Date() is not typed array');
 
    t.test('numbers', function (st) {
        st.equal(false, typedArrayByteLength(42), 'number is not typed array');
        st.equal(false, typedArrayByteLength(Object(42)), 'number object is not typed array');
        st.equal(false, typedArrayByteLength(NaN), 'NaN is not typed array');
        st.equal(false, typedArrayByteLength(Infinity), 'Infinity is not typed array');
        st.end();
    });
 
    t.test('strings', function (st) {
        st.equal(false, typedArrayByteLength('foo'), 'string primitive is not typed array');
        st.equal(false, typedArrayByteLength(Object('foo')), 'string object is not typed array');
        st.end();
    });
 
    t.end();
});
 
test('Functions', function (t) {
    t.equal(false, typedArrayByteLength(function () {}), 'function is not typed array');
    t.end();
});
 
test('Generators', { skip: generators.length === 0 }, function (t) {
    forEach(generators, function (genFn) {
        t.equal(false, typedArrayByteLength(genFn), 'generator function ' + inspect(genFn) + ' is not typed array');
    });
    t.end();
});
 
test('Arrow functions', { skip: !arrowFn }, function (t) {
    t.equal(false, typedArrayByteLength(arrowFn), 'arrow function is not typed array');
    t.end();
});
 
test('Typed Arrays', { skip: availableTypedArrays.length === 0 }, function (t) {
    var length = 64;
    var byteLength = 32;
 
    forEach(availableTypedArrays, function (typedArray) {
        var buffer = new ArrayBuffer(length);
        var TypedArray = global[typedArray];
        if (isCallable(TypedArray)) {
            var arr = new TypedArray(buffer, byteLength);
            t.equal(typedArrayByteLength(arr), byteLength, 'new ' + typedArray + '(new ArrayBuffer(' + length + '), ' + byteLength + ') is typed array of byte Length ' + byteLength);
        } else {
            t.comment('# SKIP ' + typedArray + ' is not supported');
        }
    });
 
    var buffer = new ArrayBuffer(8);
    var uint8 = new Uint8Array(buffer, 2);
 
    t.equal(typedArrayByteLength(uint8), 6, 'byteLength is as expected');
 
    t.end();
});