All files main.js

95.65% Statements 110/115
88.23% Branches 30/34
100% Functions 2/2
95.65% Lines 110/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 30x 45x 45x 45x 45x 45x 45x 4x 4x 12x           4x 4x 41x 41x 41x 41x 41x 41x 20x 20x 20x 41x 146x 146x 146x 23x 23x 19x 23x 41x 41x 30x 38x 38x 38x 38x 38x 11x 11x 11x 11x 11x 11x 32x 32x 32x 32x 32x 32x 7x 4x 4x 4x 4x 7x 32x 32x 32x 9x 9x 9x 23x 23x 23x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x      
'use strict'
 
import dtbox   from 'dt-toolbox'
import load    from './load.js'
import keyAnalyzer from './keyAnalyzer.js'
 
 
 
function dtShape ( dt , shape ) {
            const 
                  shapeEntries = Object.entries ( shape )
                , isShapeArray = shape instanceof Array
                ;
 
            return dt.query ( store => {
                                const 
                                      objectNames = [ 'root' ]
                                    , cache = {}
                                    ;
 
                                if ( isShapeArray )   store.set ( 'root', [])
                                else                  store.set ( 'root', {})
 
                                shapeEntries.forEach ( ([k, list ]) => {
                                                let 
                                                      result
                                                    , [ keyList, action ] = keyAnalyzer ( k )
                                                    , hasAction = ( action != null )
                                                    ;
                                                // actions - list, fold, load;
                                                if ( action === 'list' )   result = []
                                                if ( action === 'fold' )   result = {}
                                                if ( action === 'load' )   result = load ( list )
 
                                                if ( action !== 'load' ) {
                                                list.forEach ( el => {
                                                            let 
                                                                  container = null
                                                                , hasLine = ( dt.index(`root/${el}`) != null )
                                                                ;
 
                                                            if ( hasLine ) {
                                                                    store.get ( `root/${el}` )
                                                                         .look ( ({key, value, flatData}) => {
                                                                                        if      ( action === 'list' )   result.push (value) 
                                                                                        else if ( action === 'fold' )   result[key] = value
                                                                                        else {                        
                                                                                                result = flatData
                                                                                                return 'next'
                                                                                            }
                                                                                })
                                                                }
                                                            else {
                                                                    const 
                                                                          searchList = el.split ( '/' )
                                                                        , prop = searchList.pop ()
                                                                        ;
                                                                    if ( searchList.length > 0 ) {
                                                                                container = `root/${searchList.join('/') }`
                                                                                hasLine = ( dt.index ( container ) != null )
                                                                        }
                                                                    store.look ( ({key, value, breadcrumbs }) => {
                                                                                        // If container is specified - check name or breadcrumbs first
                                                                                        if ( hasLine && breadcrumbs != container )   return 'next'
                                                                                        if ( key === prop ) {
                                                                                                 if      ( action === 'list' )   result.push ( value )
                                                                                                 else if ( action === 'fold' )   result[key] = value
                                                                                                 else                            result = value
                                                                                            }
                                                                                    })
                                                                }
                                                    }) // forEach list
                                                } // if action != 'load'
 
                                                if ( typeof(result)==='object' && Object.entries(result).length === 0 )   return
                                                if ( result != null )   cache[keyList.join('/')] = result
                                                
                                        }) // forEach shapeEntry
 
 
                                
                                const cacheEntries = Object.entries ( cache );
                                cacheEntries.forEach ( ([k,r]) => {
                                                        const 
                                                              keyList = k.split('/')
                                                            , spot = keyList.pop ()
                                                            ;
                                                        let parent = 'root';
                                                        keyList.forEach ( k => {   // Setup the container objects needed
                                                                    if ( !objectNames.includes(k)) {
                                                                                store.set ( k, {} )
                                                                                store.connect ( [`${parent}/${k}`])
                                                                                objectNames.push ( k )
                                                                        }
                                                                    parent = k
                                                            })
                                                        if ( r == null )   return
                                                        else if ( typeof(r) === 'object' ) {   // Create a new structure
                                                                    store.set ( spot, r )
                                                                    store.connect ([`${parent}/${spot}`])                
                                                            }
                                                        else {  // Save a property
                                                                    store.save ( parent, spot, r )
                                                            }
                                        }) // cacheEntries forEach
                        }) // dt.query
} // dtShape func.
 
 
 
dtShape.getDTtoolbox = () => dtbox;
 
 
 
export default dtShape