All files / ethers.js/src.ts/wordlists decode-owl.ts

100% Statements 58/58
100% Branches 14/14
100% Functions 3/3
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 48 49 50 51 52 53 54 55 56 57 58 591x 1x 1x 1x 1x 1x 12x 12x 12x 27768x 3042x 27768x 24576x 24726x 150x 150x 150x 27768x 12x 12x 1x 1x 1x 1x 1x 6x 6x 6x 174x 174x 6x 6x 6x 6x 13453x 1015x 13453x 12438x 12438x 13453x 6x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x  
import { assertArgument } from "../utils/index.js";
 
 
const subsChrs = " !#$%&'()*+,-./<=>?@[]^_`{|}~";
const Word = /^[a-z]*$/i;
 
function unfold(words: Array<string>, sep: string): Array<string> {
    let initial = 97;
    return words.reduce((accum, word) => {
        if (word === sep) {
            initial++;
        } else if (word.match(Word)) {
            accum.push(String.fromCharCode(initial) + word);
        } else {
            initial = 97;
            accum.push(word);
        }
        return accum;
    }, <Array<string>>[]);
}
 
/**
 *  @_ignore
 */
export function decode(data: string, subs: string): Array<string> {
 
    // Replace all the substitutions with their expanded form
    for (let i = subsChrs.length - 1; i >= 0; i--) {
        data = data.split(subsChrs[i]).join(subs.substring(2 * i, 2 * i + 2));
    }
 
    // Get all tle clumps; each suffix, first-increment and second-increment
    const clumps: Array<string> = [ ];
    const leftover = data.replace(/(:|([0-9])|([A-Z][a-z]*))/g, (all, item, semi, word) => {
        if (semi) {
            for (let i = parseInt(semi); i >= 0; i--) { clumps.push(";"); }
        } else {
            clumps.push(item.toLowerCase());
        }
        return "";
    });
    /* c8 ignore start */
    if (leftover) { throw new Error(`leftovers: ${ JSON.stringify(leftover) }`); }
    /* c8 ignore stop */
 
    return unfold(unfold(clumps, ";"), ":");
}
 
/**
 *  @_ignore
 */
export function decodeOwl(data: string): Array<string> {
    assertArgument(data[0] === "0", "unsupported auwl data", "data", data);
 
    return decode(
        data.substring(1 + 2 * subsChrs.length),
        data.substring(1, 1 + 2 * subsChrs.length));
}