Skip to content

Commit a517d4d

Browse files
committed
Auto-generated commit
1 parent 76de5aa commit a517d4d

File tree

8 files changed

+57
-45
lines changed

8 files changed

+57
-45
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`05cf2e6`](https://github.com/stdlib-js/stdlib/commit/05cf2e6e36c42b9db322e14f5bc78adf4d3901f9) - add support for returning empty tuples
1314
- [`5d9761c`](https://github.com/stdlib-js/stdlib/commit/5d9761cfdea3389f82ca75aa262a9fc3ddf89439) - implement `toLocaleString` method in `dstructs/named-typed-tuple` [(#8009)](https://github.com/stdlib-js/stdlib/pull/8009)
1415
- [`54f310f`](https://github.com/stdlib-js/stdlib/commit/54f310fb122b05038de174d03acbec6d2f84a5a9) - add `dstructs/named-typed-tuple`
1516

@@ -35,6 +36,7 @@ This release closes the following issue:
3536

3637
<details>
3738

39+
- [`05cf2e6`](https://github.com/stdlib-js/stdlib/commit/05cf2e6e36c42b9db322e14f5bc78adf4d3901f9) - **feat:** add support for returning empty tuples _(by Athan Reines)_
3840
- [`9629e2e`](https://github.com/stdlib-js/stdlib/commit/9629e2ee35e901567a51fcd79e2c13156ed46053) - **test:** add tests for `slice` method in `dstructs/named-typed-tuple` [(#8057)](https://github.com/stdlib-js/stdlib/pull/8057) _(by Gaurav Kaushik, Athan Reines)_
3941
- [`d8ad76b`](https://github.com/stdlib-js/stdlib/commit/d8ad76b37fa2b01cd66585f6389f415f61246a63) - **test:** add tests for `toJSON` method in `dstructs/named-typed-tuple` [(#8049)](https://github.com/stdlib-js/stdlib/pull/8049) _(by Gaurav Kaushik, Athan Reines)_
4042
- [`a540e07`](https://github.com/stdlib-js/stdlib/commit/a540e076b9bfeacd4c7e85add92d095f1ca042bf) - **test:** add tests for `forEach` method in `dstructs/named-typed-tuple` [(#8042)](https://github.com/stdlib-js/stdlib/pull/8042) _(by Gaurav Kaushik, Athan Reines)_

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,19 @@ var y = p2.y;
18311831
// returns 0.0
18321832
```
18331833

1834+
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns an empty tuple.
1835+
1836+
```javascript
1837+
var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
1838+
1839+
var p1 = factory( [ 1.0, 0.0, -1.0 ] );
1840+
1841+
var p2 = p1.slice( 10, -1 );
1842+
1843+
var len = p2.length;
1844+
// returns 0
1845+
```
1846+
18341847
<a name="method-some"></a>
18351848

18361849
#### tuple.some( predicate\[, thisArg] )
@@ -2107,15 +2120,17 @@ var y = p2.y;
21072120
// returns 0.0
21082121
```
21092122

2110-
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns `null`.
2123+
If the method is unable to resolve indices to a non-empty tuple subsequence, the method returns an empty tuple.
21112124

21122125
```javascript
21132126
var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
21142127

21152128
var p1 = factory( [ 1.0, 0.0, -1.0 ] );
21162129

21172130
var p2 = p1.subtuple( 10, -1 );
2118-
// returns null
2131+
2132+
var len = p2.length;
2133+
// returns 0
21192134
```
21202135

21212136
<a name="method-to-json"></a>
@@ -2145,7 +2160,7 @@ var factory = namedtypedtuple( [ 'x', 'y', 'z' ] );
21452160
var tuple = factory( [ 1.0, 0.0, -1.0 ], 'int32' );
21462161

21472162
var str = tuple.toLocaleString();
2148-
// returns '1,0,-1'
2163+
// returns 'tuple(x=1, y=0, z=-1)'
21492164
```
21502165

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

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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/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
/**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@stdlib/assert-has-own-property": "^0.2.2",
4646
"@stdlib/assert-is-arraybuffer": "^0.2.2",
4747
"@stdlib/assert-is-collection": "^0.2.2",
48+
"@stdlib/assert-is-empty-array-like-object": "^0.2.2",
4849
"@stdlib/assert-is-function": "^0.2.2",
4950
"@stdlib/assert-is-integer": "^0.2.2",
5051
"@stdlib/assert-is-object": "^0.2.2",

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)