This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathobjects.js
71 lines (70 loc) · 2.25 KB
/
objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*******************************************************************************
* @license
* Copyright (c) 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
/*eslint-env browser, amd*/
define([], function() {
function mixin(target/*, source..*/) {
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var j = 1, len = arguments.length; j < len; j++) {
var source = arguments[j];
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
}
/**
* @name orion.objects
* @class Object-oriented helpers.
*/
return {
/**
* Creates a shallow clone of the given <code>object</code>.
* @name orion.objects.clone
* @function
* @static
* @param {Object|Array} object The object to clone. Must be a "normal" Object or Array. Other built-ins,
* host objects, primitives, etc, will not work.
* @returns {Object|Array} A clone of <code>object</code>.
*/
clone: function(object) {
if (Array.isArray(object)) {
return Array.prototype.slice.call(object);
}
var clone = Object.create(Object.getPrototypeOf(object));
mixin(clone, object);
return clone;
},
/**
* Mixes all <code>source</code>'s own enumerable properties into <code>target</code>. Multiple source objects
* can be passed as varargs.
* @name orion.objects.mixin
* @function
* @static
* @param {Object} target
* @param {Object} source
*/
mixin: mixin,
/**
* Wraps an object into an Array if necessary.
* @name orion.objects.toArray
* @function
* @static
* @param {Object} obj An object.
* @returns {Array} Returns <code>obj</code> unchanged, if <code>obj</code> is an Array. Otherwise returns a 1-element Array
* whose sole element is <code>obj</code>.
*/
toArray: function(o) {
return Array.isArray(o) ? o : [o];
}
};
});