Skip to content

Commit 05cf2e6

Browse files
committed
feat: add support for returning empty tuples
--- 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: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - 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 9629e2e commit 05cf2e6

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

lib/node_modules/@stdlib/dstructs/named-typed-tuple/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,19 @@ var y = p2.y;
17981798
// returns 0.0
17991799
```
18001800

1801+
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns an empty tuple.
1802+
1803+
```javascript
1804+
var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
1805+
1806+
var p1 = factory( [ 1.0, 0.0, -1.0 ] );
1807+
1808+
var p2 = p1.slice( 10, -1 );
1809+
1810+
var len = p2.length;
1811+
// returns 0
1812+
```
1813+
18011814
<a name="method-some"></a>
18021815

18031816
#### tuple.some( predicate\[, thisArg] )
@@ -2074,15 +2087,17 @@ var y = p2.y;
20742087
// returns 0.0
20752088
```
20762089

2077-
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns `null`.
2090+
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns an empty tuple.
20782091

20792092
```javascript
20802093
var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
20812094

20822095
var p1 = factory( [ 1.0, 0.0, -1.0 ] );
20832096

20842097
var p2 = p1.subtuple( 10, -1 );
2085-
// returns null
2098+
2099+
var len = p2.length;
2100+
// returns 0
20862101
```
20872102

20882103
<a name="method-to-json"></a>
@@ -2112,7 +2127,7 @@ var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
21122127
var tuple = factory( [ 1.0, 0.0, -1.0 ], 'int32' );
21132128

