jsUri icon indicating copy to clipboard operation
jsUri copied to clipboard

v2 using ES5 properties?

Open nathanboktae opened this issue 8 years ago • 0 comments

Have you considered freshening up the API with ES5 Properties? I'll take the readme and offer a proposal of what could be done with it:


Pass any URL into the constructor:

var uri = new Uri('http://user:[email protected]:81/index.html?q=books#fragment')

Use property methods to get at the various parts:

uri.protocol    // http
uri.userInfo    // user:pass
uri.host        // www.test.com
uri.port        // 81
uri.path        // /index.html
uri.query       // q=books
uri.anchor      // fragment

Property methods accept an optional value to set:

uri.protocol = 'https'
uri.toString()    // https://user:[email protected]:81/index.html?q=books#fragment

uri.host = 'mydomain.com'
uri.toString()    // https://user:[email protected]:81/index.html?q=books#fragment

ES5 Propery setter methods help you compose strings:

Object.assign(new Uri(), {
    path: '/archives/1979/',
    query: { page: 1 }
}).toString()                   // /archives/1979?page=1

Object.assign(new Uri(), {
    path: '/index.html',
    anchor: 'content',
    host: 'www.test.com',
    port: 8080,
    userInfo: 'username:password',
    protocol: 'https'
    query: {
        this: 'that',
        some: 'thing'
    }
}).toString()                  // https://username:[email protected]:8080/index.html?this=that&some=thing#content

especially with the query changes, that would really freshen up the API. Poor souls that still need to support IE8 can stay on v1.

Thoughts?

nathanboktae avatar Sep 11 '15 04:09 nathanboktae