-
-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for using index arrays in indexing expressions
- Loading branch information
Showing
12 changed files
with
277 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lib/node_modules/@stdlib/array/to-fancy/lib/get_elements.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @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'; | ||
|
||
// MODULES // | ||
|
||
var take = require( '@stdlib/array/take' ); | ||
var mskfilter = require( '@stdlib/array/base/mskfilter' ); | ||
var mskreject = require( '@stdlib/array/base/mskreject' ); | ||
var format = require( '@stdlib/string/format' ); | ||
var prop2array = require( './prop2array.js' ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Returns the elements specified by an array index. | ||
* | ||
* @private | ||
* @param {Object} target - target object | ||
* @param {string} property - index string | ||
* @param {Object} ctx - context object | ||
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking | ||
* @param {Object} ctx.cache - cache for resolving array index objects | ||
* @param {Function} ctx.postGetArray - function to process a retrieved array | ||
* @throws {Error} invalid array index | ||
* @throws {RangeError} index exceeds array bounds | ||
* @returns {Collection} result | ||
*/ | ||
function getElements( target, property, ctx ) { | ||
var idx = prop2array( property, ctx.cache ); | ||
if ( idx.type === 'int' ) { | ||
return ctx.postGetArray( take( target, idx.data ) ); | ||
} | ||
if ( idx.type === 'bool' ) { | ||
return ctx.postGetArray( mskfilter( target, idx.data ) ); | ||
} | ||
if ( idx.type === 'mask' ) { | ||
return ctx.postGetArray( mskreject( target, idx.data ) ); | ||
} | ||
throw new Error( format( 'invalid operation. Unrecognized array index type. Value: `%s`.', idx.type ) ); | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = getElements; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/array/to-fancy/lib/is_array_index_string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @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'; | ||
|
||
// MODULES // | ||
|
||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var RE_ARRAY_INDEX = require( './re_array_index.js' ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Tests if an indexing expression is a serialized array index. | ||
* | ||
* @private | ||
* @param {(string|symbol)} prop - property name | ||
* @returns {boolean} result | ||
* | ||
* @example | ||
* var out = isArrayIndexString( 'ArrayIndex<0>' ); | ||
* // returns true | ||
* | ||
* @example | ||
* var out = isArrayIndexString( ':' ); | ||
* // returns false | ||
*/ | ||
function isArrayIndexString( prop ) { | ||
return ( isString( prop ) && RE_ARRAY_INDEX.test( prop ) ); | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = isArrayIndexString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @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'; | ||
|
||
// MODULES // | ||
|
||
var trim = require( '@stdlib/string/base/trim' ); | ||
var format = require( '@stdlib/string/format' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Extracts an array index identifier from an array index indexing expression. | ||
* | ||
* @private | ||
* @param {string} str - input string | ||
* @returns {string} identifier | ||
* | ||
* @example | ||
* var str = 'ArrayIndex<0>'; | ||
* | ||
* var id = getIdentifier( str ); | ||
* // returns '0' | ||
*/ | ||
function getIdentifier( str ) { | ||
return str.substring( 11, str.length-1 ); // ArrayIndex<XX> => XX | ||
} | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Converts an indexing expression to an array index. | ||
* | ||
* @private | ||
* @param {string} property - property name | ||
* @param {Object} cache - cache for resolving array index objects | ||
* @throws {Error} invalid array index | ||
* @returns {(Object|null)} index object (or null) | ||
*/ | ||
function prop2array( property, cache ) { | ||
var o = cache.get( getIdentifier( trim( property ) ) ); | ||
if ( o === null ) { | ||
throw new Error( format( 'invalid operation. Unable to resolve array index. Value: `%s`.', property ) ); | ||
} | ||
return o; | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = prop2array; |
47 changes: 47 additions & 0 deletions
47
lib/node_modules/@stdlib/array/to-fancy/lib/re_array_index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @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 // | ||
|
||
/** | ||
* Regular expression for testing whether a string is a serialized array index. | ||
* | ||
* @private | ||
* @name RE_ARRAY_INDEX | ||
* @type {RegExp} | ||
* | ||
* @example | ||
* var bool = RE_ARRAY_INDEX.test( 'ArrayIndex<0>' ); | ||
* // returns true | ||
* | ||
* @example | ||
* var bool = RE_ARRAY_INDEX.test( '0' ); | ||
* // returns false | ||
* | ||
* @example | ||
* var bool = RE_ARRAY_INDEX.test( 'Slice(0,10,2)' ); | ||
* // returns false | ||
*/ | ||
var RE_ARRAY_INDEX = /\s*ArrayIndex<[^>]+>\s*/; | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = RE_ARRAY_INDEX; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
993f468
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report
The above coverage report was generated for the changes in this push.