Utilities
Only a very cool Shiba would even dare use a cat's tools.
Alternative Documentation WIP.
Installation & Usage
npm -i meowsyarn add meows
const U = const isType isEmpty notEmpty =
Strings
/** Split string by spaces. */ /** Alphabetical character → prime number. */ /** * 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 */ /** * Strips a string of all normal whitespaces. * @example * stripSpaces(' a b c ' ) // => 'abc' */
Date and Time
/** Convert an integer from 1 to 12 to a month. */ /** A utility to store multiple timestamps for performance timing. */
Error and Logging
/** Alias for `console.log()`. */ /** `console.log()` with an introductory string message. */ /** * Log something to console with optional message, then returns identical input. * * @example * Promise.all(pending) * .then(x => logId(x, 'task: done')) * .then(sendTask) */
Arrays
/** * A function for quickly generating arrays populated by a range of integers, * with an optional skip interval. * * @example * range(3) // => [0, 1, 2, 3] * range(-3) // => [0, -1, -2, -3] * range(-2, 2) // => [-2, -1, 0, 1, 2] * range(2, -2) // => [2, 1, 0, -1, -2] * range(-10, 10, 5) // => [-10, -5, 0, 5, 10] * range(10, -10, 5) // => [10, 5, 0, -5, -10] */ /** * Shallow filters out redundant members. * @example * uniques([0, 1, 2, 2, 3, 3]) // => [0, 1, 2, 3] */ /** * Shallow checks for repeat members. * @example * has_repeat([1, 2, 3]) // => false * has_repeat([1, 2, 1]) // => true */ /** * Choose random member in array. * @example * rand_choice([1, 'a', null]) // => 1 * rand_choice([1, 'a', null]) // => null */ /** * Uses `λ` to populate an array of `size` length. * * @example * series(x => 0.5*x**2 + 0.5*x, 5) // => [0, 1, 3, 6, 10] */ /** * Returns a shallow copy of the last value of your array. The default behavior * is to return `undefined` on an empty array while `throwOnEmpty` is set to * `false`. * * @example * last([3, 2, 1]) // => 1 * last([3, 2, [1, 0]]) // => [1, 0] * last([null]) // => null * last([]) // => undefined if throwOnEmpty is false (!) */
Objects
/** An empty object with no prototype. */ /** Merge objects with an empty object; can be used to strip prototype. */ /** Grab an object based on pathway of sequence of keys. * @example * const store = { * a: 0, * b: { * a: [0, 0, 0], * b: 1, * } * } * const node = path(store, 'a', 'b', 'a') * const value = node[0] * * node[0] = 1 * console.log(store.a.b.a) // => [1, 0, 0] * console.log(value * 2) // => 2 */
Functions
/** composes a sequence of functions from right to left. */ /** composes a sequence of functions from left to right. */ /** zip, but with a function */ /** * @example * [true, true, false].filter(identity) // => [true, true] */ /** * Given a lambda and a value `x`, run the lambda with optional arguments as a * side effect, and then return the original given value `x`. * * @example * function sayHi(msg = 'hi') { console.log(msg) } * effectId(sayHi, 42) // => logs 'hi' and returns 42 * effectId(sayHi, 42, 'yo') // => logs 'yo' and returns 42 */ /** * See if at least one array element * @example * const v1 = [2, 4, 6, 8] * const v2 = [2, 4, 6, 9] * const even = x => x % 2 === 0 * * count(even, v1) // => 4 * count(even, v2) // => 1 * count(even, []) // => 0 */ /** * Given λ → returns `(compose not λ)`. * @example * const notArray = not(Array.isArray) * notArray([]) // => false * notArray({}) // => true */ /** * Checks if an array is a non-empty array of things that would always pass * your boolean function. Like `orMap` or `.every()` except empty arrays return * false. * * @example * always(even, [0, 2, 4]) // => true * always(even, [0, 1, 2]) // => false * always(even, []) // => false */ /** * Coerces values to `true`. If input is a function, `truthy` will run the * function with optional arguments for side effects before returning `true`. * @example * truthy(console.log('hi')) && 42 // logs 'hi' and returns 42 * * const say = (msg='hi') => console.log(msg) * truthy(say) // logs 'hi' and returns true * truthy(say, 'yo') // logs 'yo' and returns true */ /** * Coerces values to `false`. If input is a function, `falsy` will run the * function with optional arguments for side effects before returning `false` * * @example * falsy(console.log('hi')) || 42 // logs 'hi' and returns 42 * * const say = (msg='hi') => console.log(msg) * falsy(say) // logs 'hi' and returns false * falsy(say, 'yo') // logs 'yo' and returns false */
Numbers and Random
/** * A suite of random tools. * * @example * Rand.int() // => -42 * Rand.real() // => -42.42 * Rand.bool() ? 'heads' : 'tails' * Rand.sBool() ? 'heads' : 'tails' */ /** Generate an array of `n` strong random integers. */ /** Sums numeric arguments. */ /** Multiplies numeric arguments. */ /** Finds the difference in magnitude between two numbers. */ /** Finds the delta of unidimensional array of reals. */ /** Converts an integer to an array of its digits. * @example * int_to_digits(123450) // => [1, 2, 3, 4, 5, 0] */ /** * Randomly signs a signable number. If the value isn't signable, return `NaN`. * @example * const backoff = (tries) => 2**tries + plus_or_minus(Math.random()) */
Logic and Types
/** * Class of static boolean functions for finding whether a value <is> an empty * instance of a type. */ /** * Class of static boolean functions for determining non-canonical subtypes in * JavaScript, such as reals or signable 'numbers'. * * @example * isType.num(1/0) // => false * isType.int(3.0) // => true * isType.real(3) // => true * isType.err(NaN) // => false */ /** * Returns the type of an unknown value as a lower-cased string. Type 'number' * is defined separately from 'infinity' and 'nan' because most functions on * numbers don't expect those types. * * @example * typeName(new Map) // => 'map' * typeName(void f()) // => 'undefined' * typeName(null) // => 'null' * typeName([]) // => 'array' * typeName({}) // => 'object' * typeName(/\s+/) // => 'regexp' * typeName(0 / 0) // => 'nan' * typeName(-1 / 0) // => 'infinity' * * const buffer = new ArrayBuffer(16) * const view = new Int16Array(buffer) * const err = new Error() * * typeName(err) // => 'error' * typeName(buffer) // => 'arraybuffer' * typeName(view) // => 'int16view' */
Async
/** * Promisified setTimeout that resolves to `<any>` or `<void>`. * @example * // logs 'hi' after delay * let hi = async () => await wait(4000, 'hi') * .then(console.log) */