Skip to content

Commit

Permalink
fix clone
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Jan 27, 2022
1 parent 5669e40 commit 5ea30b4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
17 changes: 8 additions & 9 deletions func/clone.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var objectToString = require('./staticObjectToString')

var isArray = require('./isArray')
var objectEach = require('./objectEach')
var arrayEach = require('./arrayEach')

function getCtorObject (val, args) {
function getCativeCtor (val, args) {
var Ctor = val.__proto__.constructor
return args ? new Ctor(args) : new Ctor()
}
Expand All @@ -16,34 +15,34 @@ function handleValueClone (item, isDeep) {
function copyValue (val, isDeep) {
if (val) {
switch(objectToString.call(val)) {
case "[object Object]":
case "[object Arguments]": {
var restObj = getCtorObject(val)
case "[object Object]": {
var restObj = Object.create(val)
objectEach(val, function (item, key) {
restObj[key] = handleValueClone(item, isDeep)
})
return restObj
}
case "[object Date]":
case "[object RegExp]": {
return getCtorObject(val, val.valueOf())
return getCativeCtor(val, val.valueOf())
}
case "[object Array]": {
case "[object Array]":
case "[object Arguments]": {
var restArr = []
arrayEach(val, function (item) {
restArr.push(handleValueClone(item, isDeep))
})
return restArr
}
case "[object Set]": {
var restSet = getCtorObject(val)
var restSet = getCativeCtor(val)
restSet.forEach(function (item) {
restSet.add(handleValueClone(item, isDeep))
})
return restSet
}
case "[object Map]": {
var restMap = getCtorObject(val)
var restMap = getCativeCtor(val)
restMap.forEach(function (item, key) {
restMap.set(handleValueClone(item, isDeep))
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-utils",
"version": "3.5.2",
"version": "3.5.3",
"description": "JavaScript 函数库、工具类",
"main": "index.js",
"unpkg": "dist/xe-utils.umd.min.js",
Expand Down
53 changes: 53 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,15 @@ describe('Base functions', () => {
expect(
XEUtils.clone(/\n/)
).toEqual(/\n/)
expect(
XEUtils.clone([11, 22, { bb: 22}])
).toEqual([11, 22, { bb: 22}])
expect(
XEUtils.clone({ aa: 11, bb: [{ hh: 44 }] })
).toEqual({ aa: 11, bb: [{ hh: 44 }] })
expect(
XEUtils.clone([['11', /\d/], [[11, [[new Date()], 22, [{ aa: 33 }, 44]]], { jj: 99 }], { uu: 88 }])
).toEqual([['11', /\d/], [[11, [[new Date()], 22, [{ aa: 33 }, 44]]], { jj: 99 }], { uu: 88 }])

let v1 = {
num: 11,
Expand Down Expand Up @@ -2539,6 +2548,50 @@ describe('Base functions', () => {
expect(
v1.map === v3.map
).toEqual(false)

function Func1 () {
this.hhh = 22
}
Func1.prototype.val = 11

const f1 = new Func1()
const f2 = XEUtils.clone(f1, true)

class Demo1 {
constructor () {
this.name = 1
}
}
const d1 = new Demo1()
const d2 = XEUtils.clone(d1, true)
expect(
d2 instanceof Demo1
).toEqual(true)
expect(d2.name).toEqual(1)
expect(
d1.name === d2.name
).toEqual(true)
expect(
d1 === d2
).toEqual(false)

class Test1 {
constructor (name) {
this.name = name
}
}
const t1 = new Test1(123)
const t2 = XEUtils.clone(t1, true)
expect(
t2 instanceof Test1
).toEqual(true)
expect(t2.name).toEqual(123)
expect(
t1.name === t2.name
).toEqual(true)
expect(
t1 === t2
).toEqual(false)
})

test('clear()', () => {
Expand Down

0 comments on commit 5ea30b4

Please sign in to comment.