Skip to content

Commit 5225c67

Browse files
committed
updating docs
1 parent 37c8fa4 commit 5225c67

File tree

10 files changed

+438
-65
lines changed

10 files changed

+438
-65
lines changed

Diff for: .prettierrc.cjs

-7
This file was deleted.

Diff for: .prettierrc.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @see https://prettier.io/docs/en/configuration.html
3+
* @type {import("prettier").Config}
4+
*/
5+
export default {
6+
printWidth: 80,
7+
trailingComma: 'all',
8+
arrowParens: 'always',
9+
semi: false,
10+
singleQuote: true,
11+
}

Diff for: .vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"[ts]": {
66
"editor.formatOnSave": false
77
},
8+
"[markdown]": {
9+
"editor.formatOnSave": true
10+
},
811
"editor.codeActionsOnSave": {
912
"source.fixAll.eslint": "explicit"
1013
}

Diff for: packages/core/__tests__/utils.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,30 @@ describe('`toValidableType()`', () => {
330330
})
331331
})
332332

333+
describe('`deepClone()`', () => {
334+
it('deep clones an object', () => {
335+
const obj = { a: 1, b: { c: 2 } }
336+
337+
const clone = utils.deepClone(obj)
338+
339+
expect(clone).not.toBe(obj)
340+
341+
clone.b.c = 3
342+
expect(obj.b.c).not.toBe(clone.b.c)
343+
})
344+
345+
it('deep clones an array', () => {
346+
const arr = [{ b: { c: 2 } }]
347+
const clone = utils.deepClone(arr)
348+
349+
expect(clone).not.toBe(arr)
350+
351+
clone[0].b.c = 3
352+
353+
expect(arr[0].b.c).not.toBe(clone[0].b.c)
354+
})
355+
})
356+
333357
describe('`clone()`', () => {
334358
it('clones an object', () => {
335359
const obj = { a: true }

Diff for: packages/core/src/global-this.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Support globalThis in E2019 and lower
3+
* @see https://mathiasbynens.be/notes/globalthis
4+
*/
5+
/* eslint-disable @typescript-eslint/ban-ts-comment */
6+
;(function () {
7+
if (typeof globalThis === 'object') return
8+
Object.defineProperty(Object.prototype, '__magic__', {
9+
get: function () {
10+
return this
11+
},
12+
configurable: true, // This makes it possible to `delete` the getter later.
13+
})
14+
// @ts-expect-error
15+
__magic__.globalThis = __magic__ // lolwat
16+
// @ts-expect-error
17+
delete Object.prototype.__magic__
18+
})()

Diff for: packages/core/src/shim.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { isPlainObject } from './is-plain-obj'
3+
import './global-this'
34
import { typeDefaults } from './sensibles'
45
import { config } from './config'
56
import type { VueTypesDefaults } from './types'
@@ -12,6 +13,19 @@ const isArray =
1213
return Object.prototype.toString.call(value) === '[object Array]'
1314
}
1415

16+
function deepClone<T>(input: T): T {
17+
if ('structuredClone' in globalThis) {
18+
return structuredClone(input)
19+
}
20+
if (Array.isArray(input)) {
21+
return [...input] as T
22+
}
23+
if (isPlainObject(input)) {
24+
return Object.assign({}, input)
25+
}
26+
return input
27+
}
28+
1529
function type(name: string, props: any = {}, validable = false): any {
1630
const descriptors: PropertyDescriptorMap = {
1731
_vueTypes_name: {
@@ -29,9 +43,9 @@ function type(name: string, props: any = {}, validable = false): any {
2943
return this
3044
}
3145
if (isArray(v)) {
32-
t.default = () => [].concat(v as any)
46+
t.default = () => deepClone(v)
3347
} else if (isPlainObject(v)) {
34-
t.default = () => Object.assign({}, v)
48+
t.default = () => deepClone(v)
3549
} else {
3650
t.default = v
3751
}

Diff for: packages/core/src/utils.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './global-this'
12
import { config } from './config'
23
import {
34
VueTypeDef,
@@ -36,6 +37,19 @@ export function getNativeType(value: any): string {
3637
return match ? match[1].replace(/^Async/, '') : ''
3738
}
3839

40+
export function deepClone<T>(input: T): T {
41+
if ('structuredClone' in globalThis) {
42+
return structuredClone(input)
43+
}
44+
if (Array.isArray(input)) {
45+
return [...input] as T
46+
}
47+
if (isPlainObject(input)) {
48+
return Object.assign({}, input)
49+
}
50+
return input
51+
}
52+
3953
/**
4054
* No-op function
4155
*/
@@ -295,9 +309,9 @@ export function toType<T = any>(name: string, obj: PropOptions<T>) {
295309
return this
296310
}
297311
if (isArray(def)) {
298-
this.default = () => [...def]
312+
this.default = () => deepClone(def)
299313
} else if (isPlainObject(def)) {
300-
this.default = () => Object.assign({}, def)
314+
this.default = () => deepClone(def)
301315
} else {
302316
this.default = def
303317
}

Diff for: packages/docs/advanced/typescript.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ props: {
252252
}
253253
```
254254

255-
::: warning
255+
::: warning {id=oneof-warning}
256256

257257
Note that union types don't put any constrain on the presence of all of their members in the validation array. This can lead to runtime bugs not detected by the type checker:
258258

Diff for: packages/docs/guide/namespaced.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
---
22
---
3+
<script setup>
4+
import CodeExample from '../components/CodeExample.vue'
5+
</script>
36

47
# Namespaced Usage
58

69
The default export of `vue-types` exposes an ES6 class object that mimics React prop-type.
710

8-
The class object exposes both [native](/guide/validators.html#native-validators) and [custom](/guide/validators.html#custom-validators) validators.
11+
The class object exposes both [native](./validators.md#native-validators) and [custom](./validators.md#custom-validators) validators.
12+
13+
::: tip
14+
While namespaced usage is not deprecated, [named validators](./validators.md) are usually a better and more type-safe option for your project.
15+
:::
916

1017
## Native Validators
1118

1219
Native validators are exposed as static getter factories:
1320

21+
<CodeExample>
22+
1423
```js
1524
import VueTypes from 'vue-types'
1625

@@ -20,11 +29,26 @@ export default {
2029
},
2130
}
2231
```
32+
---
33+
```js
34+
import VueTypes from 'vue-types'
2335

24-
The main difference between namespaced native validators and those directly imported from the library is that the former come (usually) with a default value already defined.
36+
defineProps({
37+
message: VueTypes.string.isRequired,
38+
})
39+
```
40+
</CodeExample>
41+
42+
::: warning NOTE
43+
44+
The main difference between namespaced native validators and those directly imported from the library is that the former come (usually) **with a default value already defined** (see table below).
45+
46+
:::
2547

2648
<div id="default-values">
2749

50+
### Native validators default values
51+
2852
| Validator | Default | `validate()` method |
2953
| --------- | ---------- | ------------------- |
3054
| any | - | yes |
@@ -53,7 +77,7 @@ const stringProp = VueTypes.string
5377
// stringProp === { type: String, default : '' }
5478
```
5579

56-
## Native Types Configuration
80+
## Native types configuration
5781

5882
All native validators (with the exception of `any` and `symbol`) come with a sensible default value. To customize or disable that value, you can set the global option `VueTypes.sensibleDefaults`:
5983

0 commit comments

Comments
 (0)