Skip to content

feat: add array/base/join #1463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions lib/node_modules/@stdlib/array/base/join/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# join

> Return a string created by joining array elements using a specified separator.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var join = require( '@stdlib/array/base/join' );
```

#### join( x, separator )

Returns a string created by joining array elements using a specified separator.

```javascript
var x = [ 1, 2, 3, 4, 5, 6 ];

var out = join( x, ',' );
// returns '1,2,3,4,5,6'
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- 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:

```text
x.join( separator )
```

- If an array element is either `null` or `undefined`, the function will serialize the element as an empty string.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex64Array = require( '@stdlib/array/complex64' );
var AccessorArray = require( '@stdlib/array/base/accessor' );
var Float64Array = require( '@stdlib/array/float64' );
var join = require( '@stdlib/array/base/join' );

var x = [ 0, 1, 2, 3, 4, 5 ];
var s = join( x, ',' );
// returns '0,1,2,3,4,5'

x = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
s = join( x, ',' );
// returns '0,1,2,3,4,5'

s = join( x, '-' );
// returns '0-1-2-3-4-5'

s = new AccessorArray( [ 1, 2, 3, 4 ] );
s = join( s, ',' );
// returns '1,2,3,4'

x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
s = join( x, ',' );
// returns '1 + 2i,3 + 4i,5 + 6i'

x = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] );
s = join( x, ',' );
// returns '1 - 1i,2 - 2i'
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isString = require( '@stdlib/assert/is-string' );
var ones = require( '@stdlib/array/base/ones' );
var pkg = require( './../package.json' ).name;
var join = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = ones( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var s;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
s = join( x, ',' );
if ( !isString( s ) ) {
b.fail( 'should return a string' );
}
}
if ( !isString( s ) ) {
b.fail( 'should return a string' );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

f = createBenchmark( len );
bench( pkg+':dtype=generic,len='+len, f );
}
}

main();
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/array/base/join/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

{{alias}}( x, separator )
Return a string created by joining array elements using a
specified separator.

If provided an array-like object having a `join` method, the function
defers execution to that method and assumes that the method has the
following signature:

x.join( separator )

If provided an array-like object without a `join` method, the function
manually constructs the output string.

Parameters
----------
x: ArrayLikeObject
Input array.

separator: string
Separator to be used.

Returns
-------
out: string
Joined string.

Examples
--------
> var out = {{alias}}( [ 1, 2, 3, 4 ], ',' )
'1,2,3,4'

See Also
--------

74 changes: 74 additions & 0 deletions lib/node_modules/@stdlib/array/base/join/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection, TypedArray, ComplexTypedArray } from '@stdlib/types/array';

/**
* Returns a string created by joining array elements using a specified separator.
*
* @param x - input array
* @param separator - separator element
* @returns string
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
*
* var out = join( x, ',' );
* // returns '1,2,3'
*
* @example
* var Complex128Array = require( '@stdlib/array/complex128' );
*
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* var out = join( x, ',' );
* // returns '1 + 2i,3 + 4i,5 + 6i'
*/
declare function join<T extends TypedArray | ComplexTypedArray>( x: T, separator: string ): string;

/**
* Returns a string created by joining array elements using a specified separator.
*
* @param x - input array
* @param separator - separator element
* @returns string
*
* @example
* var x = [ 1, 2, 3 ];
*
* var out = join( x, ',' );
* // returns '1,2,3'
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var out = join( x, '-' );
* // returns '1-2-3-4-5-6'
*/
declare function join<T = unknown>( x: Collection<T>, separator: string ): string;


// EXPORTS //

export = join;
Loading