File tree Expand file tree Collapse file tree 7 files changed +8900
-25
lines changed Expand file tree Collapse file tree 7 files changed +8900
-25
lines changed Original file line number Diff line number Diff line change 4
4
Test whether an array-like is a palindrome for JavaScript.
5
5
See [ docs] ( https://array-like.github.io/is-palindrome/index.html ) .
6
6
7
- > :building_construction : Caveat emptor! This is work in progress. Code may be
8
- > working. Documentation may be present. Coherence may be. Maybe.
9
-
10
- > :warning : Depending on your environment, the code may require
11
- > ` regeneratorRuntime ` to be defined, for instance by importing
12
- > [ regenerator-runtime/runtime] ( https://www.npmjs.com/package/regenerator-runtime ) .
7
+ ``` js
8
+ import {isPalindrome } from ' @array-like/is-palindrome' ;
9
+ isPalindrome (' ΝΙΨΟΝΑΝΟΜΗΜΑΤΑΜΗΜΟΝΑΝΟΨΙΝ' ); // true
10
+ ```
13
11
14
12
[ ![ License] ( https://img.shields.io/github/license/array-like/is-palindrome.svg )] ( https://raw.githubusercontent.com/array-like/is-palindrome/main/LICENSE )
15
13
[ ![ Version] ( https://img.shields.io/npm/v/@array-like/is-palindrome.svg )] ( https://www.npmjs.org/package/@array-like/is-palindrome )
Original file line number Diff line number Diff line change 1
1
# Usage
2
2
3
- > :warning : Depending on your environment, the code may require
4
- > ` regeneratorRuntime ` to be defined, for instance by importing
5
- > [ regenerator-runtime/runtime] ( https://www.npmjs.com/package/regenerator-runtime ) .
6
-
7
- First, require the polyfill at the entry point of your application
8
- ``` js
9
- await import (' regenerator-runtime/runtime.js' );
10
- // or
11
- import ' regenerator-runtime/runtime.js' ;
12
- ```
13
-
14
- Then, import the library where needed
3
+ Import the library where needed
15
4
``` js
16
- const isPalindrome = await import (' @array-like/is-palindrome' );
5
+ const { isPalindrome } = await import (' @array-like/is-palindrome' );
17
6
// or
18
- import * as isPalindrome from ' @array-like/is-palindrome' ;
7
+ import { isPalindrome } from ' @array-like/is-palindrome' ;
19
8
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Tests whether a slice of an array-like is a palindrome.
3
+ *
4
+ * @param {ArrayLike } a Array-like input
5
+ * @param {number } i Inclusive left bound
6
+ * @param {number } j Non-inclusive right bound
7
+ * @return boolean True if the input slice is a palindrome.
8
+ */
9
+ export default function _isPalindrome ( a , i , j ) {
10
+ while ( i < j ) {
11
+ if ( a [ i ++ ] !== a [ -- j ] ) {
12
+ return false ;
13
+ }
14
+ }
15
+
16
+ return true ;
17
+ }
Original file line number Diff line number Diff line change 1
- const answer = 42 ;
2
- export default answer ;
1
+ export { default as _isPalindrome } from './_isPalindrome.js' ;
2
+ export { default as isPalindrome } from './isPalindrome.js' ;
Original file line number Diff line number Diff line change
1
+ import _isPalindrome from './_isPalindrome.js' ;
2
+
3
+ /**
4
+ * Tests whether an array-like is a palindrome.
5
+ *
6
+ * @param {ArrayLike } a Array-like input
7
+ * @return boolean True if the input is a palindrome.
8
+ */
9
+ export default function isPalindrome ( a ) {
10
+ return _isPalindrome ( a , 0 , a . length ) ;
11
+ }
Original file line number Diff line number Diff line change 1
1
import test from 'ava' ;
2
2
3
- test ( 'API' , ( t ) => {
4
- t . fail ( ) ;
5
- } ) ;
3
+ import { isPalindrome } from '../../src/index.js' ;
4
+
5
+ const macro = ( t , s , expected ) => {
6
+ t . is ( expected , isPalindrome ( s ) ) ;
7
+ } ;
8
+
9
+ macro . title = ( title , s , expected ) =>
10
+ title ?? expected ? `'${ s } ' is a palindrome` : `'${ s } ' is not a palindrome` ;
11
+
12
+ test ( macro , '' , true ) ;
13
+ test ( macro , 'Ö' , true ) ;
14
+ test ( macro , 'x' , true ) ;
15
+ test ( macro , 'xx' , true ) ;
16
+ test ( macro , 'xyx' , true ) ;
17
+ test ( macro , 'sator arepo tenet opera rotas' , true ) ;
18
+ test ( macro , 'ΝΙΨΟΝΑΝΟΜΗΜΑΤΑΜΗΜΟΝΑΝΟΨΙΝ' , true ) ;
19
+
20
+ test ( macro , 'ab' , false ) ;
21
+ test ( macro , 'abc' , false ) ;
22
+ test ( macro , 'abca' , false ) ;
23
+ test ( macro , 'sator arepo tenët opera rotas' , false ) ;
24
+ test ( macro , 'Sator Arepo Tenet Opera Rotas' , false ) ;
25
+ test ( macro , 'ΝΙΨΟΝ ΑΝΟΜΗΜΑΤΑ ΜΗ ΜΟΝΑΝ ΟΨΙΝ' , false ) ;
You can’t perform that action at this time.
0 commit comments