1
- import { get , find } from 'lodash' ;
1
+ import { get , find , isObject } from 'lodash' ;
2
2
import { getCodebook } from '../protocol' ;
3
3
import { asOptions } from '../utils' ;
4
4
import { makeOptionsWithIsUsed } from './isUsed' ;
@@ -23,24 +23,36 @@ const getType = (state, subject) => {
23
23
*/
24
24
const getVariablesForSubject = ( state , subject ) => get ( getType ( state , subject ) , 'variables' , { } ) ;
25
25
26
- const getAllVariablesByUUID = ( { node : nodeTypes = { } , edge : edgeTypes = { } , ego = { } } ) => {
26
+ const getAllVariablesByUUID = ( codebook ) => {
27
+ if ( ! codebook ) { throw new Error ( 'Codebook not found' ) ; }
28
+
29
+ const { node : nodeTypes = { } , edge : edgeTypes = { } , ego = { } } = codebook ;
27
30
const flattenedVariables = { } ;
28
31
29
32
const addVariables = ( variables ) => {
33
+ if ( ! variables ) { return ; }
34
+ if ( ! isObject ( variables ) ) { throw new Error ( 'Variables must be an object' ) ; }
35
+
30
36
Object . keys ( variables ) . forEach ( ( variable ) => {
31
37
flattenedVariables [ variable ] = variables [ variable ] ;
32
38
} ) ;
33
39
} ;
34
40
35
- Object . values ( nodeTypes ) . forEach ( ( nodeType ) => {
36
- addVariables ( nodeType . variables ) ;
37
- } ) ;
41
+ if ( nodeTypes && nodeTypes . variables ) {
42
+ Object . values ( nodeTypes ) . forEach ( ( nodeType ) => {
43
+ addVariables ( nodeType . variables ) ;
44
+ } ) ;
45
+ }
38
46
39
- Object . values ( edgeTypes ) . forEach ( ( edgeType ) => {
40
- addVariables ( edgeType . variables ) ;
41
- } ) ;
47
+ if ( edgeTypes && edgeTypes . variables ) {
48
+ Object . values ( edgeTypes ) . forEach ( ( edgeType ) => {
49
+ addVariables ( edgeType . variables ) ;
50
+ } ) ;
51
+ }
42
52
43
- addVariables ( ego . variables ) ;
53
+ if ( ego . variables ) {
54
+ addVariables ( ego . variables ) ;
55
+ }
44
56
return flattenedVariables ;
45
57
} ;
46
58
0 commit comments