CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-intlify--shared

Shared utility package for intlify project providing performance tools, string formatting, event emitters, and type checking utilities.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

@intlify/shared

A 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.

Package Information

  • Package Name: @intlify/shared
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @intlify/shared

Core Imports

import {
  // 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");

Basic Usage

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>");

Architecture

@intlify/shared is organized into several key functional areas:

  • Type System: Comprehensive type checking utilities covering all JavaScript types
  • String Processing: Formatting, escaping, and sanitization functions for secure text handling
  • Event System: Type-safe event emitter implementation with wildcard support
  • Environment Detection: Runtime environment detection and global object access
  • Performance Tools: Development-time performance measurement utilities
  • Warning System: Consistent warning and debugging message display
  • Object Utilities: Object manipulation and deep copying functions

Capabilities

Type Checking Utilities

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;

Type Checking

String Processing

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;

String Processing

Event System

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;
}

Event System

Environment & Performance

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;

Environment & Performance

Object Utilities

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;

Object Utilities

Warning System

Consistent warning and debugging message display system.

function warn(msg: string, err?: Error): void;
function warnOnce(msg: string): void;

Warning System

Core Types

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;
}

docs

environment-performance.md

event-system.md

index.md

object-utilities.md

string-processing.md

type-checking.md

warning-system.md

tile.json