@arpansaha13/utils
v0.8.0
Published
A personal collection of utils.
Downloads
19
Maintainers
Readme
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
- Alias:
cn
A shorthand for twMerge(clsx(...classes))
.
classNames('mx-auto max-w-7xl p-4', primary ? 'bg-indigo-600 text-white' : 'bg-transparent text-gray-900')
Note:
clsx
is imported from clsx/lite.
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?
clamp
Returns a number whose value is limited to the given range.
Source: Stack Overflow - What's the most elegant way to cap a number to a segment?
truncate
Truncate a long string to a specific number of characters.
hasLowerCase
Check whether a string has a lowercase alphabet or not.
Source: Stack Overflow - JavaScript - checking for any lowercase letters in a string
uint8ArrayToJson
Convert Uint8Array readable stream to json.
Source: Stack Overflow - Retrieve data from a ReadableStream object?
sanitize
Sanitize HTML by escaping special characters in a string.
nFormatter
Format a large number by adding suffixes like "k" and "M".
Source: Stack Overflow - Format a number as 2.5K if a thousand or more, otherwise 900
getOrdinalSuffix
Returns the appropriate ordinal suffix for that day (e.g., "st" for 1, "nd" for 2, "rd" for 3, and "th" for the rest).
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', { ... })
createEmptyFile
Creates an empty file. If the filePath
already exists, an error is thrown.
Source: Stack Overflow - Create an empty file in Node.js?
createEmptyFile('path/to/file')