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 25e9be2 commit 0f6a792
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 114 deletions.
86 changes: 45 additions & 41 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/index.js.map

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions 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 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 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
Loading

0 comments on commit 0f6a792

Please sign in to comment.