Skip to content

assign

將多個對象的屬性合並到一個新對象中。

語法

typescript
assign<T extends Record<string, any>>(
  target: T,
  ...sources: Partial<T>[]
): T

參數

  • target (T): 目標對象
  • ...sources (Partial<T>[]): 要合並的源對象

返回值

  • T: 合並後的新對象

示例

基本用法

typescript
import { assign } from 'radash'

const user = { name: 'Alice', age: 25 }
const updates = { age: 26, city: 'New York' }

const updatedUser = assign(user, updates)
// { name: 'Alice', age: 26, city: 'New York' }

合並多個對象

typescript
import { assign } from 'radash'

const base = { name: 'Alice', age: 25 }
const updates1 = { age: 26 }
const updates2 = { city: 'New York' }
const updates3 = { country: 'USA' }

const result = assign(base, updates1, updates2, updates3)
// { name: 'Alice', age: 26, city: 'New York', country: 'USA' }

處理嵌套對象

typescript
import { assign } from 'radash'

const user = {
  name: 'Alice',
  profile: {
    age: 25,
    city: 'Boston'
  }
}

const updates = {
  profile: {
    age: 26,
    country: 'USA'
  }
}

const updatedUser = assign(user, updates)
// {
//   name: 'Alice',
//   profile: {
//     age: 26,
//     country: 'USA'
//   }
// }

處理數組

typescript
import { assign } from 'radash'

const config = {
  settings: {
    theme: 'dark',
    features: ['feature1', 'feature2']
  }
}

const updates = {
  settings: {
    features: ['feature3', 'feature4']
  }
}

const updatedConfig = assign(config, updates)
// {
//   settings: {
//     theme: 'dark',
//     features: ['feature3', 'feature4']
//   }
// }

處理函數屬性

typescript
import { assign } from 'radash'

const component = {
  name: 'Button',
  render: () => '<button>Click me</button>',
  props: { text: 'Click me' }
}

const updates = {
  render: () => '<button>Updated</button>',
  props: { text: 'Updated' }
}

const updatedComponent = assign(component, updates)
// {
//   name: 'Button',
//   render: () => '<button>Updated</button>',
//   props: { text: 'Updated' }
// }

處理日期對象

typescript
import { assign } from 'radash'

const event = {
  title: 'Meeting',
  date: new Date('2023-01-01'),
  attendees: ['Alice', 'Bob']
}

const updates = {
  date: new Date('2023-01-02'),
  attendees: ['Alice', 'Bob', 'Charlie']
}

const updatedEvent = assign(event, updates)
// {
//   title: 'Meeting',
//   date: new Date('2023-01-02'),
//   attendees: ['Alice', 'Bob', 'Charlie']
// }

處理空值

typescript
import { assign } from 'radash'

const user = {
  name: 'Alice',
  email: 'alice@example.com',
  phone: '123-456-7890'
}

const updates = {
  email: null,
  phone: undefined,
  city: 'New York'
}

const updatedUser = assign(user, updates)
// {
//   name: 'Alice',
//   email: null,
//   phone: undefined,
//   city: 'New York'
// }

處理類型安全

typescript
import { assign } from 'radash'

interface User {
  name: string
  age: number
  email?: string
}

const user: User = {
  name: 'Alice',
  age: 25
}

const updates: Partial<User> = {
  age: 26,
  email: 'alice@example.com'
}

const updatedUser = assign(user, updates)
// TypeScript 會正確推斷類型

處理只讀屬性

typescript
import { assign } from 'radash'

const config = {
  readonly: 'cannot change',
  mutable: 'can change'
} as const

const updates = {
  mutable: 'updated value'
}

const updatedConfig = assign(config, updates)
// {
//   readonly: 'cannot change',
//   mutable: 'updated value'
// }

處理Symbol屬性

typescript
import { assign } from 'radash'

const sym = Symbol('key')

const obj1 = {
  [sym]: 'value1',
  regular: 'prop1'
}

const obj2 = {
  [sym]: 'value2',
  regular: 'prop2'
}

const result = assign(obj1, obj2)
// {
//   [sym]: 'value2',
//   regular: 'prop2'
// }

處理不可枚舉屬性

typescript
import { assign } from 'radash'

const obj1 = {}
Object.defineProperty(obj1, 'hidden', {
  value: 'secret',
  enumerable: false
})

const obj2 = { visible: 'public' }

const result = assign(obj1, obj2)
// { visible: 'public' }
// hidden 屬性不會被復制

注意事項

  1. 淺拷貝: 只進行淺拷貝,嵌套對象會被引用
  2. 不可枚舉屬性: 不會復制不可枚舉的屬性
  3. Symbol屬性: 會復制Symbol屬性
  4. 原型鏈: 不會復制原型鏈上的屬性

與其他函數的區別

  • assign: 合並對象屬性
  • Object.assign: 原生方法,功能相同
  • spread: 使用展開運算符
  • merge: 深度合並對象

性能

  • 時間復雜度: O(n × m),其中 n 是源對象數量,m 是屬性數量
  • 空間復雜度: O(k),其中 k 是目標對象的屬性數量
  • 適用場景: 對象屬性合並和更新

Released under the MIT License.