Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function extend(...objects)
  2. {
  3.     objects = objects.filter(x => exists(x))
  4.  
  5.     if (objects.length === 0)
  6.     {
  7.         return
  8.     }
  9.    
  10.     if (objects.length === 1)
  11.     {
  12.         return objects[0]
  13.     }
  14.    
  15.     const to   = objects[0]
  16.     const from = objects[1]
  17.  
  18.     if (objects.length > 2)
  19.     {
  20.         const last = objects.pop()
  21.         const intermediary_result = extend.apply(this, objects)
  22.         return extend(intermediary_result, last)
  23.     }
  24.  
  25.     for (let key of Object.keys(from))
  26.     {
  27.         if (is_object(from[key]))
  28.         {
  29.             if (!is_object(to[key]))
  30.             {
  31.                 to[key] = {}
  32.             }
  33.  
  34.             extend(to[key], from[key])
  35.         }
  36.         else if (Array.isArray(from[key]))
  37.         {
  38.             if (!Array.isArray(to[key]))
  39.             {
  40.                 to[key] = []
  41.             }
  42.  
  43.             to[key] = to[key].concat(clone(from[key]))
  44.         }
  45.         else
  46.         {
  47.             to[key] = from[key]
  48.         }
  49.     }
  50.  
  51.     return to
  52. }
  53.  
  54. export function merge()
  55. {
  56.     const parameters = Array.prototype.slice.call(arguments, 0)
  57.     parameters.unshift({})
  58.     return extend.apply(this, parameters)
  59. }
  60.  
  61. export function clone(object)
  62. {
  63.     if (is_object(object))
  64.     {
  65.         return merge({}, object)
  66.     }
  67.     else if (Array.isArray(object))
  68.     {
  69.         return object.map(x => clone(x))
  70.     }
  71.     else
  72.     {
  73.         return object
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement