Skip to content

Commit cd45405

Browse files
committed
feat: add method to resolve a field type
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent eac188f commit cd45405

File tree

2 files changed

+89
-23
lines changed

2 files changed

+89
-23
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 indexOf = require( '@stdlib/array/base/index-of' );
24+
var join = require( '@stdlib/array/base/join' );
25+
var format = require( '@stdlib/string/format' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Returns the index of a specified field name in a provided list of field names.
32+
*
33+
* @private
34+
* @param {Array<string>} names - list of field names
35+
* @param {string} name - field name
36+
* @throws {Error} struct must have at least one field
37+
* @throws {TypeError} must provide a recognized field name
38+
* @returns {(integer|Error)} index or an error object
39+
*/
40+
function fieldIndex( names, name ) {
41+
var idx;
42+
if ( names.length === 0 ) {
43+
return new Error( 'invalid operation. struct does not have any fields.' );
44+
}
45+
idx = indexOf( names, name, 0 );
46+
if ( idx < 0 ) {
47+
return new TypeError( format( 'invalid argument. Field name must be one of the following: "%s". Value: `%s`.', join( names, ', ' ), name ) );
48+
}
49+
return idx;
50+
}
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = fieldIndex;

lib/node_modules/@stdlib/dstructs/struct/lib/main.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
3030
var isObject = require( '@stdlib/assert/is-object' );
3131
var hasProp = require( '@stdlib/assert/has-property' );
3232
var min = require( '@stdlib/math/base/special/fast/min' );
33-
var join = require( '@stdlib/array/base/join' );
3433
var filled = require( '@stdlib/array/base/filled' );
35-
var indexOf = require( '@stdlib/array/base/index-of' );
3634
var ArrayBuffer = require( '@stdlib/array/buffer' );
3735
var DataView = require( '@stdlib/array/dataview' );
3836
var format = require( '@stdlib/string/format' );
@@ -42,6 +40,7 @@ var createPrototypeAccessors = require( './create_prototype_accessors.js' );
4240
var isStruct = require( './is_struct.js' );
4341
var normalize = require( './normalize_field_list.js' );
4442
var fieldNames = require( './field_names.js' );
43+
var fieldIndex = require( './field_index.js' );
4544
var resolveAlignment = require( './resolve_alignment.js' );
4645
var byteOffsets = require( './byte_offsets.js' );
4746
var partitions = require( './partitions.js' );
@@ -329,17 +328,14 @@ function factory( fields ) {
329328
* @readonly
330329
* @type {Function}
331330
* @param {string} name - field name
331+
* @throws {Error} struct must have at least one field
332332
* @throws {TypeError} must provide a recognized field name
333333
* @returns {NonNegativeInteger} byte length
334334
*/
335335
setReadOnly( Struct, 'byteLengthOf', function byteLengthOf( name ) {
336-
var idx;
337-
if ( FIELD_NAMES.length === 0 ) {
338-
throw new Error( 'invalid operation. struct does not have any fields.' );
339-
}
340-
idx = indexOf( FIELD_NAMES, name, 0 );
341-
if ( idx < 0 ) {
342-
throw new TypeError( format( 'invalid argument. First argument must be one of the following: "%s". Value: `%s`.', join( FIELD_NAMES, ', ' ), name ) );
336+
var idx = fieldIndex( FIELD_NAMES, name );
337+
if ( idx instanceof Error ) {
338+
throw idx;
343339
}
344340
return FIELDS[ idx ].byteLength;
345341
});
@@ -353,17 +349,14 @@ function factory( fields ) {
353349
* @readonly
354350
* @type {Function}
355351
* @param {string} name - field name
352+
* @throws {Error} struct must have at least one field
356353
* @throws {TypeError} must provide a recognized field name
357354
* @returns {NonNegativeInteger} byte offset
358355
*/
359356
setReadOnly( Struct, 'byteOffsetOf', function byteOffsetOf( name ) {
360-
var idx;
361-
if ( FIELD_NAMES.length === 0 ) {
362-
throw new Error( 'invalid operation. struct does not have any fields.' );
363-
}
364-
idx = indexOf( FIELD_NAMES, name, 0 );
365-
if ( idx < 0 ) {
366-
throw new TypeError( format( 'invalid argument. First argument must be one of the following: "%s". Value: `%s`.', join( FIELD_NAMES, ', ' ), name ) );
357+
var idx = fieldIndex( FIELD_NAMES, name );
358+
if ( idx instanceof Error ) {
359+
throw idx;
367360
}
368361
return FIELDS[ idx ].byteOffset;
369362
});
@@ -377,21 +370,39 @@ function factory( fields ) {
377370
* @readonly
378371
* @type {Function}
379372
* @param {string} name - field name
373+
* @throws {Error} struct must have at least one field
380374
* @throws {TypeError} must provide a recognized field name
381375
* @returns {string} description
382376
*/
383377
setReadOnly( Struct, 'descriptionOf', function descriptionOf( name ) {
384-
var idx;
385-
if ( FIELD_NAMES.length === 0 ) {
386-
throw new Error( 'invalid operation. struct does not have any fields.' );
387-
}
388-
idx = indexOf( FIELD_NAMES, name, 0 );
389-
if ( idx < 0 ) {
390-
throw new TypeError( format( 'invalid argument. First argument must be one of the following: "%s". Value: `%s`.', join( FIELD_NAMES, ', ' ), name ) );
378+
var idx = fieldIndex( FIELD_NAMES, name );
379+
if ( idx instanceof Error ) {
380+
throw idx;
391381
}
392382
return FIELDS[ idx ].description;
393383
});
394384

385+
/**
386+
* Returns the type associated with a provided field name.
387+
*
388+
* @private
389+
* @name typeOf
390+
* @memberof Struct
391+
* @readonly
392+
* @type {Function}
393+
* @param {string} name - field name
394+
* @throws {Error} struct must have at least one field
395+
* @throws {TypeError} must provide a recognized field name
396+
* @returns {(string|Object)} type
397+
*/
398+
setReadOnly( Struct, 'typeOf', function typeOf( name ) {
399+
var idx = fieldIndex( FIELD_NAMES, name );
400+
if ( idx instanceof Error ) {
401+
throw idx;
402+
}
403+
return FIELDS[ idx ].type;
404+
});
405+
395406
/**
396407
* Returns the underlying byte buffer of a `struct`.
397408
*

0 commit comments

Comments
 (0)