Repository navigation
Expand file tree
/
Copy pathtest.ts
More file actions
130 lines (107 loc) · 3.79 KB
/
Copy pathtest.ts
File metadata and controls
130 lines (107 loc) · 3.79 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="@stdlib/types"/>
import { AccessorArrayLike, ArrayLike } from '@stdlib/types/array';
import Complex128Array = require( '@stdlib/array-complex128' );
import Complex64Array = require( '@stdlib/array-complex64' );
import Float16Array = require( '@stdlib/array-float16' );
import arraylike2object = require( './index' );
/**
* Returns an array-like object supporting the get/set protocol.
*
* @returns array-like object
*/
function accessorArray(): AccessorArrayLike<number> {
const arr: AccessorArrayLike<number> = {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'length': 4,
'get': ( idx: number ): number => {
return arr[ idx ];
},
'set': ( value: number, idx: number ): void => {
arr[ idx ] = value;
}
};
return arr;
}
/**
* Returns an array-like object.
*
* @returns array-like object
*/
function arrayLike(): ArrayLike<number> {
const arr: ArrayLike<number> = {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'length': 4
};
return arr;
}
// TESTS //
// The function returns an array object...
{
const x1 = [ 1, 2, 3, 4, 5, 6 ];
arraylike2object( x1 ); // $ExpectType GenericAccessorObject<number>
const x2 = new Float64Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x2 ); // $ExpectType Float64AccessorObject
const x3 = new Float32Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x3 ); // $ExpectType Float32AccessorObject
const x4 = new Float16Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x4 ); // $ExpectType Float16AccessorObject
const x5 = new Int32Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x5 ); // $ExpectType Int32AccessorObject
const x6 = new Int16Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x6 ); // $ExpectType Int16AccessorObject
const x7 = new Int8Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x7 ); // $ExpectType Int8AccessorObject
const x8 = new Uint32Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x8 ); // $ExpectType Uint32AccessorObject
const x9 = new Uint16Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x9 ); // $ExpectType Uint16AccessorObject
const x10 = new Uint8Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x10 ); // $ExpectType Uint8AccessorObject
const x11 = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x11 ); // $ExpectType Uint8cAccessorObject
const x12 = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x12 ); // $ExpectType Complex128AccessorObject
const x13 = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
arraylike2object( x13 ); // $ExpectType Complex64AccessorObject
const x14 = accessorArray();
arraylike2object( x14 ); // $ExpectType GetSetAccessorObject<number>
const x15 = arrayLike();
arraylike2object( x15 ); // $ExpectType IndexedAccessorObject<number>
}
// The compiler throws an error if the function is not provided an array-like object...
{
arraylike2object( 123 ); // $ExpectError
arraylike2object( true ); // $ExpectError
arraylike2object( false ); // $ExpectError
arraylike2object( null ); // $ExpectError
arraylike2object( {} ); // $ExpectError
}
// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = [ 1, 2, 3, 4, 5, 6 ];
arraylike2object(); // $ExpectError
arraylike2object( x, 5 ); // $ExpectError
}