| 
 | 1 | +/**  | 
 | 2 | +* @license Apache-2.0  | 
 | 3 | +*  | 
 | 4 | +* Copyright (c) 2023 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 | +var trim = require( '@stdlib/string/base/trim' );  | 
 | 25 | +var str2multislice = require( '@stdlib/slice/base/str2multislice' );  | 
 | 26 | +var format = require( '@stdlib/string/format' );  | 
 | 27 | +var hasProperty = require( './has_property.js' );  | 
 | 28 | +var options = require( './array_options.js' );  | 
 | 29 | +var RE_INTEGER = require( './re_integer.js' );  | 
 | 30 | + | 
 | 31 | + | 
 | 32 | +// MAIN //  | 
 | 33 | + | 
 | 34 | +/**  | 
 | 35 | +* Trap for retrieving property values.  | 
 | 36 | +*  | 
 | 37 | +* @private  | 
 | 38 | +* @param {Object} target - target object  | 
 | 39 | +* @param {(string|symbol)} property - property name  | 
 | 40 | +* @param {Object} receiver - the proxy object or an object inheriting from the proxy  | 
 | 41 | +* @throws {Error} invalid slice operation  | 
 | 42 | +* @throws {RangeError} number of slice dimensions must match the number of array dimensions  | 
 | 43 | +* @returns {*} result  | 
 | 44 | +*/  | 
 | 45 | +function get( target, property, receiver ) {  | 
 | 46 | +	var dtype;  | 
 | 47 | +	var value;  | 
 | 48 | +	var prop;  | 
 | 49 | +	var ch;  | 
 | 50 | +	var sh;  | 
 | 51 | +	var s;  | 
 | 52 | +	if ( hasProperty( property ) ) {  | 
 | 53 | +		value = target[ property ];  | 
 | 54 | +		if ( isFunction( value ) ) {  | 
 | 55 | +			return wrapper;  | 
 | 56 | +		}  | 
 | 57 | +		return value;  | 
 | 58 | +	}  | 
 | 59 | +	prop = trim( property );  | 
 | 60 | + | 
 | 61 | +	// Resolve target meta data:  | 
 | 62 | +	dtype = target.dtype;  | 
 | 63 | +	sh = target.shape;  | 
 | 64 | + | 
 | 65 | +	// Retrieve the first character in order to to detect how a slice operation was specified:  | 
 | 66 | +	ch = prop[ 0 ];  | 
 | 67 | + | 
 | 68 | +	// Case: multi-slice  | 
 | 69 | +	if ( ch === 'M' ) {  | 
 | 70 | +		// Convert the string to a slice object:  | 
 | 71 | +		s = str2multislice( prop );  | 
 | 72 | +		if ( s === null ) {  | 
 | 73 | +			throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) );  | 
 | 74 | +		}  | 
 | 75 | +		// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, s.data );  | 
 | 76 | + | 
 | 77 | +		// Ensure that we were provided an empty multi-slice:  | 
 | 78 | +		if ( s.ndims !== sh.length ) {  | 
 | 79 | +			throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), s.ndims ) );  | 
 | 80 | +		}  | 
 | 81 | +	}  | 
 | 82 | +	// Case: non-empty string  | 
 | 83 | +	else if ( prop.length !== 0 ) {  | 
 | 84 | +		// TODO: the following can be generalized by going ahead and parsing the slice string or integer and passing to a functional API which then verifies that s.ndims !== sh.length. We need only retain the error raised for an invalid operation.  | 
 | 85 | + | 
 | 86 | +		// Case: slice or an integer  | 
 | 87 | +		if ( ch === 'S' || RE_INTEGER.test( prop ) ) {  | 
 | 88 | +			throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), 1 ) );  | 
 | 89 | +		}  | 
 | 90 | +		throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) );  | 
 | 91 | +	}  | 
 | 92 | +	// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [] );  | 
 | 93 | + | 
 | 94 | +	// Return an zero-dimensional array view:  | 
 | 95 | +	return new receiver.constructor( dtype, target.data, sh, target.strides, target.offset, target.order, options() ); // eslint-disable-line max-len  | 
 | 96 | + | 
 | 97 | +	/**  | 
 | 98 | +	* Method wrapper.  | 
 | 99 | +	*  | 
 | 100 | +	* @private  | 
 | 101 | +	* @returns {*} results  | 
 | 102 | +	*/  | 
 | 103 | +	function wrapper() {  | 
 | 104 | +		var args;  | 
 | 105 | +		var i;  | 
 | 106 | + | 
 | 107 | +		args = [];  | 
 | 108 | +		for ( i = 0; i < arguments.length; i++ ) {  | 
 | 109 | +			args.push( arguments[ i ] );  | 
 | 110 | +		}  | 
 | 111 | +		return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this  | 
 | 112 | +	}  | 
 | 113 | +}  | 
 | 114 | + | 
 | 115 | + | 
 | 116 | +// EXPORTS //  | 
 | 117 | + | 
 | 118 | +module.exports = get;  | 
0 commit comments