1
1
import _ from "lodash"
2
2
3
+ import type { IGatsbyNode } from "../redux/types"
4
+ import type { GatsbyIterable } from "../datastore/common/iterable"
5
+
6
+ type data = IGatsbyNode | GatsbyIterable < IGatsbyNode >
7
+
8
+ /**
9
+ * @param {Object|Array } data
10
+ * @returns {Object|Array } data without undefined values
11
+ */
12
+ type omitUndefined = ( data : data ) => Partial < data >
13
+
14
+ const omitUndefined : omitUndefined = data => {
15
+ const isPlainObject = _ . isPlainObject ( data )
16
+ if ( isPlainObject ) {
17
+ return _ . pickBy ( data , p => p !== undefined )
18
+ }
19
+
20
+ return ( data as GatsbyIterable < IGatsbyNode > ) . filter ( p => p !== undefined )
21
+ }
22
+
23
+ /**
24
+ * @param {* } data
25
+ * @return {boolean }
26
+ */
27
+ type isTypeSupported = ( data : data ) => boolean
28
+
29
+ const isTypeSupported : isTypeSupported = data => {
30
+ if ( data === null ) {
31
+ return true
32
+ }
33
+
34
+ const type = typeof data
35
+ const isSupported =
36
+ type === `number` ||
37
+ type === `string` ||
38
+ type === `boolean` ||
39
+ data instanceof Date
40
+
41
+ return isSupported
42
+ }
43
+
3
44
/**
4
45
* Make data serializable
5
46
* @param {(Object|Array) } data to sanitize
6
47
* @param {boolean } isNode = true
7
48
* @param {Set<string> } path = new Set
8
49
*/
9
- const sanitizeNode = ( data , isNode = true , path = new Set ( ) ) => {
50
+
51
+ type sanitizeNode = (
52
+ data : data ,
53
+ isNode ?: boolean ,
54
+ path ?: Set < unknown >
55
+ ) => data | undefined
56
+
57
+ const sanitizeNode : sanitizeNode = ( data , isNode = true , path = new Set ( ) ) => {
10
58
const isPlainObject = _ . isPlainObject ( data )
11
59
12
60
if ( isPlainObject || _ . isArray ( data ) ) {
@@ -20,15 +68,15 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => {
20
68
returnData [ key ] = o
21
69
return
22
70
}
23
- returnData [ key ] = sanitizeNode ( o , false , path )
71
+ returnData [ key ] = sanitizeNode ( o as data , false , path )
24
72
25
73
if ( returnData [ key ] !== o ) {
26
74
anyFieldChanged = true
27
75
}
28
76
} )
29
77
30
78
if ( anyFieldChanged ) {
31
- data = omitUndefined ( returnData )
79
+ data = omitUndefined ( returnData as data ) as data
32
80
}
33
81
34
82
// arrays and plain objects are supported - no need to to sanitize
@@ -42,36 +90,4 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => {
42
90
}
43
91
}
44
92
45
- /**
46
- * @param {Object|Array } data
47
- * @returns {Object|Array } data without undefined values
48
- */
49
- const omitUndefined = data => {
50
- const isPlainObject = _ . isPlainObject ( data )
51
- if ( isPlainObject ) {
52
- return _ . pickBy ( data , p => p !== undefined )
53
- }
54
-
55
- return data . filter ( p => p !== undefined )
56
- }
57
-
58
- /**
59
- * @param {* } data
60
- * @return {boolean }
61
- */
62
- const isTypeSupported = data => {
63
- if ( data === null ) {
64
- return true
65
- }
66
-
67
- const type = typeof data
68
- const isSupported =
69
- type === `number` ||
70
- type === `string` ||
71
- type === `boolean` ||
72
- data instanceof Date
73
-
74
- return isSupported
75
- }
76
-
77
- module . exports = sanitizeNode
93
+ export default sanitizeNode
0 commit comments