Skip to content

Commit a817de9

Browse files
committed
Auto-generated commit
1 parent b5d3c85 commit a817de9

File tree

10 files changed

+341
-69
lines changed

10 files changed

+341
-69
lines changed

to-fancy/lib/ctor.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 trap for constructing new array instances.
25+
*
26+
* @private
27+
* @param {Object} ctx - context object
28+
* @param {Function} ctx.array2fancy - function for creating a proxied array
29+
* @returns {Function} handler
30+
*/
31+
function factory( ctx ) {
32+
return constructor;
33+
34+
/**
35+
* Trap for constructing new array instances.
36+
*
37+
* @private
38+
* @param {Object} target - target object
39+
* @param {Array} args - list of constructor arguments
40+
* @param {Object} newTarget - constructor that was originally called
41+
* @returns {*} new instance
42+
*/
43+
function constructor( target, args ) {
44+
var x;
45+
var a;
46+
47+
a = args;
48+
switch ( a.length ) {
49+
case 0:
50+
x = new target();
51+
break;
52+
case 1:
53+
x = new target( a[0] );
54+
break;
55+
case 2:
56+
x = new target( a[0], a[1] );
57+
break;
58+
case 3:
59+
x = new target( a[0], a[1], a[2] );
60+
break;
61+
case 4:
62+
x = new target( a[0], a[1], a[2], a[3] );
63+
break;
64+
case 5:
65+
x = new target( a[0], a[1], a[2], a[3], a[4] );
66+
break;
67+
case 6:
68+
x = new target( a[0], a[1], a[2], a[3], a[4], a[5] );
69+
break;
70+
case 7:
71+
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6] );
72+
break;
73+
case 8:
74+
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] );
75+
break;
76+
case 9:
77+
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8] ); // eslint-disable-line max-len
78+
break;
79+
case 10:
80+
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ); // eslint-disable-line max-len
81+
break;
82+
default:
83+
// Fallback to using `apply`; however, some constructors may error if the constructor is not callable (i.e., if a constructor always requires `new`):
84+
x = target.apply( null, a );
85+
}
86+
return ctx.array2fancy( x );
87+
}
88+
}
89+
90+
91+
// EXPORTS //
92+
93+
module.exports = factory;

to-fancy/lib/get.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ var getSlice = require( './get_slice.js' );
3535
*
3636
* @private
3737
* @param {Object} ctx - context object
38-
* @param {Function} ctx.array2fancy - function for creating a proxied array
3938
* @param {Function} ctx.getter - accessor for retrieving array elements
4039
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
40+
* @param {Function} ctx.ctor - proxied array constructor
41+
* @param {Function} ctx.postGetSlice - function to process a retrieved slice
4142
* @returns {Function} handler
4243
*/
4344
function factory( ctx ) {
@@ -61,7 +62,7 @@ function factory( ctx ) {
6162
if ( hasProperty( target, property ) || !isString( property ) ) {
6263
return getValue( target, property, receiver, ctx );
6364
}
64-
return getSlice( target, property, receiver, ctx.strict );
65+
return getSlice( target, property, receiver, ctx );
6566
}
6667
}
6768

