All files index.js

100% Statements 8/8
87.5% Branches 21/24
100% Functions 1/1
100% Lines 8/8
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                    1x   12x 12x     2x     2x     2x     3x   3x      
"use strict";
 
/**
 * HS code is best represented without dots and labels for processing.
 * However, that representaton is hard to read.
 * 
 * @param {string} code HS code. 
 * @param {boolean} [showLabel=false] HS category label. e.g. heading, subheading, chapter etc.
 * @param {boolean} [isItem=false] Is this a product category item? I.e., does it have tariffs? 
 */
module.exports = function (code, showLabel=false, isItem=false)
{
  var hscode = code.replace(/[^0-9]/g, '');
  switch (hscode.length)
  {
  case 0:
    return (showLabel ? 'Section ' : '') + code;
  case 1:
  case 2:
    return (showLabel ? 'Chapter ' : '') + hscode;
  case 3:
  case 4:
    return (showLabel ? 'Heading ' : '') + hscode;
  case 5:
  case 6:
    return (showLabel ? (isItem ? 'Item ' : 'Subheading ') : '') + hscode.substr(0, 4) + '.' + hscode.substr(4);
  default:
    return (showLabel ? (isItem ? 'Item ' : 'Subheading ') : '') + hscode.substr(0, 4) + '.' + hscode.substr(4, 2) + '.' + hscode.substr(6);
  }
};