Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Apr 8, 2024
1 parent 76a8a6e commit 7810595
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 81 deletions.
6 changes: 4 additions & 2 deletions base/join/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ var out = join( x, ',' );
x.join( separator )
```
- If provided an array-like object without a `join` method, the function manually constructs the output string.
- If an array element is either `null` or `undefined`, the function will serialize the element as an empty string.
</section>
Expand Down Expand Up @@ -99,8 +101,8 @@ s = join( x, ',' );
s = join( x, '-' );
// returns '0-1-2-3-4-5'
s = new AccessorArray( [ 1, 2, 3, 4 ] );
s = join( s, ',' );
x = new AccessorArray( [ 1, 2, 3, 4 ] );
s = join( x, ',' );
// returns '1,2,3,4'
x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
Expand Down
4 changes: 2 additions & 2 deletions base/join/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isString = require( '@stdlib/assert/is-string' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var ones = require( './../../../base/ones' );
var pkg = require( './../package.json' ).name;
var join = require( './../lib' );
Expand Down Expand Up @@ -58,10 +58,10 @@ function createBenchmark( len ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( s ) ) {
b.fail( 'should return a string' );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();
}
Expand Down
14 changes: 7 additions & 7 deletions base/join/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

{{alias}}( x, separator )
Return a string created by joining array elements using a
specified 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:
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 )

Expand All @@ -18,12 +18,12 @@
Input array.

separator: string
Separator to be used.
Separator.

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

Examples
--------
Expand Down
37 changes: 14 additions & 23 deletions base/join/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

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

/**
* Returns a string created by joining array elements using a specified separator.
Expand All @@ -30,43 +30,34 @@ import { Collection, TypedArray, ComplexTypedArray } from '@stdlib/types/array';
* @returns string
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
* var x = [ 1, 2, 3 ];
*
* 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.
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* @param x - input array
* @param separator - separator element
* @returns string
* var out = join( x, '-' );
* // returns '1-2-3-4-5-6'
*
* @example
* var x = [ 1, 2, 3 ];
* 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 x = [ 1, 2, 3, 4, 5, 6 ];
* var Complex128Array = require( '@stdlib/array/complex128' );
*
* var out = join( x, '-' );
* // returns '1-2-3-4-5-6'
* 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 = unknown>( x: Collection<T>, separator: string ): string;
declare function join<T = unknown>( x: Collection<T> | AccessorArrayLike<T>, separator: string ): string;


// EXPORTS //
Expand Down
1 change: 1 addition & 0 deletions base/join/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import join = require( './index' );
{
const x = [ 1, 2, 3 ];

join( x, 5 ); // $ExpectError
join( x, true ); // $ExpectError
join( x, false ); // $ExpectError
join( x, null ); // $ExpectError
Expand Down
12 changes: 3 additions & 9 deletions base/join/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@
var Complex128Array = require( './../../../complex128' );
var Complex64Array = require( './../../../complex64' );
var Float64Array = require( './../../../float64' );
var zeroTo = require( './../../../base/zero-to' );
var AccessorArray = require( './../../../base/accessor' );
var join = require( './../lib' );

var x = [ 0, 1, 2, 3, 4, 5 ];

var s = join( x, ',' );
console.log( s );
// => '0,1,2,3,4,5'

x = new Float64Array( zeroTo( 6 ) );

x = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
s = join( x, ',' );
console.log( s );
// => '0,1,2,3,4,5'
Expand All @@ -41,20 +38,17 @@ s = join( x, '-' );
console.log( s );
// => '0-1-2-3-4-5'

s = new AccessorArray( [ 1, 2, 3, 4 ] );

s = join( s, ',' );
x = new AccessorArray( [ 1, 2, 3, 4 ] );
s = join( x, ',' );
console.log( s );
// => '1,2,3,4'

x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );

s = join( x, ',' );
console.log( s );
// => '1 + 2i,3 + 4i,5 + 6i'

x = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] );

s = join( x, ',' );
console.log( s );
// => '1 - 1i,2 - 2i'
68 changes: 36 additions & 32 deletions base/join/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var arraylike2object = require( './../../../base/arraylike2object' );
var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' );


// FUNCTIONS //
Expand All @@ -46,12 +47,12 @@ function hasMethod( obj, method ) {
}

/**
* Returns a string created by joining array elements using a specified separator when input is an accessor array.
* Returns a string created by joining elements in an accessor array using a specified separator.
*
* @private
* @param {Object} x - input array object
* @param {integer} separator - separator
* @returns {string} joined string
* @returns {string} output string
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
Expand All @@ -63,25 +64,28 @@ function hasMethod( obj, method ) {
* // returns '1,2,3,4'
*/
function accessors( x, separator ) {
var output;
var data;
var out;
var get;
var i;
var N;
var v;
var i;

data = x.data;
get = x.accessors[ 0 ];
output = '';
for ( i = 0; i < data.length; i++ ) {

N = data.length - 1;
out = '';
for ( i = 0; i <= N; i++ ) {
v = get( data, i );
if ( typeof v === 'undefined' || v === null ) {
v = '';
if ( !isUndefinedOrNull( v ) ) {
out += String( v );
}
output += v;
if ( i < data.length - 1 ) {
output += separator;
if ( i < N ) {
out += separator;
}
}
return output;
return out;
}

/**
Expand All @@ -90,29 +94,32 @@ function accessors( x, separator ) {
* @private
* @param {Object} x - input array object
* @param {integer} separator - separator
* @returns {string} joined string
* @returns {string} output string
*
* @example
* var x = [ 1, 2, 3, 4 ];
* var out = constructString( x, ',' );
*
* var out = indexed( x, ',' );
* // returns '1,2,3,4'
*/
function constructString( x, separator ) {
var i;
var s;
function indexed( x, separator ) {
var out;
var N;
var v;
s = '';
for ( i = 0; i < x.length; i++ ) {
var i;

N = x.length - 1;
out = '';
for ( i = 0; i <= N; i++ ) {
v = x[ i ];
if ( typeof v === 'undefined' || v === null ) {
v = '';
if ( !isUndefinedOrNull( v ) ) {
out += String( v );
}
s += v;
if ( i < x.length - 1 ) {
s += separator;
if ( i < N ) {
out += separator;
}
}
return s;
return out;
}


Expand All @@ -122,8 +129,8 @@ function constructString( x, separator ) {
* Returns a string created by joining array elements using a specified separator.
*
* @param {Collection} x - input array
* @param {integer} separator - separator to be used in string
* @returns {string} joined string
* @param {integer} separator - separator
* @returns {string} output string
*
* @example
* var x = [ 1, 2, 3, 4 ];
Expand All @@ -132,7 +139,7 @@ function constructString( x, separator ) {
* // returns '1,2,3,4'
*
* @example
* var x = [ 1, 2, 3, null, undefined, 4 ];
* var x = [ 1, 2, 3, null, undefined, 4 ];
*
* var out = join( x, '-' );
* // returns '1-2-3---4'
Expand All @@ -146,10 +153,7 @@ function join( x, separator ) {
if ( obj.accessorProtocol ) {
return accessors( obj, separator );
}
if ( obj.dtype === 'generic' || obj.dtype === null ) {
return constructString( x, separator );
}
return x.join( separator );
return indexed( x, separator );
}


Expand Down
Loading

0 comments on commit 7810595

Please sign in to comment.