@@ -6,7 +6,7 @@ import isObject from 'lodash-es/isObject'
6
6
import set from 'lodash-es/set'
7
7
8
8
// Create a new model by schema default values
9
- export const createDefaultObject = ( schema : any , obj : Record < string , any > = { } ) => {
9
+ export const createDefaultObject = ( schema : any , obj : Record < string , any > = { } ) : Record < string , any > => {
10
10
each ( schema . fields , ( field : any ) => {
11
11
if ( get ( obj , field . model ) === undefined && field . default !== undefined ) {
12
12
if ( isFunction ( field . default ) ) {
@@ -16,22 +16,26 @@ export const createDefaultObject = (schema: any, obj: Record<string, any> = {})
16
16
} else set ( obj , field . model , field . default )
17
17
}
18
18
} )
19
+
19
20
return obj
20
21
}
21
22
22
23
// Get a new model which contains only properties of multi-edit fields
23
- export const getMultipleFields = ( schema : any ) => {
24
- const res : any = [ ]
24
+ export const getMultipleFields = ( schema : any ) : any [ ] => {
25
+ const res : any [ ] = [ ]
26
+
25
27
each ( schema . fields , field => {
26
- if ( field . multi === true ) res . push ( field )
28
+ if ( field . multi === true ) {
29
+ res . push ( field )
30
+ }
27
31
} )
28
32
29
33
return res
30
34
}
31
35
32
36
// Merge many models to one 'work model' by schema
33
- export const mergeMultiObjectFields = ( schema : any , objs : any ) => {
34
- const model = { }
37
+ export const mergeMultiObjectFields = ( schema : any , objs : any ) : Record < string , any > => {
38
+ const model : Record < string , any > = { }
35
39
36
40
const fields = getMultipleFields ( schema )
37
41
@@ -41,11 +45,12 @@ export const mergeMultiObjectFields = (schema: any, objs: any) => {
41
45
const path = field . model
42
46
43
47
each ( objs , ( obj : any ) => {
44
- const v = get ( obj , path )
48
+ const val = get ( obj , path )
49
+
45
50
if ( notSet ) {
46
- mergedValue = v
51
+ mergedValue = val
47
52
notSet = false
48
- } else if ( mergedValue !== v ) {
53
+ } else if ( mergedValue !== val ) {
49
54
mergedValue = undefined
50
55
}
51
56
} )
@@ -56,50 +61,50 @@ export const mergeMultiObjectFields = (schema: any, objs: any) => {
56
61
return model
57
62
}
58
63
59
- export const slugifyFormID = ( schema : any , prefix : any = '' ) => {
64
+ export const slugifyFormID = ( schema : any , prefix : any = '' ) : string => {
60
65
// Try to get a reasonable default id from the schema,
61
66
// then slugify it.
62
67
if ( typeof schema . id !== 'undefined' ) {
63
68
// If an ID's been explicitly set, use it unchanged
64
- return prefix + schema . id
69
+ return prefix + schema . id + ''
65
70
} else {
66
71
// Return the slugified version of either:
67
72
return (
68
73
prefix +
69
74
( schema . inputName || schema . label || schema . model || '' )
70
- // NB: This is a very simple, conservative, slugify function,
71
- // avoiding extra dependencies.
75
+ // NB: This is a very simple, conservative, slugify function,
76
+ // avoiding extra dependencies.
72
77
. toString ( )
73
78
. trim ( )
74
79
. toLowerCase ( )
75
- // Spaces & underscores to dashes
80
+ // Spaces & underscores to dashes
76
81
. replace ( / | _ / g, '-' )
77
- // Multiple dashes to one
82
+ // Multiple dashes to one
78
83
. replace ( / - { 2 , } / g, '-' )
79
- // Remove leading & trailing dashes
84
+ // Remove leading & trailing dashes
80
85
. replace ( / ^ - + | - + $ / g, '' )
81
- // Remove anything that isn't a (English/ASCII) letter, number or dash.
86
+ // Remove anything that isn't a (English/ASCII) letter, number or dash.
82
87
. replace ( / ( [ ^ a - z A - Z 0 - 9 - ] + ) / g, '' )
83
88
)
84
89
}
85
90
}
86
91
87
- export const slugify = ( name : any = '' ) => {
92
+ export const slugify = ( name : any = '' ) : string => {
88
93
// Return the slugified version of either:
89
94
return (
90
95
name
91
- // NB: This is a very simple, conservative, slugify function,
92
- // avoiding extra dependencies.
96
+ // NB: This is a very simple, conservative, slugify function,
97
+ // avoiding extra dependencies.
93
98
. toString ( )
94
99
. trim ( )
95
- // .toLowerCase()
96
- // Spaces to dashes
100
+ // .toLowerCase()
101
+ // Spaces to dashes
97
102
. replace ( / / g, '-' )
98
- // Multiple dashes to one
103
+ // Multiple dashes to one
99
104
. replace ( / - { 2 , } / g, '-' )
100
- // Remove leading & trailing dashes
105
+ // Remove leading & trailing dashes
101
106
. replace ( / ^ - + | - + $ / g, '' )
102
- // Remove anything that isn't a (English/ASCII) letter, number or dash.
107
+ // Remove anything that isn't a (English/ASCII) letter, number or dash.
103
108
. replace ( / ( [ ^ a - z A - Z 0 - 9 - _ / ./ : ] + ) / g, '' )
104
109
)
105
110
}
0 commit comments