Skip to content

Commit ebc4cee

Browse files
committed
Remove copy function
1 parent 769a4b0 commit ebc4cee

18 files changed

+91
-1081
lines changed

dist/angular-ts.cjs.js

+23-280
Large diffs are not rendered by default.

dist/angular-ts.esm.js

+23-280
Large diffs are not rendered by default.

dist/angular-ts.umd.js

+23-280
Large diffs are not rendered by default.

src/loader.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
minErr,
3-
copy,
43
extend,
54
forEach,
65
getNgAttribute,
@@ -63,7 +62,6 @@ export class Angular {
6362

6463
// Utility methods kept for backwards purposes
6564
this.bind = bind;
66-
this.copy = copy;
6765
this.equals = equals;
6866
this.element = jqLite;
6967
this.extend = extend;

src/ng/animateCss.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { copy } from "./utils";
2-
31
/**
42
* @ngdoc service
53
* @name $animateCss
@@ -14,17 +12,16 @@ import { copy } from "./utils";
1412
*/
1513
export function CoreAnimateCssProvider() {
1614
this.$get = [
17-
"$q",
1815
"$$AnimateRunner",
19-
($q, $$AnimateRunner) =>
16+
($$AnimateRunner) =>
2017
function (element, initialOptions) {
2118
// all of the animation functions should create
2219
// a copy of the options data, however, if a
2320
// parent service has already created a copy then
2421
// we should stick to using that
2522
let options = initialOptions || {};
2623
if (!options.$$prepared) {
27-
options = copy(options);
24+
options = structuredClone(options);
2825
}
2926

3027
// there is no point in applying the styles since

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ import { createEventDirective } from "./directive/ngEventDirs";
270270
* }
271271
* if (!angular.equals(this.items_clone, this.items)) {
272272
* this.log.push('doCheck: items mutated');
273-
* this.items_clone = angular.copy(this.items);
273+
* this.items_clone = structuredClone(this.items);
274274
* }
275275
* };
276276
* }

src/ng/directive/ngOptions.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NODE_TYPE_COMMENT } from "../../constants";
22
import { jqLite, jqLiteRemove, startingTag } from "../../jqLite";
33
import {
4-
copy,
54
equals,
65
forEach,
76
hashKey,
@@ -356,7 +355,9 @@ export const ngOptionsDirective = [
356355
getViewValueFromOption(option) {
357356
// If the viewValue could be an object that may be mutated by the application,
358357
// we need to make a copy and not return the reference to the value on the option.
359-
return trackBy ? copy(option.viewValue) : option.viewValue;
358+
return trackBy
359+
? structuredClone(option.viewValue)
360+
: option.viewValue;
360361
},
361362
};
362363
},

src/ng/directive/ngRepeat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ import { getBlockNodes } from "../../jqLite";
121121
};
122122
123123
$scope.copy = function() {
124-
$scope.friends = angular.copy($scope.friends);
124+
$scope.friends = structuredClone($scope.friends);
125125
};
126126
127127
$scope.reset = function() {
128-
$scope.friends = angular.copy(friends);
128+
$scope.friends = structuredClone(friends);
129129
};
130130
131131
$scope.reset();

