clone-zmj 1.0.0
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/dist/bundle.js +32 -0
- package/index.html +19 -0
- package/package.json +15 -0
- package/rollup.config.js +12 -0
- package/src/clone.js +23 -0
package/dist/bundle.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var _ = (function (exports) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
function type(data) {
|
|
5
|
+
return Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function clone(data) {
|
|
9
|
+
const t = type(data);
|
|
10
|
+
if (t !== "object" && t !== "array") {
|
|
11
|
+
return data
|
|
12
|
+
}
|
|
13
|
+
let target = t === "object" ? {} : [];
|
|
14
|
+
if (t === "object") {
|
|
15
|
+
for (let i in data) {
|
|
16
|
+
if (data.hasOwnProperty(i)) {
|
|
17
|
+
target[i] = clone(data[i]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
for (let i = 0; i < data.length; i++) {
|
|
22
|
+
target[i] = clone(data[i]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return target
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.clone = clone;
|
|
29
|
+
|
|
30
|
+
return exports;
|
|
31
|
+
|
|
32
|
+
})({});
|
package/index.html
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
+
<title>Document</title>
|
|
8
|
+
<script src="./dist/bundle.js"></script>
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<script>
|
|
13
|
+
const obj = { a: 1 }
|
|
14
|
+
const copy = _.clone(obj)
|
|
15
|
+
console.log(copy)
|
|
16
|
+
</script>
|
|
17
|
+
</body>
|
|
18
|
+
|
|
19
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clone-zmj",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "zmj深拷贝库",
|
|
5
|
+
"main": "clone.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "rollup -c"
|
|
9
|
+
},
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"rollup": "^3.26.0"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
// https://cn.rollupjs.org/configuration-options/#input
|
|
3
|
+
input: 'src/clone.js',
|
|
4
|
+
output: {
|
|
5
|
+
// https://cn.rollupjs.org/configuration-options/#output-file
|
|
6
|
+
file: 'dist/bundle.js',
|
|
7
|
+
// https://cn.rollupjs.org/configuration-options/#output-format
|
|
8
|
+
format: 'iife',
|
|
9
|
+
// https://cn.rollupjs.org/configuration-options/#output-name
|
|
10
|
+
name: '_',
|
|
11
|
+
},
|
|
12
|
+
}
|
package/src/clone.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function type(data) {
|
|
2
|
+
return Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function clone(data) {
|
|
6
|
+
const t = type(data)
|
|
7
|
+
if (t !== "object" && t !== "array") {
|
|
8
|
+
return data
|
|
9
|
+
}
|
|
10
|
+
let target = t === "object" ? {} : [];
|
|
11
|
+
if (t === "object") {
|
|
12
|
+
for (let i in data) {
|
|
13
|
+
if (data.hasOwnProperty(i)) {
|
|
14
|
+
target[i] = clone(data[i])
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
for (let i = 0; i < data.length; i++) {
|
|
19
|
+
target[i] = clone(data[i])
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return target
|
|
23
|
+
}
|