Skip to content

Commit 8064444

Browse files
feat: add assert/is-palindrome
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 108e110 commit 8064444

10 files changed

Lines changed: 560 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# isPalindrome
22+
23+
> Test if a value is a palindrome.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isPalindrome = require( '@stdlib/assert/is-palindrome' );
31+
```
32+
33+
#### isPalindrome( value )
34+
35+
Tests if a `value` is a palindrome `string`.
36+
37+
```javascript
38+
var bool = isPalindrome( 'racecar' );
39+
// returns true
40+
41+
bool = isPalindrome( 'level' );
42+
// returns true
43+
44+
bool = isPalindrome( 'abc' );
45+
// returns false
46+
47+
bool = isPalindrome( null );
48+
// returns false
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
```javascript
60+
var isPalindrome = require( '@stdlib/assert/is-palindrome' );
61+
62+
var bool = isPalindrome( 'racecar' );
63+
// returns true
64+
65+
bool = isPalindrome( 'ada' );
66+
// returns true
67+
68+
bool = isPalindrome( 'website' );
69+
// returns false
70+
71+
bool = isPalindrome( null );
72+
// returns false
73+
```
74+
75+
</section>
76+
77+
<!-- /.examples -->
78+
79+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
80+
81+
<section class="related">
82+
83+
* * *
84+
85+
## See Also
86+
87+
- <span class="package-name">[`@stdlib/assert/is-string`][@stdlib/assert/is-string]</span><span class="delimiter">: </span><span class="description">test if a value is a string.</span>
88+
89+
</section>
90+
91+
<!-- /.related -->
92+
93+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
94+
95+
<section class="links">
96+
97+
<!-- <related-links> -->
98+
99+
[@stdlib/assert/is-string]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-string
100+
101+
<!-- </related-links> -->
102+
103+
</section>
104+
105+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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' );
25+
var pkg = require( './../package.json' ).name;
26+
var isPalindrome = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var bool;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
bool = isPalindrome( 'racecar' );
38+
if ( typeof bool !== 'boolean' ) {
39+
b.fail( 'should return a boolean' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isString( pkg ) ) {
44+
b.fail( 'should be a string' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg + ':long_string', function benchmark( b ) {
51+
var bool;
52+
var str;
53+
var i;
54+
55+
str = 'tattarrattat';
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
bool = isPalindrome( str );
59+
if ( typeof bool !== 'boolean' ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
}
63+
b.toc();
64+
if ( !bool ) {
65+
b.fail( 'should return true' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a palindrome.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating if a value is a palindrome.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( 'racecar' )
18+
true
19+
> bool = {{alias}}( 'level' )
20+
true
21+
> bool = {{alias}}( 'noon' )
22+
true
23+
> bool = {{alias}}( 'stdlib' )
24+
false
25+
> bool = {{alias}}( null )
26+
false
27+
28+
See Also
29+
--------
30+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
* Tests if a value is a palindrome.
23+
*
24+
* @param v - value to test
25+
* @returns boolean indicating if a value is a palindrome
26+
*
27+
* @example
28+
* var bool = isPalindrome( 'racecar' );
29+
* // returns true
30+
*/
31+
declare function isPalindrome( v: any ): boolean;
32+
33+
34+
// EXPORTS //
35+
36+
export = isPalindrome;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isPalindrome = require( '@stdlib/assert/is-palindrome' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isPalindrome( 'racecar' ); // $ExpectType boolean
27+
isPalindrome( '' ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided an invalid number of arguments...
31+
{
32+
isPalindrome(); // $ExpectError
33+
isPalindrome( 'racecar', {} ); // $ExpectError
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isPalindrome = require( './../lib' );
22+
23+
console.log( isPalindrome( 'racecar' ) );
24+
// => true
25+
26+
console.log( isPalindrome( 'ada' ) );
27+
// => true
28+
29+
console.log( isPalindrome( 'website' ) );
30+
// => false
31+
32+
console.log( isPalindrome( null ) );
33+
// => false
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) 2026 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+
* Assert if a value is a palindrome.
23+
*
24+
* @module @stdlib/assert/is-palindrome
25+
*
26+
* @example
27+
* var isPalindrome = require( '@stdlib/assert/is-palindrome' );
28+
*
29+
* var bool = isPalindrome( 'level' );
30+
* // returns true
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)