Skip to content

Commit 3d60550

Browse files
committed
feat(array/base): add join function
In progress on issue stdlib-js#1327.
1 parent 615bcf1 commit 3d60550

File tree

10 files changed

+722
-0
lines changed

10 files changed

+722
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# join
22+
23+
> Returns a string created by joining array elements using a specified separator.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var join = require( '@stdlib/array/base/join' );
41+
```
42+
43+
#### join( x, separator )
44+
45+
Returns a string which is the concatenation of all the elements in an array with a given separator
46+
47+
```javascript
48+
var x = [ 1, 2, 3, 4, 5, 6 ];
49+
50+
var out = join( x, ',' );
51+
// returns '1,2,3,4,5,6'
52+
```
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- If provided an array-like object having a `join` method, the function defers execution to that method and assumes that the method API has the following signature:
65+
66+
```text
67+
x.join( separator )
68+
```
69+
70+
- If provided an array with null and undefined elements present in it, it will treat them as an empty string
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var join = require( '@stdlib/array/base/join' );
86+
87+
var x = [ 0, 1, 2, 3, 4, 5 ];
88+
89+
var s = join( x, ',' );
90+
// returns '0,1,2,3,4,5'
91+
92+
s = join( x, '-' );
93+
// returns '0-1-2-3-4-5'
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="references">
103+
104+
</section>
105+
106+
<!-- /.references -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
</section>
121+
122+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 isString = require( '@stdlib/assert/is-string' );
26+
var ones = require( '@stdlib/array/base/ones' );
27+
var pkg = require( './../package.json' ).name;
28+
var join = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = ones( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = join( x, ',' );
57+
if ( !isString( out ) ) {
58+
b.fail( 'should return a string' );
59+
}
60+
}
61+
b.toc();
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
}
65+
}
66+
67+
68+
// MAIN //
69+
70+
/**
71+
* Main execution sequence.
72+
*
73+
* @private
74+
*/
75+
function main() {
76+
var len;
77+
var min;
78+
var max;
79+
var f;
80+
var i;
81+
82+
min = 1; // 10^min
83+
max = 6; // 10^max
84+
85+
for ( i = min; i <= max; i++ ) {
86+
len = pow( 10, i );
87+
88+
f = createBenchmark( len );
89+
bench( pkg+':dtype=generic,len='+len, f );
90+
}
91+
}
92+
93+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( x, separator )
3+
Returns a string which is the concatenation of all the elements in
4+
an array with a given separator
5+
6+
If provided an array-like object having a `join` method, the function
7+
defers execution to that method and assumes that the method has the
8+
following signature:
9+
10+
x.join( separator )
11+
12+
If provided an array-like object without a `join` method, the function
13+
manually constructs the output string.
14+
15+
Parameters
16+
----------
17+
x: ArrayLikeObject
18+
Input array.
19+
20+
separator: string
21+
Separator to be used.
22+
23+
Returns
24+
-------
25+
out: string
26+
Concatenated string.
27+
28+
Examples
29+
--------
30+
> var out = {{alias}}( [ 1, 2, 3, 4 ], ',' )
31+
'1,2,3,4'
32+
33+
See Also
34+
--------
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection, TypedArray, ComplexTypedArray } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a string which concatenates array elements using a specified separator.
27+
*
28+
* @param x - input array
29+
* @param separator - separator element
30+
* @returns string
31+
*
32+
* @example
33+
* var Float64Array = require( '@stdlib/array/float64' );
34+
*
35+
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
36+
*
37+
* var out = join( x, ',' );
38+
* // returns '1,2,3'
39+
*
40+
* @example
41+
* var Complex128Array = require( '@stdlib/array/complex128' );
42+
*
43+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
44+
*
45+
* var out = slice( x, '-' );
46+
* // returns '1-2-3-4-5-6'
47+
*/
48+
declare function join<T extends TypedArray | ComplexTypedArray>( x: T, separator: string ): string;
49+
50+
/**
51+
* Returns a string which concatenates array elements using a specified separator.
52+
*
53+
* @param x - input array
54+
* @param start - starting index (inclusive)
55+
* @param end - ending index (exclusive)
56+
* @returns output array
57+
*
58+
* @example
59+
* var x = [ 1, 2, 3 ];
60+
*
61+
* var out = join( x, ',' );
62+
* // returns '1,2,3'
63+
*
64+
* @example
65+
* var x = [ 1, 2, 3, 4, 5, 6 ];
66+
*
67+
* var out = slice( x, '-' );
68+
* // returns '1-2-3-4-5-6'
69+
*/
70+
declare function join<T = unknown>( x: Collection<T>, separator: string ): string;
71+
72+
73+
// EXPORTS //
74+
75+
export = join;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
import Complex128Array = require( '@stdlib/array/complex128' );
20+
import Complex64Array = require( '@stdlib/array/complex64' );
21+
import join = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns an array...
27+
{
28+
join( [ 1, 2, 3 ], ',' ); // $ExpectType string
29+
join( new Float64Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
30+
join( new Float32Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
31+
join( new Int32Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
32+
join( new Int16Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
33+
join( new Int8Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
34+
join( new Uint32Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
35+
join( new Uint16Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
36+
join( new Uint8Array( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
37+
join( new Uint8ClampedArray( [ 1, 2, 3 ] ), ',' ); // $ExpectType string
38+
join( new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] ), ',' ); // $ExpectType string
39+
join( new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] ), ',' ); // $ExpectType string
40+
}
41+
42+
// The compiler throws an error if the function is provided a first argument which is not a collection...
43+
{
44+
join( 5, ',' ); // $ExpectError
45+
join( true, ',' ); // $ExpectError
46+
join( false, ',' ); // $ExpectError
47+
join( null, ',' ); // $ExpectError
48+
join( void 0, ',' ); // $ExpectError
49+
join( {}, ',' ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a second argument which is not a string...
53+
{
54+
const x = [ 1, 2, 3 ];
55+
56+
join( x, true ); // $ExpectError
57+
join( x, false ); // $ExpectError
58+
join( x, null ); // $ExpectError
59+
join( x, {} ); // $ExpectError
60+
join( x, ( x: number ): number => x ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided an unsupported number of arguments...
64+
{
65+
join(); // $ExpectError
66+
join( [ 1, 2, 3 ] ); // $ExpectError
67+
join( [ 1, 2, 3 ], 0, 3, {} ); // $ExpectError
68+
}

0 commit comments

Comments
 (0)