Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function extend(...objects)
- {
- objects = objects.filter(x => exists(x))
- if (objects.length === 0)
- {
- return
- }
- if (objects.length === 1)
- {
- return objects[0]
- }
- const to = objects[0]
- const from = objects[1]
- if (objects.length > 2)
- {
- const last = objects.pop()
- const intermediary_result = extend.apply(this, objects)
- return extend(intermediary_result, last)
- }
- for (let key of Object.keys(from))
- {
- if (is_object(from[key]))
- {
- if (!is_object(to[key]))
- {
- to[key] = {}
- }
- extend(to[key], from[key])
- }
- else if (Array.isArray(from[key]))
- {
- if (!Array.isArray(to[key]))
- {
- to[key] = []
- }
- to[key] = to[key].concat(clone(from[key]))
- }
- else
- {
- to[key] = from[key]
- }
- }
- return to
- }
- export function merge()
- {
- const parameters = Array.prototype.slice.call(arguments, 0)
- parameters.unshift({})
- return extend.apply(this, parameters)
- }
- export function clone(object)
- {
- if (is_object(object))
- {
- return merge({}, object)
- }
- else if (Array.isArray(object))
- {
- return object.map(x => clone(x))
- }
- else
- {
- return object
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement