Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ObjectMixinOptions<V, K, D>

Type parameters

  • V = unknown

  • K = unknown

  • D = unknown

Hierarchy

  • ObjectMixinOptions

Index

Properties

concatArray?: boolean
concatArrays?: boolean | ((a: unknown[], b: unknown[], key: K) => unknown[])

If true, then to merge two arrays will be used a concatenation strategy (works only with the deep mode). Also, the parameter can be passed as a function to concatenate arrays.

default

false

example
// {a: [2]}
Object.mixin({deep: true, concatArrays: false}, {a: [1]}, {a: [2]});

// {a: [1, 2]}
Object.mixin({deep: true, concatArrays: true}, {a: [1]}, {a: [2]});

// {a: [1, 1, 2]}
Object.mixin({deep: true, concatArrays: true}, {a: [1]}, {a: [1, 2]});

// {a: [1, 2]}
Object.mixin({deep: true, concatArrays: (a, b) => a.union(b)}, {a: [1]}, {a: [1, 2]});
deep?: boolean

If true, then object properties are copied recursively. Also, this mode enables copying properties from a prototype.

default

false

example
// {a: {c: 2}}
Object.mixin({deep: false}, {a: {b: 1}}, {a: {c: 2}});

// {a: {b: 1, c: 2}}
Object.mixin({deep: true}, {a: {b: 1}}, {a: {c: 2}});

// {a: {c: 2}}
Object.mixin({deep: true}, {}, {__proto__: {a: {c: 2}}});
onlyNew?: boolean | -1
propsToCopy?: "all" | "new" | "exist"

Strategy to resolve collisions of properties when merging:

  1. 'all' - all properties are merged in spite of possible collisions (by default)
  2. 'new' - properties with collisions aren't merged
  3. 'exist' - properties without collisions aren't merged
default

'all'

example
// {a: 2, b: 3}
Object.mixin({propsToCopy: 'all'}, {a: 1}, {a: 2, b: 3});

// {a: 1, b: 3}
Object.mixin({propsToCopy: 'new'}, {a: 1}, {a: 2, b: 3});

// {a: 2}
Object.mixin({propsToCopy: 'exist'}, {a: 1}, {a: 2, b: 3});
skipUndefs?: boolean

If true, all properties with undefined value aren't copied

default

true

example
// {a: 1}
Object.mixin({skipUndefs: true}, {a: 1}, {a: undefined});

// {a: undefined}
Object.mixin({skipUndefs: false}, {a: 1}, {a: undefined});
traits?: boolean | -1
withAccessors?: boolean
withDescriptor?: boolean
withDescriptors?: boolean | "onlyAccessors"

Should or shouldn't copy property descriptors too. If passed onlyAccessors, the descriptor properties like enumerable or configurable are ignored.

default

false

withNonEnumerables?: boolean

If true, the function will merge all object properties, but not only enumerable. Non-enumerable properties from a prototype are ignored.

default

false

example
const obj = {a: 1};

Object.defineProperty(obj, 'b', {value: 2});

// {a: 1, b: 2}
Object.mixin({withNonEnumerables: true}, {}, obj);
withProto?: boolean

If true, then merging preserve prototypes of properties (works only with the deep mode)

default

false

example
const proto = {
a: {
b: 2
},

c: 3
};

const obj = Object.create(proto);
Object.mixin({deep: true, withProto: false}, obj, {c: 2, a: {d: 4}});

// 2
// 4
// 2
// true
// true
console.log(
obj.c,
obj.a.d,
obj.a.b,
obj.a.hasOwnProperty('d'),
obj.a.hasOwnProperty('b')
);

const obj2 = Object.create(proto);
Object.mixin({deep: true, withProto: true}, obj2, {c: 2, a: {d: 4}});

// 2
// 4
// 2
// true
// false
console.log(
obj2.c,
obj2.a.d,
obj2.a.b,
obj2.a.hasOwnProperty('d'),
obj2.a.hasOwnProperty('b')
);
withUndef?: boolean

Methods

  • concatFn(a: unknown[], b: unknown[], key: K): unknown[]
  • concatFn(a: unknown[], b: unknown[], key: K): unknown[]
  • extendFilter(el: unknown, key: K, data: V): any
  • extendFilter(el: unknown, key: K, data: V): any
  • Function to filter values that support deep extending (works only with the deep mode)

    example
    // {a: {a: 1, b: 2}}
    Object.mixin({deep: true}, {a: {a: 1}}, {a: {b: 2}});

    // {a: {b: 2}}
    Object.mixin({deep: true, extendFilter: (el) => !el.b}, {a: {a: 1}}, {a: {b: 2}});

    Parameters

    • el: unknown

      element value

    • key: K

      element key

    • data: V

      element container

    Returns any

  • Function to filter values that support deep extending (works only with the deep mode)

    example
    // {a: {a: 1, b: 2}}
    Object.mixin({deep: true}, {a: {a: 1}}, {a: {b: 2}});

    // {a: {b: 2}}
    Object.mixin({deep: true, extendFilter: (el) => !el.b}, {a: {a: 1}}, {a: {b: 2}});

    Parameters

    • el: unknown

      element value

    • key: K

      element key

    • data: V

      element container

    Returns any

  • filter(el: V, key: K, data: D): any
  • filter(el: V, key: K, data: D): any
  • Function to filter values that shouldn't be copied

    example
    // {a: 1, b: 2}
    Object.mixin({deep: true}, {a: 1}, {b: 2});

    // {a: 1}
    Object.mixin({deep: true, filter: (el, key) => key !== 'b'}, {a: 1}, {b: 2});

    Parameters

    • el: V

      element value

    • key: K

      element key

    • data: D

      element container

    Returns any

  • Function to filter values that shouldn't be copied

    example
    // {a: 1, b: 2}
    Object.mixin({deep: true}, {a: 1}, {b: 2});

    // {a: 1}
    Object.mixin({deep: true, filter: (el, key) => key !== 'b'}, {a: 1}, {b: 2});

    Parameters

    • el: V

      element value

    • key: K

      element key

    • data: D

      element container

    Returns any