@arpansaha13/utils v0.6.0
Utilities
This package is a compilation of some utility functions that might be needed in general. Some of them are taken from different documentations and articles, and then modifed.
Published as @arpansaha13/utils
on npm.
General utilities
classNames
Specifically made for appending classNames in React.
classNames('mx-auto max-w-7xl p-4', primary ? 'bg-indigo-600 text-white' : 'bg-transparent text-gray-900')
deepFreeze
Recursively freeze each non-primitive property of an object.
Source: MDN web docs - Object.freeze()
isNullOrUndefined
Check if a value is null
or undefined
.
if (!isNullOrUndefined(variable)) {
// do something
}
random
Generates a random number between the given min and max values.
Source: MDN web docs - Math.random()
random(5, 10)
slugify
Source: Dev.to - 10 Helpful JavaScript Utility Functions
slugify('Hello, World!')
// Expected output: "hello-world"
slugify('Hello, Universe!', '_')
// Expected output: "hello_universe"
trim
Removes the extra spaces in between a string along with the leading and trailing white space and line terminator characters.
Source: Stack Overflow - How to remove the extra spaces in a string?
Browser utilities
These utility functions are only meant to be used in the browser and will raise error if used in node.js environment.
getCookie
Returns the value of the cookie if it exists, else returns an empty string.
isTouchDevice
Detect whether the device has a touch screen.
Source: Stack Overflow - What's the best way to detect a 'touch screen' device using JavaScript?
Node utilities
These utility functions are only meant to be used in node.js environment and will raise error if used in the browser environment.
readJsonFile
A shorthand for JSON.parse(await readFile(path, 'utf8'))
.
readJsonFile('path/to/file')
writeJsonFile
A shorthand for writeFile(path, JSON.stringify(json, null, 2))
.
writeJsonFile('path/to/file', { ... })