Socialify

Folder ..

Viewing index.js
172 lines (156 loc) • 4.1 KB

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var nodeUrl = require('url');
var querystring = require('querystring');
var _ = require('underscore');

var UrlGrey = function(url){
  this.url = url;
  this._parsed = null;
};

UrlGrey.prototype.parsed = function(){
  if (!this._parsed){
    this._parsed = nodeUrl.parse(this.url);
    var p = this._parsed;
    if (p.protocol){
      p.protocol = p.protocol.slice(0,-1);
    } else {
      p.protocol = 'http';
    }
    if (p.hash){
      p.hash = p.hash.substring(1);
    }
    p.username = ''; 
    p.password = '';
    if (!p.hostname){
      p.hostname = 'localhost';
    }
    if (!p.port){
      p.port = 80;
    } else {
      p.port = parseInt(p.port, 10);
    }
    if (p.auth){
      var auth = p.auth.split(':');
      p.username = auth[0]; 
      p.password = auth[1];
    } 
  }
  return this._parsed;
};

UrlGrey.prototype.query = function(mergeObject){
  var path;
  if (mergeObject === false){
    // clear the query entirely if the input === false
    return this.queryString('');
  }
  
  var url = this.url;
  if (!mergeObject){
    var parsed = nodeUrl.parse(url);
    if (!!parsed.search){
      var qstr = parsed.search.substring(1);
      return querystring.parse(qstr);
    }
    return {};
  } else {
    // read the object out
    var oldQuery = querystring.parse(this.queryString());
    _.each(mergeObject, function(v, k){
      if (v === null){
        delete oldQuery[k];
      } else {
        oldQuery[k] = v;
      }
    });
    var newString = querystring.stringify(oldQuery, '&', '=');
    return this.queryString(newString);
  }
};


addPropertyGetterSetter('protocol');
addPropertyGetterSetter('port');
addPropertyGetterSetter('username');
addPropertyGetterSetter('password');
addPropertyGetterSetter('hostname');
addPropertyGetterSetter('hash');
// add a method called queryString that manipulates 'query'
addPropertyGetterSetter('query', 'queryString');  
addPropertyGetterSetter('pathname', 'path');  

UrlGrey.prototype.path = function(){
  var args = _.toArray(arguments);
  if (args.length !== 0){
    var obj = new UrlGrey(this.toString());
    var str = _.flatten(args).join('/');
    str = str.replace(/\/+/g, '/'); // remove double slashes
    str = str.replace(/\/$/, '');  // remove all trailing slashes
    if (str[0] !== '/'){ str = '/' + str; }
    obj.parsed().pathname = str;
    return obj;
  }
  return this.parsed().pathname;
};


UrlGrey.prototype.encode = function(str){
  return querystring.escape(str);
};

UrlGrey.prototype.decode = function(str){
  return querystring.unescape(str);
};

UrlGrey.prototype.parent = function(){
  // read-only.  (can't SET parent)
  var pieces = this.path().split("/");
  var popped = pieces.pop();
  if (popped === ''){  // ignore trailing slash
    pieces.pop();
  }
  return this.path(pieces.join("/"));
};

UrlGrey.prototype.child = function(suffix){
  if (suffix){
    suffix = encodeURIComponent(suffix);
    return this.path(this.path(), suffix);
  } else {
    // if no suffix, return the child
    var pieces = this.path().split("/");
    var last = _.last(pieces);
    if ((pieces.length > 1) && (last === '')){
      // ignore trailing slashes
      pieces.pop();
      last = _.last(pieces);
    }
    return last;
  }
};

UrlGrey.prototype.toJSON = function(){
  return this.toString();
};

UrlGrey.prototype.toString = function(){
  var p = this.parsed();
  var userinfo = p.username + ':' + p.password;
  var retval = this.protocol() + '://';
  if (userinfo != ':'){
    retval += userinfo + '@';
  }
  retval += p.hostname;
  if (this.port() !== 80){
    retval += ':' + this.port();
  }
  retval += this.path() === '/' ? '' : this.path();
  var qs = this.queryString();
  if (qs){
    retval += '?' + qs;
  }
  if (p.hash){
    retval += '#' + p.hash;
  }
  return retval;
};

module.exports = function(url){ return new UrlGrey(url); };

function addPropertyGetterSetter(propertyName, methodName){
  if (!methodName){
    methodName = propertyName;
  }
  UrlGrey.prototype[methodName] = function(str){
    if (!!str || str === ''){
      var obj = new UrlGrey(this.toString());
      obj.parsed()[propertyName] = str;
      return obj;
    }
    return this.parsed()[propertyName];  
  };
}