Skip to content

Commit 542e5b4

Browse files
🌱 initial: First draft.
1 parent 2c49708 commit 542e5b4

File tree

7 files changed

+8900
-25
lines changed

7 files changed

+8900
-25
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
Test whether an array-like is a palindrome for JavaScript.
55
See [docs](https://array-like.github.io/is-palindrome/index.html).
66

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+
```
1311

1412
[![License](https://img.shields.io/github/license/array-like/is-palindrome.svg)](https://raw.githubusercontent.com/array-like/is-palindrome/main/LICENSE)
1513
[![Version](https://img.shields.io/npm/v/@array-like/is-palindrome.svg)](https://www.npmjs.org/package/@array-like/is-palindrome)

doc/manual/usage.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
# Usage
22

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
154
```js
16-
const isPalindrome = await import('@array-like/is-palindrome');
5+
const {isPalindrome} = await import('@array-like/is-palindrome');
176
// or
18-
import * as isPalindrome from '@array-like/is-palindrome';
7+
import {isPalindrome} from '@array-like/is-palindrome';
198
```

src/_isPalindrome.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as _isPalindrome} from './_isPalindrome.js';
2+
export {default as isPalindrome} from './isPalindrome.js';

src/isPalindrome.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

test/src/api.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
import test from 'ava';
22

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);

0 commit comments

Comments
 (0)