Shuutils
My collection of mostly pure JS utils functions
Install
npm install shuutils
Use
import { getTimestamp } from 'shuutils'
// or in commonjs, on node >=22.12 : const { getTimestamp } = require('shuutils')
console.log('Hello world, current timestamp is', getTimestamp())
Bundler plugin
shuutils/plugins exposes uniqueMark, a plugin for vite, rolldown, rollup and tsdown that injects a mark like 4.2.0 - 123abc45 - 01/01/2021 12:00:00 (package.json version, git commit, build date) in the built files :
import { uniqueMark } from 'shuutils/plugins'
export default defineConfig({
plugins: [uniqueMark()],
})
Put one of these placeholders in your sources : __unique-mark__, {{ unique-mark }}, <span id="unique-mark"></span> or <meta name="unique-mark" content="">.
By default, js and css files also get a /* unique-mark : ... */ first line, use uniqueMark({ banner: false }) to skip it, or uniqueMark({ placeholder: 'my-mark' }) to use another placeholder.
In JSX, keep the placeholder element empty, like <span id="unique-mark" /> : text inside an element that also has an object or template literal prop, like style={{ ... }}, keeps its text instead of the mark.
List uniqueMark() before any plugin that hashes the built files, like a subresource integrity plugin, so the hashes match the marked content.
The same marking is available after the build through the unique-mark cli : npx unique-mark dist, it targets every js, css and html file in a folder, or a glob like "dist/*.js", skips files that are not utf8 text, and fails when no mark was injected.
Migrating from v11
v12 is esm only and requires Node >=22.12.0, where require('shuutils') still works through require(esm).
Tools that replace node's own require, like Jest in its default commonjs mode, cannot load it : switch them to esm mode, or move to Vitest.
Boolean flags moved into an options object with plain names :
v11 |
v12 |
|---|---|
objectSerialize(object, true, true) |
objectSerialize(object, { sortKeys: true, indent: true }) |
objectEqual(a, b, true), objectSum(object, true) |
objectEqual(a, b, { sortKeys: true }), objectSum(object, { sortKeys: true }) |
stringify(data, true) |
stringify(data, { indent: true }) |
readableTime(ms, false) |
readableTime(ms, { short: true }) |
dateToIsoString(date, true) |
dateToIsoString(date, { removeTimezone: true }) |
sanitize(text, false) |
sanitize(text, { lowercase: false }) |
capitalize(text, true) |
capitalize(text, { lowercaseRest: true }) |
link(classes, content, href, true) |
link(classes, content, href, { newTab: true }) |
copyToClipboard(stuff, true) |
copyToClipboard(stuff, { log: true }) |
readClipboard(true) |
readClipboard({ log: true }) |
waitToDetect(selector, 200, 0, 10) |
waitToDetect(selector, { wait: 200, maxTries: 10 }) |
createAnalytics({ willLog: true, willPile: true }) |
createAnalytics({ log: true, pile: true }) |
A v11 call that still passes a boolean is not rejected, the boolean is silently ignored and the defaults apply, so check every call above.
findOne returns undefined instead of null when nothing matches.
parseJson and objectDeserialize keep a serialized function as plain data, use parseJson(json, { functions: true }) to revive it, only on a json you trust.
Migrating from v10
v11 removes a few top-level exports. The functionality is still there, just reached differently :
| Removed in v11 | Use instead |
|---|---|
ok, err, unwrap, trySafe |
Result.ok, Result.err, Result.unwrap, Result.trySafe |
expectType, expectEqualTypes |
dropped, use your test runner's own type assertions |
cn, genClass |
dropped |
access, jsonStartRegex |
dropped |
Behavior changes :
sanitizeandslugifynow treat.as a separator, soslugify('example.com')returnsexample-comwherev10returnedexamplecomparseJsonreturns aResult: checkresult.ok, then readresult.valueorresult.error, wherev10returned{ error, value }with an emptyerroron successisRecordonly accepts plain objects, like{}orObject.create(null), wherev10also accepted functions,Map,Set,Dateand class instances, so deep merges no longer walk into themisBrowserEnvironmentreturnsfalsein headless browsers and whennavigatoris missing, wherev10returnedtrueand threw
Thanks
- Angus Croll : for the inspiration of some dependency free functions
- Box icon : nice icon
- Boxy Svg : simple & effective svg editor
- Bun : super fast runtime for JavaScript and TypeScript
- Github : for all their great work year after year, pushing OSS forward
- Knip : super tool to find & fix problems
- Oxc : a lovely super-fast collection of JavaScript tools written in Rust
- Oxlint : super fast linter and formatter
- Jakub Juszczak's article: nice article about publishing on npm
- Josh Crowther's repo: great ressource about modules
- Shields.io : for the nice badges on top of this readme
- Svg Omg : the great king of svg file size reduction
- Tsdown : elegant library bundler powered by rolldown <3
- V8 : simple & effective cli for code coverage
- Vite : super fast frontend tooling
- Vitest : super fast vite-native testing framework
- Yoctocolors : for the extra-lightweight color utilities
Contributors
Thanks goes to these wonderful people :
Romain Racamier |
babforlife |
jordan boyer |
This project follows the all-contributors specification. Contributions of any kind welcome!