ivue 1.0.2 → 1.0.4

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.
Files changed (114) hide show
  1. package/.idea/codeStyles/Project.xml +65 -0
  2. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  3. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  4. package/.idea/ivue.iml +8 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/php.xml +19 -0
  7. package/.idea/vcs.xml +6 -0
  8. package/README.md +23 -7
  9. package/coverage/clover.xml +159 -159
  10. package/coverage/coverage-final.json +1 -1
  11. package/coverage/index.html +17 -17
  12. package/coverage/index.ts.html +86 -107
  13. package/dist/index.es.js +74 -90
  14. package/dist/index.umd.js +1 -1
  15. package/dist/src/index.d.ts +1 -196
  16. package/dist/src/ivue.d.ts +218 -0
  17. package/docs/docs/.vitepress/config.ts +110 -22
  18. package/docs/docs/.vitepress/dist/404.html +4 -4
  19. package/docs/docs/.vitepress/dist/api/ivue.html +8 -8
  20. package/docs/docs/.vitepress/dist/api/propsWithDefaults.html +8 -8
  21. package/docs/docs/.vitepress/dist/assets/{api_ivue.md.BCZ4q_0D.js → api_ivue.md.MRwb9Lcb.js} +1 -1
  22. package/docs/docs/.vitepress/dist/assets/{api_ivue.md.BCZ4q_0D.lean.js → api_ivue.md.MRwb9Lcb.lean.js} +1 -1
  23. package/docs/docs/.vitepress/dist/assets/{api_propsWithDefaults.md.Cr5FwI9o.lean.js → api_propsWithDefaults.md.CeE4IC-K.js} +1 -1
  24. package/docs/docs/.vitepress/dist/assets/{api_propsWithDefaults.md.Cr5FwI9o.js → api_propsWithDefaults.md.CeE4IC-K.lean.js} +1 -1
  25. package/docs/docs/.vitepress/dist/assets/{app.DRiljpSE.js → app.ConZRsN0.js} +1 -1
  26. package/docs/docs/.vitepress/dist/assets/chunks/@localSearchIndexroot.CGLWArGd.js +1 -0
  27. package/docs/docs/.vitepress/dist/assets/chunks/Button.vue_vue_type_script_setup_true_lang.P7niV-n8.js +1 -0
  28. package/docs/docs/.vitepress/dist/assets/chunks/VPLocalSearchBox.C7VDyivS.js +7 -0
  29. package/docs/docs/.vitepress/dist/assets/chunks/framework.CNtPHGsN.js +1 -0
  30. package/docs/docs/.vitepress/dist/assets/chunks/index.es.DnWLDAUc.js +1 -0
  31. package/docs/docs/.vitepress/dist/assets/chunks/theme.B7n-r1TY.js +2 -0
  32. package/docs/docs/.vitepress/dist/assets/index.md.wPLoTLBY.js +1 -0
  33. package/docs/docs/.vitepress/dist/assets/index.md.wPLoTLBY.lean.js +1 -0
  34. package/docs/docs/.vitepress/dist/assets/pages_advanced-usage.md.BZgX-VP7.js +1 -0
  35. package/docs/docs/.vitepress/dist/assets/pages_advanced-usage.md.BZgX-VP7.lean.js +1 -0
  36. package/docs/docs/.vitepress/dist/assets/pages_api.md.C_PvCZns.js +7 -0
  37. package/docs/docs/.vitepress/dist/assets/pages_api.md.C_PvCZns.lean.js +7 -0
  38. package/docs/docs/.vitepress/dist/assets/pages_browse-code.md.CPluoaap.js +1 -0
  39. package/docs/docs/.vitepress/dist/assets/pages_browse-code.md.CPluoaap.lean.js +1 -0
  40. package/docs/docs/.vitepress/dist/assets/pages_getting-started.md.knQcOZyP.js +8 -0
  41. package/docs/docs/.vitepress/dist/assets/pages_getting-started.md.knQcOZyP.lean.js +1 -0
  42. package/docs/docs/.vitepress/dist/assets/pages_guidelines.md.DddPdPom.js +101 -0
  43. package/docs/docs/.vitepress/dist/assets/pages_guidelines.md.DddPdPom.lean.js +101 -0
  44. package/docs/docs/.vitepress/dist/assets/pages_how-it-works.md.OJGcGEXF.js +4 -0
  45. package/docs/docs/.vitepress/dist/assets/pages_how-it-works.md.OJGcGEXF.lean.js +4 -0
  46. package/docs/docs/.vitepress/dist/assets/pages_how-its-made.md.Vv9Z9TEi.js +1 -0
  47. package/docs/docs/.vitepress/dist/assets/pages_how-its-made.md.Vv9Z9TEi.lean.js +1 -0
  48. package/docs/docs/.vitepress/dist/assets/pages_introduction.md.DrDm-R8F.js +1 -0
  49. package/docs/docs/.vitepress/dist/assets/pages_introduction.md.DrDm-R8F.lean.js +1 -0
  50. package/docs/docs/.vitepress/dist/assets/pages_usage.md.zSOVjtFF.js +430 -0
  51. package/docs/docs/.vitepress/dist/assets/pages_usage.md.zSOVjtFF.lean.js +430 -0
  52. package/docs/docs/.vitepress/dist/assets/style.Fgx7OB_D.css +1 -0
  53. package/docs/docs/.vitepress/dist/hashmap.json +1 -1
  54. package/docs/docs/.vitepress/dist/index.html +9 -8
  55. package/docs/docs/.vitepress/dist/pages/advanced-usage.html +24 -0
  56. package/docs/docs/.vitepress/dist/pages/api.html +30 -0
  57. package/docs/docs/.vitepress/dist/pages/browse-code.html +24 -0
  58. package/docs/docs/.vitepress/dist/pages/getting-started.html +11 -11
  59. package/docs/docs/.vitepress/dist/pages/guidelines.html +125 -0
  60. package/docs/docs/.vitepress/dist/pages/how-it-works.html +11 -8
  61. package/docs/docs/.vitepress/dist/pages/how-its-made.html +24 -0
  62. package/docs/docs/.vitepress/dist/pages/introduction.html +9 -8
  63. package/docs/docs/.vitepress/dist/pages/usage.html +428 -14
  64. package/docs/docs/.vitepress/theme/index.js +5 -0
  65. package/docs/docs/.vitepress/theme/main.css +29 -0
  66. package/docs/docs/components/Button.vue +1 -1
  67. package/docs/docs/components/guidelines/CounterExternalRefsDetailed.vue +41 -0
  68. package/docs/docs/components/usage/CounterBasic.vue +17 -0
  69. package/docs/docs/components/usage/CounterComposables.vue +19 -0
  70. package/docs/docs/components/usage/CounterComposablesDestructuring.vue +46 -0
  71. package/docs/docs/components/usage/CounterComputeds.vue +25 -0
  72. package/docs/docs/components/usage/CounterComputedsDisabled.vue +32 -0
  73. package/docs/docs/components/usage/CounterDefineExpose.vue +12 -0
  74. package/docs/docs/components/usage/CounterDefineExposeAdvanced.vue +25 -0
  75. package/docs/docs/components/usage/CounterDefineExposeClass.ts +11 -0
  76. package/docs/docs/components/usage/CounterExternalRefs.vue +25 -0
  77. package/docs/docs/components/usage/CounterInsideComposables.vue +27 -0
  78. package/docs/docs/components/usage/CounterInternalRefs.vue +26 -0
  79. package/docs/docs/components/usage/CounterLifecycleHooks.vue +22 -0
  80. package/docs/docs/components/usage/CounterWatch.vue +25 -0
  81. package/docs/docs/components/usage/CounterWithProps.vue +29 -0
  82. package/docs/docs/components/usage/CounterWithPropsAndEmits.vue +36 -0
  83. package/docs/docs/components/usage/functions/useMouse.ts +22 -0
  84. package/docs/docs/index.md +10 -9
  85. package/docs/docs/pages/advanced-usage.md +11 -0
  86. package/docs/docs/pages/api.md +106 -0
  87. package/docs/docs/pages/browse-code.md +18 -0
  88. package/docs/docs/pages/getting-started.md +23 -5
  89. package/docs/docs/pages/guidelines.md +128 -0
  90. package/docs/docs/pages/how-it-works.md +71 -0
  91. package/docs/docs/pages/how-its-made.md +32 -0
  92. package/docs/docs/pages/introduction.md +37 -20
  93. package/docs/docs/pages/usage.md +347 -10
  94. package/docs/package.json +4 -3
  95. package/package.json +4 -2
  96. package/src/__tests__/ivue.vitest.spec.ts +228 -132
  97. package/src/index.ts +1 -463
  98. package/src/ivue.ts +495 -0
  99. package/vite.config.ts +4 -0
  100. package/docs/.env +0 -1
  101. package/docs/docs/.vitepress/dist/assets/chunks/framework.CEhE-9HF.js +0 -1
  102. package/docs/docs/.vitepress/dist/assets/chunks/theme.CldUEgAJ.js +0 -1
  103. package/docs/docs/.vitepress/dist/assets/index.md.B1vDz5mb.js +0 -1
  104. package/docs/docs/.vitepress/dist/assets/index.md.B1vDz5mb.lean.js +0 -1
  105. package/docs/docs/.vitepress/dist/assets/pages_getting-started.md.C7qtPEWU.js +0 -8
  106. package/docs/docs/.vitepress/dist/assets/pages_getting-started.md.C7qtPEWU.lean.js +0 -1
  107. package/docs/docs/.vitepress/dist/assets/pages_how-it-works.md.DKizt6Ny.js +0 -1
  108. package/docs/docs/.vitepress/dist/assets/pages_how-it-works.md.DKizt6Ny.lean.js +0 -1
  109. package/docs/docs/.vitepress/dist/assets/pages_introduction.md.Dkqu-XCz.js +0 -1
  110. package/docs/docs/.vitepress/dist/assets/pages_introduction.md.Dkqu-XCz.lean.js +0 -1
  111. package/docs/docs/.vitepress/dist/assets/pages_usage.md.qo7ICfLC.js +0 -17
  112. package/docs/docs/.vitepress/dist/assets/pages_usage.md.qo7ICfLC.lean.js +0 -17
  113. package/docs/docs/.vitepress/dist/assets/style.BSZVsGuP.css +0 -1
  114. package/docs/docs/components/examples/Counter.vue +0 -17
