UNPKG

meows

Version:
65 lines (64 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const functions_1 = require("./functions"); // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ // Strings exports.space = ' '; exports.spaces = /\s+/g; exports.character = ''; exports.alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]; /** Split string by spaces. */ exports.split_by_spaces = (str) => str.trim().split(exports.spaces); /** Alphabetical character → prime number. */ exports.alpha_to_prime = (alphabet) => ({ a: 2, b: 3, c: 5, d: 7, e: 11, f: 13, g: 17, h: 19, i: 23, j: 29, k: 31, l: 37, m: 41, n: 43, o: 47, p: 53, q: 59, r: 61, s: 67, t: 71, u: 73, v: 79, w: 83, x: 89, y: 97, z: 101, }[alphabet]); /** * word anagram → prime product * * @description * An anagram is any rearrangement of a string. Conveniently, the product of * primes is the same regardless of order, and also allows us to count how many * of each prime was used, acting as an identity for anagrams. * * @example * anagram_to_id('listen') === anagram_to_id('silent') // => true * anagram_to_id('admirer') === anagram_to_id('married') // => true */ exports.anagram_to_id = (word) => word.split(exports.character) .map(exports.alpha_to_prime) .reduce((a, b) => a * b); /** * Strips a string of all normal whitespaces. * @example * stripSpaces(' a b c ' ) // => 'abc' */ exports.stripSpaces = (str) => functions_1.truthy(() => { if (typeof str !== 'string') throw new TypeError(`stripSpaces() requires a string, but got: ${str}.`); }) && str.replace(/\s/g, ''); /** * Converts a natural number from 0 up to some hundred trillions to words in the * American English style. * * @example * naturalToWords(1,234,567,890) // => 'one billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred and ninety' */ // function naturalToWords(nat) { // ifOrThrow([isType.nat], nat, `naturalToWords() requires a natural number, but got: ${nat}.`) // if (nat === 0) return 'zero' // const digits = int_to_digits(nat) // const from1to9 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] // const from11to19 = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] // const tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] // const from1to99 = [...from1to9, 'ten', ...from11to19, tens.reduce( // (state_tens, n_ten) => state_tens.concat(from1to9.reduce( // (state_ones, n_one) => state_ones.concat(`${n_ten} ${n_one}`), // [n_ten], // )), // [], // )] // }