src/ng/location.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { jqLite } from "../jqLite";
22
import { urlResolve } from "./urlUtils";
33
import {
4-
copy,
54
encodeUriSegment,
65
forEach,
76
isBoolean,
@@ -582,7 +581,7 @@ const locationPrototype = {
582581
search = search.toString();
583582
this.$$search = parseKeyValue(search);
584583
} else if (isObject(search)) {
585-
search = copy(search, {});
584+
search = structuredClone(search, {});
586585
// remove object undefined or null properties
587586
forEach(search, (value, key) => {
588587
if (value == null) delete search[key];

src/ng/parse.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
/* eslint-disable no-param-reassign */
2-
/* eslint-disable max-classes-per-file */
3-
/* eslint-disable no-use-before-define */
4-
51
import {
6-
copy,
72
createMap,
83
csp,
94
forEach,
@@ -523,7 +518,7 @@ AST.prototype = {
523518
this.peek().text,
524519
)
525520
) {
526-
primary = copy(this.selfReferential[this.consume().text]);
521+
primary = structuredClone(this.selfReferential[this.consume().text]);
527522
} else if (
528523
Object.prototype.hasOwnProperty.call(
529524
this.options.literals,
@@ -2082,7 +2077,7 @@ export function $ParseProvider() {
20822077
var noUnsafeEval = csp().noUnsafeEval;
20832078
var $parseOptions = {
20842079
csp: noUnsafeEval,
2085-
literals: copy(literals),
2080+
literals: structuredClone(literals),
20862081
isIdentifierStart: isFunction(identStart) && identStart,
20872082
isIdentifierContinue: isFunction(identContinue) && identContinue,
20882083
};

src/ng/rootScope.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
isObject,
99
isArrayLike,
1010
isNumberNaN,
11-
copy,
1211
arrayRemove,
1312
equals,
1413
} from "./utils";
@@ -294,10 +293,10 @@ class Scope {
294293
* (see next point)
295294
* - When `objectEquality == true`, inequality of the `watchExpression` is determined
296295
* according to the {@link angular.equals} function. To save the value of the object for
297-
* later comparison, the {@link angular.copy} function is used. This therefore means that
296+
* later comparison, the {@link structuredClone} function is used. This therefore means that
298297
* watching complex objects will have adverse memory and performance implications.
299298
* - This should not be used to watch for changes in objects that are (or contain)
300-
* [File](https://developer.mozilla.org/docs/Web/API/File) objects due to limitations with {@link angular.copy `angular.copy`}.
299+
* [File](https://developer.mozilla.org/docs/Web/API/File) objects due to limitations with {@link structuredClone `structuredClone`}.
301300
* - The watch `listener` may change the model, which may trigger other `listener`s to fire.
302301
* This is achieved by rerunning the watchers until no changes are detected. The rerun
303302
* iteration limit is 10 to prevent an infinite loop deadlock.
@@ -822,7 +821,7 @@ class Scope {
822821
) {
823822
dirty = true;
824823
lastDirtyWatch = watch;
825-
watch.last = watch.eq ? copy(value, null) : value;
824+
watch.last = watch.eq ? structuredClone(value) : value;
826825
fn = watch.fn;
827826
fn(value, last === initWatchVal ? value : last, current);
828827
if (ttl < 5) {

src/ng/utils.js

+5-181
Original file line numberDiff line numberDiff line change
@@ -625,174 +625,6 @@ export function arrayRemove(array, value) {
625625
return index;
626626
}
627627

628-
/**
629-
* Creates a deep copy of the source, which should be an object or an array.
630-
* - If no destination is supplied, a copy of the object or array is created.
631-
* - If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted,
632-
* and then all elements/properties from the source are copied to it.
633-
* - If the source is not an object or array (including null and undefined), the source is returned.
634-
* - If the source is identical to the destination, an exception will be thrown.
635-
*
636-
* @template T The type of the source and destination.
637-
* @param {T} source The source that will be used to make a copy. Can be any type, including primitives, null, and undefined.
638-
* @param {T} [destination] Destination into which the source is copied. If provided, must be of the same type as source.
639-
* @returns {T} A deep copy of the source.
640-
*/
641-
export function copy(source, destination, maxDepth) {
642-
const stackSource = [];
643-
const stackDest = [];
644-
maxDepth = isValidObjectMaxDepth(maxDepth) ? maxDepth : NaN;
645-
646-
if (destination) {
647-
if (isTypedArray(destination) || isArrayBuffer(destination)) {
648-
throw ngMinErr(
649-
"cpta",
650-
"Can't copy! TypedArray destination cannot be mutated.",
651-
);
652-
}
653-
if (source === destination) {
654-
throw ngMinErr(
655-
"cpi",
656-
"Can't copy! Source and destination are identical.",
657-
);
658-
}
659-
660-
// Empty the destination object
661-
if (isArray(destination)) {
662-
destination.length = 0;
663-
} else {
664-
forEach(destination, (value, key) => {
665-
if (key !== "$$hashKey") {
666-
delete destination[key];
667-
}
668-
});
669-
}
670-
671-
stackSource.push(source);
672-
stackDest.push(destination);
673-
return copyRecurse(source, destination, maxDepth);
674-
}
675-
676-
return copyElement(source, maxDepth);
677-
678-
function copyRecurse(source, destination, maxDepth) {
679-
maxDepth--;
680-
if (maxDepth < 0) {
681-
return "...";
682-
}
683-
const h = destination.$$hashKey;
684-
let key;
685-
if (isArray(source)) {
686-
for (let i = 0, ii = source.length; i < ii; i++) {
687-
destination.push(copyElement(source[i], maxDepth));
688-
}
689-
} else if (isBlankObject(source)) {
690-
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
691-
for (key in source) {
692-
destination[key] = copyElement(source[key], maxDepth);
693-
}
694-
} else if (source && typeof source.hasOwnProperty === "function") {
695-
// Slow path, which must rely on hasOwnProperty
696-
for (key in source) {
697-
if (Object.prototype.hasOwnProperty.call(source, key)) {
698-
destination[key] = copyElement(source[key], maxDepth);
699-
}
700-
}
701-
}
702-
setHashKey(destination, h);
703-
return destination;
704-
}
705-
706-
function copyElement(source, maxDepth) {
707-
// Simple values
708-
if (!isObject(source)) {
709-
return source;
710-
}
711-
712-
// Already copied values
713-
const index = stackSource.indexOf(source);
714-
if (index !== -1) {
715-
return stackDest[index];
716-
}
717-
718-
if (isWindow(source) || isScope(source)) {
719-
throw ngMinErr(
720-
"cpws",
721-
"Can't copy! Making copies of Window or Scope instances is not supported.",
722-
);
723-
}
724-
725-
let needsRecurse = false;
726-
let destination = copyType(source);
727-
728-
if (destination === undefined) {
729-
destination = isArray(source)
730-
? []
731-
: Object.create(Object.getPrototypeOf(source));
732-
needsRecurse = true;
733-
}
734-
735-
stackSource.push(source);
736-
stackDest.push(destination);
737-
738-
return needsRecurse
739-
? copyRecurse(source, destination, maxDepth)
740-
: destination;
741-
}
742-
743-
function copyType(source) {
744-
switch (toString.call(source)) {
745-
case "[object Int8Array]":
746-
case "[object Int16Array]":
747-
case "[object Int32Array]":
748-
case "[object Float32Array]":
749-
case "[object Float64Array]":
750-
case "[object Uint8Array]":
751-
case "[object Uint8ClampedArray]":
752-
case "[object Uint16Array]":
753-
case "[object Uint32Array]":
754-
return new source.constructor(
755-
copyElement(source.buffer),
756-
source.byteOffset,
757-
source.length,
758-
);
759-
760-
case "[object ArrayBuffer]":
761-
// Support: IE10
762-
if (!source.slice) {
763-
// If we're in this case we know the environment supports ArrayBuffer
764-
/* eslint-disable no-undef */
765-
const copied = new ArrayBuffer(source.byteLength);
766-
new Uint8Array(copied).set(new Uint8Array(source));
767-
/* eslint-enable */
768-
return copied;
769-
}
770-
return source.slice(0);
771-
772-
case "[object Boolean]":
773-
case "[object Number]":
774-
case "[object String]":
775-
case "[object Date]":
776-
return new source.constructor(source.valueOf());
777-
778-
case "[object RegExp]":
779-
const re = new RegExp(
780-
source.source,
781-
source.toString().match(/[^/]*$/)[0],
782-
);
783-
re.lastIndex = source.lastIndex;
784-
return re;
785-
786-
case "[object Blob]":
787-
return new source.constructor([source], { type: source.type });
788-
}
789-
790-
if (isFunction(source.cloneNode)) {
791-
return source.cloneNode(true);
792-
}
793-
}
794-
}
795-
796628
export function simpleCompare(a, b) {
797629
return a === b || (a !== a && b !== b);
798630
}
@@ -1407,7 +1239,7 @@ export function minErr(module) {
14071239
const template = arguments[1];
14081240
let message = `[${module ? `${module}:` : ""}${code}] `;
14091241
const templateArgs = sliceArgs(arguments, 2).map((arg) =>
1410-
toDebugString(arg, minErrConfig.objectMaxDepth),
1242+
toDebugString(arg),
14111243
);
14121244
let paramPrefix;
14131245
let i;
@@ -1443,17 +1275,9 @@ export function minErr(module) {
14431275
};
14441276
}
14451277

1446-
export function serializeObject(obj, maxDepth) {
1278+
export function serializeObject(obj) {
14471279
const seen = [];
1448-
let copyObj = obj;
1449-
// There is no direct way to stringify object until reaching a specific depth
1450-
// and a very deep object can cause a performance issue, so we copy the object
1451-
// based on this specific depth and then stringify it.
1452-
if (isValidObjectMaxDepth(maxDepth)) {
1453-
// This file is also included in `angular-loader`, so `copy()` might not always be available in
1454-
// the closure. Therefore, it is lazily retrieved as `angular.copy()` when needed.
1455-
copyObj = copy(obj, null, maxDepth);
1456-
}
1280+
let copyObj = structuredClone(obj);
14571281
return JSON.stringify(copyObj, (key, val) => {
14581282
const replace = toJsonReplacer(key, val);
14591283
if (isObject(replace)) {
@@ -1465,15 +1289,15 @@ export function serializeObject(obj, maxDepth) {
14651289
});
14661290
}
14671291

1468-
export function toDebugString(obj, maxDepth) {
1292+
export function toDebugString(obj) {
14691293
if (typeof obj === "function") {
14701294
return obj.toString().replace(/ \{[\s\S]*$/, "");
14711295
}
14721296
if (isUndefined(obj)) {
14731297
return "undefined";
14741298
}
14751299
if (typeof obj !== "string") {
1476-
return serializeObject(obj, maxDepth);
1300+
return serializeObject(obj);
14771301
}
14781302
return obj;
14791303
}

src/ngAnimate/animateQueue.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isArray,
77
isString,
88
isElement,
9-
copy,
109
isDefined,
1110
extend,
1211
} from "../ng/utils";
@@ -400,7 +399,7 @@ export const $$AnimateQueueProvider = [
400399
// we always make a copy of the options since
401400
// there should never be any side effects on
402401
// the input data when running `$animateCss`.
403-
let options = copy(initialOptions);
402+
let options = structuredClone(initialOptions);
404403

405404
let element = stripCommentsFromElement(originalElement);
406405
const node = getDomNode(element);

0 commit comments

Comments
 (0)