miau-utilities
v0.0.2
Published
A collection of utilities used by miau for day to day needs.
Downloads
15
Readme
A collection of useful utilities for my various projects, constantly being expanded upon and improved.
Table of Contents
- Description
- Features
- Installation
- Usage
- Utilities
- arrayEmpty
- arrayRandom
- capitalize
- caseCompareString
- caseCompareStrings
- chunk
- coalesce
- getArrayDifferences
- getMethods
- getTimeFromNow
- getType
- getURLParameters
- hasMany
- hasOnlyOne
- inRange
- isAsyncFunc
- isFunc
- isObject
- isObjectEmpty
- isPrime
- mask
- mostPerformant
- objectCompact
- omit
- omitBy
- paginate
- pick
- pickBy
- randomRange
- retry
- retrySync
- takeUntil
- throttle
- timeTaken
- wordWrap
- Utilities
Description
This utilities package is a collection of various helper functions that I have personally designed and foudn useful across my own projects. The goal is to provide a centralized library of commonly used utilities that can be easily imported and utilized for day-to-day needs. Sometimes people create these and they tend to be under-optimized or not well maintained so I strive to keep improving and expanding this collection over time. Providing support for many types of projects and use-cases.
Features
- Written in TypeScript for type safety and modern JavaScript features
- Fully tested
- Comprehensive documentation
Installation
You can install the package via npm:
npm i miau-utilitiesOr via yarn:
yarn add miau-utilitiesUsage
Each individual utility function can be imported from subpaths for example: @miau/utilities/arrayCompact or the entire library
import { arrayCompact, capitalize } from 'miau-utilities'
// or
import { isFunc } from 'miau-utilities/isFunc'Utilities
arrayEmpty
Returns whether the array is empty or not.
console.log(arrayEmpty([])) // true
console.log(arrayEmpty([1, 2, 3])) // falsearrayRandom
Returns a random array element.
console.log(arrayRandom([1, 2, 3, 4, 5])) // Example output: 3
console.log(arrayRandom(['a', 'b', 'c', 'd', 'e'])) // Example output: 'c'
console.log(arrayRandom([{ id: 1 }, { id: 2 }, { id: 3 }])) // Example output: { id: 2 }capitalize
Capitalizes a string so that it will start with a capital letter.
console.log(capitalize('hello world')) // Hello worldcaseCompareString
Checks if an argument string is the same despite the case it is in
console.log(caseCompareString('Hello', 'hello')) // true
console.log(caseCompareString('Apple', 'Banana')) // false
console.log(caseCompareString('grape', 'Grape')) // true
console.log(caseCompareString('orange', 'cheese')) // falsecaseCompareStrings
Checks if all of the argument string(s) are the same despite the case it is in
console.log(caseCompareStrings('Hello', 'hello', 'HELLO')) // true
console.log(caseCompareStrings('Apple', 'Banana', 'apple')) // false
console.log(caseCompareStrings('grape', 'Grape', 'GRAPE')) // true
console.log(caseCompareStrings('orange', 'cheese', 'blueberry')) // falsechunk
Returns a new array that splits an array into multiple arrays of a fixed size
console.log(chunk([1, 2, 3, 4, 5], 2)) // [[1, 2], [3, 4], [5]]
console.log(chunk(['a', 'b', 'c', 'd'], 3)) // [['a', 'b', 'c'], ['d']]
console.log(chunk([true, false, true, false, true], 4)) // [[true, false, true, false], [true]]coalesce
Iterates through the array and finds the first defined, non-null argument
console.log(coalesce([undefined, null, 0, false, 'hello'])) // Output: 0
console.log(coalesce(['first', 'second', null, 'third'])) // Output: 'first'getArrayDifferences
Omits duplicate entries between both arrays and returns an mutated array with both values A & B for entries with differences
console.log(getArrayDifferences(['a', 'b', 'c'], ['a', 'b', 'd'])) // ['c', 'd']getMethods
Takes in an object and returns all the methods and properties attached to that object's type
console.log(getMethods({}))
console.log(getMethods([]))getTimeFromNow
Will retrieve a date representing the time at a point from now
console.log(getTimeFromNow(60000)) // 1 minute agogetType
Gets the constructor type of a variable/object
console.log(getType(1)) // 'Number'
console.log(getType('string')) // 'String'
console.log(getType(true)) // 'Boolean'getURLParameters
Returns a string array containing all the query parameters from a URL
console.log(getURLParameters('https://example.com?param1=value1¶m2=value2')) // [ param1: 'value1', param2: 'value2' ]hasMany
Checks using a predicate more than one element matches the predicate, otherwise false
console.log(hasMany([1, 2, 3], (value) => value % 2 === 0)) // false
console.log(hasMany([1, 1, 2, 3, 4], (value) => value === 1)) // truehasOnlyOne
Checks using a predicate if only one element matches the predicate, otherwise false
console.log(hasOnlyOne([1, 2, 3], (value) => value % 2 === 0)) // true
console.log(hasOnlyOne([1, 1, 2, 3, 4], (value) => value === 1)) // falseinRange
Returns if the number is in the range of a minimum and max number
console.log(inRange(5, 1, 10)) // true
console.log(inRange(0, 1, 10)) // falseisAsyncFunc
Checks if the given object is an asynchronous function
console.log(isAsyncFunc(async () => {})) // true
console.log(isAsyncFunc(function () {})) // falseisFunc
Checks if the given object is a function
console.log(isFunc(async () => {})) // true
console.log(isFunc(function () {})) // true
console.log(isFunc(123)) // falseisObject
Checks if the given object is a plain object (excludes arrays)
console.log(isObject({})) // true
console.log(isObject([])) // false
console.log(isObject(null)) // false
console.log(isObject(42)) // false
console.log(isObject('hello')) // falseisObjectEmpty
Determines whether an object/array has no values or is empty
console.log(isObjectEmpty({})) // true
console.log(isObjectEmpty({ a: 1 })) // false
console.log(isObjectEmpty([])) // trueisPrime
Checks whether a provided number is a prime number or not
console.log(isPrime(7)) // true
console.log(isPrime(10)) // false
console.log(isPrime(13)) // true
console.log(isPrime(1)) // falsemask
Replaces the first num amount of characters with a provided mask to hide the inputted characters
console.log(mask('Hello World', 4, '*')) //****o WorldmostPerformant
Will iterate through multiple functions and return an index of the highest performing function
console.log(
mostPerformant([
function test() {
for (let i = 0; i < 10; i++) {
console.log('HELLO!!!!')
}
},
function test2() {
console.log('HELLO!')
}
], 2) // 1 / Promise { 1 }
)objectCompact
Mutates an object to remove all falsy values.
console.log(objectCompact({ a: 1, b: null, c: 3 })) // { a: 1, c: 3 }
console.log(objectCompact({ a: 2, b: 'wow!', c: 3, d: '' })) // { a: 2, b: 'wow!', c: 3 }omit
Filters an object with all key-value pairs from a provided array omitted from the object
console.log(omit({ a: 1, b: 2, c: 3 }, ['b', 'c'])) // Expected output: { a: 1 }
console.log(omit({ name: 'Alice', age: 30, city: 'New York' }, ['age'])) // Expected output: { name: 'Alice', city: 'New York' }
console.log(omit({ x: 10, y: 20, z: 30 }, [])) // Expected output: { x: 10, y: 20, z: 30 }omitBy
Converts an array into a new array/object that returns everything omitted by the predicate
console.log(omitBy([ 'a', 'b', 'c', 'd', null ], value => value === null)) // [ 'a', 'b', 'c', 'd' ]
console.log(omitBy([ 1, 2, 3, 4, 5, 6 ], value => value % 2 === 0)) // [ 1, 3, 5 ]
console.log(omitBy([ 'apple', 'banana', 'cherry', 'date' ], value => value.startsWith('b'))) // [ 'apple', 'cherry', 'date' ]
console.log(omitBy([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }, { e: 5 }], value => value.a! % 2 === 1, true)) // { '1': { b: 2 }, '2': { c: 3 }, '3': { d: 4 }, '4': { e: 5 } }paginate
Cuts an array into pages and returns both data of an inputted page and the max pages it accepts.
console.log(paginate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, 2)) // [ [5, 6], 5]pick
Filters an object and only picks the key-value pairs from a provided array
console.log(pick({ a: 1, b: 2, c: 3 }, ['a', 'c'])) // { a: 1, c: 3 }
console.log(pick({ a: 1, b: 2, c: 3 }, ['b'])) // { b: 2 }
console.log(pick({ a: 1, b: 2, c: 3 }, ['d'])) // {}pickBy
Converts an array into a new array/object that returns everything picked by the predicate
console.log(pickBy([1, 2, 3, 4, 5], (value) => value % 2 !== 0)) // [ 1, 3, 5 ]
console.log(pickBy([1, 2, 3], (value) => value > 2)) // [ 3 ]
console.log(pickBy([1, 2, 3], (value) => value < 0)) // []
console.log(pickBy([1, 2, 3, 4, 5], (value) => value % 2 === 0, true)) // { '1': 2, '3': 4 }randomRange
Retrieves a random number between 2 provided numbers
console.log(randomRange(1, 10)) // Example output: 7
console.log(randomRange(-5, 5)) // Example output: 1
console.log(randomRange(0, 1)) // Example output: 0
console.log(randomRange(100, 200)) // Example output: 150retry
Retries an async function a specified number of times before throwing the last error encountered.
retry(async () => {
if (Math.random() < 0.7) {
throw new Error('Failed attempt')
}
return 'Success'
}, 5).then(res => console.log(res)) // Example output: 'Success' or throws an error after 5 failed attemptsretrySync
Retries a synchronous function a specified number of times before throwing the last error encountered.
console.log(retrySync(() => {
if (Math.random() < 0.7) {
throw new Error('Failed attempt')
}
return 'Success'
}, 5)) // Example output: 'Success' or throws an error after 5 failed attemptstakeUntil
Takes values from an array until the predicate returns true
console.log(takeUntil([1, 2, 3, 4, 5], (value) => value === 3)) // [1, 2]throttle
Creates a throttled function that only invokes func at most once every wait milliseconds.
const throttled = throttle(() => { console.log('throttled') }, 5000)
throttled() // will log 'throttled'
throttled() // will NOT log anything
throttled() // will NOT log anything
setTimeout(() => { throttled() }, 5000)
throttled.flush() // will reset the throttle timer immediately
throttled() // will log 'throttled'timeTaken
Retrieves the time taken to run a function
console.log(timeTaken(() => { console.log('Hello World!')})) // Example Output: "0.4937999999999647"wordWrap
Produces a new string where there is a new line escape character after every max characters
console.log(wordWrap('Hello World!', 10)) // Expected Output: "Hello\nWorld!"
console.log(wordWrap('This is a test string for word wrapping.', 8)) // Expected Output: "This is\na test\nstring\nfor word\nwrapping."
console.log(wordWrap('Short', 10)) // Expected Output: "Short"
console.log(wordWrap('A verylongwordthatneedstobewrapped', 5, '\n'))