repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/proquint.nu

Raw
const CONSONANTS = "bdfghjklmnprstvz" | split chars
const VOWELS = "aiou" | split chars

# Encode a value as lowercase proquint blocks.
# Scalars use Nushell's compact big-endian representation. Compound values use
# compact JSON; binary and strings keep their raw bytes. Odd byte counts get
# zero-padded for the final 16-bit block.
export def main [value?: any] {
    let input = if ($value == null) { $in } else { $value }
    let bytes = (to-bytes $input)

    $bytes
    | chunks 2
    | each {|word|
        let high = $word.0
        let low = ($word.1? | default 0)
        let number = ($high * 256 + $low)

        [
            ($CONSONANTS | get (($number // 4096) mod 16))
            ($VOWELS | get (($number // 1024) mod 4))
            ($CONSONANTS | get (($number // 64) mod 16))
            ($VOWELS | get (($number // 16) mod 4))
            ($CONSONANTS | get ($number mod 16))
        ] | str join
    }
    | str join "-"
}

def to-bytes [value: any] {
    let type = ($value | describe)

    if $type == "binary" {
        $value
    } else if $type == "string" {
        $value | into binary
    } else if $type in ["int" "float" "bool" "filesize" "duration" "datetime"] {
        $value | into binary --compact --endian big
    } else {
        $value | to json --raw --serialize | into binary
    }
}