Code coverage report for lib/formatLocale.js

Statements: 100% (21 / 21)      Branches: 100% (12 / 12)      Functions: 100% (2 / 2)      Lines: 100% (21 / 21)      Ignored: none     

All files » lib/ » formatLocale.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            1 19 19 17     19 19 38           19 29 14   15 14   1   19 19 38   19   17   2   1   1      
'use strict';
 
// formatLocale currently depends on the javascript interpreter to preserve the
// property order of objects. Should this "de facto" standard change, this
// method can be modified to provide similar output by another means.
 
module.exports = function(locale, options) {
  options = options || {};
  if (typeof options.output === 'undefined') {
    options.output = 'object';
  }
 
  var strings = [];
  for (var hash in locale) {
    strings.push({
      hash: hash,
      string: locale[hash]
    });
  }
  // strings are ordered alphabetically for better readability & source control
  strings.sort(function(a, b) {
    if (a.string.toLowerCase() < b.string.toLowerCase()) {
      return -1;
    }
    if (a.string.toLowerCase() > b.string.toLowerCase()) {
      return 1;
    }
    return 0;
  });
  var sortedLocale = {};
  for (var i = 0; i < strings.length; i++) {
    sortedLocale[strings[i].hash] = strings[i].string;
  }
  if (options.output === 'object') {
    // depends on persistent javascript object property order
    return sortedLocale;
  }
  if (options.output === 'string') {
    // depends on persistent javascript object property order
    return JSON.stringify(sortedLocale, null, '  ');
  } else {
    throw new Error('Unrecognized `output` type');
  }
};