Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with `@sv443-network/userutils` and `@sv443-network/djsutils`, but can be used independently as well.
npm install @sv443-network/coreutils@sv443-network/userutils and @sv443-network/djsutils, but can be used independently as well.- Array:
- š£ function randomItem() - Returns a random item from the given array
- š£ function randomItemIndex() - Returns a random array item and index as a tuple
- š£ function randomizeArray() - Returns a new array with the items in random order
- š£ function takeRandomItem() - Returns a random array item and mutates the array to remove it
- š£ function takeRandomItemIndex() - Returns a random array item and index as a tuple and mutates the array to remove it
- š· type NonEmptyArray - Non-empty array type
- Colors:
- š£ function darkenColor() - Darkens the given color by the given percentage
- š£ function hexToRgb() - Converts a hex color string to an RGB object
- š£ function lightenColor() - Lightens the given color by the given percentage
- š£ function rgbToHex() - Converts an RGB object to a hex color string
- Crypto:
- š£ function abtoa() - Converts an ArrayBuffer to a string
- š£ function atoab() - Converts a string to an ArrayBuffer
- š£ function compress() - Compresses the given string using the given algorithm and encoding
- š£ function decompress() - Decompresses the given string using the given algorithm and encoding
- š£ function computeHash() - Computes a string's hash using the given algorithm
- š£ function randomId() - Generates a random ID of the given length
- DataStore: - Cross-platform, general-purpose, sync/async hybrid, JSON-serializable database infrastructure:
- š§ class DataStore - The main class for the data store
- š· type DataStoreOptions - Options for the data store
- š· type DataMigrationsDict - Dictionary of data migration functions
- š· type DataStoreData - The type of the serializable data
- š§ class DataStoreSerializer - Serializes and deserializes data for multiple DataStore instances
- š· type DataStoreSerializerOptions - Options for the DataStoreSerializer
- š· type LoadStoresDataResult - Result of calling loadStoresData()
- š· type SerializedDataStore - Meta object and serialized data of a DataStore instance
- š· type StoreFilter - Filter for selecting data stores
- š§ class DataStoreEngine - Base class for DataStore storage engines, which handle the data storage
- š· type DataStoreEngineDSOptions - Reduced version of DataStoreOptions
- Storage Engines:
- š§ class BrowserStorageEngine - Storage engine for browser environments (localStorage, sessionStorage)
- š· type BrowserStorageEngineOptions - Options for the browser storage engine
- š§ class FileStorageEngine - File-based storage engine for Node.js and Deno
- š· type FileStorageEngineOptions - Options for the file storage engine
- Debouncer:
- š£ function debounce() - Function wrapper for the Debouncer class
- š§ class Debouncer - Class that manages listeners whose calls are rate-limited
- š· type DebouncerType - The triggering type for the debouncer
- š· type DebouncedFunction - Function type that is returned by the debounce() function
- š· type DebouncerEventMap - Event map type for the Debouncer class
- Errors:
- š§ class DatedError - Base error class with a date property
- š§ class ChecksumMismatchError - Error thrown when two checksums don't match
- š§ class CustomError - Custom error with a configurable name for one-off situations
- š§ class MigrationError - Error thrown in a failed data migration
- š§ class ValidationError - Error while validating data
- Math:
- š£ function bitSetHas() - Checks if a bit is set in a bitset
- š£ function clamp() - Clamps a number between a given range
- š£ function digitCount() - Returns the number of digits in a number
- š£ function formatNumber() - Formats a number to a string using the given locale and format identifier
- š· type NumberFormat - Number format identifier
- š£ function mapRange() - Maps a number from one range to another
- š£ function overflowVal() - Makes sure a number is in a range by over- & underflowing it
- š£ function randRange() - Returns a random number in the given range
- š£ function roundFixed() - Rounds the given number to the given number of decimal places
- š£ function valsWithin() - Checks if the given numbers are within a certain range of each other
- Misc:
- š£ function consumeGen() - Consumes a ValueGen object
- š· type ValueGen - A value that can be either type T, or a sync or async function that returns T
- š£ function consumeStringGen() - Consumes a StringGen object
- š· type StringGen - A value that can be either of type string, or a sync or async function that returns a string
- š£ function fetchAdvanced() - Wrapper around fetch() with options like a timeout
- š· type FetchAdvancedOpts - Options for the fetchAdvanced() function
- š£ function getListLength() - Returns the length of a ListLike object
- š· type ListLike - Any value with a quantifiable length, count or size property
- š£ function pauseFor() - Pauses async execution for the given amount of time
- š£ function pureObj() - Applies an object's props to a null object (object without prototype chain) or just returns a new null object
- š£ function setImmediateInterval() - Like setInterval(), but instantly calls the callback and supports passing an AbortSignal
- š£ function setImmediateTimeoutLoop() - Like a recursive setTimeout() loop, but instantly calls the callback and supports passing an AbortSignal
- š£ function scheduleExit() - Schedules a process exit after the next event loop tick, to allow operations like IO writes to finish.
- NanoEmitter:
- š§ class NanoEmitter - Simple, lightweight event emitter class that can be used in both FP and OOP, inspired by EventEmitter from node:events, based on nanoevents
- š· type NanoEmitterOptions - Options for the NanoEmitter class
- Text:
- š£ function autoPlural() - Turns the given term into its plural form, depending on the given number or list length
- š£ function capitalize() - Capitalizes the first letter of the given string
- š£ function createProgressBar() - Creates a progress bar string with the given percentage and length
- ⬠const defaultPbChars - Default characters for the progress bar
- š· type ProgressBarChars - Type for the progress bar characters object
- š£ function joinArrayReadable() - Joins the given array into a string, using the given separators and last separator
- š£ function secsToTimeStr() - Turns the given number of seconds into a string in the format (hh:)mm:ss with intelligent zero-padding
- š£ function truncStr() - Truncates the given string to the given length
- Misc. Types:
- š· type LooseUnion - A union type that allows for autocomplete suggestions as well as substitutions of the same type
- š· type ListLike - Any value with a quantifiable length, count or size property
- š· type Newable - Any class reference that can be instantiated with new
- š· type NonEmptyArray - Non-empty array type
- š· type NonEmptyString - String type with at least one character
- š· type NumberFormat - Number format identifier
- š· type Prettify - Makes the structure of a type more readable by fully expanding it (recursively)
- š· type SerializableVal - Any value that can be serialized to JSON
- š· type StringGen - A value that can be either of type string, or a sync or async function that returns a string
- š· type ValueGen - A value that can be either the generic type T, or a sync or async function that returns T
- š· type Stringifiable - Any value that can be implicitly converted to a string
> [!NOTE]
> š£ = function
> š§ = class
> š· = type
> ⬠= const
bash
npm i @sv443-network/coreutils
pnpm i @sv443-network/coreutils
yarn add @sv443-network/coreutils
npx jsr install @sv443-network/coreutils
deno add jsr:@sv443-network/coreutils
`
- If you are in a DOM environment, you can include the UMD bundle using your favorite CDN (after inserting the version number):
`html
`
- Then, import parts of the library as needed:
`ts
// >> EcmaScript Modules (ESM):// - import parts of the library:
import { randomItem } from "@sv443-network/coreutils";
// - or import the full library:
import * as CoreUtils from "@sv443-network/coreutils";
// - or import raw TS files, after installing via JSR:
import { DataStore } from "jsr:@sv443-network/coreutils/lib/DataStore.ts";
// >> CommonJS (CJS):
// - import parts of the library:
const { debounce } = require("@sv443-network/coreutils");
// - or import the full library:
const CoreUtils = require("@sv443-network/coreutils");
// >> Universal Module Definition (UMD):
// - to make the global variable
CoreUtils available, import this file:
// "@sv443-network/coreutils/dist/CoreUtils.min.umd.js"
// - or import the library on your HTML page:
//
// (make sure to insert the version number above. Use 'latest' (not recommended) or a specific version, e.g. '9.4.3')
``Made with ā¤ļø by Sv443
If you like this userscript, please consider supporting the development
Ā© 2025 Sv443 & Sv443 Network
MIT license