21142129
var str = tuple.toLocaleString();
2115-
// returns '1,0,-1'
2130+
// returns 'tuple(x=1, y=0, z=-1)'
21162131
```
21172132

21182133
<a name="method-to-string"></a>

lib/node_modules/@stdlib/dstructs/named-typed-tuple/docs/repl.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ tuple.slice( [begin[, end]] )
11441144
the host tuple.
11451145

11461146
If the method is unable to resolve indices to a non-empty tuple subsequence,
1147-
the method returns `null`.
1147+
the method returns an empty typed array.
11481148

11491149
Parameters
11501150
----------
@@ -1303,8 +1303,8 @@ tuple.subtuple( [begin[, end]] )
13031303
Creates a new named typed tuple over the same underlying ArrayBuffer and
13041304
with the same underlying data type as the host tuple.
13051305

1306-
If the function is unable to resolve indices to a non-empty tuple
1307-
subsequence, the function returns `null`.
1306+
If the method is unable to resolve indices to a non-empty tuple subsequence,
1307+
the method returns an empty typed array.
13081308

13091309
Parameters
13101310
----------
@@ -1318,7 +1318,7 @@ tuple.subtuple( [begin[, end]] )
13181318

13191319
Returns
13201320
-------
1321-
tuple: TypedArray|null
1321+
tuple: TypedArray
13221322
A new named typed tuple.
13231323

13241324
Examples

lib/node_modules/@stdlib/dstructs/named-typed-tuple/lib/main.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// MODULES //
2424

2525
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
26+
var isEmptyArrayLikeObject = require( '@stdlib/assert/is-empty-array-like-object' );
2627
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2728
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2829
var isFunction = require( '@stdlib/assert/is-function' );
@@ -95,7 +96,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
9596
var opts;
9697
var err;
9798
var i;
98-
if ( !isStringArray( names ) ) {
99+
if ( !isStringArray( names ) && !isEmptyArrayLikeObject( names ) ) {
99100
throw new TypeError( format( 'invalid argument. Must provide an array of strings. Value: `%s`.', names ) );
100101
}
101102
if ( !hasDistinctElements( names ) ) {
@@ -251,17 +252,6 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
251252
}
252253
}
253254

254-
/**
255-
* Returns the list of tuple fields.
256-
*
257-
* @private
258-
* @memberof tuple
259-
* @returns {StringArray} tuple fields
260-
*/
261-
function getFields() {
262-
return fields.slice();
263-
}
264-
265255
/**
266256
* Returns the list of tuple fields in index order.
267257
*
@@ -943,18 +933,14 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
943933
/**
944934
* Copies elements to a new tuple with the same underlying data type as the host tuple.
945935
*
946-
* ## Notes
947-
*
948-
* - If the function is unable to resolve indices to a non-empty tuple subsequence, the function returns `null`.
949-
*
950936
* @private
951937
* @memberof tuple
952938
* @param {integer} [begin=0] - start element index (inclusive)
953939
* @param {integer} [end=tuple.length] - end element index (exclusive)
954940
* @throws {TypeError} `this` must be the host tuple
955941
* @throws {TypeError} first argument must be an integer
956942
* @throws {TypeError} second argument must be an integer
957-
* @returns {(TypedArray|null)} new tuple
943+
* @returns {TypedArray} new tuple
958944
*/
959945
function slice( begin, end ) {
960946
var tmp;
@@ -994,9 +980,6 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
994980
j = nfields;
995981
}
996982
}
997-
if ( i >= j ) {
998-
return null;
999-
}
1000983
f = [];
1001984
tmp = [];
1002985
for ( ; i < j; i++ ) {
@@ -1117,18 +1100,14 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
11171100
/**
11181101
* Creates a new tuple over the same underlying `ArrayBuffer` and with the same underlying data type as the host tuple.
11191102
*
1120-
* ## Notes
1121-
*
1122-
* - If the function is unable to resolve indices to a non-empty tuple subsequence, the function returns `null`.
1123-
*
11241103
* @private
11251104
* @memberof tuple
11261105
* @param {integer} [begin=0] - start element index (inclusive)
11271106
* @param {integer} [end=tuple.length] - end element index (exclusive)
11281107
* @throws {TypeError} `this` must be the host tuple
11291108
* @throws {TypeError} first argument must be an integer
11301109
* @throws {TypeError} second argument must be an integer
1131-
* @returns {(TypedArray|null)} new tuple
1110+
* @returns {TypedArray} new tuple
11321111
*/
11331112
function subtuple( begin, end ) {
11341113
var f;
@@ -1168,8 +1147,8 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
11681147
j = nfields;
11691148
}
11701149
}
1171-
if ( i >= j ) {
1172-
return null;
1150+
if ( j <= i ) {
1151+
return factory( [], opts )( tuple.buffer, tuple.byteOffset, dtype );
11731152
}
11741153
f = [];
11751154
for ( k = i; k < j; k++ ) {
@@ -1275,6 +1254,17 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
12751254
}
12761255
}
12771256

1257+
/**
1258+
* Returns the list of tuple fields.
1259+
*
1260+
* @private
1261+
* @memberof tuple
1262+
* @returns {StringArray} tuple fields
1263+
*/
1264+
function getFields() {
1265+
return fields.slice();
1266+
}
1267+
12781268
// Note: keep the following methods in alphabetical order...
12791269

12801270
/**

lib/node_modules/@stdlib/dstructs/named-typed-tuple/test/test.slice.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ tape( 'the method supports negative indices', function test( t ) {
339339
t.end();
340340
});
341341

342-
tape( 'the method returns null if a resolved beginning index exceeds a resolved ending index', function test( t ) {
342+
tape( 'the method returns an empty tuple if a resolved beginning index exceeds a resolved ending index', function test( t ) {
343343
var actual;
344344
var Point;
345345
var p;
@@ -348,11 +348,12 @@ tape( 'the method returns null if a resolved beginning index exceeds a resolved
348348
p = new Point( [ 1, 2, 3, 4 ] );
349349
actual = p.slice( 2, 0 );
350350

351-
t.strictEqual( actual, null, 'returns expected value' );
351+
t.strictEqual( actual.length, 0, 'returns expected value' );
352+
t.deepEqual( actual.fields, [], 'returns expected value' );
352353
t.end();
353354
});
354355

355-
tape( 'the method returns null if a resolved beginning index exceeds the maximum tuple index', function test( t ) {
356+
tape( 'the method returns an empty tuple if a resolved beginning index exceeds the maximum tuple index', function test( t ) {
356357
var actual;
357358
var Point;
358359
var p;
@@ -361,11 +362,12 @@ tape( 'the method returns null if a resolved beginning index exceeds the maximum
361362
p = new Point( [ 1, 2, 3 ] );
362363
actual = p.slice( 5 );
363364

364-
t.strictEqual( actual, null, 'returns expected value' );
365+
t.strictEqual( actual.length, 0, 'returns expected value' );
366+
t.deepEqual( actual.fields, [], 'returns expected value' );
365367
t.end();
366368
});
367369

368-
tape( 'the method returns null if a resolved ending index is less than or equal to zero', function test( t ) {
370+
tape( 'the method returns an empty tuple if a resolved ending index is less than or equal to zero', function test( t ) {
369371
var actual;
370372
var Point;
371373
var p;
@@ -374,9 +376,11 @@ tape( 'the method returns null if a resolved ending index is less than or equal
374376
p = new Point( [ 1, 2, 3, 4 ] );
375377

376378
actual = p.slice( 2, -8 );
377-
t.strictEqual( actual, null, 'returns expected value' );
379+
t.strictEqual( actual.length, 0, 'returns expected value' );
380+
t.deepEqual( actual.fields, [], 'returns expected value' );
378381

379382
actual = p.slice( 1, 0 );
380-
t.strictEqual( actual, null, 'returns expected value' );
383+
t.strictEqual( actual.length, 0, 'returns expected value' );
384+
t.deepEqual( actual.fields, [], 'returns expected value' );
381385
t.end();
382386
});

0 commit comments

Comments
 (0)