/ cli / source / datapack / helpers / TransformerOperations.ts
TransformerOperations.ts
 1  import _ from 'lodash'
 2  
 3  export const TransformerOperations = {
 4      normalizeValues ( item: any, path: string, finalPath: string ) {
 5          if ( _.has( item, path ) ) {
 6              const value = _.get( item, path )
 7              _.set( item, finalPath, _.castArray( value ) )
 8          }
 9      },
10  
11      getJson( value: any ): string {
12          if ( _.isEmpty( value ) ) {
13              return null
14          }
15  
16          return JSON.stringify( value )
17      },
18  
19      normalizeAllValues( properties: any ) {
20          _.each( properties, ( value: any, key: string ) => {
21              value = _.castArray( value )
22              properties[ key ] = value
23          } )
24  
25          return properties
26      },
27  
28      createQuery( tableName: string, columns: Array<string>, useReplace: boolean = false ) : string {
29          return `INSERT ${useReplace ? 'OR REPLACE ' : ''}INTO ${tableName} (${ columns.join( ',' ) }) values (${ _.join( _.times( columns.length, _.constant( '?' ) ), ',' ) })`
30      },
31  
32      parseInt( value: string ) : number | null {
33          if ( !value ) {
34              return null
35          }
36  
37          const outcome = parseInt( value, 10 )
38          if ( !Number.isInteger( outcome ) ) {
39              return null
40          }
41  
42          return outcome
43      },
44  
45      parseFloat( value: string ) : number | null {
46          if ( !value ) {
47              return null
48          }
49  
50          const outcome = parseFloat( value )
51          if ( Number.isNaN( outcome ) ) {
52              return null
53          }
54  
55          return outcome
56      },
57  
58      /*
59          Convert chance value (%) to value between 0 and 1
60       */
61      parseChance( value: string ) : number | null {
62          if ( !value ) {
63              return null
64          }
65  
66          if ( value.length > 2 ) {
67              return 1
68          }
69  
70          const adjustedValue = value.length === 1 ? `0.0${value}` : `0.${value}`
71          const outcome = parseFloat( adjustedValue )
72          if ( Number.isNaN( outcome ) ) {
73              return null
74          }
75  
76          return outcome
77      }
78  }