Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @tomato-js/object

Index

Functions

deepClone

  • deepClone(value: any): any
  • 深拷贝对象、数组、函数

    新增于v0.0.14

    脚本举例

      import { deepClone } from '@tomato-js/object'
      const obj = { a: 1, b: 2, c: [1, 2, 3, { e: 5, f: [{ g: 1 }, { h: 8 }], i: 9 }]}
      const c = deepClone(obj);

    Parameters

    • value: any

      被拷贝对象

    Returns any

    拷贝对象

deepFreeze

  • deepFreeze<T>(object: T): {}
  • 函数描述

    新增于v0.0.19

    脚本举例

      import { deepFreeze } from '@tomato-js/object'
      let obj2 = {
        internal: {
          a: null
        }
      };
      deepFreeze(obj2);
      obj2.internal.a = 'anotherValue'; // fails silently in non-strict mode
      obj2.internal.a; // null

    Type parameters

    • T: object

    Parameters

    • object: T

    Returns {}

deepGet

  • deepGet(path: string, object: ObjectType<any>): any
  • 获取对象深层次内容

    脚本举例

      import { deepGet } from '@tomato-js/object'
      const obj = {a:{b:{c:'d'}},aa:'bb',e:'f'}
      const c = deepGet('a.b.c',obj);//'d'

    Parameters

    • path: string

      对象寻找路径如a.b.c

    • object: ObjectType<any>

      需要解析的对象

    Returns any

    找到的内容

deepMerge

  • deepMerge(...objects: ObjectType<any>[]): ObjectType<any>
  • 对对象进行深层merge

    新增于v0.0.24

    脚本举例

      import { deepMerge } from '@tomato-js/object'
      deepMerge(
        a: { b:{ c: 'd' } },
        a: { b:{ e: 'f' } },
        a: { g: 'h' }
      );
    //{
    //  a: {
    //    b: {
    //      c: 'd',
    //      e: 'f'
    //    }
    //    g: 'h'
    //  }
    //}
    

    Parameters

    • Rest ...objects: ObjectType<any>[]

    Returns ObjectType<any>

lazy

  • lazy(object: ObjectType<any>, prop: string, valueResolver: Function): void
  • 给对象增加懒依赖属性

    新增于v0.0.14

    脚本举例

       const obj = {};
       object.lazy(obj, "x", function() {
         return 2;
       });
       obj.x//2

    Parameters

    • object: ObjectType<any>
    • prop: string
    • valueResolver: Function

    Returns void

    拷贝对象

omit

  • omit<T, K>(object: T, keys: K[]): Omit<T, K>
  • 返回一个去除了某些项的新对象

    新增于v0.0.22

    脚本举例

      import { omit } from '@tomato-js/object'
      omit({ a: 1, b: 2, c: 3, d: 4 }, ['a', 'd'])
      //{ b: 2, c: 3 }

    Type parameters

    • T: {}

    • K: keyof T

    Parameters

    • object: T

      待挑选的对象

    • keys: K[]

      需要剔除的对象

    Returns Omit<T, K>

    返回去除了某些项后的新对象

pick

  • pick<T>(object: ObjectType<T>, keys?: string[], handler?: undefined | ((k: string, v: T) => any)): ObjectType<T>
  • 获取对象某些值,组成新对象

    脚本举例

      import { pick } from '@tomato-js/object'
      const obj = {a:{b:{c:'d'}},aa:'bb',e:'f'}
      const obj1 = pick(obj,['aa']);//{aa:'bb'}
      const obj2 = pick(obj,['aa','e']);//{aa:'bb',e:'f'}
      const obj3 = pick(obj,['aa','e'],(k,v)=>k==='e'?v+1:v);//{aa:'bb',e:'f1'}

    Type parameters

    • T

    Parameters

    • object: ObjectType<T>

      需要解析的对象

    • Default value keys: string[] = []

      需要获取的key

    • Optional handler: undefined | ((k: string, v: T) => any)

    Returns ObjectType<T>

    组成的新对象

pickX

  • pickX<T>(object: ObjectType<T>, keys?: string[], handler?: undefined | ((k: string, v: T) => any)): ObjectType<T>
  • 深度获取对象某些值及更换key名,组成新对象,pick的升级版

    脚本举例

      import { pickX } from '@tomato-js/object'
      const obj = {a:{b:{c:'d'}},aa:'bb',e:'f'}
      const obj1 = pick(obj,['a.b.c as g']);//{g:'d'}
      const obj2 = pick(obj,['aa as aaa','e as ee']);//{aaa:'bb',ee:'f'}
      const obj3 = pick(obj,['aa as aaa','e as ee'],(k,v)=>k==='ee'?v+1:v);//{aaa:'bb',ee:'f1'}

    Type parameters

    • T

    Parameters

    • object: ObjectType<T>

      需要解析的对象

    • Default value keys: string[] = []

      需要获取的key

    • Optional handler: undefined | ((k: string, v: T) => any)

    Returns ObjectType<T>

    组成的新对象

Generated using TypeDoc