moxules
v1.1.0
Published
Modules for m-ox
Readme
moxules
Tiny modules from m.ox. Moxules, if you will.
This package is a small handful of utility functions for the moments when your code needs a little polish, a little color, or a little less accidental whitespace.
Install
npm install moxulesUsage
import {
chunk,
clamp,
hex,
pascalToHuman,
stripDoubleSpaces
} from 'moxules'
hex()
// "#3fa91c"
pascalToHuman('TinyUsefulModule')
// "Tiny Useful Module"
stripDoubleSpaces('easy there, spacebar')
// "easy there, spacebar"
chunk([1, 2, 3, 4], 2)
// [[1, 2], [3, 4]]
clamp(12, 0, 10)
// 10API
Arrays
chunk(items, size)splits an array into smaller arrays.compact(items)removesnullandundefined.unique(items)removes duplicate values.sample(items)returns one random item, orundefinedfor an empty array.
Design
hex()returns a random six-digit hex color string.isHexColor(value)checks whether a string is a valid 3- or 6-digit hex color.hexToRgb(value)converts a hex color to{ red, green, blue }, ornullif invalid.rgbToHex(red, green, blue)converts RGB channels to a hex color string.
Numbers
clamp(value, min, max)keeps a number inside a range.randomInt(min, max)returns a random integer between two bounds, inclusive.roundTo(value, places)rounds a number to a fixed number of decimal places.sum(values)adds an array of numbers.
Strings
pascalToHuman(value)turns PascalCase into a human-readable title.stripDoubleSpaces(value)collapses runs of regular spaces into one space.capitalize(value)uppercases the first character.slugify(value)converts text into a lowercase URL-style slug.normalizeWhitespace(value)collapses all whitespace runs and trims the result.truncate(value, maxLength)shortens text and adds...when there is room.
hex()
Returns a random six-digit hex color string.
hex() // "#a0f4c9"pascalToHuman(value)
Turns PascalCase into a human-readable title with spaces.
pascalToHuman('PascalCaseTitle') // "Pascal Case Title"stripDoubleSpaces(value)
Collapses accidental runs of regular spaces into a single space.
stripDoubleSpaces('too much room') // "too much room"License
ISC
