Vandium Utils
Vandium Utils is a JavaScript utility library providing a collection of common, reusable functions primarily intended for use within Vandium-io projects, though suitable for general Node.js applications. Currently at stable version 2.0.0, the library offers functionalities such as deep object cloning, date-to-ISO string conversion, various type-checking predicates (e.g., `isArray`, `isFunction`, `isObject`, `isPromise`), null/undefined checks, object emptiness determination, boolean parsing from diverse inputs, and a robust string templating engine. The library maintains a steady release cadence driven by the needs of the Vandium ecosystem. Its key differentiators include a focused set of utilities designed for serverless environments (given its association with AWS Lambda keywords), lightweight footprint, and built-in TypeScript definitions, promoting type-safe development. It supports Node.js versions 10.16 and above.
Common errors
-
TypeError: (0 , vandium_utils_1.templateString) is not a function
cause This error typically occurs when using CommonJS `require` syntax (e.g., `const { templateString } = require('vandium-utils');`) in an environment configured for ES Modules, or when attempting to import a non-existent or incorrectly named export.fixEnsure your module context matches your import statement. For ESM, use `import { templateString } from 'vandium-utils';`. If in CJS, verify the export name and ensure the path is correct. -
ReferenceError: require is not defined
cause Attempting to use the CommonJS `require()` function within an ECMAScript Module (ESM) file, where `import` statements are expected.fixConvert `require` statements to `import` statements: `const { funcName } = require('pkg');` becomes `import { funcName } from 'pkg';`. Ensure your `package.json` has `"type": "module"` if you intend to use ESM, or use a `.mjs` file extension.
Warnings
- breaking While specific breaking changes from v1 to v2 are not extensively documented in the README excerpt, major version bumps often introduce API changes. Developers should consult the full change log or release notes when migrating from `vandium-utils@1.x` to `2.x` to identify potential breaking changes in function signatures or behavior.
- gotcha The `clone` function performs a deep clone for objects and arrays. However, if the input `value` is not an object or `null`, the function simply returns the existing value without modification. This can be surprising if expecting a new instance for all data types.
- gotcha The `parseBoolean` function has specific string values it recognizes as `true` or `false` (case-insensitive: "on", "yes", "true", "off", "no", "false"). Any other string or non-boolean value that doesn't match these specific strings will resolve to `false` (after internal string conversions like `String(value).toLowerCase()`), which might not be the expected boolean logic for all arbitrary inputs.
Install
-
npm install vandium-utils -
yarn add vandium-utils -
pnpm add vandium-utils
Imports
- templateString
const templateString = require('vandium-utils').templateString;import { templateString } from 'vandium-utils'; - isArray
import isArray from 'vandium-utils';
import { isArray } from 'vandium-utils'; - clone
import * as VandiumUtils from 'vandium-utils';
const { clone } = require('vandium-utils');
Quickstart
import { templateString, isObjectEmpty, clone } from 'vandium-utils';
// Example 1: Basic string templating
const user = { name: 'Alice', city: 'Wonderland' };
const greeting = templateString('Hello ${name} from ${city}!', user);
console.log(greeting); // Expected: "Hello Alice from Wonderland!"
// Example 2: Checking for empty objects
const emptyObj = {};
const filledObj = { id: 1 };
console.log(`Is emptyObj empty? ${isObjectEmpty(emptyObj)}`); // Expected: "Is emptyObj empty? true"
console.log(`Is filledObj empty? ${isObjectEmpty(filledObj)}`); // Expected: "Is filledObj empty? false"
// Example 3: Cloning an object
const originalObject = { a: 1, b: { c: 2 } };
const clonedObject = clone(originalObject);
clonedObject.b.c = 99;
console.log('Original object after clone modification:', originalObject); // Expected: { a: 1, b: { c: 2 } }
console.log('Cloned object:', clonedObject); // Expected: { a: 1, b: { c: 99 } }