dbl-utils is a JavaScript/TypeScript utility library designed to simplify common application development tasks. This collection includes functions for event handling, queue processing, text manipulation, and date and currency formatting, among others.
You can install dbl-utils via npm:
npm install dbl-utils
Or using yarn:
yarn add dbl-utils
Import the modules and functions in your project as needed. Below is a basic example of how to use dbl-utils:
// Import individual functions
import { formatCurrency, flatten, t } from 'dbl-utils';
// Use functions
console.log(formatCurrency(1234.56)); // Example of currency formatting
console.log(flatten({ a: { b: { c: 1 } } })); // Converts a nested object into a flat object
Minimal example of the CLI logic for CSS purging:
import purgeCss from 'dbl-utils/src/purge-css';
purgeCss('styles.css', 'index.html', 'clean.css');
Imports directly from any file to don't include all:
CommonJS use /dist/cjs/*
// Import individual functions
const i18n = require('dbl-utils/dist/cjs/i18n');
// Use functions
console.log(i18n.formatCurrency(1234.56));
ESM use /dist/esm/*
// Import individual functions
import t, { formatCurrency } from 'dbl-utils/dist/esm/i18n';
// Use functions
console.log(i18n.formatCurrency(1234.56));
TypeScript use /src/*
// Import individual functions
import t, { formatCurrency } from 'dbl-utils/src/i18n';
// Use functions
console.log(i18n.formatCurrency(1234.56));
For a complete reference of all available functions, visit the full documentation here.
Below are the main modules available in dbl-utils:
Decouple communication by subscribing to and dispatching custom events.
import { EventHandler } from 'dbl-utils';
const handler = new EventHandler();
handler.subscribe('ping', msg => console.log(msg), 'id'); // listen to events
await handler.dispatch('ping', 'pong'); // emit an event
handler.unsubscribe('ping', 'id'); // stop listening
Deduplicate concurrent HTTP calls so the same request is only made once.
import FetchQueue from 'dbl-utils/src/fetch-queue';
const queue = new FetchQueue(fetch);
const [a, b] = await Promise.all([
queue.addRequest('https://example.com'),
queue.addRequest('https://example.com')
]);
// a === b
Convert nested objects to and from dot notation.
import { flatten, unflatten } from 'dbl-utils';
flatten({ a: { b: 1 } }); // { 'a.b': 1 }
unflatten({ 'a.b': 1 }); // { a: { b: 1 } }
Format numbers, dates, or dictionary entries using locale-aware helpers.
import formatValue from 'dbl-utils/src/format-value';
formatValue(1000, { format: 'currency', currency: 'USD' }); // "$1,000.00"
Manage dictionaries and locale-aware formatting.
import t, { addDictionary, setLang, formatDate } from 'dbl-utils';
addDictionary({ es: { hello: 'Hola' } });
setLang('es');
t('hello'); // 'Hola'
formatDate(); // date formatted in Spanish
Combine and transform objects without modifying the originals.
import { deepMerge, mergeWithMutation, transformJson } from 'dbl-utils';
deepMerge({}, { a: 1 }, { b: 2 }); // merge nested structures
await mergeWithMutation({ a: { b: 1 } }, { mutation: () => ({ c: 2 }) }); // async mutation
transformJson({ a: { b: 1 } }, { filter: 'a' }); // extract subset
Advanced reference resolution system for dynamic object composition and template inheritance. Perfect for configuration management, content templating, and complex data transformations.
1. Global References
const config = { api: { url: "https://api.com", key: "secret" } };
const settings = {
endpoint: "$api/url", // Direct reference โ "https://api.com"
auth: "Bearer ${api/key}", // String interpolation โ "Bearer secret"
full: "${api/url}/v1/users" // Mixed interpolation โ "https://api.com/v1/users"
};
2. Template Inheritance
const data = {
// Base template
userTemplate: { name: "Guest", role: "viewer", active: false },
// Inherit and override properties
admin: { ref: "$userTemplate", name: "Admin", role: "admin", active: true },
john: { ref: "$userTemplate", name: "John Doe" }
};
const result = resolveRefs(data);
// result.admin = { name: "Admin", role: "admin", active: true }
// result.john = { name: "John Doe", role: "viewer", active: false }
3. Relative References with Template System
const data = {
user1: { ref: "$templates/user", firstName: "Alice", lastName: "Smith" },
user2: { ref: "$templates/user", firstName: "Bob", lastName: "Jones" },
templates: {
user: {
firstName: "",
lastName: "",
".": {
// These resolve AFTER template merging
fullName: "${./firstName} ${./lastName}", // Relative interpolation
email: "${./firstName}.${./lastName}@company.com", // Complex relative refs
profile: {
displayName: "$./fullName", // Reference to another relative ref
initials: "${./firstName/0}${./lastName/0}" // Access array/string indices
}
}
}
}
};
const result = resolveRefs(data);
// result.user1.fullName = "Alice Smith"
// result.user1.email = "Alice.Smith@company.com"
// result.user1.profile.displayName = "Alice Smith"
// result.user1.profile.initials = "AS"
Configuration Management
const config = {
// Environment settings
env: {
name: "production",
domain: "myapp.com",
apiVersion: "v2"
},
// Service definitions using templates
services: {
auth: { ref: "$templates/service", path: "/auth", timeout: 5000 },
users: { ref: "$templates/service", path: "/users", port: 8080 },
payments: { ref: "$templates/service", path: "/payments", ssl: true }
},
templates: {
service: {
path: "/",
port: 3000,
timeout: 30000,
ssl: false,
".": {
// Dynamic URL generation
baseUrl: "http${./ssl ? 's' : ''}://${env/domain}:${./port}",
fullUrl: "${./baseUrl}/${env/apiVersion}${./path}",
// Service configuration
config: {
endpoint: "$./fullUrl",
timeout: "$./timeout",
retries: 3,
headers: {
"X-Service-Name": "${env/name}",
"X-API-Version": "${env/apiVersion}"
}
}
}
}
}
};
const result = resolveRefs(config);
// result.services.auth.fullUrl = "http://myapp.com:3000/v2/auth"
// result.services.users.fullUrl = "http://myapp.com:8080/v2/users"
// result.services.payments.fullUrl = "https://myapp.com:3000/v2/payments"
Custom Rules and Tasks
const data = {
users: ["Alice", "Bob", "Charlie"],
products: [{ name: "Widget", price: 10 }, { name: "Gadget", price: 25 }]
};
const template = {
welcomeMessage: "$greetUsers",
productSummary: "$summarizeProducts",
totalValue: "$calculateTotal"
};
// Define custom processing rules
const rules = {
"$greetUsers": ["join", "$users", " and "],
"$summarizeProducts": ["iterate", "$products", "product"],
"$calculateTotal": ["sum", "$products", "price"]
};
// Custom task implementations
const extraTasks = {
sum: (array: any[], property: string) =>
array.reduce((total, item) => total + item[property], 0),
format: (template: string, ...values: any[]) =>
template.replace(/{(\d+)}/g, (_, i) => values[i]),
conditional: (condition: boolean, trueVal: any, falseVal: any) =>
condition ? trueVal : falseVal
};
const result = resolveRefs(template, data, rules, extraTasks);
// result.welcomeMessage = "Alice and Bob and Charlie"
// result.totalValue = 35
iterate(array, itemName) - Process array items with current schema contextjoin(values, separator, ...extras) - Join arrays or concatenate valuesignore(path, fallback) - Safe property access with fallback valuesif(condition, trueValue, falseValue) - Conditional value resolutionresolveRefs<T>(
object: ResolvableValue, // Object/array with references to resolve
schema?: Record<string, any>, // Base schema for global references
rules?: ResolveRefsRules, // Custom rule definitions
extraTasks?: ResolveRefsTasks // Additional task implementations
): T
templates/, definitions/)config/database/url over abbreviated versionsFor comprehensive examples and advanced use cases, see the detailed examples documentation.
Split an array into smaller arrays for batching.
import { sliceIntoChunks } from 'dbl-utils';
sliceIntoChunks([1,2,3,4],2); // [[1,2],[3,4]]
Turn a list of strings into unique tokens.
import { splitAndFlat } from 'dbl-utils';
splitAndFlat(['a b','c']); // ['a','b','c']
Generate distinct colors for visualizations.
import { generateRandomColors } from 'dbl-utils';
generateRandomColors(2); // ['#aabbcc', '#ddeeff']
Check how close colors are to each other.
import { evaluateColorSimilarity } from 'dbl-utils';
evaluateColorSimilarity(['#fff','#ffe']); // value near 1
Remove accents and lowercase text.
import { normalize } from 'dbl-utils';
normalize('รก'); // 'a'
Create URL-friendly slugs.
import { slugify } from 'dbl-utils';
slugify('Hello World'); // 'hello-world'
Produce a short hex segment.
import { randomS4 } from 'dbl-utils';
randomS4(); // '9f3b'
Generate a random alphanumeric string.
import { randomString } from 'dbl-utils';
randomString(5); // e.g., 'abcde'
Build time intervals between two dates.
import { timeChunks } from 'dbl-utils';
timeChunks({ from:'2020-01-01', to:'2020-01-02', step:3600 }); // [...]
Pause execution for a given time.
import { delay } from 'dbl-utils';
await delay(10); // waits 10ms
Generate a numeric hash.
import { hash } from 'dbl-utils';
hash('data'); // 123456789
Deterministic pseudo-random number generator.
import { LCG } from 'dbl-utils';
const gen = new LCG(123);
gen.random(); // 0.5967...
For a detailed description of each module and function, visit the full documentation automatically generated with Typedoc. The documentation includes usage examples and in-depth explanations of each function.
For an in-depth analysis of the project architecture, codebase structure, and implementation details, check out the comprehensive DeepWiki analysis.
Run the test suite with:
npm test
This project uses Jest with ts-jest. New tests cover the i18n and object-mutation modules.
$./ and ${./} syntax
"." key for relative referencesunflatten so arrays are reconstructed correctly.i18n and object-mutation modules.utils, format-value, and i18n.When developing or debugging tests, use yarn test -- <pattern> to run only the relevant test cases.
This project is under the ISC license. See the LICENSE file for more details.