-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotor.ts
30 lines (27 loc) · 910 Bytes
/
rotor.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
import type { Rotor } from "./interface.ts";
import { shiftValues, size, substitute } from "./util.ts";
/**
* Function to create a rotor from a list (`map`) of codes specifying the
* `notch` index
*
* A full cycle occurs when the _current rotor index_ is equal to the `notch` index
*/
export function createRotor(map: number[] = [], notch = size - 1): Rotor {
const original = map.slice();
let id: number;
let onCycleCallback: () => void;
// function to rotate the rotor map
function setIndex(n: number) {
id = n % (size - 1);
map = shiftValues(original.slice(), id);
if (id === notch) onCycleCallback?.call(undefined);
}
return {
setIndex,
get: (x: number, flag: boolean) => substitute(map, x, flag),
rotate: () => setIndex(id + 1),
set oncycle(fn: () => void) {
onCycleCallback = fn;
},
};
}