All files / src/modules detectFormat.ts

5% Statements 1/20
0% Branches 0/16
0% Functions 0/1
5% Lines 1/20
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                                                                                            1x
import IElementLike   from '../NodeLike/ParentNodeLike/ElementLike/IElementLike';
import TDetectionMode from '../TypeAliases/TDetectionMode';
function detectFormat(
  value:         IElementLike | string,
  detectionMode: TDetectionMode): string
{
  let formatStr: IElementLike | string | null = value;
  if (typeof value !== 'string') {
    /* Currently only present on 2^. */
    formatStr = value.getAttribute('format');
    if (!formatStr) {
      if (detectionMode === 'manual') {
        throw new Error('Detection mode was manual, ' +
                        'but there was no format attribute.');
      }
      
      /* Only present on 1^. */
      if (value.getAttribute('id') === 'storeArea') {
        formatStr = 'sugarcane';
      }
 
      /* No reliable information on storyData element. Check contents
       * instead. */
      if (!formatStr) {
        for (let ii = 0; ii < value.children.length; ii += 1) {
          const child = value.children[ii];
          if (child.getAttribute('tiddler')) {
            formatStr = 'sugarcane';
            break;
          }
        }
 
        if (!formatStr) {
          throw new Error('Format could not be detected in ' +
                          'element value.');
        }
      }
    }
  } else if (!formatStr) {
    throw new Error('The value argument was a string, but the string ' +
                    'was empty.');
  }
 
  return (<string>formatStr).toUpperCase();
}
 
export default detectFormat;