package/src/index.ts CHANGED
@@ -1,463 +1 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
- import type {
4
- ComputedRef,
5
- ExtractPropTypes,
6
- ToRef,
7
- UnwrapNestedRefs,
8
- } from 'vue';
9
- import { computed, reactive, toRef } from 'vue';
10
-
11
- /** Types */
12
- /**
13
- * IVue core reactive instance type with an extended .toRefs() method added.
14
- */
15
- export type IVue<T extends AnyClass> = InstanceType<T> & ExtendWithToRefs<T>;
16
-
17
- /**
18
- * Extend with .toRefs() method, so that a reactive class can be converted to a composable definition.
19
- */
20
- export interface ExtendWithToRefs<T extends AnyClass> {
21
- toRefs: IVueToRefsFn<T>;
22
- }
23
-
24
- /**
25
- * .toRefs() method type definition converts reactive class properties to composable .value properties.
26
- */
27
- export type IVueToRefsFn<T extends AnyClass> = (
28
- props?: (keyof InstanceType<T>)[]
29
- ) => IVueToRefsFnReturn<InstanceType<T>>;
30
-
31
- /**
32
- * Converts a class properties to a composable .value Refs using ToRef vue type,
33
- * also knows NOT to convert functions to .value Refs but to leave them as is.
34
- */
35
- export type IVueToRefsFnReturn<T = any> = {
36
- [K in keyof T]: T[K] extends Function ? T[K] : ToRef<T[K]>;
37
- };
38
-
39
- /**
40
- * Unwraps the returned Refs of composable method T to a reactive type definition.
41
- */
42
- export type UnwrapComposable<T extends (...args: any[]) => any> =
43
- UnwrapNestedRefs<ReturnType<T>>;
44
-
45
- /**
46
- * Extracts object defined emit types by converting them to a plain interface
47
- */
48
- export type ExtractEmitTypes<T extends Record<string, any>> =
49
- UnionToIntersection<
50
- RecordToUnion<{
51
- [K in keyof T]: (evt: K, ...args: Parameters<T[K]>) => void;
52
- }>
53
- >;
54
-
55
- /**
56
- * Extract properties as all assigned properties because they have defaults.
57
- */
58
- export type ExtractPropDefaultTypes<O> = RequiredProperties<O>;
59
-
60
- /**
61
- * Extend slots interface T with prefixed 'before--' & 'after--' slots to create fully extensible flexible slots.
62
- */
63
- export type ExtendSlots<T> = PrefixKeys<T, 'before--'> &
64
- T &
65
- PrefixKeys<T, 'after--'>;
66
-
67
- /** Helper Types. */
68
- export type AnyClass = abstract new (...args: any[]) => any;
69
-
70
- /**
71
- * Descriptors map.
72
- */
73
- export type Descriptors = Map<string, PropertyDescriptor>;
74
-
75
- /**
76
- * Computeds hash map.
77
- */
78
- export type Computeds = Record<string, ComputedRef>;
79
-
80
- /**
81
- * Infer paramaters of a constructor function of a Class.
82
- */
83
- export type InferredArgs<T> = T extends { new (...args: infer P): any }
84
- ? P
85
- : never[];
86
-
87
- /**
88
- * Prefix keys of an interface T with a prefix P.
89
- */
90
- export type PrefixKeys<T, P extends string | undefined = undefined> = {
91
- [K in Extract<keyof T, string> as P extends string ? `${P}${K}` : K]: T[K];
92
- };
93
-
94
- /**
95
- * Get Interface's property's function arguments Parmeters<F> parameter by key K
96
- */
97
- export type IFnParameter<
98
- T extends Record<any, any>,
99
- P extends keyof T,
100
- K extends number
101
- > = FnParameter<ValueOf<T, P>, K>;
102
-
103
- /**
104
- * Get function arguments Parmeters<F> parameter by key K
105
- */
106
- export type FnParameter<
107
- F extends (...args: any[]) => any,
108
- K extends number
109
- > = Parameters<F>[K];
110
-
111
- /**
112
- * Convert Union Type to Intersection Type.
113
- */
114
- export type UnionToIntersection<U> = (
115
- U extends any ? (k: U) => void : never
116
- ) extends (k: infer I) => void
117
- ? I
118
- : never;
119
-
120
- /**
121
- * Convert Record to Union Type.
122
- */
123
- export type RecordToUnion<T extends Record<string, any>> = T[keyof T];
124
-
125
- /**
126
- * Required Properties converts an interface with possible
127
- * optionally assigned properties like: { optional?: any; }
128
- * to a definitevely defined interface.
129
- */
130
- export type RequiredProperties<O> = {
131
- [K in keyof O]: ValueOf<ExtractPropTypes<O>, K>;
132
- };
133
-
134
- /**
135
- * Gets object T property by key [K].
136
- */
137
- export type ValueOf<T extends Record<any, any>, K> = T[K];
138
- /** Types End. */
139
-
140
- /**
141
- * Store all descriptors for each class prototype processed by
142
- * @see {getAllClassDescriptors}
143
- */
144
- export const descriptorsMap = new Map();
145
-
146
- /**
147
- * Get descriptors of an entire class prototype chain as a Map.
148
- * Completely emulates JavaScript class inheritance chain for getters and setters.
149
- *
150
- * @param className
151
- * @return {Descriptors}
152
- */
153
- export function getAllClassDescriptors(className: AnyClass): Descriptors {
154
- /* Retrieve descriptors from cache */
155
- if (descriptorsMap.has(className)) {
156
- return descriptorsMap.get(className);
157
- }
158
-
159
- const savedDescriptors: Descriptors = new Map();
160
-
161
- let prototype = className.prototype;
162
- while (prototype.constructor !== Object) {
163
- Object.entries(Object.getOwnPropertyDescriptors(prototype)).forEach(
164
- ([propertyName, currentDescriptor]) => {
165
- const savedDescriptor = savedDescriptors.get(propertyName);
166
- /** Emulate inheritance chain of getters and setters. */
167
- if (
168
- savedDescriptor !== currentDescriptor &&
169
- (currentDescriptor?.get || currentDescriptor?.set)
170
- ) {
171
- /** Overwrite descriptors if they were already set. */
172
- if (savedDescriptor?.get)
173
- currentDescriptor.get = savedDescriptor?.get;
174
- if (savedDescriptor?.set)
175
- currentDescriptor.set = savedDescriptor?.set;
176
-
177
- /** Store descriptors. */
178
- savedDescriptors.set(propertyName, currentDescriptor);
179
- }
180
- }
181
- );
182
- /** Walk up the prototype chain. */
183
- prototype = Object.getPrototypeOf(prototype);
184
- }
185
-
186
- /** Save descriptors in the descriptors map for each class. */
187
- descriptorsMap.set(className, savedDescriptors);
188
-
189
- return savedDescriptors;
190
- }
191
-
192
- /**
193
- * Infinite Vue (ivue) class reactive initializer.
194
- *
195
- * Converts class instance to a reactive object,
196
- * where descriptors are converted to computeds.
197
- *
198
- * You can turn off computed behaviour by adding static
199
- * ivue object and setting the getter props to false.
200
- * class ClassName {
201
- * static ivue = {
202
- * getter: false
203
- * }
204
- * // .getter -> will be a standard JS non-computed getter
205
- * get getter () { return 'hello world'; }
206
- * }
207
- *
208
- * @param className Any Class
209
- * @param args Class constructor arguments that you would pass to a `new AnyClass(args...)`
210
- * @returns {IVue<T>}
211
- */
212
- export function ivue<T extends AnyClass>(
213
- className: T,
214
- ...args: InferredArgs<T>
215
- ): IVue<T> {
216
- const descriptors: Descriptors = getAllClassDescriptors(className);
217
- const computeds: Computeds | any = descriptors?.size ? {} : null;
218
-
219
- // @ts-expect-error Abstract class initialization
220
- const vue = reactive(new className(...args));
221
-
222
- /** Setup descriptors as computeds. */
223
- for (const [prop, descriptor] of descriptors) {
224
- /* If prop exists on static getter className.ivue[prop]
225
- * We do not convert it to computed. Because sometimes
226
- * we want a normal getter. */
227
- if ((className as any)?.ivue?.[prop] === false) continue;
228
- /** Convert descriptor to computed. */
229
- Object.defineProperty(vue, prop, {
230
- get: descriptor.get
231
- ? () =>
232
- prop in computeds
233
- ? /** Get the existing computed, because we are in reactive scope, .value will auto unwrap itself. */
234
- computeds[prop]
235
- : /** Create the computed and return it, because we are in reactive scope, .value will auto unwrap itself. */
236
- (computeds[prop] = computed({
237
- get: descriptor.get?.bind(vue) as any,
238
- set: descriptor.set?.bind(vue),
239
- } as any))
240
- : undefined,
241
- set: descriptor.set?.bind(vue),
242
- enumerable: false,
243
- });
244
- }
245
-
246
- Object.defineProperty(vue, 'toRefs', {
247
- get: () => ivueToRefs(vue, descriptors as Descriptors, computeds),
248
- enumerable: false,
249
- });
250
-
251
- /** Run ivue .init() initializer method, if it exists in the class. */
252
- if ('init' in vue) vue.init();
253
-
254
- return vue;
255
- }
256
-
257
- /**
258
- * Store all props for each class prototype processed by:
259
- * @see {getAllClassProperties}
260
- */
261
- export const propsMap: Map<object, Set<string> | any> = new Map();
262
-
263
- /**
264
- * Get properties of an entire class prototype chain as a Map.
265
- */
266
- export function getAllClassProperties(obj: object): Set<string> {
267
- /* Retrieve props from cache */
268
- if (propsMap.has(obj.constructor)) {
269
- return propsMap.get(obj.constructor);
270
- }
271
-
272
- const originalConstructor = obj.constructor;
273
-
274
- const allProps: Set<string> = new Set();
275
- do {
276
- Object.getOwnPropertyNames(obj).forEach((prop) => {
277
- /* 'caller', 'callee', 'arguments', 'constructor' are
278
- * special object properties, so should be skipped. */
279
- if (!['caller', 'callee', 'arguments', 'constructor'].includes(prop)) {
280
- allProps.add(prop);
281
- }
282
- });
283
- obj = Object.getPrototypeOf(obj);
284
- } while (obj.constructor !== Object);
285
-
286
- /** Save props in the props map. */
287
- propsMap.set(originalConstructor, allProps);
288
-
289
- return allProps;
290
- }
291
-
292
- /**
293
- * Convert reactive ivue class to Vue 3 refs.
294
- *
295
- * @param vue @see IVue
296
- * @param descriptors @see Descriptors
297
- * @param computeds @see Computeds
298
- * @returns {ExtendWithToRefs<T>['toRefs']}
299
- */
300
- export function ivueToRefs<T extends AnyClass>(
301
- vue: IVue<T>,
302
- descriptors: Descriptors,
303
- computeds: Computeds
304
- ): ExtendWithToRefs<T>['toRefs'] {
305
- return function (
306
- props?: (keyof InstanceType<T>)[]
307
- ): IVueToRefsFnReturn<InstanceType<T>> {
308
- /** Resulting refs store. */
309
- const result: Record<string | number | symbol, any> = {};
310
-
311
- /** Output specific props only, if props are specified. */
312
- if (Array.isArray(props) && props.length) {
313
- for (let i = 0; i < props.length; i++) {
314
- const prop = props[i] as any;
315
- /** Handle descriptors. */
316
- if (descriptors.has(prop)) {
317
- if (prop in computeds) {
318
- /** Return vue computed with .value from computeds store. */
319
- result[prop] = computeds[prop];
320
- } else {
321
- /** Initialize & store vue computed. */
322
- const descriptor = descriptors.get(prop);
323
- result[prop] = computeds[prop] = computed({
324
- get: descriptor?.get?.bind(vue) as any,
325
- set: descriptor?.set?.bind(vue),
326
- } as any);
327
- }
328
- } else {
329
- /** Handle methods. */
330
- if (typeof vue[prop] === 'function') {
331
- /** Bind method to vue, makes method destructuring point to right instance. */
332
- result[prop] = vue[prop].bind(vue);
333
- } else {
334
- /** Convert simple reactive prop to a Ref. */
335
- result[prop] = toRef(vue, prop);
336
- }
337
- }
338
- }
339
- } else {
340
- /** Convert all props to refs and leave functions as is. */
341
- let allProps: null | Set<string> = new Set(getAllClassProperties(vue));
342
-
343
- /** Convert descriptors (non enumerable by default in JS). */
344
- descriptors.forEach((descriptor, prop) => {
345
- if (prop in computeds) {
346
- /** Return vue computed with .value from computeds store. */
347
- result[prop] = computeds[prop];
348
- } else {
349
- /** Initialize vue computed ref & store it in result. */
350
- result[prop] = computeds[prop] = computed({
351
- get: descriptor.get?.bind(vue) as any,
352
- set: descriptor.set?.bind(vue),
353
- } as any);
354
- }
355
- /** Delete descriptor from props as it was already processed. */
356
- allProps?.delete(prop as string);
357
- });
358
-
359
- allProps.forEach((prop) => {
360
- if (typeof vue[prop] === 'function') {
361
- /** Bind method to vue, makes method destructuring point to right instance. */
362
- result[prop] = vue[prop].bind(vue);
363
- } else {
364
- /** Convert simple reactive prop to a Ref. */
365
- result[prop] = toRef(vue, prop);
366
- }
367
- /** Delete prop from props as it was already processed. */
368
- allProps?.delete(prop as string);
369
- });
370
-
371
- /** Memory optimization. */
372
- allProps = null;
373
- }
374
-
375
- return result as IVueToRefsFnReturn<InstanceType<T>>;
376
- };
377
- }
378
-
379
-
380
- /**
381
- * Vue props interface in defineComponent() style.
382
- */
383
- export type VuePropsObject = Record<
384
- string,
385
- { type: any; default?: any; required?: boolean }
386
- >;
387
-
388
- /**
389
- * Vue Props with default properties declared as existing and having values.
390
- */
391
- export type VuePropsWithDefaults<T extends VuePropsObject> = {
392
- [K in keyof T]: {
393
- type: T[K]['type'];
394
- default: T[K]['default'];
395
- required?: boolean;
396
- };
397
- };
398
-
399
- /**
400
- * Determines if the value is a JavaScript Class.
401
- * Note that class is a class function in JavaScript.
402
- *
403
- * @param val Any value
404
- * @returns boolean If it's a JavaScript Class returns true
405
- */
406
- export function isClass(val: any): boolean {
407
- if (typeof val !== 'function') return false; // Not a function, so not a class function either
408
-
409
- if (!val.prototype) return false; // Arrow function, so not a class
410
-
411
- // Finally -> distinguish between a normal function and a class function
412
- if (Object.getOwnPropertyDescriptor(val, 'prototype')?.writable) { // Has writable prototype
413
- return false; // Normal function
414
- } else {
415
- return true; // Class -> Not a function
416
- }
417
- }
418
-
419
- /**
420
- * Creates props with defaults in defineComponent() style.
421
- *
422
- * Merge defaults regular object with Vue types object
423
- * declared in defineComponent() style.
424
- *
425
- * This is made so that the defaults can be declared "as they are"
426
- * without requiring objects to be function callbacks returning an object.
427
- *
428
- * // You don't need to wrap objects in () => ({ nest: { nest :{} } })
429
- * // You can just delcare them normally.
430
- * const defaults = {
431
- * nest: {
432
- * nest
433
- * }
434
- * }
435
- *
436
- * This function will create the Vue expected callbacks for Objects, Arrays & Classes
437
- * but leave primitive properties and functions intact so that
438
- * the final object is fully defineComponent() style compatible.
439
- *
440
- * @param defaults Regular object of default key -> values
441
- * @param typedProps Props declared in defineComponent() style with type and possibly required declared, but without default
442
- * @returns Props declared in defineComponent() style with all properties having default property declared.
443
- */
444
- export function propsWithDefaults<T extends VuePropsObject>(
445
- defaults: Record<string, any>,
446
- typedProps: T
447
- ): VuePropsWithDefaults<T> {
448
- for (const prop in typedProps) {
449
- if (typeof defaults?.[prop] === 'object' && defaults?.[prop] !== null) {
450
- /** Handle Arrays & Objects -> wrap them with an arrow function. */
451
- typedProps[prop].default = () => defaults?.[prop];
452
- } else {
453
- if (isClass(defaults?.[prop])) {
454
- /** Handle JavaScript Classes -> wrap them with an arrow function */
455
- typedProps[prop].default = () => defaults?.[prop];
456
- } else {
457
- /** Handle JavaScript Function And All primitive properties -> output directly */
458
- typedProps[prop].default = defaults?.[prop];
459
- }
460
- }
461
- }
462
- return typedProps as VuePropsWithDefaults<T>;
463
- }
1
+ export * from './ivue';