Code coverage report for src/metadata.js

Statements: 97.37% (37 / 38)      Branches: 91.67% (22 / 24)      Functions: 100% (6 / 6)      Lines: 97.37% (37 / 38)      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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78    1 1   1 1   1 78     78 78 78     78 78   78 182 182           182   182     182 18 20 20 22     18       18   164 169   164 164 2 2     2   2 1       1         182 180 180             1  
'use strict';
 
var path = require('path');
var fs = require('fs');
 
require('string.fromcodepoint');
require('string.prototype.codepointat');
 
function getMetadataService(options) {
  var usedUnicodes = [];
 
  // Default options
  options = options || {};
  options.appendUnicode = !!options.appendUnicode;
  options.startUnicode = 'number' === typeof options.startUnicode ?
    options.startUnicode :
    0xEA01;
  options.log = options.log || console.log;
  options.err = options.err || console.err;
 
  return function getMetadataFromFile(file, cb) {
    var basename = path.basename(file);
    var metadata = {
      path: file,
      name: '',
      unicode: [],
      renamed: false,
    };
    var matches = basename.match(/^(?:((?:u[0-9a-f]{4,6},?)+)\-)?(.+)\.svg$/i);
 
    metadata.name = matches && matches[2] ?
      matches[2] :
      'icon' + options.startUnicode;
    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.fromCodePoint(parseInt(code, 16));
        }).join('');
      });
      Iif(-1 !== usedUnicodes.indexOf(metadata.unicode[0])) {
        return cb(new Error('The unicode codepoint of the glyph ' + metadata.name +
          ' seems to be already used by another glyph.'));
      }
      usedUnicodes = usedUnicodes.concat(metadata.unicode);
    } else {
      do {
        metadata.unicode[0] = String.fromCodePoint(options.startUnicode++);
      } while(-1 !== usedUnicodes.indexOf(metadata.unicode[0]));
      usedUnicodes.push(metadata.unicode[0]);
      if(options.appendUnicode) {
        metadata.renamed = true;
        metadata.path = path.dirname(file) + '/' +
          'u' + metadata.unicode[0].codePointAt(0).toString(16).toUpperCase() +
          '-' + basename;
        fs.rename(file, metadata.path,
          function(err) {
            if(err) {
              return cb(new Error('Could not save codepoint: ' +
                'u' + metadata.unicode[0].codePointAt(0).toString(16).toUpperCase() +
                ' for ' + basename));
            }
            cb(null, metadata);
          }
        );
      }
    }
    if(!metadata.renamed) {
      setImmediate(function() {
        cb(null, metadata);
      });
    }
  };
 
}
 
module.exports = getMetadataService;