Code coverage report for src/metadata.js

Statements: 100% (30 / 30)      Branches: 100% (16 / 16)      Functions: 100% (5 / 5)      Lines: 100% (30 / 30)      Ignored: none     

All files » src/ » metadata.js
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 611 1   1 65     65 65 65     65 65   65 116 116       116 116 116 12 14 14 16     12   104 104   104 104 2       2 1       1               116         1  
var path = require('path');
var fs = require('fs');
 
function getMetadataService(options) {
  var usedUnicodes = [];
 
  // Default options
  options = options || {};
  options.appendUnicode = !!options.appendUnicode;
  options.startUnicode = 'number' === typeof options.startUnicode ?
    options.startUnicode :
    0xE001;
  options.log = options.log || console.log;
  options.err = options.err || console.err;
 
  return function getMetadataFromFile(file) {
    var basename = path.basename(file);
    var metadata = {
      name: '',
      unicode: []
    };
    matches = basename.match(/^(?:((?:u[0-9a-f]{4,6},?)+)\-)?(.*).svg$/i);
    metadata.name = matches[2];
    if(matches && matches[1]) {
      metadata.unicode = matches[1].split(',').map(function(match) {
        match = match.substr(1);
        return match.split('u').map(function(code) {
          return String.fromCharCode(parseInt(code, 16));
        }).join('');
      });
      usedUnicodes = usedUnicodes.concat(metadata.unicode);
    } else {
      do {
        metadata.unicode[0] = String.fromCharCode(options.startUnicode++);
      } while(-1 !== usedUnicodes.indexOf(metadata.unicode[0]));
      usedUnicodes.push(metadata.unicode[0]);
      if(options.appendUnicode) {
        fs.rename(file, path.dirname(file) + '/' +
          'u' + metadata.unicode[0].charCodeAt(0).toString(16).toUpperCase() +
          '-' + basename,
          function(err) {
            if(err) {
              options.error(new Error('Could not save codepoint: ' +
                'u' + metadata.unicode[0].charCodeAt(0).toString(16).toUpperCase() +
                ' for ' + basename));
            } else {
              options.log('Saved codepoint: ' +
                'u' + metadata.unicode[0].charCodeAt(0).toString(16).toUpperCase() +
                ' for ' + basename);
            }
          }
        );
      }
    }
    return metadata;
  };
 
}
 
module.exports = getMetadataService;