Skip to content

Commit

Permalink
fix toFormValues
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Oct 29, 2023
1 parent d01797a commit c4c4190
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"preview": "vite preview",
"build-only": "vite build -l error",
"type-check": "vue-tsc --noEmit",
"test:vitest": "vitest",
"test": "vitest",
"postversion": "git push && git push --tags"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/use/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ export function makeDto(requestDto:string, obj?:any, ctx:{ createResponse?:() =>
return new dtoCtor(obj)
}

/** Convert Request DTO values to supported HTML Input values */
/** Mutates Request DTO values to supported HTML Input values */
export function toFormValues(dto:any, metaType?:MetadataType|null) {
if (!dto) return {}
Object.keys(dto).forEach((key:string) => {
let value = dto[key]
if (typeof value == 'string') {
if (value.startsWith('/Date'))
dto[key] = dateInputFormat(toDate(value))
} else if (typeof value == 'object') {
} else if (value != null && typeof value == 'object') {
// shallow clone
if (Array.isArray(value)) {
dto[key] = Array.from(value)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createApp,h } from 'vue'
import { test, expect} from 'vitest'
import { useMetadata } from '../../src/index'

test('toFormValues', () => {
const { toFormValues } = useMetadata()
const a = { a: "foo", b: 1, c:null }
const b = toFormValues(a)

// toFormValues mutates and returns original argument
expect(a === b).true
expect(a.a).eq('foo')
expect(a.b).eq(1)
expect(a.c).eq(null)
})

0 comments on commit c4c4190

Please sign in to comment.