codeforamerica/intake

View on GitHub
intake/static/intake/js/utils.js

Summary

Maintainability
A
35 mins
Test Coverage
module.exports = {
    getJson: function(name) {
        var json
        elements = document.getElementsByName(name)
        if (elements.length) {
            json = elements[0].text;
        }
        if (json) {
            try {
                return JSON.parse(json);
            } catch (_error) {
                console.warn("Error parsing json!");
                return console.warn(json);
            }
        }
    },
    combineObjs: function(obj1, obj2) {
        for (var attrName in obj2) {
            obj1[attrName] = obj2[attrName];
        }
    },
    getCookie: function(name) {
      var cookieValue = null;
      if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
          var cookie = cookies[i].trim();
          // Does this cookie string begin with the name we want?
          if (cookie.substring(0, name.length + 1) === (name + '=')) {
            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
            break;
          }
        }
      }
      return cookieValue;
    }
}