to-fancy/lib/get_slice.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ var prop2slice = require( './prop2slice.js' );
3434
* @param {Object} target - target object
3535
* @param {string} property - property name
3636
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
37-
* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking
37+
* @param {Object} ctx - context object
38+
* @param {Function} ctx.postGetSlice - function to process a retrieved slice
39+
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
3840
* @throws {Error} invalid slice operation
3941
* @throws {RangeError} slice exceeds array bounds
4042
* @returns {(Collection|void)} result
4143
*/
42-
function getSlice( target, property, receiver, strict ) {
43-
var s = prop2slice( target, property, strict );
44+
function getSlice( target, property, receiver, ctx ) {
45+
var s = prop2slice( target, property, ctx.strict );
4446
if ( s === null ) {
4547
// Ensure consistency with normal array behavior by returning `undefined` for any "unrecognized" property name:
4648
return;
4749
}
4850
try {
49-
return slice( receiver, s, strict );
51+
return ctx.postGetSlice( slice( target, s, ctx.strict ) );
5052
} catch ( err ) {
5153
// In principle, we should only error when in "strict" mode and a slice exceeds array bounds...
5254
throw new err.constructor( errMessage( err.message ) );

to-fancy/lib/get_slice_wrapper.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 slices 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+
* @returns {Function} wrapper function
31+
*/
32+
function wrapper( array2fancy, opts ) {
33+
return wrap;
34+
35+
/**
36+
* Returns a proxied array.
37+
*
38+
* @private
39+
* @param {Array} x - input array
40+
* @returns {Array} proxied array
41+
*/
42+
function wrap( x ) {
43+
return array2fancy( x, opts );
44+
}
45+
}
46+
47+
48+
// EXPORTS //
49+
50+
module.exports = wrapper;

to-fancy/lib/get_value.js

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ var isFunction = require( '@stdlib/assert/is-function' );
3333
* @param {(string|symbol)} property - property
3434
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
3535
* @param {Object} ctx - context object
36-
* @param {Function} ctx.array2fancy - function for creating a proxied array
36+
* @param {Function} ctx.ctor - proxied array constructor
3737
* @returns {*} result
3838
*/
3939
function getValue( target, property, receiver, ctx ) {
4040
var value = target[ property ];
4141
if ( isFunction( value ) ) {
4242
if ( value === target.constructor ) {
43-
return ctor;
43+
return ctx.ctor;
4444
}
4545
return wrapper;
4646
}
@@ -62,62 +62,6 @@ function getValue( target, property, receiver, ctx ) {
6262
}
6363
return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this
6464
}
65-
66-
/**
67-
* Constructor wrapper.
68-
*
69-
* @private
70-
* @returns {*} results
71-
*/
72-
function ctor() {
73-
var x;
74-
var a;
75-
var i;
76-
77-
a = [];
78-
for ( i = 0; i < arguments.length; i++ ) {
79-
a.push( arguments[ i ] );
80-
}
81-
switch ( a.length ) {
82-
case 0:
83-
x = new value();
84-
break;
85-
case 1:
86-
x = new value( a[0] );
87-
break;
88-
case 2:
89-
x = new value( a[0], a[1] );
90-
break;
91-
case 3:
92-
x = new value( a[0], a[1], a[2] );
93-
break;
94-
case 4:
95-
x = new value( a[0], a[1], a[2], a[3] );
96-
break;
97-
case 5:
98-
x = new value( a[0], a[1], a[2], a[3], a[4] );
99-
break;
100-
case 6:
101-
x = new value( a[0], a[1], a[2], a[3], a[4], a[5] );
102-
break;
103-
case 7:
104-
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6] );
105-
break;
106-
case 8:
107-
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] );
108-
break;
109-
case 9:
110-
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8] ); // eslint-disable-line max-len
111-
break;
112-
case 10:
113-
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ); // eslint-disable-line max-len
114-
break;
115-
default:
116-
// Fallback to using `apply`; however, some constructors may error if the constructor is not callable (i.e., always requires `new`):
117-
x = value.apply( null, a );
118-
}
119-
return ctx.array2fancy( x );
120-
}
12165
}
12266

12367

to-fancy/lib/main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ var Proxy = require( '@stdlib/proxy/ctor' );
2525
var arraylike2object = require( './../../base/arraylike2object' );
2626
var format = require( '@stdlib/string/format' );
2727
var setElementWrapper = require( './set_element_wrapper.js' );
28+
var getSliceWrapper = require( './get_slice_wrapper.js' );
2829
var hasProxySupport = require( './has_proxy_support.js' );
2930
var defaults = require( './defaults.js' );
3031
var validate = require( './validate.js' );
3132
var validator = require( './validator.js' );
33+
var ctor = require( './ctor.js' );
3234
var get = require( './get.js' );
3335
var set = require( './set.js' );
3436

@@ -91,11 +93,16 @@ function array2fancy( x ) {
9193
'dtype': dt,
9294
'getter': arr.accessors[ 0 ],
9395
'setter': arr.accessors[ 1 ],
94-
'valueWrapper': setElementWrapper( dt ),
96+
'preSetElement': setElementWrapper( dt ),
97+
'postGetSlice': getSliceWrapper( array2fancy, opts ),
9598
'strict': opts.strict,
9699
'validator': validator( dt ),
97-
'array2fancy': array2fancy
100+
'array2fancy': array2fancy,
101+
'ctor': null
98102
};
103+
o.ctor = new Proxy( x.constructor || Array, {
104+
'construct': ctor( o )
105+
});
99106
return new Proxy( x, {
100107
'get': get( o ),
101108
'set': set( o )

to-fancy/lib/set.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ var setSlice = require( './set_slice.js' );
3838
* @param {Function} ctx.setter - accessor for setting array elements
3939
* @param {string} ctx.dtype - array data type
4040
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
41+
* @param {Function} ctx.validator - function for validating new values
42+
* @param {Function} ctx.setter - accessor for setting array elements
43+
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
4144
* @returns {Function} handler
4245
*/
4346
function factory( ctx ) {

to-fancy/lib/set_element.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var resolveIndex = require( './resolve_index.js' );
3737
* @param {string} ctx.dtype - target array data type
3838
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
3939
* @param {Function} ctx.validator - function for validating new values
40-
* @param {(Function|null)} ctx.valueWrapper - function for normalizing new values (if necessary)
40+
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
4141
* @throws {TypeError} assigned value cannot be safely cast to the target array data type
4242
* @throws {TypeError} target array must have a supported data type
4343
* @returns {boolean} boolean indicating whether assignment succeeded
@@ -50,8 +50,8 @@ function setElement( target, property, value, ctx ) {
5050
if ( err ) {
5151
throw err;
5252
}
53-
if ( ctx.valueWrapper ) {
54-
v = ctx.valueWrapper( value );
53+
if ( ctx.preSetElement ) {
54+
v = ctx.preSetElement( value );
5555
} else {
5656
v = value;
5757
}

0 commit comments

Comments
 (0)