/** * 判断是否为object * @category 对象 */ export declare function isObj(input: unknown): input is object; /** * 是否为纯对象 * * ```ts * import { isPlainObj } from 'sunny-js' * console.log(isPlainObj({ a: 1, b: true })) // => true * console.log(isPlainObj(window)) // => false * ``` * * @category 对象 */ export declare function isPlainObj(input: unknown): input is Record; /** * 反转对象键值 * ```ts * import { invert } from 'sunny-js' * console.log( * invert({ * a: 'x', * b: 'y', * } as const) * ) * ``` * @since 2.1.33 * @see {@link https://stackoverflow.com/questions/56415826/is-it-possible-to-precisely-type-invert-in-typescript Is it possible to precisely type _.invert in TypeScript?} * @see {@link https://stackoverflow.com/questions/66993264/what-does-the-as-const-mean-in-typescript-and-what-is-its-use-case What does the "as const" mean in TypeScript and what is its use case?} * @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions const assertions} * @param obj 如果想要得到更精准的类型推导,备忘了给传入对象加上 as const 尾巴 * @category 对象 */ export declare function invert>(obj: TObj): { [K in keyof TObj as TObj[K]]: K; }; /** * 根据字典左右字段的映射关系对数据属性进行重命名 * * ```ts * import { renameObjKey, invert } from 'sunny-js' * * // 这种重命名对象key的操作 * const data1 = { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * } * ;(data1 as any).newKey1 = data1.key1 * delete data1.key1 * // 等同于 * renameObjKey(data1, { key1: 'newKey1' } as const) * * // 基本用法 * const _objA = renameObjKey( * { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * }, * { key1: 'newKey1' } as const * ) * console.log(_objA.newKey1, _objA.key2, _objA.key3) * * // 反转dict对象 * const _objB = renameObjKey( * { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * }, * invert({ * newKey1: 'key1', * } as const) * ) * console.log(_objB.newKey1, _objB.key2, _objB.key3) * ``` * * @since 2.1.33 * @param data 被操作的对象 * @param dict 传入 `{ key: 'newKey' } as const` 模式来定义需要被重命名的键。如果需要反转对象可以使用{@link invert} * @category 对象 */ export declare function renameObjKey, TDict extends { [K in keyof TData]?: PropertyKey; }>(data: TData, dict: TDict): Omit & { [K in keyof TData as TDict[K]]: TData[K]; }; /** * 根据字典左右字段的映射关系对数据属性进行重命名 * * ```ts * import { mapByDict } from 'sunny-js' * * // 这种重命名对象key的操作 * const data2 = { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * } * ;(data2 as any).newKey1 = data2.key1 * delete data2.key1 * // 等同于 * mapByDict(data2, { newKey1: 'key1' } as const) * * // 基本用法 * const _objA = mapByDict( * { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * }, * { * newKey1: 'key1', * } as const * ) * console.log(_objA.newKey1, _objA.key2, _objA.key3) * * // 反转dict对象 * const _objB = mapByDict( * { * key1: { childKey1: 'value1' }, * key2: [{ childKey2: 'value2' }], * key3: true, * }, * { * key1: 'newKey1', * } as const, * true * ) * console.log(_objB.newKey1, _objB.key2, _objB.key3) * ``` * * @param data 被操作的对象 * @param dict 传入 `{ newKey: 'key' } as const` 模式来定义需要被重命名的键 * @param reverse 是否反转dict对象 * @deprecated 请使用 {@link renameObjKey} 替代该函数的功能 * @category 对象 */ export declare function mapByDict, TDict extends NonNullable extends true ? { [K in keyof TData]?: PropertyKey; } : Record, TReverse extends boolean>(data: TData, dict: TDict, reverse?: TReverse): NonNullable extends true ? Omit & { [K in keyof TData as TDict[K]]: TData[K]; } : Omit & { [K in keyof TDict]: TData[TDict[K]]; }; /** * 对象浅合并 * * ```ts * import { mixin } from 'sunny-js' * const _objA = { key1: 1 } * const _objB = { key1: 'value1', key2: 'value2' } * const _objC = { key1: [1], key2: ['value2'], key3: { childKey1: 1 } } * const _objD = mixin(_objA, _objB, _objC) * console.log(_objD, _objD.key1, _objD.key2, _objD.key3) * ``` * * @category 对象 */ export declare function mixin(...obj: Array<{ [P in K]?: any; }>): Record; /** * 对象深度合并 * * Note: 该函数会修改原对象 * * ```ts * import { merge } from 'sunny-js' * const _objA = { key1: { childKey1: 1 } } * const _objB = { key1: { childKey2: 2 } } * const _objD = merge(_objA, _objB) * console.log(_objD, _objD.key1) * ``` * * @deprecated 请使用{@link deepMerge}替代 * @category 对象 */ export declare function merge(...obj: Array<{ [P in K]?: any; }>): Record; /** * 对象深度合并 * * 该函数在合并对象时,如果属性值被修改,会创建新对象引用,否则使用原对象(不修改引用) * * ```ts * import { deepMerge } from 'sunny-js' * const _objA = { key1: { childKey1: 1 } } * const _objB = { key1: { childKey2: 2 } } * const _objD = deepMerge(_objA, _objB) * console.log(_objD, _objD.key1) * ``` * * @category 对象 */ export declare function deepMerge(...obj: Array<{ [P in K]?: any; }>): Record; /** * recursively copies only the missing properties/values to the target object. * * ```ts * import { defaults } from 'sunny-js' * const _objA = { key1: { childKey1: 1 } } * const _objB = { key1: { childKey2: 2, childKey3: 3 } } * const _objD = defaults(_objA, _objB, { key1: { childKey3: 4 } }) * console.log(_objD.key1) * // => {childKey1: 1, childKey2: 2, childKey3: 3} * ``` * * @see https://lodash.com/docs/4.17.15#defaultsDeep * @category 对象 */ export declare function defaults(...obj: Array<{ [P in K]?: any; }>): Record; /** * 给个对象属性路径,逐级访问属性值 * * ```ts * import { propBy } from 'sunny-js' * const obj = { * prop: { * child: function () {}, * brother: 'hello', * sister: false, * }, * } * // 间接径访问对象属性 * propBy('obj.prop.child') * // => function (){} * propBy('obj.prop.brother') * // => "hello" * propBy('obj.prop.sister') * // => false * propBy('obj.prop.other') * // => undefined * * // 指定context,对象上下文 * propBy('prop.child', obj) * // => function (){} * propBy('prop.brother', obj) * // => "hello" * propBy('prop.sister', obj) * // => false * propBy('prop.other', obj) * // => undefined * ``` * * @param chain required, like 'obj.prop.child' * @param context optional, like an obj * @see 大部分场景均可以使用{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining Optional chaining}替代 * @category 对象 */ export declare function propBy(chain: string, context?: unknown): TReturn | undefined;