/** * 差異類型列舉 * Difference type enumeration * * @example * ```typescript * import { EnumKinds } from 'deep-diff'; * * console.log(EnumKinds.DiffEdit); // 'E' - 項目已被編輯 * console.log(EnumKinds.DiffNew); // 'N' - 項目為新增 * console.log(EnumKinds.DiffDeleted); // 'D' - 項目已被刪除 * console.log(EnumKinds.DiffArray); // 'A' - 陣列已被修改 * ``` */ export declare const enum EnumKinds { /** 項目已被編輯 / Item was edited */ DiffEdit = "E", /** 項目為新增 / Item is new */ DiffNew = "N", /** 項目已被刪除 / Item was deleted */ DiffDeleted = "D", /** 陣列已被修改 / Array was modified */ DiffArray = "A" } /** * 路徑鍵類型 * Path key type * * 用於表示物件路徑中的鍵可以是字串、數字或 Symbol * Keys in object paths can be strings, numbers, or Symbols */ export type IPathKey = string | number | symbol; /** * 路徑陣列類型 * Paths array type * * 表示從根物件到目標屬性的完整路徑 * Represents the complete path from root object to target property */ export type IPaths = IPathKey[]; /** * 差異節點聯合類型 * Diff node union type * * 包含所有可能的差異類型 * Contains all possible difference types */ export type IDiffNode = DiffNew | DiffDeleted | DiffEdit | DiffArray; /** * 觀察者回調函式類型 * Observer callback function type * * 當計算出差異時調用的回調函式 * Callback function invoked when differences are calculated * * @param diff - 差異記錄 / Difference record */ export type IObserver = (diff: IDiffNode) => void; /** * 預過濾函式類型 * Pre-filter function type * * 用於跳過特定路徑的比較 * Used to skip comparison of specific paths * * @param path - 當前路徑 / Current path * @param key - 當前鍵 / Current key * @returns true 表示跳過 / Returns true to skip */ export type IPreFilterFunction = (path: any[], key: any) => boolean; /** * 預過濾物件介面 * Pre-filter object interface * * 提供 prefilter 和 normalize 兩種選項 * Provides prefilter and normalize two options */ export interface IPreFilterObject { /** * 預過濾函式 * Pre-filter function * * @param path - 當前路徑 / Current path * @param key - 當前鍵 / Current key * @returns true 表示跳過比較 / Returns true to skip comparison */ prefilter?(path: any[], key: any): boolean; /** * 標準化函式 * Normalize function * * 在比較前標準化 lhs 和 rhs 的值 * Normalize lhs and rhs values before comparison * * @param currentPath - 當前路徑 / Current path * @param key - 當前鍵 / Current key * @param lhs - 左側值 / Left-hand-side value * @param rhs - 右側值 / Right-hand-side value * @returns 標準化後的 [lhs, rhs] 或 undefined / Normalized [lhs, rhs] or undefined */ normalize?(currentPath: any, key: any, lhs: LHS, rhs: RHS): [LHS, RHS] | undefined; } /** * 預過濾類型 * Pre-filter type * * 可以是函式形式或物件形式 * Can be function form or object form */ export type IPreFilter = IPreFilterFunction | IPreFilterObject; /** * 過濾器函式類型 * Filter function type * * 用於 applyDiff 中過濾要套用的差異 * Used in applyDiff to filter differences to apply * * @param target - 目標物件 / Target object * @param source - 來源物件 / Source object * @param change - 差異記錄 / Difference record * @returns true 表示要套用此差異 / Returns true to apply this difference */ export type IFilter = (target: LHS, source: RHS, change: IDiffNode) => boolean; /** * 差異基底抽象類別 * Difference base abstract class * * 所有差異類型的基底類別 * Base class for all difference types */ export declare abstract class Diff { kind: EnumKinds; path: IPaths; protected constructor(kind: EnumKinds, path?: IPaths); } /** * 編輯差異類別 * Edit difference class * * 當值被修改時產生(例如:"a" → "b") * Generated when a value is modified (e.g., "a" → "b") * * @example * ```typescript * import { DiffEdit, EnumKinds } from 'deep-diff'; * * const diff = new DiffEdit(['name'], 'Alice', 'Bob'); * console.log(diff.kind); // 'E' * console.log(diff.path); // ['name'] * console.log(diff.lhs); // 'Alice' * console.log(diff.rhs); // 'Bob' * ``` */ export declare class DiffEdit extends Diff { lhs: LHS; rhs: RHS; kind: EnumKinds.DiffEdit; /** * 建構函式 * Constructor * * @param path - 差異路徑 / Difference path * @param lhs - 左側值 (原始值) / Left-hand-side value (original) * @param rhs - 右側值 (新值) / Right-hand-side value (new) */ constructor(path: IPaths, lhs: LHS, rhs: RHS); } /** * 新增差異類別 * New difference class * * 當屬性/元素被新增時產生 * Generated when a property/element is added * * @example * ```typescript * import { DiffNew } from 'deep-diff'; * * const diff = new DiffNew(['email'], 'test@example.com'); * console.log(diff.kind); // 'N' * console.log(diff.rhs); // 'test@example.com' * ``` */ export declare class DiffNew extends Diff { rhs: RHS; kind: EnumKinds.DiffNew; /** * 建構函式 * Constructor * * @param path - 差異路徑 / Difference path * @param rhs - 右側值 (新增的值) / Right-hand-side value (new value) */ constructor(path: IPaths, rhs: RHS); } /** * 刪除差異類別 * Deleted difference class * * 當屬性/元素被刪除時產生 * Generated when a property/element is deleted * * @example * ```typescript * import { DiffDeleted } from 'deep-diff'; * * const diff = new DiffDeleted(['oldField'], 'some value'); * console.log(diff.kind); // 'D' * console.log(diff.lhs); // 'some value' * ``` */ export declare class DiffDeleted extends Diff { lhs: LHS; kind: EnumKinds.DiffDeleted; /** * 建構函式 * Constructor * * @param path - 差異路徑 / Difference path * @param lhs - 左側值 (被刪除的值) / Left-hand-side value (deleted value) */ constructor(path: IPaths, lhs: LHS); } /** * 陣列差異類別 * Array difference class * * 當陣列內的元素被修改/新增/刪除時產生 * Generated when array elements are modified/added/deleted * * @example * ```typescript * import { DiffArray, DiffNew } from 'deep-diff'; * * const diff = new DiffArray(['items'], 2, new DiffNew(['newItem'], 'value')); * console.log(diff.kind); // 'A' * console.log(diff.index); // 2 * console.log(diff.item.kind); // 'N' * ``` */ export declare class DiffArray extends Diff { index: number; item: IDiffNode; kind: EnumKinds.DiffArray; /** * 建構函式 * Constructor * * @param path - 差異路徑 / Difference path * @param index - 陣列索引 / Array index * @param item - 陣列元素的差異 / Array element's difference */ constructor(path: IPaths, index: number, item: IDiffNode); } /** * 取得物件的實際類型 * Get actual type of object * * JavaScript typeof 的擴展,區分 Array、Date、RegExp、Math、null 等特殊類型 * Extension of JavaScript typeof, distinguishes Array, Date, RegExp, Math, null, etc. * * @param subject - 要檢查的物件 / Object to check * @returns 類型名稱:'string', 'number', 'boolean', 'null', 'array', 'date', 'regexp', 'math', 'object' */ declare function _realTypeOf(subject: unknown): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "function" | "object" | "math" | "null" | "array" | "date" | "regexp"; /** * 計算字串的 Java hashCode * Calculate Java hashCode for string * * 實現 Java String 的 hashCode 方法,用於產生與順序無關的雜湊值 * Implements Java String's hashCode method for generating order-independent hash values * * @see http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ * @param string - 要計算雜湊的字串 / String to hash * @returns 32 位元整數雜湊值 / 32-bit integer hash value * * @example * ```typescript * const hash = _hashThisString('hello'); * console.log(hash); // 99162322 * ``` */ export declare function _hashThisString(string: string): number; /** * 取得與順序無關的物件雜湊值 * Get order-independent object hash * * 計算物件的雜湊值,無論陣列元素顺序或物件鍵顺序如何,只要內容相同則雜湊值相同 * Calculates object hash where same content yields same hash regardless of array element order or object key order * * @param object - 要計算雜湊的物件 / Object to hash * @returns 與順序無關的雜湊值 / Order-independent hash value * * @example * ```typescript * const obj1 = { b: 1, a: 2 }; * const obj2 = { a: 2, b: 1 }; * console.log(getOrderIndependentHash(obj1) === getOrderIndependentHash(obj2)); // true * * const arr1 = [1, 2, 3]; * const arr2 = [3, 2, 1]; * console.log(getOrderIndependentHash(arr1) === getOrderIndependentHash(arr2)); // true * ``` */ export declare function getOrderIndependentHash(object: unknown): number; /** * 正規化雜湊描述 * Normalize hash description * * 將類型和相關資訊轉換為標準化的字串格式,用於產生與順序無關的雜湊值 * Convert type and related information to standardized string format for generating order-independent hash values * * @param type - 類型名稱 (来自 realTypeOf) / Type name (from realTypeOf) * @param options - 雜湊選項 / Hash options * @param options.value - 要雜湊的值 / Value to hash * @param options.key - 物件鍵 / Object key * @param options.hash - 雜湊值 / Hash value * @returns 正規化描述字串 / Normalized description string * * @example * ```typescript * const desc = _normalizeHashDesc('object', { key: 'name', hash: 12345 }); * console.log(desc); // "name:12345" * * const valueDesc = _normalizeHashDesc('string', { value: 'test' }); * console.log(valueDesc); // "S:4:test" * ``` */ export declare function _normalizeHashDesc(type: ReturnType, options: { value?: any; key?: IPathKey; hash?: number; }): string; /** * 可觀察的深度差異計算 * Observable deep difference calculation * * 計算兩個物件之間的結構差異,並通過觀察者回調報告每個差異 * Calculates structural differences between two objects and reports each difference via observer callback * * @param lhs - 左側物件 (原始值) / Left-hand-side object (original value) * @param rhs - 右側物件 (比較值) / Right-hand-side object (comparison value) * @param observer - 觀察者回調函式 / Observer callback function * @param prefilter - 預過濾選項 / Pre-filter options * @param orderIndependent - 是否進行順序無關的陣列比較 / Whether to perform order-independent array comparison * @returns 差異陣列 / Differences array * * @example * ```typescript * import { observableDiff } from 'deep-diff'; * * const lhs = { name: 'Alice', age: 30 }; * const rhs = { name: 'Bob', age: 30, city: 'Taipei' }; * * const differences: any[] = []; * observableDiff(lhs, rhs, (diff) => { * differences.push(diff); * console.log('Difference:', diff.kind, diff.path); * }); * console.log(differences.length); // 2 * ``` */ export declare function observableDiff(lhs: LHS, rhs: RHS, observer?: IObserver, prefilter?: IPreFilter, orderIndependent?: boolean): IDiffNode[]; /** * 順序無關的可觀察差異計算 * Order-independent observable difference calculation * * @param lhs - 左側物件 / Left-hand-side object * @param rhs - 右側物件 / Right-hand-side object * @param changes - 差異陣列 / Differences array * @param prefilter - 預過濾 / Pre-filter * @param path - 路徑 / Path * @param key - 鍵 / Key * @param stack - 堆疊 / Stack * * @example * ```typescript * import { orderIndependentObservableDiff } from 'deep-diff'; * * const lhs = { items: [1, 2, 3] }; * const rhs = { items: [3, 1, 2] }; * const changes: any[] = []; * * // 使用順序無關模式計算差異 * // Calculate differences with order-independent mode * orderIndependentObservableDiff(lhs, rhs, changes, null, null, null, null); * console.log(changes); // [] (無差異 because same content) * ``` */ export declare function orderIndependentObservableDiff(lhs: any, rhs: any, changes: any, prefilter: any, path: any, key: any, stack: any): void; /** * 深度差異計算 * Deep difference calculation * * 計算兩個物件之間的結構差異 * Calculates structural differences between two objects * * @param lhs - 左側物件 (原始值) / Left-hand-side object (original value) * @param rhs - 右側物件 (比較值) / Right-hand-side object (comparison value) * @param prefilter - 預過濾選項 / Pre-filter options * @param accum - 累加器陣列 (可選) / Accumulator array (optional) * @returns 差異陣列,若無差異則回傳 undefined / Differences array, or undefined if no differences * * @example * ```typescript * import { diff } from 'deep-diff'; * * const lhs = { name: 'my object', description: 'it\'s an object!', details: { it: 'has', an: 'array' } }; * const rhs = { name: 'updated object', description: 'it\'s an object!', details: { it: 'has', an: 'array', now: 'modified' } }; * * const differences = diff(lhs, rhs); * console.log(differences?.length); // 2 (name changed, now added) * ``` */ export declare function deepDiff(lhs: LHS, rhs: RHS, prefilter?: IPreFilter, accum?: IDiffNode[]): IDiffNode[]; /** * 順序無關的深度差異計算 * Order-independent deep difference calculation * * @param lhs - 左側物件 / Left-hand-side object * @param rhs - 右側物件 / Right-hand-side object * @param prefilter - 預過濾 / Pre-filter * @param accum - 累加器 / Accumulator * @returns 差異陣列 / Differences array * * @example * ```typescript * import { orderIndependentDiff } from 'deep-diff'; * * // Different order, same content = no difference * const lhs = { items: [1, 2, 3] }; * const rhs = { items: [3, 1, 2] }; * const result = orderIndependentDiff(lhs, rhs); * console.log(result); // undefined (no differences) * ``` */ export declare function orderIndependentDiff(lhs: LHS, rhs: RHS, prefilter?: IPreFilter, accum?: IDiffNode[]): IDiffNode[]; /** * 對象遍歷輔助函式 * Object traversal helper function * * 根據路徑遍歷到目標物件的深層屬性 * Traverses to deep property of target object based on path * * @param target - 目標物件 / Target object * @param path - 要遍歷的路徑 / Path to traverse * @returns [父物件, 最後的鍵] / [Parent object, final key] * * @example * ```typescript * import { _traversalObject } from 'deep-diff'; * * const obj = { user: { profile: { name: 'Alice' } } }; * const [parent, key] = _traversalObject(obj, ['user', 'profile', 'name']); * console.log(parent[key]); // 'Alice' * parent[key] = 'Bob'; * console.log(obj.user.profile.name); // 'Bob' * ``` */ export declare function _traversalObject(target: any, path: IPaths): readonly [T, IPathKey]; /** * 套用陣列變更 * Apply array change * * 將差異記錄套用到陣列元素 * Apply difference record to array element * * @param arr - 目標陣列 / Target array * @param index - 陣列索引 / Array index * @param change - 差異記錄 / Difference record * @returns 修改後的陣列 / Modified array * * @example * ```typescript * import { _applyArrayChange, DiffNew, DiffEdit } from 'deep-diff'; * * const arr = [1, 2, 3]; * _applyArrayChange(arr, 3, new DiffNew(['new'], 4)); * console.log(arr); // [1, 2, 3, 4] * * _applyArrayChange(arr, 0, new DiffEdit(['edit'], 1, 10)); * console.log(arr); // [10, 2, 3, 4] * ``` */ export declare function _applyArrayChange(arr: T, index: number, change: IDiffNode): T; /** * 類型守衛:判斷是否為 IDiffNode * Type guard: Check if it's IDiffNode * * @param source - 要檢查的物件 / Object to check * @returns 是否為有效的差異記錄 / Whether it's a valid difference record * * @example * ```typescript * import { isIDiffNode, diff } from 'deep-diff'; * * const lhs = { a: 1 }; * const rhs = { a: 2 }; * const differences = diff(lhs, rhs); * * if (differences && isIDiffNode(differences[0])) { * console.log(differences[0].kind); // 'E' * } * ``` */ export declare function isIDiffNode(source?: unknown): source is T; /** * 套用單一變更記錄 * Apply single change record * * 將差異記錄套用到目標物件 * Apply difference record to target object * * @param target - 目標物件 / Target object * @param source - 來源物件 (未使用) / Source object (unused) * @param change - 差異記錄 / Difference record * * @example * ```typescript * import { diff, applyChange } from 'deep-diff'; * * const lhs = { name: 'Alice', age: 30 }; * const rhs = { name: 'Bob', age: 30, city: 'Taipei' }; * const differences = diff(lhs, rhs); * * const target = { ...lhs }; * if (differences && differences[0]) { * applyChange(target, rhs, differences[0]); * console.log(target.name); // 'Bob' * } * ``` */ export declare function applyChange(target: LHS, source: any, change?: IDiffNode): void; /** * 還原單一變更記錄 * Revert single change record * * 將差異記錄還原到目標物件 * Revert difference record on target object * * @param target - 目標物件 / Target object * @param source - 來源物件 (未使用) / Source object (unused) * @param change - 差異記錄 / Difference record * * @example * ```typescript * import { diff, revertChange } from 'deep-diff'; * * const lhs = { name: 'Alice' }; * const rhs = { name: 'Bob' }; * const differences = diff(lhs, rhs); * * const target = { ...rhs }; * if (differences && differences[0]) { * revertChange(target, lhs, differences[0]); * console.log(target.name); // 'Alice' * } * ``` */ export declare function revertChange(target: LHS, source: any, change: IDiffNode): void; /** * 套用結構差異 * Apply structural differences * * 將來源物件的結構差異套用到目標物件 * Apply structural differences from source object to target object * * @param target - 目標物件 / Target object * @param source - 來源物件 / Source object * @param filter - 過濾函式 (可選) / Filter function (optional) * @returns 目標物件 / Target object * * @example * ```typescript * import { applyDiff } from 'deep-diff'; * * const target = { name: 'Alice', age: 30 }; * const source = { name: 'Bob', age: 31 }; * applyDiff(target, source); * console.log(target); // { name: 'Bob', age: 31 } * * // With filter * const target2 = { name: 'Alice', age: 30 }; * applyDiff(target2, source, (t, s, change) => change.path[0] !== 'age'); * console.log(target2); // { name: 'Bob', age: 30 } * ``` */ export declare function applyDiff(target: LHS, source: RHS, filter?: IFilter): LHS; /** * 批量套用差異並回傳 * Batch apply differences and return * * 一次性套用多個差異記錄到目標物件 * Apply multiple difference records to target object at once * * @param lhs - 目標物件 / Target object * @param differences - 差異陣列 / Differences array * @returns 修改後的物件 / Modified object * * @example * ```typescript * import { diff, applyDiffChange } from 'deep-diff'; * * const lhs = { name: 'Alice', age: 30 }; * const rhs = { name: 'Bob', age: 31 }; * const differences = diff(lhs, rhs); * * const target = { name: 'Original' }; * const result = applyDiffChange(target, differences!); * console.log(result); // { name: 'Bob', age: 31 } * ``` */ export declare function applyDiffChange(lhs: unknown, differences: IDiffNode[]): RHS; /** * 批量還原差異並回傳 * Batch revert differences and return * * 一次性還原多個差異記錄到目標物件 * Revert multiple difference records on target object at once * * @param lhs - 目標物件 / Target object * @param differences - 差異陣列 / Differences array * @returns 還原後的物件 / Reverted object * * @example * ```typescript * import { diff, revertDiffChange } from 'deep-diff'; * * const lhs = { name: 'Alice' }; * const rhs = { name: 'Bob' }; * const differences = diff(lhs, rhs); * * const target = { name: 'Bob' }; * const result = revertDiffChange(target, differences!); * console.log(result); // { name: 'Alice' } * ``` */ export declare function revertDiffChange(lhs: unknown, differences: IDiffNode[]): LHS; /** * 將 deepDiff 匯出為 diff * Export deepDiff as diff * * 用於符合 CommonJS 習慣 * For CommonJS convention */ export { deepDiff as diff }; /** * 預設匯出 deepDiff * Default export deepDiff */ export default deepDiff;