wiseService/wiseSource.js
/**
   * Util function to parse JSON formatted data
   * @param {string} body - the raw JSON data
   * @param {function} setCb - the function to call for each row found
   * @param {function} endCB - all done parsing
   */
Util function to parse JSON formatted data
@param {string} body - the raw JSON data
@param {function} setCb - the function to call for each row found
@param {function} endCB - all done parsing
Util function to parse JSON formatted data @param { string } body - the raw JSON data @param { function } setCb - the function to call for each row found @param { function } endCB - all done parsing
parseJSON (body, setCb, endCb) {
    try {
      let json = JSON.parse(body);
      if (this.keyPath === undefined) {
        return endCb('No keyPath set');
      }
      if (this.arrayPath !== undefined) {
        const arrayPath = this.arrayPath.split('.');
        for (let i = 0; i < arrayPath.length; i++) {
          json = json[arrayPath[i]];
          if (!json) {
            return endCb(`Couldn't find ${arrayPath[i]} in results`);
          }
        }
      }
      if (!Array.isArray(json)) {
        json = [json];
      }
      const keyPath = this.keyPath.split('.');
      const shortcuts = [];
      const shortcutsValue = [];
      for (const k in this.shortcuts) {
        shortcuts.push(k.split('.'));
        shortcutsValue.push(this.shortcuts[k]);
      }
      for (let i = 0; i < json.length; i++) {
        let key = json[i];
        for (let j = 0; key && j < keyPath.length; j++) {
          key = key[keyPath[j]];
        }
        if (key === undefined || key === null) {
          continue;
        }
        const args = [];
        for (let k = 0; k < shortcuts.length; k++) {
          let objs = json[i];
          for (let j = 0; objs && j < shortcuts[k].length; j++) {
            objs = objs[shortcuts[k][j]];
          }
          if (!Array.isArray(objs)) {
            objs = [objs];
          }
          for (let o = 0; o < objs.length; o++) {
            const obj = objs[o];
            if (obj !== undefined && obj !== null && obj !== '') {
              args.push(shortcutsValue[k].pos);
              if (shortcutsValue[k].mod === 1) {
                args.push(obj.toLowerCase());
              } else if (shortcutsValue[k].mod === 2) {
                args.push(obj.toUpperCase());
              } else {
                args.push(obj);
              }
            }
          }
        }
        if (Array.isArray(key)) {
          key.forEach((part) => {
            setCb(part, WISESource.encodeResult.apply(null, args));
          });
        } else {
          setCb(key, WISESource.encodeResult.apply(null, args));
        }
      }
      endCb(null);
    } catch (e) {
      endCb(e);
    }
  }
parseJSON ( body , setCb , endCb ) { try { let json = JSON . parse ( body ) ; if ( this . keyPath === undefined ) { return endCb ( 'No keyPath set' ) ; } if ( this . arrayPath !== undefined ) { const arrayPath = this . arrayPath . split ( '.' ) ; for ( let i = 0 ; i < arrayPath . length ; i ++ ) { json = json [ arrayPath [ i ] ] ; if ( ! json ) { return endCb ( ` ${ arrayPath [ i ] } ` ) ; } } } if ( ! Array . isArray ( json ) ) { json = [ json ] ; } const keyPath = this . keyPath . split ( '.' ) ; const shortcuts = [ ] ; const shortcutsValue = [ ] ; for ( const k in this . shortcuts ) { shortcuts . push ( k . split ( '.' ) ) ; shortcutsValue . push ( this . shortcuts [ k ] ) ; } for ( let i = 0 ; i < json . length ; i ++ ) { let key = json [ i ] ; for ( let j = 0 ; key && j < keyPath . length ; j ++ ) { key = key [ keyPath [ j ] ] ; } if ( key === undefined || key === null ) { continue ; } const args = [ ] ; for ( let k = 0 ; k < shortcuts . length ; k ++ ) { let objs = json [ i ] ; for ( let j = 0 ; objs && j < shortcuts [ k ] . length ; j ++ ) { objs = objs [ shortcuts [ k ] [ j ] ] ; } if ( ! Array . isArray ( objs ) ) { objs = [ objs ] ; } for ( let o = 0 ; o < objs . length ; o ++ ) { const obj = objs [ o ] ; if ( obj !== undefined && obj !== null && obj !== '' ) { args . push ( shortcutsValue [ k ] . pos ) ; if ( shortcutsValue [ k ] . mod === 1 ) { args . push ( obj . toLowerCase ( ) ) ; } else if ( shortcutsValue [ k ] . mod === 2 ) { args . push ( obj . toUpperCase ( ) ) ; } else { args . push ( obj ) ; } } } } if ( Array . isArray ( key ) ) { key . forEach ( ( part ) => { setCb ( part , WISESource . encodeResult . apply ( null , args ) ) ; } ) ; } else { setCb ( key , WISESource . encodeResult . apply ( null , args ) ) ; } } endCb ( null ) ; } catch ( e ) { endCb ( e ) ; } }
Util function to parse JSON formatted data
 @param {string} body - the raw JSON data
 @param {function} setCb - the function to call for each row found
 @param {function} endCB - all done parsing
Util function to parse JSON formatted data @param { string } body - the raw JSON data @param { function } setCb - the function to call for each row found @param { function } endCB - all done parsing
// The rest of the code assumes an array // Convert shortcuts into array of key path // Walk the key path // Check each shortcut // Walk the shortcut path // Always pretend we have an array