1
0
mirror of synced 2025-01-19 00:41:43 +03:00

Adds array support to cloneDeep

This commit is contained in:
Justin Schroeder 2020-04-19 14:34:25 -04:00
parent 8b25eee634
commit 978a209e3e
3 changed files with 54 additions and 20 deletions

View File

@ -1,27 +1,38 @@
<template>
<div class="specimens specimens--group">
<FormulateInput
v-model="groupValue"
label="Invite some new users"
type="group"
placeholder="users"
help="Fields can be grouped"
<FormulateForm
v-model="formResult"
@submit="save"
>
<FormulateInput
label="First and last name"
name="Name"
type="text"
placeholder="Users name"
/>
name="users"
label="Invite some new users"
type="group"
placeholder="users"
help="Fields can be grouped"
>
<FormulateInput
label="First and last name"
name="name"
type="text"
placeholder="Users name"
/>
<FormulateInput
name="email"
label="Email address"
type="email"
placeholder="Users email"
validation="required|email"
/>
</FormulateInput>
<FormulateInput
name="Email"
label="Email address"
type="email"
placeholder="Users email"
validation="required|email"
type="submit"
/>
</FormulateInput>
{{ groupValue }}
</FormulateForm>
<span>Form Values</span>
<pre>{{ formResult }}</pre>
<span>Save Values</span>
<pre>{{ saveValues }}</pre>
</div>
</template>
@ -29,7 +40,13 @@
export default {
data () {
return {
groupValue: null
formResult: null,
saveValues: null
}
},
methods: {
save (values) {
this.saveValues = values
}
}
}

View File

@ -178,7 +178,11 @@ export function isValueType (data) {
* case of needing to unbind reactive watchers.
*/
export function cloneDeep (obj) {
const newObj = {}
if (typeof obj !== 'object') {
return obj
}
const isArr = Array.isArray(obj)
const newObj = isArr ? [] : {}
for (const key in obj) {
if (obj[key] instanceof FileUpload || isValueType(obj[key])) {
newObj[key] = obj[key]

View File

@ -116,6 +116,19 @@ describe('cloneDeep', () => {
const clone = cloneDeep({ a: 123, b: c })
expect(clone.b === c).toBe(false)
})
it('retains array structures inside of a pojo', () => {
const obj = { a: 'abcd', d: ['first', 'second'] }
const clone = cloneDeep(obj)
expect(Array.isArray(clone.d)).toBe(true)
})
it('removes references inside array structures', () => {
const deepObj = {foo: 'bar'}
const obj = { a: 'abcd', d: ['first', deepObj] }
const clone = cloneDeep(obj)
expect(clone.d[1] === deepObj).toBe(false)
})
})
describe('snakeToCamel', () => {