Code coverage report for sc/libs/strlib.js

Statements: 100% (29 / 29)      Branches: 100% (12 / 12)      Functions: 100% (8 / 8)      Lines: 100% (29 / 29)      Ignored: none     

All files » sc/libs/ » strlib.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 551     1   1 1   1 1835 1282   553     1 22608 22608     1 3814 3814 7163       3814     1 1 1 4 3         1     1 3815 107   3708 1   3707     1    
(function(sc) {
  "use strict";
 
  require("./libs");
 
  var slice = [].slice;
  var strlib = {};
 
  strlib.article = function(name) {
    if (/^[AEIOU]/i.test(name)) {
      return "an";
    }
    return "a";
  };
 
  strlib.isClassName = function(name) {
    var ch = name.charCodeAt(0);
    return 0x41 <= ch && ch <= 0x5a;
  };
 
  function formatWithList(fmt, list) {
    var msg = fmt;
    list.forEach(function(value, index) {
      msg = msg.replace(
        new RegExp("#\\{" + index + "\\}", "g"), String(value)
      );
    });
    return msg;
  }
 
  function formatWithDict(fmt, dict) {
    var msg = fmt;
    Object.keys(dict).forEach(function(key) {
      if (/^\w+$/.test(key)) {
        msg = msg.replace(
          new RegExp("#\\{" + key + "\\}", "g"), String(dict[key])
        );
      }
    });
    return msg;
  }
 
  strlib.format = function(fmt, arg) {
    if (Array.isArray(arg)) {
      return formatWithList(fmt, arg);
    }
    if (arg && arg.constructor === Object) {
      return formatWithDict(fmt, arg);
    }
    return formatWithList(fmt, slice.call(arguments, 1));
  };
 
  sc.libs.strlib = strlib;
})(sc);