@bhbr/copycat 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -4
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Description
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Some functions for object copying:
|
|
4
|
+
|
|
5
|
+
- copy: a shallow copy
|
|
6
|
+
- deepCopy: a deep copy
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,wBAAgB,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAwClC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAM,GAAG,GAAG,CAuF1E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { isInstanceOf } from '@bhbr/upper-class';
|
|
2
|
+
export function copy(obj) {
|
|
3
|
+
// shallow copy
|
|
4
|
+
// numbers, string, booleans
|
|
5
|
+
// are passed by value anyway,
|
|
6
|
+
// so no need to create an explicit copy
|
|
7
|
+
if (typeof obj != 'object' || obj === null) {
|
|
8
|
+
return obj;
|
|
9
|
+
}
|
|
10
|
+
// Arrays are by default passed by reference.
|
|
11
|
+
// So for a copy, we create a new
|
|
12
|
+
// empty Array and fill it
|
|
13
|
+
if (obj.constructor.name == 'Array') {
|
|
14
|
+
let newObj = [];
|
|
15
|
+
for (let x of obj) {
|
|
16
|
+
newObj.push(x);
|
|
17
|
+
}
|
|
18
|
+
return newObj;
|
|
19
|
+
}
|
|
20
|
+
// If the object has a copy method (with no arguments), use that
|
|
21
|
+
if (obj.copy !== undefined) {
|
|
22
|
+
if (typeof obj.copy === 'function') {
|
|
23
|
+
let copy = obj.copy();
|
|
24
|
+
if (copy !== undefined) {
|
|
25
|
+
return copy;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Objects have a convenience method:
|
|
30
|
+
// assign all of the original object's
|
|
31
|
+
// properties to an empty object
|
|
32
|
+
return Object.assign(new obj.constructor(), obj);
|
|
33
|
+
// This is a "shallow" copy though:
|
|
34
|
+
// Porperties that are objects will be shared
|
|
35
|
+
// with the original. Modifying or reassigning
|
|
36
|
+
// them in the original will affect the copy,
|
|
37
|
+
// and vice versa
|
|
38
|
+
}
|
|
39
|
+
export function deepCopy(obj, memo = []) {
|
|
40
|
+
// A deep copy recursively creates copies of all the objects
|
|
41
|
+
// encountered as properties. Shared objects (i. e.
|
|
42
|
+
// the same object assigned to different properties)
|
|
43
|
+
// must be tracked and their identity retained.
|
|
44
|
+
// This is done via memoization and the reason
|
|
45
|
+
// why this implementation is so convoluted
|
|
46
|
+
if (obj['deepCopy'] !== undefined) {
|
|
47
|
+
if (typeof obj['deepCopy'] === 'function') {
|
|
48
|
+
let copy = obj['deepCopy']();
|
|
49
|
+
if (copy !== undefined) {
|
|
50
|
+
return copy;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (typeof obj != 'object' || obj === null) {
|
|
55
|
+
return obj;
|
|
56
|
+
}
|
|
57
|
+
if (obj.constructor.name.endsWith('Event')) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (obj.constructor.name == 'Array') {
|
|
61
|
+
let newObj = [];
|
|
62
|
+
memo.push([obj, newObj]);
|
|
63
|
+
for (let value of obj) {
|
|
64
|
+
var copiedValue;
|
|
65
|
+
var alreadyCopied = false;
|
|
66
|
+
for (let pair of memo) {
|
|
67
|
+
if (pair[0] === value) {
|
|
68
|
+
alreadyCopied = true;
|
|
69
|
+
copiedValue = pair[1];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (alreadyCopied) {
|
|
73
|
+
newObj.push(copiedValue);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
let y = deepCopy(value, memo);
|
|
77
|
+
newObj.push(y);
|
|
78
|
+
memo.push([value, y]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return newObj;
|
|
82
|
+
}
|
|
83
|
+
let newObj;
|
|
84
|
+
if (isInstanceOf(obj, 'Element')) {
|
|
85
|
+
newObj = document.createElement(obj['namespaceURI'], obj['localName']);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
newObj = Object.create(obj.constructor.prototype);
|
|
89
|
+
}
|
|
90
|
+
memo.push([obj, newObj]);
|
|
91
|
+
if (newObj === null) {
|
|
92
|
+
return newObj;
|
|
93
|
+
}
|
|
94
|
+
for (let [key, value] of Object.entries(obj)) {
|
|
95
|
+
var copiedValue;
|
|
96
|
+
var alreadyCopied = false;
|
|
97
|
+
for (let pair of memo) {
|
|
98
|
+
if (pair[0] === value) {
|
|
99
|
+
alreadyCopied = true;
|
|
100
|
+
copiedValue = pair[1];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (alreadyCopied) {
|
|
104
|
+
newObj[key] = copiedValue;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (typeof value == 'object') {
|
|
108
|
+
newObj[key] = deepCopy(value, memo);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
newObj[key] = value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return newObj;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAA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AED,OAAO,MAAM,CAAA;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bhbr/copycat",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Some functions for object copying (deep and shallow)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/",
|
|
16
|
+
"!dist/_tests"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "./build.sh",
|
|
20
|
+
"test": "node dist/_tests/tests.js",
|
|
21
|
+
"prepack": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/bhbr/copycat"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/bhbr/copycat/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/bhbr/copycat#readme",
|
|
31
|
+
"keywords": [],
|
|
32
|
+
"author": "Ben Hambrecht",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@bhbr/upper-class": "^1.0.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@bhbr/tsts": "^1.1.3",
|
|
39
|
+
"@bhbr/egalite": "^1.2.0"
|
|
40
|
+
}
|
|
41
|
+
}
|