Minimalist Utility Library (ul)
ul is a minimalist JavaScript utility library, currently at version 5.2.16, providing essential object manipulation functions such as one-level merging (`merge`), deep merging (`deepMerge`), and deep cloning (`clone`), alongside a cross-platform helper for retrieving the user's home directory (`home`). Its release cadence is primarily focused on documentation updates and maintainer support links, indicating a stable and mature project rather than one under rapid feature development. Key differentiators include its small footprint and straightforward API for common utility tasks, making it suitable for projects that prefer to avoid larger utility libraries like Lodash for basic operations.
Common errors
-
TypeError: Ul.deepMerge is not a function
cause Attempting to destructure utility functions like `deepMerge` directly from the `ul` package (e.g., `import { deepMerge } from 'ul'`) when they are properties of the default `Ul` object.fixEnsure you import the main `Ul` object first and then access its methods: `import Ul from 'ul'; const merged = Ul.deepMerge(obj1, obj2);` -
ReferenceError: Ul is not defined
cause The `ul` package was not correctly imported or required, or the variable `Ul` was not assigned the module's export.fixIn CommonJS, use `const Ul = require('ul');`. In ESM, use `import Ul from 'ul';` at the top of your file.
Warnings
- gotcha The `Ul.merge()` function performs a shallow, one-level merge. It will only merge top-level properties and will not deeply merge nested objects. This can lead to unexpected results if deep merging is intended.
- gotcha When using `Ul.deepMerge()`, properties with a `null` value in the destination object (`dst`) will *not* be overwritten by non-null values from the source object (`src`). This deviates from some other merge utilities that treat `null` as 'empty' and would overwrite it.
- gotcha The `ul` library is primarily a CommonJS module. While it can often be imported using ESM syntax (`import Ul from 'ul';`) in modern Node.js environments, there is no explicit `type: 'module'` in its `package.json` or dual CommonJS/ESM packaging. This could lead to issues in stricter ESM contexts or older bundlers.
Install
-
npm install ul -
yarn add ul -
pnpm add ul
Imports
- Ul
const Ul = require('ul');import Ul from 'ul';
- Ul.deepMerge
import { deepMerge } from 'ul';import Ul from 'ul'; const mergedObject = Ul.deepMerge(obj1, obj2);
- Ul.home
import { home } from 'ul';import Ul from 'ul'; const userHomeDir = Ul.home();
Quickstart
import Ul from 'ul';
// Input data
let obj = {
n: null,
v: 1
};
, def = {
n: 1,
v: 10,
a: 20
};
, tmp = null;
// Merge the two objects and store the result in tmp
console.log('Deep Merge Result:', tmp = Ul.deepMerge(obj, def));
// Expected output: { n: null, v: 1, a: 20 }
// Illustrates how `null` values in `obj` are preserved even if `def` has a non-null value.
// Clone the tmp object -- the clone will have a different reference
const clonedTmp = Ul.clone(tmp);
console.log('Cloned Object:', clonedTmp);
console.log('Is clone identical to original reference?', tmp === clonedTmp);
// Expected output: false (demonstrates deep cloning creates a new reference)
// Show the absolute path to the home directory
console.log('Home Directory (function call):', Ul.home());
console.log('Home Directory (property access):', Ul.HOME_DIR);
// Path will vary by OS, e.g., /home/user or C:\Users\user
// One level merge
console.log('One-Level Merge Result:', Ul.merge({
foo: {
bar: 42
}
}, {
foo: {
bar: 1,
baz: 7
}
}));
// Expected output: { foo: { bar: 42 } } (demonstrates shallow merge behavior, inner 'foo' object from first arg is preserved)