Skip to content

⬅️ Back to Table of Contents

📄 makeDestructurable

📊 Analysis Summary

Metric Count
🔧 Functions 1

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/makeDestructurable/index.ts

Functions

makeDestructurable(obj: T, arr: A): T & A

Parameters:

  • obj T
  • arr A

Returns: T & A

Calls:

  • Object.defineProperty
  • Object.assign
Code
export function makeDestructurable<
  T extends Record<string, unknown>,
  A extends readonly any[],
>(obj: T, arr: A): T & A {
  if (typeof Symbol !== 'undefined') {
    const clone = { ...obj }

    Object.defineProperty(clone, Symbol.iterator, {
      enumerable: false,
      value() {
        let index = 0
        return {
          next: () => ({
            value: arr[index++],
            done: index > arr.length,
          }),
        }
      },
    })

    return clone as T & A
  }
  else {
    return Object.assign([...arr], obj) as unknown as T & A
  }
}

Generated by Syntax Scribe