@formvk/core 0.1.0 → 1.0.0

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 (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +63 -0
  3. package/package.json +8 -4
  4. package/src/__tests__/array.spec.ts +0 -168
  5. package/src/__tests__/effects.spec.ts +0 -595
  6. package/src/__tests__/externals.spec.ts +0 -101
  7. package/src/__tests__/field.spec.ts +0 -572
  8. package/src/__tests__/form.spec.ts +0 -720
  9. package/src/__tests__/graph.spec.ts +0 -56
  10. package/src/__tests__/heart.spec.ts +0 -60
  11. package/src/__tests__/internals.spec.ts +0 -138
  12. package/src/__tests__/lifecycle.spec.ts +0 -51
  13. package/src/__tests__/object.spec.ts +0 -107
  14. package/src/__tests__/query.spec.ts +0 -100
  15. package/src/__tests__/shared.ts +0 -10
  16. package/src/__tests__/typed.spec.ts +0 -52
  17. package/src/__tests__/void.spec.ts +0 -163
  18. package/src/effects/index.ts +0 -2
  19. package/src/effects/onFieldEffects.ts +0 -97
  20. package/src/effects/onFormEffects.ts +0 -48
  21. package/src/global.d.ts +0 -8
  22. package/src/index.ts +0 -4
  23. package/src/models/ArrayField.ts +0 -133
  24. package/src/models/BaseField.ts +0 -359
  25. package/src/models/Field.ts +0 -450
  26. package/src/models/Form.ts +0 -586
  27. package/src/models/Graph.ts +0 -56
  28. package/src/models/Heart.ts +0 -75
  29. package/src/models/LifeCycle.ts +0 -42
  30. package/src/models/ObjectField.ts +0 -48
  31. package/src/models/Query.ts +0 -97
  32. package/src/models/VoidField.ts +0 -64
  33. package/src/models/index.ts +0 -9
  34. package/src/models/types.ts +0 -9
  35. package/src/shared/checkers.ts +0 -85
  36. package/src/shared/constants.ts +0 -60
  37. package/src/shared/effective.ts +0 -69
  38. package/src/shared/externals.ts +0 -65
  39. package/src/shared/internals.ts +0 -965
  40. package/src/types.ts +0 -401
  41. package/tsconfig.build.json +0 -9
  42. package/tsconfig.json +0 -4
  43. package/vite.config.ts +0 -46
@@ -1,42 +0,0 @@
1
- import { each, isFn, isStr } from '@formvk/shared'
2
-
3
- import type { LifeCycleHandler, LifeCyclePayload } from '../types'
4
-
5
- type LifeCycleParams<Payload> = Array<string | LifeCycleHandler<Payload> | { [key: string]: LifeCycleHandler<Payload> }>
6
- export class LifeCycle<Payload = any> {
7
- private listener: LifeCyclePayload<Payload>
8
-
9
- constructor(...params: LifeCycleParams<Payload>) {
10
- this.listener = this.buildListener(params)
11
- }
12
- buildListener = (params: any[]) => {
13
- return function (this: any, payload: { type: string; payload: Payload }, ctx: any) {
14
- for (let index = 0; index < params.length; index++) {
15
- const item = params[index]
16
- if (isFn(item)) {
17
- item.call(this, payload, ctx)
18
- } else if (isStr(item) && isFn(params[index + 1])) {
19
- if (item === payload.type) {
20
- params[index + 1].call(this, payload.payload, ctx)
21
- }
22
- index++
23
- } else {
24
- each<any, any>(item, (handler, type) => {
25
- if (isFn(handler) && isStr(type)) {
26
- if (type === payload.type) {
27
- handler.call(this, payload.payload, ctx)
28
- return false
29
- }
30
- }
31
- })
32
- }
33
- }
34
- }
35
- }
36
-
37
- notify = <P>(type: any, payload?: P, ctx?: any) => {
38
- if (isStr(type)) {
39
- this.listener.call(ctx, { type, payload: payload as unknown as Payload }, ctx)
40
- }
41
- }
42
- }
@@ -1,48 +0,0 @@
1
- import { reaction } from '@formvk/reactive'
2
-
3
- import { cleanupObjectChildren } from '../shared/internals'
4
- import type { FormPathPattern, IFieldProps } from '../types'
5
- import { Field } from './Field'
6
- import type { Form } from './Form'
7
-
8
- export class ObjectField<
9
- Value extends object = Record<string, any>,
10
- ComponentProps = any,
11
- DecoratorProps = any,
12
- IRecord extends object = any,
13
- > extends Field<Value, ComponentProps, DecoratorProps, IRecord> {
14
- displayName = 'ObjectField'
15
- private additionalProperties: string[] = []
16
- constructor(address: FormPathPattern, props: IFieldProps<any, any, any, Value>, form: Form, designable?: boolean) {
17
- super(address, props, form, designable)
18
- this.makeAutoCleanable()
19
- }
20
-
21
- protected makeAutoCleanable() {
22
- this.disposers.push(
23
- reaction(
24
- () => Object.keys(this.value || {}),
25
- newKeys => {
26
- const filterKeys = this.additionalProperties.filter(key => !newKeys.includes(key))
27
- cleanupObjectChildren(this, filterKeys)
28
- }
29
- )
30
- )
31
- }
32
-
33
- addProperty = (key: string, value: any) => {
34
- this.form.setValuesIn(this.path.concat(key), value)
35
- this.additionalProperties.push(key)
36
- return this.onInput(this.value)
37
- }
38
-
39
- removeProperty = (key: string) => {
40
- this.form.deleteValuesIn(this.path.concat(key))
41
- this.additionalProperties.splice(this.additionalProperties.indexOf(key), 1)
42
- return this.onInput(this.value)
43
- }
44
-
45
- existProperty = (key: string) => {
46
- return this.form.existValuesIn(this.path.concat(key))
47
- }
48
- }
@@ -1,97 +0,0 @@
1
- import type { FormPathPattern } from '@formvk/shared'
2
- import { each, FormPath, isFn } from '@formvk/shared'
3
-
4
- import { buildDataPath } from '../shared/internals'
5
- import type { GeneralField, IGeneralFieldState, IQueryProps } from '../types'
6
- import type { Form } from './Form'
7
-
8
- function output(field: GeneralField, taker: (field: GeneralField, address: FormPath) => any) {
9
- if (!field) return
10
- if (isFn(taker)) {
11
- return taker(field, field.address)
12
- }
13
- return field
14
- }
15
-
16
- function takeMatchPattern(form: Form, pattern: FormPath) {
17
- const identifier = pattern.toString()
18
- const indexIdentifier = form.indexes[identifier]
19
- const absoluteField = form.fields[identifier]
20
- const indexField = form.fields[indexIdentifier]
21
- if (absoluteField) {
22
- return identifier
23
- } else if (indexField) {
24
- return indexIdentifier
25
- }
26
- }
27
-
28
- export class Query {
29
- private pattern: FormPath
30
- private addresses: string[] = []
31
- private form: Form
32
- constructor(props: IQueryProps) {
33
- this.pattern = FormPath.parse(props.pattern, props.base)
34
- this.form = props.form
35
- if (!this.pattern.isMatchPattern) {
36
- const matched = takeMatchPattern(
37
- this.form,
38
- this.pattern.haveRelativePattern ? buildDataPath(props.form.fields, this.pattern) : this.pattern
39
- )
40
- if (matched) {
41
- this.addresses = [matched]
42
- }
43
- } else {
44
- each(this.form.fields, (field, address) => {
45
- if (!field) {
46
- delete this.form.fields[address]
47
- return
48
- }
49
- if (field.match(this.pattern)) {
50
- this.addresses.push(address)
51
- }
52
- })
53
- }
54
- }
55
-
56
- take(): GeneralField | undefined
57
- take<Result>(getter: (field: GeneralField, address: FormPath) => Result): Result
58
- take(taker?: any): any {
59
- return output(this.form.fields[this.addresses[0]], taker)
60
- }
61
-
62
- map(): GeneralField[]
63
- map<Result>(iterator?: (field: GeneralField, address: FormPath) => Result): Result[]
64
- map(iterator?: any): any {
65
- return this.addresses.map(address => output(this.form.fields[address], iterator))
66
- }
67
-
68
- forEach<Result>(iterator: (field: GeneralField, address: FormPath) => Result) {
69
- return this.addresses.forEach(address => output(this.form.fields[address], iterator))
70
- }
71
-
72
- reduce<Result>(reducer: (value: Result, field: GeneralField, address: FormPath) => Result, initial?: Result): Result {
73
- return this.addresses.reduce<Result>(
74
- (value, address) => output(this.form.fields[address], (field, address) => reducer(value, field, address)),
75
- initial as Result
76
- )
77
- }
78
-
79
- get<K extends keyof IGeneralFieldState>(key: K): IGeneralFieldState[K] {
80
- const results: any = this.take()
81
- if (results) {
82
- return results[key]
83
- }
84
- }
85
-
86
- getIn(pattern?: FormPathPattern) {
87
- return FormPath.getIn(this.take(), pattern!)
88
- }
89
-
90
- value() {
91
- return this.get('value')
92
- }
93
-
94
- initialValue() {
95
- return this.get('initialValue')
96
- }
97
- }
@@ -1,64 +0,0 @@
1
- import type { FormPathPattern } from '@formvk/shared'
2
- import { toArr } from '@formvk/shared'
3
-
4
- import {
5
- createReactions,
6
- createStateGetter,
7
- createStateSetter,
8
- initializeEnd,
9
- initializeStart,
10
- } from '../shared/internals'
11
- import type { IModelGetter, IModelSetter, IVoidFieldProps, IVoidFieldState } from '../types'
12
- import { BaseField } from './BaseField'
13
- import type { Form } from './Form'
14
-
15
- export class VoidField<ComponentProps = any, DecoratorProps = any, IRecord extends object = any> extends BaseField<
16
- ComponentProps,
17
- DecoratorProps,
18
- IRecord
19
- > {
20
- displayName = 'VoidField' as const
21
- props: IVoidFieldProps
22
-
23
- constructor(address: FormPathPattern, props: IVoidFieldProps, form: Form, designable?: boolean) {
24
- super()
25
- this.form = form
26
- this.props = props
27
- this.designable = designable
28
- initializeStart()
29
- this.locate(address)
30
- this.initialize()
31
- this.makeReactive()
32
- this.onInit()
33
- initializeEnd()
34
- }
35
-
36
- protected initialize() {
37
- this.mounted = false
38
- this.unmounted = false
39
- this.initialized = false
40
- this.title = this.props.title
41
- this.description = this.props.description
42
- this.pattern = this.props.pattern
43
- this.display = this.props.display
44
- this.hidden = this.props.hidden
45
- this.editable = this.props.editable
46
- this.disabled = this.props.disabled
47
- this.readOnly = this.props.readOnly
48
- this.readPretty = this.props.readPretty
49
- this.visible = this.props.visible
50
- this.content = this.props.content
51
- this.data = this.props.data
52
- this.decorator = toArr(this.props.decorator)
53
- this.component = toArr(this.props.component)
54
- }
55
-
56
- protected makeReactive() {
57
- if (this.designable) return
58
- createReactions(this)
59
- }
60
-
61
- setState: IModelSetter<IVoidFieldState> = createStateSetter(this)
62
-
63
- getState: IModelGetter<IVoidFieldState> = createStateGetter(this)
64
- }
@@ -1,9 +0,0 @@
1
- export * from './Heart'
2
- export * from './LifeCycle'
3
- export * from './Graph'
4
- export * from './Query'
5
- export * from './Form'
6
- export * from './Field'
7
- export * from './ArrayField'
8
- export * from './ObjectField'
9
- export * from './VoidField'
@@ -1,9 +0,0 @@
1
- export type { Form } from './Form'
2
- export type { Field } from './Field'
3
- export type { Query } from './Query'
4
- export type { Heart } from './Heart'
5
- export type { Graph } from './Graph'
6
- export type { LifeCycle } from './LifeCycle'
7
- export type { ArrayField } from './ArrayField'
8
- export type { ObjectField } from './ObjectField'
9
- export type { VoidField } from './VoidField'
@@ -1,85 +0,0 @@
1
- import { isFn } from '@formvk/shared'
2
-
3
- import type { DataField } from '..'
4
- import { ArrayField, Field, Form, ObjectField, Query, VoidField } from '../models'
5
- import type { GeneralField, IFieldState, IFormState, IGeneralFieldState, IVoidFieldState } from '../types'
6
-
7
- export function isForm(node: any): node is Form {
8
- return node instanceof Form
9
- }
10
-
11
- export function isGeneralField(node: any): node is GeneralField {
12
- return node instanceof Field || node instanceof VoidField
13
- }
14
-
15
- export function isField<Value = any, ComponentProps = any, DecoratorProps = any, IRecord extends object = any>(
16
- node: any
17
- ): node is Field<Value, ComponentProps, DecoratorProps, IRecord> {
18
- return node instanceof Field
19
- }
20
-
21
- export function isArrayField<
22
- Value extends any[] = any[],
23
- ComponentProps = any,
24
- DecoratorProps = any,
25
- IRecord extends object = any,
26
- >(node: any): node is ArrayField<Value, ComponentProps, DecoratorProps, IRecord> {
27
- return node instanceof ArrayField
28
- }
29
-
30
- export function isObjectField<
31
- Value extends object = Record<string, any>,
32
- ComponentProps = any,
33
- DecoratorProps = any,
34
- IRecord extends object = any,
35
- >(node: any): node is ObjectField<Value, ComponentProps, DecoratorProps, IRecord> {
36
- return node instanceof ObjectField
37
- }
38
-
39
- export function isVoidField<ComponentProps = any, DecoratorProps = any, IRecord extends object = any>(
40
- node: any
41
- ): node is VoidField<ComponentProps, DecoratorProps, IRecord> {
42
- return node instanceof VoidField
43
- }
44
-
45
- export function isFormState<T extends Record<any, any> = any>(state: any): state is IFormState<T> {
46
- if (isFn(state?.initialize)) return false
47
- return state?.displayName === 'Form'
48
- }
49
-
50
- export function isFieldState(state: any): state is IFieldState {
51
- if (isFn(state?.initialize)) return false
52
- return state?.displayName === 'Field'
53
- }
54
-
55
- export function isGeneralFieldState(node: any): node is IGeneralFieldState {
56
- if (isFn(node?.initialize)) return false
57
- return node?.displayName?.indexOf('Field') > -1
58
- }
59
-
60
- export function isArrayFieldState(state: any): state is IFieldState {
61
- if (isFn(state?.initialize)) return false
62
- return state?.displayName === 'ArrayField'
63
- }
64
-
65
- export function isDataField(node: any): node is DataField {
66
- return isField(node) || isArrayField(node) || isObjectField(node)
67
- }
68
-
69
- export function isDataFieldState(node: any) {
70
- return isFieldState(node) || isObjectFieldState(node) || isArrayFieldState(node)
71
- }
72
-
73
- export function isObjectFieldState(state: any): state is IFieldState {
74
- if (isFn(state?.initialize)) return false
75
- return state?.displayName === 'ObjectField'
76
- }
77
-
78
- export function isVoidFieldState(state: any): state is IVoidFieldState {
79
- if (isFn(state?.initialize)) return false
80
- return state?.displayName === 'VoidField'
81
- }
82
-
83
- export function isQuery(query: any): query is Query {
84
- return query && query instanceof Query
85
- }
@@ -1,60 +0,0 @@
1
- export const ReservedProperties: Record<string, boolean> = {
2
- form: true,
3
- parent: true,
4
- props: true,
5
- caches: true,
6
- requests: true,
7
- disposers: true,
8
- heart: true,
9
- graph: true,
10
- indexes: true,
11
- fields: true,
12
- lifecycles: true,
13
- componentType: true,
14
- componentProps: true,
15
- decoratorType: true,
16
- decoratorProps: true,
17
- record: true,
18
- records: true,
19
- index: true,
20
- destroyed: true,
21
- }
22
-
23
- export const ReadOnlyProperties: Record<string, boolean> = {
24
- address: true,
25
- path: true,
26
- valid: true,
27
- invalid: true,
28
- selfValid: true,
29
- selfInvalid: true,
30
- errors: true,
31
- successes: true,
32
- warnings: true,
33
- validateStatus: true,
34
- }
35
-
36
- const SELF_DISPLAY = 'selfDisplay'
37
- const SELF_PATTERN = 'selfPattern'
38
-
39
- export const MutuallyExclusiveProperties: Record<string, string> = {
40
- pattern: SELF_PATTERN,
41
- editable: SELF_PATTERN,
42
- readOnly: SELF_PATTERN,
43
- readPretty: SELF_PATTERN,
44
- disabled: SELF_PATTERN,
45
- display: SELF_DISPLAY,
46
- hidden: SELF_DISPLAY,
47
- visible: SELF_DISPLAY,
48
- }
49
-
50
- export const RESPONSE_REQUEST_DURATION = 100
51
-
52
- export const GlobalState = {
53
- lifecycles: [] as any[],
54
- context: [] as any[],
55
- effectStart: false,
56
- effectEnd: false,
57
- initializing: false,
58
- }
59
-
60
- export const NumberIndexReg = /^\.(\d+)/
@@ -1,69 +0,0 @@
1
- import { isFn, isValid } from '@formvk/shared'
2
-
3
- import type { Form } from '../models'
4
- import { LifeCycle } from '../models'
5
- import type { AnyFunction } from '../types'
6
- import { isForm } from './checkers'
7
- import { GlobalState } from './constants'
8
-
9
- export function createEffectHook<F extends (payload: any, ...ctxs: any[]) => AnyFunction>(type: string, callback?: F) {
10
- return (...args: Parameters<ReturnType<F>>) => {
11
- if (GlobalState.effectStart) {
12
- GlobalState.lifecycles.push(
13
- new LifeCycle(type, (payload, ctx) => {
14
- if (isFn(callback)) {
15
- callback(payload, ctx, ...GlobalState.context)(...args)
16
- }
17
- })
18
- )
19
- } else {
20
- throw new Error('Effect hooks cannot be used in asynchronous function body')
21
- }
22
- }
23
- }
24
-
25
- export function createEffectContext<T = any>(defaultValue?: T) {
26
- let index: number
27
- return {
28
- provide(value?: T) {
29
- if (GlobalState.effectStart) {
30
- index = GlobalState.context.length
31
- GlobalState.context[index] = isValid(value) ? value : defaultValue
32
- } else {
33
- throw new Error('Provide method cannot be used in asynchronous function body')
34
- }
35
- },
36
- consume(): T {
37
- if (!GlobalState.effectStart) {
38
- throw new Error('Consume method cannot be used in asynchronous function body')
39
- }
40
- return GlobalState.context[index]
41
- },
42
- }
43
- }
44
-
45
- const FormEffectContext = createEffectContext<Form>()
46
-
47
- export const useEffectForm = FormEffectContext.consume
48
-
49
- export function runEffects<Context>(
50
- context?: Context,
51
- ...args: (((context: Context) => void) | undefined)[]
52
- ): LifeCycle[] {
53
- GlobalState.lifecycles = []
54
- GlobalState.context = []
55
- GlobalState.effectStart = true
56
- GlobalState.effectEnd = false
57
- if (isForm(context)) {
58
- FormEffectContext.provide(context)
59
- }
60
- args.forEach(effects => {
61
- if (isFn(effects)) {
62
- effects(context as Context)
63
- }
64
- })
65
- GlobalState.context = []
66
- GlobalState.effectStart = false
67
- GlobalState.effectEnd = true
68
- return GlobalState.lifecycles
69
- }
@@ -1,65 +0,0 @@
1
- import { FormPath } from '@formvk/shared'
2
- import {
3
- getLocaleByPath,
4
- getValidateLocaleIOSCode,
5
- registerValidateFormats,
6
- registerValidateLocale,
7
- registerValidateMessageTemplateEngine,
8
- registerValidateRules,
9
- setValidateLanguage,
10
- } from '@formvk/validator'
11
-
12
- import { Form } from '../models'
13
- import type { IFormProps } from '../types'
14
- import {
15
- isArrayField,
16
- isArrayFieldState,
17
- isDataField,
18
- isDataFieldState,
19
- isField,
20
- isFieldState,
21
- isForm,
22
- isFormState,
23
- isGeneralField,
24
- isGeneralFieldState,
25
- isObjectField,
26
- isObjectFieldState,
27
- isQuery,
28
- isVoidField,
29
- isVoidFieldState,
30
- } from './checkers'
31
- import { createEffectContext, createEffectHook, useEffectForm } from './effective'
32
-
33
- function createForm<T extends object = any>(options?: IFormProps<T>) {
34
- return new Form(options)
35
- }
36
-
37
- export {
38
- createEffectContext,
39
- createEffectHook,
40
- createForm,
41
- FormPath,
42
- getLocaleByPath,
43
- getValidateLocaleIOSCode,
44
- isArrayField,
45
- isArrayFieldState,
46
- isDataField,
47
- isDataFieldState,
48
- isField,
49
- isFieldState,
50
- isForm,
51
- isFormState,
52
- isGeneralField,
53
- isGeneralFieldState,
54
- isObjectField,
55
- isObjectFieldState,
56
- isQuery,
57
- isVoidField,
58
- isVoidFieldState,
59
- registerValidateFormats,
60
- registerValidateLocale,
61
- registerValidateMessageTemplateEngine,
62
- registerValidateRules,
63
- setValidateLanguage,
64
- useEffectForm,
65
- }