-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAlphabeticalInsertionIndex.test.ts
47 lines (41 loc) · 1.47 KB
/
getAlphabeticalInsertionIndex.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { assertEquals } from '@std/assert';
import { getAlphabeticalInsertionIndex } from './getAlphabeticalInsertionIndex.ts';
Deno.test('getAlphabeticalInsertionIndex - case sensitive', () =>
{
const arr = ['apple', 'banana', 'cherry', 'date'];
assertEquals(getAlphabeticalInsertionIndex('banana', arr), 1);
assertEquals(getAlphabeticalInsertionIndex('apricot', arr), 1);
assertEquals(getAlphabeticalInsertionIndex('zebra', arr), 4);
assertEquals(getAlphabeticalInsertionIndex('aardvark', arr), 0);
});
Deno.test('getAlphabeticalInsertionIndex - case insensitive', () =>
{
const arr = ['Apple', 'banana', 'Cherry', 'date'];
const target = 'BANANA';
const index = getAlphabeticalInsertionIndex(
target.toLowerCase(),
arr.map((s) => s.toLowerCase()),
);
assertEquals(index, 1);
});
Deno.test('getAlphabeticalInsertionIndex - mixed case array', () =>
{
const arr = ['Apple', 'BANANA', 'cherry', 'DATE'];
const sortedArr = [...arr].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
// Test each word in the array
for (const word of arr)
{
const index = getAlphabeticalInsertionIndex(
word.toLowerCase(),
sortedArr.map((s) => s.toLowerCase()),
);
const expectedIndex = sortedArr.findIndex(
(s) => s.toLowerCase() === word.toLowerCase(),
);
assertEquals(index, expectedIndex);
}
});
Deno.test('getAlphabeticalInsertionIndex - empty array', () =>
{
assertEquals(getAlphabeticalInsertionIndex('test', []), 0);
});