Shared utility package for intlify project providing performance tools, string formatting, event emitters, and type checking utilities.
npx @tessl/cli install tessl/npm-intlify--shared@11.1.0A comprehensive shared utility library for the Intlify internationalization ecosystem, providing performance measurement tools, string formatting functions, event emitter implementations, error handling utilities, and type checking functions. The library serves as a foundational utility package that supports other Intlify projects like Vue I18n.
npm install @intlify/sharedimport {
// Utility functions
isArray, isString, isObject, format, escapeHtml,
// Event system
createEmitter, type Emittable,
// Warning system
warn, warnOnce,
// Environment detection
inBrowser, getGlobalThis
} from "@intlify/shared";For CommonJS:
const {
isArray, isString, isObject, format, escapeHtml,
createEmitter, warn, warnOnce, inBrowser, getGlobalThis
} = require("@intlify/shared");import {
format, isObject, createEmitter, warn, escapeHtml,
type Emittable, type EventHandler
} from "@intlify/shared";
// String formatting with placeholders
const message = format("Hello {name}, you have {count} messages", {
name: "Alice",
count: 5
});
// Type checking
if (isObject(someValue)) {
// Safe to access properties
}
// Event emitter
const emitter = createEmitter<{ userLogin: { userId: string } }>();
emitter.on("userLogin", (payload) => {
console.log(`User logged in: ${payload?.userId}`);
});
emitter.emit("userLogin", { userId: "123" });
// Warning system
warn("This is a warning message");
// HTML escaping for security
const safeHtml = escapeHtml("<script>alert('xss')</script>");@intlify/shared is organized into several key functional areas:
Comprehensive type checking functions for runtime type validation and type guards.
function isArray(val: unknown): val is any[];
function isString(val: unknown): val is string;
function isObject(val: unknown): val is Record<any, any>;
function isFunction(val: unknown): val is Function;
function isBoolean(val: unknown): val is boolean;
function isNumber(val: unknown): val is number;
function isPromise<T = any>(val: unknown): val is Promise<T>;
function isPlainObject(val: unknown): val is object;String formatting, HTML escaping, and sanitization functions for secure text processing.
function format(message: string, ...args: any): string;
function escapeHtml(rawText: string): string;
function sanitizeTranslatedHtml(html: string): string;
function toDisplayString(val: unknown): string;
function join(items: string[], separator?: string): string;Type-safe event emitter implementation with support for wildcard listeners and strongly-typed event payloads.
function createEmitter<Events extends Record<EventType, unknown>>(): Emittable<Events>;
interface Emittable<Events extends Record<EventType, unknown> = {}> {
events: EventHandlerMap<Events>;
on<Key extends keyof Events>(
event: Key | '*',
handler: EventHandler<Events[keyof Events]> | WildcardEventHandler<Events>
): void;
off<Key extends keyof Events>(
event: Key | '*',
handler: EventHandler<Events[keyof Events]> | WildcardEventHandler<Events>
): void;
emit<Key extends keyof Events>(
event: Key,
payload?: Events[keyof Events]
): void;
}Environment detection and performance measurement utilities for cross-platform development.
const inBrowser: boolean;
function getGlobalThis(): any;
let mark: (tag: string) => void | undefined;
let measure: (name: string, startTag: string, endTag: string) => void | undefined;Object creation, property checking, and deep copying utilities.
const assign: typeof Object.assign;
function create(obj?: object | null): object;
function hasOwn(obj: object | Array<any>, key: string): boolean;
function deepCopy(src: any, des: any): void;Consistent warning and debugging message display system.
function warn(msg: string, err?: Error): void;
function warnOnce(msg: string): void;type EventType = string | symbol;
type EventHandler<T = unknown> = (payload?: T) => void;
type WildcardEventHandler<T = Record<string, unknown>> = (
event: keyof T,
payload?: T[keyof T]
) => void;
interface BaseError {
code: number;
}