crypto_pals/utils.ts

24 lines
1001 B
TypeScript
Raw Normal View History

2024-07-31 11:19:47 +00:00
import * as fs from 'node:fs';
import { Encoding } from 'node:crypto';
2024-07-31 09:11:34 +00:00
// @ts-ignore
export const range = (from: number, to: number) => [...Array(to - from + 1).keys()].map(i => i + from);
export const englishFreqs = {
97: 0.08167, 98: 0.01492, 99: 0.02782, 100: 0.04253,
101: 0.12702, 102: 0.02228, 103: 0.02015, 104: 0.06094,
105: 0.06094, 106: 0.00153, 107: 0.00772, 108: 0.04025,
109: 0.02406, 110: 0.06749, 111: 0.07507, 112: 0.01929,
113: 0.00095, 114: 0.05987, 115: 0.06327, 116: 0.09056,
117: 0.02758, 118: 0.00978, 119: 0.02360, 120: 0.00150,
121: 0.01974, 122: 0.00074, 32: 0.13000,
};
2024-07-31 11:19:47 +00:00
type FileToBuff = (fName: string, encoding: Encoding, shouldSplit?: boolean) => Buffer[];
export const fileToBuff: FileToBuff = (fName, encoding, shouldSplit = true) => {
const data = fs.readFileSync(fName, 'utf-8');
const dataLines = shouldSplit ? data.split('\n').map(s => s.trim()) : [data.trim()];
return dataLines.map(x => Buffer.from(x, encoding));
};