Skip to content

Commit e9d4935

Browse files
sagar7162kgryte
andauthored
feat: add string/base/concat
PR-URL: #8521 Closes: #8520 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a8f91a0 commit e9d4935

File tree

10 files changed

+540
-0
lines changed

10 files changed

+540
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# concat
22+
23+
> Concatenate two strings.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var concat = require( '@stdlib/string/base/concat' );
37+
```
38+
39+
#### concat( str1, str2 )
40+
41+
Concatenates two strings.
42+
43+
```javascript
44+
var out = concat( 'beep', 'boop' );
45+
// returns 'beepboop'
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var concat = require( '@stdlib/string/base/concat' );
60+
61+
var str = concat( 'beep', 'boop' );
62+
// returns 'beepboop'
63+
64+
str = concat( 'foo', 'bar' );
65+
// returns 'foobar'
66+
67+
str = concat( 'hello', 'world' );
68+
// returns 'helloworld'
69+
70+
str = concat( '', 'abc' );
71+
// returns 'abc'
72+
73+
str = concat( '123', '' );
74+
// returns '123'
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
82+
83+
<section class="related">
84+
85+
</section>
86+
87+
<!-- /.related -->
88+
89+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="links">
92+
93+
</section>
94+
95+
<!-- /.links -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var concat = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values1;
33+
var values2;
34+
var out;
35+
var i;
36+
37+
values1 = [ 'BEEP', 'FOO', 'HELLO' ];
38+
values2 = [ 'BOOP', 'BAR', 'WORLD' ];
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
out = concat( values1[ i%values1.length ], values2[ i%values2.length ] ); // eslint-disable-line max-len
43+
if ( typeof out !== 'string' ) {
44+
b.fail( 'should return a string' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isString( out ) ) {
49+
b.fail( 'should return a string' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg + '::builtin', function benchmark( b ) {
56+
var values1;
57+
var values2;
58+
var out;
59+
var i;
60+
61+
values1 = [ 'BEEP', 'FOO', 'HELLO' ];
62+
values2 = [ 'BOOP', 'BAR', 'WORLD' ];
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
out = values1[ i%values1.length ].concat( values2[ i%values2.length ] );
67+
if ( typeof out !== 'string' ) {
68+
b.fail( 'should return a string' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isString( out ) ) {
73+
b.fail( 'should return a string' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( str1, str2 )
3+
Concatenates two strings.
4+
5+
Parameters
6+
----------
7+
str1: string
8+
First input string.
9+
10+
str2: string
11+
Second input string.
12+
13+
Returns
14+
-------
15+
out: string
16+
Concatenated string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'beep', 'boop' )
21+
'beepboop'
22+
23+
See Also
24+
--------
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* Concatenates two strings.
23+
*
24+
* @param str1 - first string
25+
* @param str2 - second string
26+
* @returns concatenated string
27+
*
28+
* @example
29+
* var out = concat( 'beep', 'boop' );
30+
* // returns 'beepboop'
31+
*/
32+
declare function concat<S1 extends string, S2 extends string>( str1: S1, str2: S2 ): `${S1}${S2}`;
33+
34+
35+
// EXPORTS //
36+
37+
export = concat;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 concat = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
concat( 'beep', 'boop' ); // $ExpectType "beepboop"
27+
concat( 'foo', 'bar' ); // $ExpectType "foobar"
28+
concat( 'abc' as string, 'xyz' as string ); // $ExpectType string
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a string...
32+
{
33+
concat( true, 'boop' ); // $ExpectError
34+
concat( null, 'boop' ); // $ExpectError
35+
concat( undefined, 'boop' ); // $ExpectError
36+
concat( 123, 'boop' ); // $ExpectError
37+
concat( {}, 'boop' ); // $ExpectError
38+
concat( [], 'boop' ); // $ExpectError
39+
concat( ( x: number ): number => x, 'boop' ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided a second argument which is not a string...
43+
{
44+
concat( 'beep', true ); // $ExpectError
45+
concat( 'beep', null ); // $ExpectError
46+
concat( 'beep', undefined ); // $ExpectError
47+
concat( 'beep', 123 ); // $ExpectError
48+
concat( 'beep', {} ); // $ExpectError
49+
concat( 'beep', [] ); // $ExpectError
50+
concat( 'beep', ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided insufficient arguments...
54+
{
55+
concat(); // $ExpectError
56+
concat( 'beep' ); // $ExpectError
57+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var concat = require( './../lib' );
22+
23+
var str = concat( 'beep', 'boop' );
24+
console.log( str );
25+
// => 'beepboop'
26+
27+
str = concat( 'foo', 'bar' );
28+
console.log( str );
29+
// => 'foobar'
30+
31+
str = concat( 'Hello, ', 'world!' );
32+
console.log( str );
33+
// => 'Hello, world!'
34+
35+
str = concat( '', 'empty' );
36+
console.log( str );
37+
// => 'empty'
38+
39+
str = concat( 'test', '' );
40+
console.log( str );
41+
// => 'test'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* Concatenate two strings.
23+
*
24+
* @module @stdlib/string/base/concat
25+
*
26+
* @example
27+
* var concat = require( '@stdlib/string/base/concat' );
28+
*
29+
* var str = concat( 'beep', 'boop' );
30+
* // returns 'beepboop'
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)