-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
170 lines (159 loc) · 4.52 KB
/
factory.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/
'use strict';
// MODULES //
var isCollection = require( '@stdlib/assert/is-collection' );
var isArrayLike = require( '@stdlib/assert/is-array-like' );
var Proxy = require( '@stdlib/proxy/ctor' );
var arraylike2object = require( './../../base/arraylike2object' );
var assign = require( '@stdlib/object/assign' );
var format = require( '@stdlib/string/format' );
var setElementWrapper = require( './set_element_wrapper.js' );
var getArrayWrapper = require( './get_array_wrapper.js' );
var hasProxySupport = require( './has_proxy_support.js' );
var defaults = require( './defaults.js' );
var validate = require( './validate.js' );
var validator = require( './validator.js' );
var ctor = require( './ctor.js' );
var get = require( './get.js' );
var set = require( './set.js' );
// MAIN //
/**
* Returns a function for converting an array to an object supporting fancy indexing.
*
* @param {Options} options - function options
* @param {boolean} [options.strict=false] - boolean indicating whether to enforce strict bounds checking by default
* @param {Function} [options.cache] - default cache for resolving array index objects
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Function} function for converting an array to an object supporting fancy indexing
*
* @example
* var array2fancy = factory();
*
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var y = array2fancy( x );
* // returns <Array>
*
* var z = y[ '1::2' ];
* // returns [ 2, 4, 6 ]
*
* var len = z.length;
* // returns 3
*
* var v = z[ 0 ];
* // returns 2
*
* v = z[ 1 ];
* // returns 4
*
* v = z[ 2 ];
* // returns 6
*/
function factory() {
var OPTIONS;
var err;
OPTIONS = defaults();
if ( arguments.length ) {
err = validate( OPTIONS, arguments[ 0 ] );
if ( err ) {
throw err;
}
}
return array2fancy;
/**
* Converts an array to an object supporting fancy indexing.
*
* @private
* @param {ArrayLike} x - input array
* @param {Options} [options] - function options
* @param {boolean} [options.strict] - boolean indicating whether to enforce strict bounds checking
* @param {Function} [options.cache] - cache for resolving array index objects
* @throws {TypeError} first argument must be array-like
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {ArrayLike} fancy array
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var y = array2fancy( x );
* // returns <Array>
*
* var z = y[ '1::2' ];
* // returns [ 2, 4, 6 ]
*
* var len = z.length;
* // returns 3
*
* var v = z[ 0 ];
* // returns 2
*
* v = z[ 1 ];
* // returns 4
*
* v = z[ 2 ];
* // returns 6
*/
function array2fancy( x ) {
var opts;
var err;
var arr;
var dt;
var o;
if ( !isArrayLike( x ) && !isCollection( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be array-like. Value: `%s`.', x ) );
}
if ( hasProxySupport ) {
opts = assign( {}, OPTIONS );
if ( arguments.length > 1 ) {
err = validate( opts, arguments[ 1 ] );
if ( err ) {
throw err;
}
}
arr = arraylike2object( x );
dt = arr.dtype || '';
o = {
'ref': x,
'dtype': dt,
'getter': arr.accessors[ 0 ],
'setter': arr.accessors[ 1 ],
'preSetElement': setElementWrapper( dt ),
'postGetArray': getArrayWrapper( array2fancy, opts ),
'cache': opts.cache,
'strict': opts.strict,
'validator': validator( dt ),
'array2fancy': array2fancy,
'ctor': new Proxy( x.constructor || Array, {
'construct': ctor( array2fancy, opts )
})
};
return new Proxy( x, {
'get': get( o ),
'set': set( o )
});
}
// TODO: replace with `@stdlib/console/warn` (or equivalent once available)
console.warn( 'WARNING: Proxy objects are not supported in the current environment. Some `FancyArray` functionality may not be available.' ); // eslint-disable-line no-console
return x;
}
}
// EXPORTS //
module.exports = factory;