Skip to content

Commit 6ddda90

Browse files
feat: add join method to array/fixed-endian-factory
PR-URL: #3287 Closes: #3147 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent c8143c4 commit 6ddda90

File tree

5 files changed

+387
-0
lines changed

5 files changed

+387
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,35 @@ var str = arr.toString();
727727
// returns '1,2,3'
728728
```
729729

730+
<a name="method-join"></a>
731+
732+
#### TypedArrayFE.prototype.join( \[separator] )
733+
734+
Serializes the array elements into a string, with elements separated by the specified `separator`. If no `separator` is provided, a comma (`,`) is used as the default.
735+
736+
```javascript
737+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
738+
739+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
740+
741+
var str = arr.join();
742+
// returns '1,2,3'
743+
744+
str = arr.join( ' - ' );
745+
// returns '1 - 2 - 3'
746+
```
747+
748+
If the provided `separator` is not a string, it is coerced to a string.
749+
750+
```javascript
751+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
752+
753+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
754+
755+
var str = arr.join( 0 );
756+
// returns '10203'
757+
```
758+
730759
</section>
731760

732761
<!-- /.usage -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var factory = require( './../lib' );
25+
var pkg = require( './../package.json' ).name;
26+
27+
28+
// VARIABLES //
29+
30+
var Float64ArrayFE = factory( 'float64' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+':join', function benchmark( b ) {
36+
var out;
37+
var arr;
38+
var i;
39+
40+
arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = arr.join();
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( typeof out !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var factory = require( './../lib' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// VARIABLES //
31+
32+
var Float64ArrayFE = factory( 'float64' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var arr;
46+
arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var out;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
out = arr.join( '/' );
62+
if ( typeof out !== 'string' ) {
63+
b.fail( 'should return a string' );
64+
}
65+
}
66+
b.toc();
67+
if ( typeof out !== 'string' ) {
68+
b.fail( 'should return a string' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
82+
*/
83+
function main() {
84+
var len;
85+
var min;
86+
var max;
87+
var f;
88+
var i;
89+
90+
min = 1; // 10^min
91+
max = 6; // 10^max
92+
93+
for ( i = min; i <= max; i++ ) {
94+
len = pow( 10, i );
95+
f = createBenchmark( len );
96+
bench( pkg+':join:len='+len, f );
97+
}
98+
}
99+
100+
main();

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

+36
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,42 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
920920
return out.join( ',' );
921921
});
922922

923+
/**
924+
* Serializes the array elements into a string, with elements separated by the specified `separator`.
925+
*
926+
* @private
927+
* @name join
928+
* @memberof TypedArray.prototype
929+
* @type {Function}
930+
* @param {string} [separator=','] - string used to separate consecutive elements
931+
* @throws {TypeError} `this` must be a typed array instance
932+
* @returns {string} joined string
933+
*/
934+
setReadOnly( TypedArray.prototype, 'join', function join( separator ) {
935+
var out;
936+
var buf;
937+
var sep;
938+
var i;
939+
940+
if ( !isTypedArray( this ) ) {
941+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
942+
}
943+
944+
if ( arguments.length > 0 ) {
945+
sep = String( separator );
946+
} else {
947+
sep = ',';
948+
}
949+
950+
out = [];
951+
buf = this._buffer;
952+
for ( i = 0; i < this._length; i++ ) {
953+
out.push( buf[ GETTER ]( i * BYTES_PER_ELEMENT, this._isLE ) );
954+
}
955+
956+
return out.join( sep );
957+
});
958+
923959
return TypedArray;
924960

925961
/**

0 commit comments

Comments
 (0)