Skip to content

Instantly share code, notes, and snippets.

@mauritslamers
Created January 18, 2017 18:46
Show Gist options
  • Select an option

  • Save mauritslamers/ab0b5fa96270ca5b3776ad80e56a923a to your computer and use it in GitHub Desktop.

Select an option

Save mauritslamers/ab0b5fa96270ca5b3776ad80e56a923a to your computer and use it in GitHub Desktop.
SC = {
copy: function (object, deep) {
var ret = object, idx;
// fast paths
if (object) {
if (object.isCopyable) return object.copy(deep);
if (object.clone) return object.clone();
}
switch (SC._nativeTypeOf(object)) {
case "array":
ret = object.slice();
if (deep) {
idx = ret.length;
while (idx--) { ret[idx] = SC.copy(ret[idx], true); }
}
break;
case "object":
ret = {};
for (var key in object) { ret[key] = deep ? SC.copy(object[key], true) : object[key]; }
}
return ret;
},
_nativeTypeOf: function(item) {
if (item === undefined) return SC.T_UNDEFINED;
if (item === null) return SC.T_NULL;
var nativeType = typeof item,
toString;
if (nativeType === "object" || nativeType === "function") {
toString = SC._nativeToString.call(item);
return SC._nativeTypeHash[toString] || "object";
} else {
return nativeType;
}
} ,
// Inlined from jQuery's class2type to avoid dependency.
_nativeTypeHash: {
"[object Boolean]": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Object]": "object"
},
// Inlined from jQuery to avoid dependency.
_nativeToString: Object.prototype.toString,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment