@stdlib/array-to-fancy 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +646 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +443 -0
- package/lib/ctor.js +94 -0
- package/lib/defaults.js +48 -0
- package/lib/error_constructor.js +53 -0
- package/lib/error_message.js +42 -0
- package/lib/factory.js +169 -0
- package/lib/get.js +79 -0
- package/lib/get_array_wrapper.js +51 -0
- package/lib/get_element.js +46 -0
- package/lib/get_elements.js +63 -0
- package/lib/get_slice.js +60 -0
- package/lib/get_value.js +70 -0
- package/lib/has_proxy_support.js +40 -0
- package/lib/index.js +64 -0
- package/lib/is_array_index_string.js +51 -0
- package/lib/is_integer_string.js +51 -0
- package/lib/main.js +68 -0
- package/lib/prop2array.js +69 -0
- package/lib/prop2slice.js +163 -0
- package/lib/re_array_index.js +47 -0
- package/lib/re_integer.js +47 -0
- package/lib/re_subseq.js +47 -0
- package/lib/resolve_index.js +66 -0
- package/lib/set.js +86 -0
- package/lib/set_element.js +65 -0
- package/lib/set_element_wrapper.js +65 -0
- package/lib/set_slice.js +83 -0
- package/lib/set_value.js +40 -0
- package/lib/validate.js +74 -0
- package/lib/validator.js +211 -0
- package/package.json +146 -0
package/lib/factory.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isArrayLike = require( '@stdlib/assert-is-array-like' );
|
|
24
|
+
var Proxy = require( '@stdlib/proxy-ctor' );
|
|
25
|
+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
|
|
26
|
+
var assign = require( '@stdlib/object-assign' );
|
|
27
|
+
var format = require( '@stdlib/string-format' );
|
|
28
|
+
var setElementWrapper = require( './set_element_wrapper.js' );
|
|
29
|
+
var getArrayWrapper = require( './get_array_wrapper.js' );
|
|
30
|
+
var hasProxySupport = require( './has_proxy_support.js' );
|
|
31
|
+
var defaults = require( './defaults.js' );
|
|
32
|
+
var validate = require( './validate.js' );
|
|
33
|
+
var validator = require( './validator.js' );
|
|
34
|
+
var ctor = require( './ctor.js' );
|
|
35
|
+
var get = require( './get.js' );
|
|
36
|
+
var set = require( './set.js' );
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// MAIN //
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns a function for converting an array to an object supporting fancy indexing.
|
|
43
|
+
*
|
|
44
|
+
* @param {Options} options - function options
|
|
45
|
+
* @param {boolean} [options.strict=false] - boolean indicating whether to enforce strict bounds checking by default
|
|
46
|
+
* @param {Function} [options.cache] - default cache for resolving array index objects
|
|
47
|
+
* @throws {TypeError} options argument must be an object
|
|
48
|
+
* @throws {TypeError} must provide valid options
|
|
49
|
+
* @returns {Function} function for converting an array to an object supporting fancy indexing
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* var array2fancy = factory();
|
|
53
|
+
*
|
|
54
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
55
|
+
*
|
|
56
|
+
* var y = array2fancy( x );
|
|
57
|
+
* // returns <Array>
|
|
58
|
+
*
|
|
59
|
+
* var z = y[ '1::2' ];
|
|
60
|
+
* // returns [ 2, 4, 6 ]
|
|
61
|
+
*
|
|
62
|
+
* var len = z.length;
|
|
63
|
+
* // returns 3
|
|
64
|
+
*
|
|
65
|
+
* var v = z[ 0 ];
|
|
66
|
+
* // returns 2
|
|
67
|
+
*
|
|
68
|
+
* v = z[ 1 ];
|
|
69
|
+
* // returns 4
|
|
70
|
+
*
|
|
71
|
+
* v = z[ 2 ];
|
|
72
|
+
* // returns 6
|
|
73
|
+
*/
|
|
74
|
+
function factory() {
|
|
75
|
+
var OPTIONS;
|
|
76
|
+
var err;
|
|
77
|
+
|
|
78
|
+
OPTIONS = defaults();
|
|
79
|
+
if ( arguments.length ) {
|
|
80
|
+
err = validate( OPTIONS, arguments[ 0 ] );
|
|
81
|
+
if ( err ) {
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return array2fancy;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Converts an array to an object supporting fancy indexing.
|
|
89
|
+
*
|
|
90
|
+
* @private
|
|
91
|
+
* @param {ArrayLike} x - input array
|
|
92
|
+
* @param {Options} [options] - function options
|
|
93
|
+
* @param {boolean} [options.strict] - boolean indicating whether to enforce strict bounds checking
|
|
94
|
+
* @param {Function} [options.cache] - cache for resolving array index objects
|
|
95
|
+
* @throws {TypeError} first argument must be array-like
|
|
96
|
+
* @throws {TypeError} options argument must be an object
|
|
97
|
+
* @throws {TypeError} must provide valid options
|
|
98
|
+
* @returns {ArrayLike} fancy array
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
102
|
+
*
|
|
103
|
+
* var y = array2fancy( x );
|
|
104
|
+
* // returns <Array>
|
|
105
|
+
*
|
|
106
|
+
* var z = y[ '1::2' ];
|
|
107
|
+
* // returns [ 2, 4, 6 ]
|
|
108
|
+
*
|
|
109
|
+
* var len = z.length;
|
|
110
|
+
* // returns 3
|
|
111
|
+
*
|
|
112
|
+
* var v = z[ 0 ];
|
|
113
|
+
* // returns 2
|
|
114
|
+
*
|
|
115
|
+
* v = z[ 1 ];
|
|
116
|
+
* // returns 4
|
|
117
|
+
*
|
|
118
|
+
* v = z[ 2 ];
|
|
119
|
+
* // returns 6
|
|
120
|
+
*/
|
|
121
|
+
function array2fancy( x ) {
|
|
122
|
+
var opts;
|
|
123
|
+
var err;
|
|
124
|
+
var arr;
|
|
125
|
+
var dt;
|
|
126
|
+
var o;
|
|
127
|
+
if ( !isArrayLike( x ) ) {
|
|
128
|
+
throw new TypeError( format( 'invalid argument. First argument must be array-like. Value: `%s`.', x ) );
|
|
129
|
+
}
|
|
130
|
+
if ( hasProxySupport ) {
|
|
131
|
+
opts = assign( {}, OPTIONS );
|
|
132
|
+
if ( arguments.length > 1 ) {
|
|
133
|
+
err = validate( opts, arguments[ 1 ] );
|
|
134
|
+
if ( err ) {
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
arr = arraylike2object( x );
|
|
139
|
+
dt = arr.dtype || '';
|
|
140
|
+
o = {
|
|
141
|
+
'ref': x,
|
|
142
|
+
'dtype': dt,
|
|
143
|
+
'getter': arr.accessors[ 0 ],
|
|
144
|
+
'setter': arr.accessors[ 1 ],
|
|
145
|
+
'preSetElement': setElementWrapper( dt ),
|
|
146
|
+
'postGetArray': getArrayWrapper( array2fancy, opts ),
|
|
147
|
+
'cache': opts.cache,
|
|
148
|
+
'strict': opts.strict,
|
|
149
|
+
'validator': validator( dt ),
|
|
150
|
+
'array2fancy': array2fancy,
|
|
151
|
+
'ctor': new Proxy( x.constructor || Array, {
|
|
152
|
+
'construct': ctor( array2fancy, opts )
|
|
153
|
+
})
|
|
154
|
+
};
|
|
155
|
+
return new Proxy( x, {
|
|
156
|
+
'get': get( o ),
|
|
157
|
+
'set': set( o )
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
// TODO: replace with `@stdlib/console/warn` (or equivalent once available)
|
|
161
|
+
console.warn( 'WARNING: Proxy objects are not supported in the current environment. Some `FancyArray` functionality may not be available.' ); // eslint-disable-line no-console
|
|
162
|
+
return x;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
// EXPORTS //
|
|
168
|
+
|
|
169
|
+
module.exports = factory;
|
package/lib/get.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
|
|
24
|
+
var hasProperty = require( '@stdlib/assert-has-property' );
|
|
25
|
+
var isIntegerString = require( './is_integer_string.js' );
|
|
26
|
+
var isArrayIndexString = require( './is_array_index_string.js' );
|
|
27
|
+
var getElements = require( './get_elements.js' );
|
|
28
|
+
var getElement = require( './get_element.js' );
|
|
29
|
+
var getValue = require( './get_value.js' );
|
|
30
|
+
var getSlice = require( './get_slice.js' );
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// MAIN //
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a trap for retrieving property values.
|
|
37
|
+
*
|
|
38
|
+
* @private
|
|
39
|
+
* @param {Object} ctx - context object
|
|
40
|
+
* @param {Function} ctx.getter - accessor for retrieving array elements
|
|
41
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
42
|
+
* @param {Function} ctx.ctor - proxied array constructor
|
|
43
|
+
* @param {Function} ctx.postGetArray - function to process a retrieved array
|
|
44
|
+
* @param {Object} ctx.cache - cache for resolving array index objects
|
|
45
|
+
* @returns {Function} handler
|
|
46
|
+
*/
|
|
47
|
+
function factory( ctx ) {
|
|
48
|
+
return get;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Trap for retrieving property values.
|
|
52
|
+
*
|
|
53
|
+
* @private
|
|
54
|
+
* @param {Object} target - target object
|
|
55
|
+
* @param {(string|symbol)} property - property name
|
|
56
|
+
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
|
|
57
|
+
* @throws {Error} invalid slice operation
|
|
58
|
+
* @throws {RangeError} slice exceeds array bounds
|
|
59
|
+
* @throws {RangeError} index exceeds array bounds
|
|
60
|
+
* @returns {*} result
|
|
61
|
+
*/
|
|
62
|
+
function get( target, property, receiver ) {
|
|
63
|
+
if ( isIntegerString( property ) ) {
|
|
64
|
+
return getElement( target, property, ctx );
|
|
65
|
+
}
|
|
66
|
+
if ( hasProperty( target, property ) || !isString( property ) ) {
|
|
67
|
+
return getValue( target, property, receiver, ctx );
|
|
68
|
+
}
|
|
69
|
+
if ( isArrayIndexString( property ) ) {
|
|
70
|
+
return getElements( target, property, ctx );
|
|
71
|
+
}
|
|
72
|
+
return getSlice( target, property, ctx );
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
// EXPORTS //
|
|
78
|
+
|
|
79
|
+
module.exports = factory;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MAIN //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns a wrapper function for processing arrays after retrieval.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
* @param {Function} array2fancy - function for creating a proxied array
|
|
28
|
+
* @param {Object} opts - options
|
|
29
|
+
* @param {boolean} opts.strict - boolean indicating whether to perform strict bounds checking
|
|
30
|
+
* @param {Function} opts.cache - cache for resolving array index objects
|
|
31
|
+
* @returns {Function} wrapper function
|
|
32
|
+
*/
|
|
33
|
+
function wrapper( array2fancy, opts ) {
|
|
34
|
+
return wrap;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns a proxied array.
|
|
38
|
+
*
|
|
39
|
+
* @private
|
|
40
|
+
* @param {Array} x - input array
|
|
41
|
+
* @returns {Array} proxied array
|
|
42
|
+
*/
|
|
43
|
+
function wrap( x ) {
|
|
44
|
+
return array2fancy( x, opts );
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// EXPORTS //
|
|
50
|
+
|
|
51
|
+
module.exports = wrapper;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var resolveIndex = require( './resolve_index.js' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns the element associated with a specified index.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {Object} target - target object
|
|
33
|
+
* @param {string} property - index string
|
|
34
|
+
* @param {Object} ctx - context object
|
|
35
|
+
* @param {Function} ctx.getter - accessor for retrieving array elements
|
|
36
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
37
|
+
* @returns {*} result
|
|
38
|
+
*/
|
|
39
|
+
function getElement( target, property, ctx ) {
|
|
40
|
+
return ctx.getter( target, resolveIndex( property, target.length, ctx.strict ) ); // eslint-disable-line max-len
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
// EXPORTS //
|
|
45
|
+
|
|
46
|
+
module.exports = getElement;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var take = require( '@stdlib/array-take' );
|
|
24
|
+
var mskfilter = require( '@stdlib/array-base-mskfilter' );
|
|
25
|
+
var mskreject = require( '@stdlib/array-base-mskreject' );
|
|
26
|
+
var format = require( '@stdlib/string-format' );
|
|
27
|
+
var prop2array = require( './prop2array.js' );
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// MAIN //
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns the elements specified by an array index.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
* @param {Object} target - target object
|
|
37
|
+
* @param {string} property - index string
|
|
38
|
+
* @param {Object} ctx - context object
|
|
39
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
40
|
+
* @param {Object} ctx.cache - cache for resolving array index objects
|
|
41
|
+
* @param {Function} ctx.postGetArray - function to process a retrieved array
|
|
42
|
+
* @throws {Error} invalid array index
|
|
43
|
+
* @throws {RangeError} index exceeds array bounds
|
|
44
|
+
* @returns {Collection} result
|
|
45
|
+
*/
|
|
46
|
+
function getElements( target, property, ctx ) {
|
|
47
|
+
var idx = prop2array( property, ctx.cache );
|
|
48
|
+
if ( idx.type === 'int' ) {
|
|
49
|
+
return ctx.postGetArray( take( target, idx.data ) );
|
|
50
|
+
}
|
|
51
|
+
if ( idx.type === 'bool' ) {
|
|
52
|
+
return ctx.postGetArray( mskfilter( target, idx.data ) );
|
|
53
|
+
}
|
|
54
|
+
if ( idx.type === 'mask' ) {
|
|
55
|
+
return ctx.postGetArray( mskreject( target, idx.data ) );
|
|
56
|
+
}
|
|
57
|
+
throw new Error( format( 'invalid operation. Unrecognized array index type. Value: `%s`.', idx.type ) );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
// EXPORTS //
|
|
62
|
+
|
|
63
|
+
module.exports = getElements;
|
package/lib/get_slice.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var slice = require( '@stdlib/array-base-fancy-slice' );
|
|
24
|
+
var errMessage = require( './error_message.js' );
|
|
25
|
+
var prop2slice = require( './prop2slice.js' );
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// MAIN //
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns a copy.
|
|
32
|
+
*
|
|
33
|
+
* @private
|
|
34
|
+
* @param {Object} target - target object
|
|
35
|
+
* @param {string} property - property name
|
|
36
|
+
* @param {Object} ctx - context object
|
|
37
|
+
* @param {Function} ctx.postGetArray - function to process a retrieved array
|
|
38
|
+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
39
|
+
* @throws {Error} invalid slice operation
|
|
40
|
+
* @throws {RangeError} slice exceeds array bounds
|
|
41
|
+
* @returns {(Collection|void)} result
|
|
42
|
+
*/
|
|
43
|
+
function getSlice( target, property, ctx ) {
|
|
44
|
+
var s = prop2slice( target, property, ctx.strict );
|
|
45
|
+
if ( s === null ) {
|
|
46
|
+
// Ensure consistency with normal array behavior by returning `undefined` for any "unrecognized" property name:
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return ctx.postGetArray( slice( target, s, ctx.strict ) );
|
|
51
|
+
} catch ( err ) {
|
|
52
|
+
// In principle, we should only error when in "strict" mode and a slice exceeds array bounds...
|
|
53
|
+
throw new err.constructor( errMessage( err.message ) );
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// EXPORTS //
|
|
59
|
+
|
|
60
|
+
module.exports = getSlice;
|
package/lib/get_value.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns the property value associated with a specified property.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {Object} target - target object
|
|
33
|
+
* @param {(string|symbol)} property - property
|
|
34
|
+
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
|
|
35
|
+
* @param {Object} ctx - context object
|
|
36
|
+
* @param {Function} ctx.ctor - proxied array constructor
|
|
37
|
+
* @returns {*} result
|
|
38
|
+
*/
|
|
39
|
+
function getValue( target, property, receiver, ctx ) {
|
|
40
|
+
var value = target[ property ];
|
|
41
|
+
if ( isFunction( value ) ) {
|
|
42
|
+
if ( value === target.constructor ) {
|
|
43
|
+
return ctx.ctor;
|
|
44
|
+
}
|
|
45
|
+
return wrapper;
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Method wrapper.
|
|
51
|
+
*
|
|
52
|
+
* @private
|
|
53
|
+
* @returns {*} results
|
|
54
|
+
*/
|
|
55
|
+
function wrapper() {
|
|
56
|
+
var args;
|
|
57
|
+
var i;
|
|
58
|
+
|
|
59
|
+
args = [];
|
|
60
|
+
for ( i = 0; i < arguments.length; i++ ) {
|
|
61
|
+
args.push( arguments[ i ] );
|
|
62
|
+
}
|
|
63
|
+
return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// EXPORTS //
|
|
69
|
+
|
|
70
|
+
module.exports = getValue;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var Proxy = require( '@stdlib/proxy-ctor' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Boolean indicating if an environment has Proxy support.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @name hasSupport
|
|
33
|
+
* @type {boolean}
|
|
34
|
+
*/
|
|
35
|
+
var hasSupport = ( typeof Proxy === 'function' ); // NOTE: cannot use `@stdlib/assert/has-proxy-support` here, as that API uses code evaluation and might violate CSPs; consequently, this is a relatively weak check for proxy support
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// EXPORTS //
|
|
39
|
+
|
|
40
|
+
module.exports = hasSupport;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Convert an array to an object supporting fancy indexing.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/array-to-fancy
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var array2fancy = require( '@stdlib/array-to-fancy' );
|
|
28
|
+
*
|
|
29
|
+
* var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
30
|
+
*
|
|
31
|
+
* var y = array2fancy( x );
|
|
32
|
+
* // returns <Array>
|
|
33
|
+
*
|
|
34
|
+
* var z = y[ '1::2' ];
|
|
35
|
+
* // returns [ 2, 4, 6 ]
|
|
36
|
+
*
|
|
37
|
+
* var len = z.length;
|
|
38
|
+
* // returns 3
|
|
39
|
+
*
|
|
40
|
+
* var v = z[ 0 ];
|
|
41
|
+
* // returns 2
|
|
42
|
+
*
|
|
43
|
+
* v = z[ 1 ];
|
|
44
|
+
* // returns 4
|
|
45
|
+
*
|
|
46
|
+
* v = z[ 2 ];
|
|
47
|
+
* // returns 6
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
// MODULES //
|
|
51
|
+
|
|
52
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
53
|
+
var main = require( './main.js' );
|
|
54
|
+
var factory = require( './factory.js' );
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// MAIN //
|
|
58
|
+
|
|
59
|
+
setReadOnly( main, 'factory', factory );
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
// EXPORTS //
|
|
63
|
+
|
|
64
|
+
module.exports = main;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
|
|
24
|
+
var RE_ARRAY_INDEX = require( './re_array_index.js' );
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// MAIN //
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Tests if an indexing expression is a serialized array index.
|
|
31
|
+
*
|
|
32
|
+
* @private
|
|
33
|
+
* @param {(string|symbol)} prop - property name
|
|
34
|
+
* @returns {boolean} result
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* var out = isArrayIndexString( 'ArrayIndex<0>' );
|
|
38
|
+
* // returns true
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var out = isArrayIndexString( ':' );
|
|
42
|
+
* // returns false
|
|
43
|
+
*/
|
|
44
|
+
function isArrayIndexString( prop ) {
|
|
45
|
+
return ( isString( prop ) && RE_ARRAY_INDEX.test( prop ) );
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// EXPORTS //
|
|
50
|
+
|
|
51
|
+
module.exports = isArrayIndexString;
|