-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3d60550
feat(array/base): add join function
adityacodes30 7dd294b
feat(array/base): add join function
adityacodes30 f2c2454
Apply suggestions from code review
kgryte 9a56529
Apply suggestions from code review
kgryte 6ab06b7
Apply suggestions from code review
kgryte f3fb6d8
feat: add tests and function edits and requested changes
adityacodes30 c27735b
feat: add tests and function edits and requested changes
adityacodes30 e360058
Update lib/node_modules/@stdlib/array/base/join/lib/main.js
adityacodes30 513c1b4
Update lib/node_modules/@stdlib/array/base/join/lib/main.js
adityacodes30 9b5d7bc
Update lib/node_modules/@stdlib/array/base/join/lib/main.js
adityacodes30 a6c97bf
feat: add array/base/join req changes
adityacodes30 4e1e47e
style: place require calls at the top of example code
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
96 changes: 96 additions & 0 deletions
96
lib/node_modules/@stdlib/array/base/join/benchmark/benchmark.length.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
adityacodes30 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
lib/node_modules/@stdlib/array/base/join/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.