Skip to content

Commit c0ef1e4

Browse files
committed
feat: add string/base/concat
- Add package to concatenate two strings - Wrap String.prototype.concat with fixed arity of 2 - Add tests, benchmarks, examples, and TypeScript definitions - Closes #[issue-number]
1 parent 81b01b7 commit c0ef1e4

File tree

10 files changed

+577
-0
lines changed

10 files changed

+577
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 str = concat( 'Hello ', 'World!' );
45+
// returns 'Hello World!'
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( 'Hello ', 'World!' );
62+
// returns 'Hello World!'
63+
64+
str = concat( 'Beep ', 'Boop' );
65+
// returns 'Beep Boop'
66+
67+
str = concat( '', 'World!' );
68+
// returns 'World!'
69+
70+
str = concat( 'Hello ', '' );
71+
// returns 'Hello '
72+
73+
str = concat( '', '' );
74+
// returns ''
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+
* * *
86+
87+
## See Also
88+
89+
- <span class="package-name">[`@stdlib/string/base/lowercase`][@stdlib/string/base/lowercase]</span><span class="delimiter">: </span><span class="description">convert a string to lowercase.</span>
90+
- <span class="package-name">[`@stdlib/string/base/uppercase`][@stdlib/string/base/uppercase]</span><span class="delimiter">: </span><span class="description">convert a string to uppercase.</span>
91+
92+
</section>
93+
94+
<!-- /.related -->
95+
96+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
97+
98+
<section class="links">
99+
100+
<!-- <related-links> -->
101+
102+
[@stdlib/string/base/lowercase]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/base/lowercase
103+
104+
[@stdlib/string/base/uppercase]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/base/uppercase
105+
106+
<!-- </related-links> -->
107+
108+
</section>
109+
110+
<!-- /.links -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 = [
38+
'Hello ',
39+
'Beep ',
40+
'Foo '
41+
];
42+
values2 = [
43+
'World!',
44+
'Boop',
45+
'Bar'
46+
];
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
out = concat( values1[ i%values1.length ], values2[ i%values2.length ] );
51+
if ( typeof out !== 'string' ) {
52+
b.fail( 'should return a string' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isString( out ) ) {
57+
b.fail( 'should return a string' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::builtin', function benchmark( b ) {
64+
var values1;
65+
var values2;
66+
var out;
67+
var i;
68+
69+
values1 = [
70+
'Hello ',
71+
'Beep ',
72+
'Foo '
73+
];
74+
values2 = [
75+
'World!',
76+
'Boop',
77+
'Bar'
78+
];
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
out = values1[ i%values1.length ].concat( values2[ i%values2.length ] );
83+
if ( typeof out !== 'string' ) {
84+
b.fail( 'should return a string' );
85+
}
86+
}
87+
b.toc();
88+
if ( !isString( out ) ) {
89+
b.fail( 'should return a string' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( str1, str2 )
3+
Concatenates two strings.
4+
5+
Parameters
6+
----------
7+
str1: string
8+
First string.
9+
10+
str2: string
11+
Second string.
12+
13+
Returns
14+
-------
15+
out: string
16+
Concatenated string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'Hello ', 'World!' )
21+
'Hello World!'
22+
23+
See Also
24+
--------
25+
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 str = concat( 'Hello ', 'World!' );
30+
* // returns 'Hello World!'
31+
*/
32+
declare function concat( str1: string, str2: string ): string;
33+
34+
35+
// EXPORTS //
36+
37+
export = concat;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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( 'Hello ', 'World!' ); // $ExpectType string
27+
concat( 'Beep ', 'Boop' ); // $ExpectType string
28+
concat( '', '' ); // $ExpectType string
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument that is not a string...
32+
{
33+
concat( true, 'abc' ); // $ExpectError
34+
concat( false, 'abc' ); // $ExpectError
35+
concat( null, 'abc' ); // $ExpectError
36+
concat( undefined, 'abc' ); // $ExpectError
37+
concat( 5, 'abc' ); // $ExpectError
38+
concat( [], 'abc' ); // $ExpectError
39+
concat( {}, 'abc' ); // $ExpectError
40+
concat( ( x: number ): number => x, 'abc' ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument that is not a string...
44+
{
45+
concat( 'abc', true ); // $ExpectError
46+
concat( 'abc', false ); // $ExpectError
47+
concat( 'abc', null ); // $ExpectError
48+
concat( 'abc', undefined ); // $ExpectError
49+
concat( 'abc', 5 ); // $ExpectError
50+
concat( 'abc', [] ); // $ExpectError
51+
concat( 'abc', {} ); // $ExpectError
52+
concat( 'abc', ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided insufficient arguments...
56+
{
57+
concat(); // $ExpectError
58+
concat( 'abc' ); // $ExpectError
59+
}
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( 'Hello ', 'World!' );
24+
console.log( str );
25+
// => 'Hello World!'
26+
27+
str = concat( 'Beep ', 'Boop' );
28+
console.log( str );
29+
// => 'Beep Boop'
30+
31+
str = concat( '', 'World!' );
32+
console.log( str );
33+
// => 'World!'
34+
35+
str = concat( 'Hello ', '' );
36+
console.log( str );
37+
// => 'Hello '
38+
39+
str = concat( '', '' );
40+
console.log( str );
41+
// => ''

0 commit comments

Comments
 (0)