mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
64896d70b0
This reflects changes in Google's policy on JavaScript license headers, which should be smaller to avoid increasing the size of the binary unnecessarily. This also updates the company name from "Google, Inc" to "Google LLC". Change-Id: I3f8b9ed3700b6351f43173d50c94d35c333e82b4
92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
/** @license
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.provide('shaka.util.ObjectUtils');
|
|
|
|
|
|
shaka.util.ObjectUtils = class {
|
|
/**
|
|
* Performs a deep clone of the given simple object. This does not copy
|
|
* prototypes, custom properties (e.g. read-only), or multiple references to
|
|
* the same object. If the caller needs these fields, it will need to set
|
|
* them after this returns.
|
|
*
|
|
* @template T
|
|
* @param {T} arg
|
|
* @return {T}
|
|
*/
|
|
static cloneObject(arg) {
|
|
const seenObjects = new Set();
|
|
// This recursively clones the value |val|, using the captured variable
|
|
// |seenObjects| to track the objects we have already cloned.
|
|
const clone = (val) => {
|
|
switch (typeof val) {
|
|
case 'undefined':
|
|
case 'boolean':
|
|
case 'number':
|
|
case 'string':
|
|
case 'symbol':
|
|
case 'function':
|
|
return val;
|
|
case 'object':
|
|
default: {
|
|
// typeof null === 'object'
|
|
if (!val) {
|
|
return val;
|
|
}
|
|
|
|
// This covers Uint8Array and friends, even without a TypedArray
|
|
// base-class constructor.
|
|
const isTypedArray =
|
|
val.buffer && val.buffer.constructor == ArrayBuffer;
|
|
if (isTypedArray) {
|
|
return val;
|
|
}
|
|
|
|
if (seenObjects.has(val)) {
|
|
return null;
|
|
}
|
|
|
|
const isArray = val.constructor == Array;
|
|
if (val.constructor != Object && !isArray) {
|
|
return null;
|
|
}
|
|
|
|
seenObjects.add(val);
|
|
const ret = isArray ? [] : {};
|
|
// Note |name| will equal a number for arrays.
|
|
for (const name in val) {
|
|
ret[name] = clone(val[name]);
|
|
}
|
|
|
|
// Length is a non-enumerable property, but we should copy it over in
|
|
// case it is not the default.
|
|
if (isArray) {
|
|
ret.length = val.length;
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
};
|
|
return clone(arg);
|
|
}
|
|
|
|
/**
|
|
* Performs a shallow clone of the given simple object. This does not copy
|
|
* prototypes or custom properties (e.g. read-only).
|
|
*
|
|
* @template T
|
|
* @param {T} original
|
|
* @return {T}
|
|
*/
|
|
static shallowCloneObject(original) {
|
|
const clone = /** @type {?} */({});
|
|
for (const k in original) {
|
|
clone[k] = original[k];
|
|
}
|
|
return clone;
|
|
}
|
|
};
|