Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Feb 10, 2024
1 parent b5d3c85 commit a817de9
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 69 deletions.
93 changes: 93 additions & 0 deletions to-fancy/lib/ctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @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';

// MAIN //

/**
* Returns a trap for constructing new array instances.
*
* @private
* @param {Object} ctx - context object
* @param {Function} ctx.array2fancy - function for creating a proxied array
* @returns {Function} handler
*/
function factory( ctx ) {
return constructor;

/**
* Trap for constructing new array instances.
*
* @private
* @param {Object} target - target object
* @param {Array} args - list of constructor arguments
* @param {Object} newTarget - constructor that was originally called
* @returns {*} new instance
*/
function constructor( target, args ) {
var x;
var a;

a = args;
switch ( a.length ) {
case 0:
x = new target();
break;
case 1:
x = new target( a[0] );
break;
case 2:
x = new target( a[0], a[1] );
break;
case 3:
x = new target( a[0], a[1], a[2] );
break;
case 4:
x = new target( a[0], a[1], a[2], a[3] );
break;
case 5:
x = new target( a[0], a[1], a[2], a[3], a[4] );
break;
case 6:
x = new target( a[0], a[1], a[2], a[3], a[4], a[5] );
break;
case 7:
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6] );
break;
case 8:
x = new target( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] );
break;
case 9:
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
break;
case 10:
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
break;
default:
// Fallback to using `apply`; however, some constructors may error if the constructor is not callable (i.e., if a constructor always requires `new`):
x = target.apply( null, a );
}
return ctx.array2fancy( x );
}
}


// EXPORTS //

module.exports = factory;
5 changes: 3 additions & 2 deletions to-fancy/lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ var getSlice = require( './get_slice.js' );
*
* @private
* @param {Object} ctx - context object
* @param {Function} ctx.array2fancy - function for creating a proxied array
* @param {Function} ctx.getter - accessor for retrieving array elements
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
* @param {Function} ctx.ctor - proxied array constructor
* @param {Function} ctx.postGetSlice - function to process a retrieved slice
* @returns {Function} handler
*/
function factory( ctx ) {
Expand All @@ -61,7 +62,7 @@ function factory( ctx ) {
if ( hasProperty( target, property ) || !isString( property ) ) {
return getValue( target, property, receiver, ctx );
}
return getSlice( target, property, receiver, ctx.strict );
return getSlice( target, property, receiver, ctx );
}
}

Expand Down
10 changes: 6 additions & 4 deletions to-fancy/lib/get_slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ var prop2slice = require( './prop2slice.js' );
* @param {Object} target - target object
* @param {string} property - property name
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking
* @param {Object} ctx - context object
* @param {Function} ctx.postGetSlice - function to process a retrieved slice
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
* @throws {Error} invalid slice operation
* @throws {RangeError} slice exceeds array bounds
* @returns {(Collection|void)} result
*/
function getSlice( target, property, receiver, strict ) {
var s = prop2slice( target, property, strict );
function getSlice( target, property, receiver, ctx ) {
var s = prop2slice( target, property, ctx.strict );
if ( s === null ) {
// Ensure consistency with normal array behavior by returning `undefined` for any "unrecognized" property name:
return;
}
try {
return slice( receiver, s, strict );
return ctx.postGetSlice( slice( target, s, ctx.strict ) );
} catch ( err ) {
// In principle, we should only error when in "strict" mode and a slice exceeds array bounds...
throw new err.constructor( errMessage( err.message ) );
Expand Down
50 changes: 50 additions & 0 deletions to-fancy/lib/get_slice_wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @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';

// MAIN //

/**
* Returns a wrapper function for processing slices after retrieval.
*
* @private
* @param {Function} array2fancy - function for creating a proxied array
* @param {Object} opts - options
* @param {boolean} opts.strict - boolean indicating whether to perform strict bounds checking
* @returns {Function} wrapper function
*/
function wrapper( array2fancy, opts ) {
return wrap;

/**
* Returns a proxied array.
*
* @private
* @param {Array} x - input array
* @returns {Array} proxied array
*/
function wrap( x ) {
return array2fancy( x, opts );
}
}


// EXPORTS //

module.exports = wrapper;
60 changes: 2 additions & 58 deletions to-fancy/lib/get_value.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ var isFunction = require( '@stdlib/assert/is-function' );
* @param {(string|symbol)} property - property
* @param {Object} receiver - the proxy object or an object inheriting from the proxy
* @param {Object} ctx - context object
* @param {Function} ctx.array2fancy - function for creating a proxied array
* @param {Function} ctx.ctor - proxied array constructor
* @returns {*} result
*/
function getValue( target, property, receiver, ctx ) {
var value = target[ property ];
if ( isFunction( value ) ) {
if ( value === target.constructor ) {
return ctor;
return ctx.ctor;
}
return wrapper;
}
Expand All @@ -62,62 +62,6 @@ function getValue( target, property, receiver, ctx ) {
}
return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this
}

/**
* Constructor wrapper.
*
* @private
* @returns {*} results
*/
function ctor() {
var x;
var a;
var i;

a = [];
for ( i = 0; i < arguments.length; i++ ) {
a.push( arguments[ i ] );
}
switch ( a.length ) {
case 0:
x = new value();
break;
case 1:
x = new value( a[0] );
break;
case 2:
x = new value( a[0], a[1] );
break;
case 3:
x = new value( a[0], a[1], a[2] );
break;
case 4:
x = new value( a[0], a[1], a[2], a[3] );
break;
case 5:
x = new value( a[0], a[1], a[2], a[3], a[4] );
break;
case 6:
x = new value( a[0], a[1], a[2], a[3], a[4], a[5] );
break;
case 7:
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6] );
break;
case 8:
x = new value( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] );
break;
case 9:
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
break;
case 10:
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
break;
default:
// Fallback to using `apply`; however, some constructors may error if the constructor is not callable (i.e., always requires `new`):
x = value.apply( null, a );
}
return ctx.array2fancy( x );
}
}


Expand Down
11 changes: 9 additions & 2 deletions to-fancy/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ var Proxy = require( '@stdlib/proxy/ctor' );
var arraylike2object = require( './../../base/arraylike2object' );
var format = require( '@stdlib/string/format' );
var setElementWrapper = require( './set_element_wrapper.js' );
var getSliceWrapper = require( './get_slice_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' );

Expand Down Expand Up @@ -91,11 +93,16 @@ function array2fancy( x ) {
'dtype': dt,
'getter': arr.accessors[ 0 ],
'setter': arr.accessors[ 1 ],
'valueWrapper': setElementWrapper( dt ),
'preSetElement': setElementWrapper( dt ),
'postGetSlice': getSliceWrapper( array2fancy, opts ),
'strict': opts.strict,
'validator': validator( dt ),
'array2fancy': array2fancy
'array2fancy': array2fancy,
'ctor': null
};
o.ctor = new Proxy( x.constructor || Array, {
'construct': ctor( o )
});
return new Proxy( x, {
'get': get( o ),
'set': set( o )
Expand Down
3 changes: 3 additions & 0 deletions to-fancy/lib/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var setSlice = require( './set_slice.js' );
* @param {Function} ctx.setter - accessor for setting array elements
* @param {string} ctx.dtype - array data type
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
* @param {Function} ctx.validator - function for validating new values
* @param {Function} ctx.setter - accessor for setting array elements
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
* @returns {Function} handler
*/
function factory( ctx ) {
Expand Down
6 changes: 3 additions & 3 deletions to-fancy/lib/set_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var resolveIndex = require( './resolve_index.js' );
* @param {string} ctx.dtype - target array data type
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
* @param {Function} ctx.validator - function for validating new values
* @param {(Function|null)} ctx.valueWrapper - function for normalizing new values (if necessary)
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
* @throws {TypeError} assigned value cannot be safely cast to the target array data type
* @throws {TypeError} target array must have a supported data type
* @returns {boolean} boolean indicating whether assignment succeeded
Expand All @@ -50,8 +50,8 @@ function setElement( target, property, value, ctx ) {
if ( err ) {
throw err;
}
if ( ctx.valueWrapper ) {
v = ctx.valueWrapper( value );
if ( ctx.preSetElement ) {
v = ctx.preSetElement( value );
} else {
v = value;
}
Expand Down
Loading

0 comments on commit a817de9

Please sign in to comment.