From 7810595f53cf1e53444ce3f2578c284d6154587c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 8 Apr 2024 01:04:41 +0000 Subject: [PATCH] Auto-generated commit --- base/join/README.md | 6 +- base/join/benchmark/benchmark.length.js | 4 +- base/join/docs/repl.txt | 14 ++-- base/join/docs/types/index.d.ts | 37 ++++----- base/join/docs/types/test.ts | 1 + base/join/examples/index.js | 12 +-- base/join/lib/main.js | 68 ++++++++-------- base/join/test/test.js | 103 ++++++++++++++++++++++-- base/lib/index.js | 9 +++ 9 files changed, 173 insertions(+), 81 deletions(-) diff --git a/base/join/README.md b/base/join/README.md index fa364e59..4396cac0 100644 --- a/base/join/README.md +++ b/base/join/README.md @@ -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. @@ -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 ] ); diff --git a/base/join/benchmark/benchmark.length.js b/base/join/benchmark/benchmark.length.js index cd7637f8..51b88612 100644 --- a/base/join/benchmark/benchmark.length.js +++ b/base/join/benchmark/benchmark.length.js @@ -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' ); @@ -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(); } diff --git a/base/join/docs/repl.txt b/base/join/docs/repl.txt index 431b9dd8..2ce6f92f 100644 --- a/base/join/docs/repl.txt +++ b/base/join/docs/repl.txt @@ -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 ) @@ -18,12 +18,12 @@ Input array. separator: string - Separator to be used. + Separator. Returns ------- out: string - Joined string. + Output string. Examples -------- diff --git a/base/join/docs/types/index.d.ts b/base/join/docs/types/index.d.ts index 5f2d0537..fc0675df 100644 --- a/base/join/docs/types/index.d.ts +++ b/base/join/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -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. @@ -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( 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( x: Collection, separator: string ): string; +declare function join( x: Collection | AccessorArrayLike, separator: string ): string; // EXPORTS // diff --git a/base/join/docs/types/test.ts b/base/join/docs/types/test.ts index 642427d6..9d4fc36a 100644 --- a/base/join/docs/types/test.ts +++ b/base/join/docs/types/test.ts @@ -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 diff --git a/base/join/examples/index.js b/base/join/examples/index.js index 8b9a3c5c..9322dd67 100644 --- a/base/join/examples/index.js +++ b/base/join/examples/index.js @@ -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' @@ -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' diff --git a/base/join/lib/main.js b/base/join/lib/main.js index d1423857..bf5633b9 100644 --- a/base/join/lib/main.js +++ b/base/join/lib/main.js @@ -21,6 +21,7 @@ // MODULES // var arraylike2object = require( './../../../base/arraylike2object' ); +var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); // FUNCTIONS // @@ -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' ); @@ -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; } /** @@ -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; } @@ -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 ]; @@ -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' @@ -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 ); } diff --git a/base/join/test/test.js b/base/join/test/test.js index a0086c3b..06735030 100644 --- a/base/join/test/test.js +++ b/base/join/test/test.js @@ -53,6 +53,13 @@ tape( 'the function joins an array-like object (generic)', function test( t ) { t.strictEqual( actual, expected, 'returns expected value' ); + x = [ null ]; + + expected = ''; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + x = []; expected = ''; @@ -60,28 +67,28 @@ tape( 'the function joins an array-like object (generic)', function test( t ) { t.strictEqual( actual, expected, 'returns expected value' ); - x = [ 'hello', '', undefined, null, NaN, undefined ]; + x = [ 'hello', '', void 0, null, NaN, void 0 ]; expected = 'hello,,,,NaN,'; actual = join( x, ',' ); t.strictEqual( actual, expected, 'returns expected value' ); - x = [ null, undefined, null, undefined ]; + x = [ null, void 0, null, void 0 ]; expected = ',,,'; actual = join( x, ',' ); t.strictEqual( actual, expected, 'returns expected value' ); - x = [ null, 1, 2, 'testString', undefined ]; + x = [ null, 1, 2, 'testString', void 0 ]; expected = ',1,2,testString,'; actual = join( x, ',' ); t.strictEqual( actual, expected, 'returns expected value' ); - x = [ null, 1, 2, NaN, undefined ]; + x = [ null, 1, 2, NaN, void 0 ]; expected = ',1,2,NaN,'; actual = join( x, ',' ); @@ -168,6 +175,13 @@ tape( 'the function joins an array-like object (complex128)', function test( t ) t.strictEqual( actual, expected, 'returns expected value' ); + x = new Complex128Array( [ 1.0, -1.0 ] ); + + expected = '1 - 1i'; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + x = new Complex128Array( [] ); expected = ''; @@ -190,7 +204,7 @@ tape( 'the function joins an array-like object (accessors)', function test( t ) t.strictEqual( actual, expected, 'returns expected value' ); - x = new AccessorArray( [ 1, 2, 3, null, undefined, 4 ] ); + x = new AccessorArray( [ 1, 2, 3, null, void 0, 4 ] ); expected = '1,2,3,,,4'; actual = join( x, ',' ); @@ -204,7 +218,7 @@ tape( 'the function joins an array-like object (accessors)', function test( t ) t.strictEqual( actual, expected, 'returns expected value' ); - x = new AccessorArray( [ null, undefined, NaN ] ); + x = new AccessorArray( [ null, void 0, NaN ] ); expected = ',,NaN'; actual = join( x, ',' ); @@ -220,3 +234,80 @@ tape( 'the function joins an array-like object (accessors)', function test( t ) t.end(); }); + +tape( 'the function joins an array-like object (array-like)', function test( t ) { + var expected; + var actual; + var x; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + + expected = '1,2,3,4'; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = { + 'length': 6, + '0': 1, + '1': 2, + '2': 3, + '3': null, + '4': void 0, + '5': 4 + }; + + expected = '1,2,3,,,4'; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = { + 'length': 1, + '0': 1 + }; + + expected = '1'; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = { + 'length': 1, + '0': null + }; + + expected = ''; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = { + 'length': 3, + '0': null, + '1': void 0, + '2': NaN + }; + + expected = ',,NaN'; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + x = { + 'length': 0 + }; + + expected = ''; + actual = join( x, ',' ); + + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/base/lib/index.js b/base/lib/index.js index c1327144..45d554dc 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -900,6 +900,15 @@ setReadOnly( ns, 'incrspace', require( './../../base/incrspace' ) ); */ setReadOnly( ns, 'indexOf', require( './../../base/index-of' ) ); +/** +* @name join +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/join} +*/ +setReadOnly( ns, 'join', require( './../../base/join' ) ); + /** * @name last * @memberof ns