-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore url.js and modifcations to ajax.js (#551)
- Loading branch information
Nate Guisinger
authored
Aug 20, 2016
1 parent
9527cc2
commit fc91b58
Showing
3 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export function parseQS(query) { | ||
return !query ? {} : query | ||
.replace(/^\?/, '') | ||
.split('&') | ||
.reduce((acc, criteria) => { | ||
let [k, v] = criteria.split('='); | ||
if (/\[\]$/.test(k)) { | ||
k = k.replace('[]', ''); | ||
acc[k] = acc[k] || []; | ||
acc[k].push(v); | ||
} else { | ||
acc[k] = v || ''; | ||
} | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
export function formatQS(query) { | ||
return Object | ||
.keys(query) | ||
.map(k => Array.isArray(query[k]) ? | ||
query[k].map(v => `${k}[]=${v}`).join('&') : | ||
`${k}=${query[k]}`) | ||
.join('&'); | ||
} | ||
|
||
export function parse(url) { | ||
let parsed = document.createElement('a'); | ||
parsed.href = decodeURIComponent(url); | ||
return { | ||
protocol: (parsed.protocol || '').replace(/:$/, ''), | ||
hostname: parsed.hostname, | ||
port: +parsed.port, | ||
pathname: parsed.pathname, | ||
search: parseQS(parsed.search || ''), | ||
hash: (parsed.hash || '').replace(/^#/, ''), | ||
host: parsed.host | ||
}; | ||
} | ||
|
||
export function format(obj) { | ||
return (obj.protocol || 'http') + '://' + | ||
(obj.host || | ||
obj.hostname + (obj.port ? `:${obj.port}` : '')) + | ||
(obj.pathname || '') + | ||
(obj.search ? `?${formatQS(obj.search || '')}` : '') + | ||
(obj.hash ? `#${obj.hash}` : ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {format, parse} from '../../src/url'; | ||
|
||
describe('helpers.url', () => { | ||
|
||
describe('parse()', () => { | ||
|
||
let parsed; | ||
|
||
beforeEach(() => { | ||
parsed = parse('http://example.com:3000/pathname/?search=test&foo=bar#hash'); | ||
}); | ||
|
||
it('extracts the protocol', () => { | ||
expect(parsed).to.have.property('protocol', 'http'); | ||
}); | ||
|
||
it('extracts the hostname', () => { | ||
expect(parsed).to.have.property('hostname', 'example.com'); | ||
}); | ||
|
||
it('extracts the port', () => { | ||
expect(parsed).to.have.property('port', 3000); | ||
}); | ||
|
||
it('extracts the pathname', () => { | ||
expect(parsed).to.have.property('pathname', '/pathname/'); | ||
}); | ||
|
||
it('extracts the search query', () => { | ||
expect(parsed).to.have.property('search'); | ||
expect(parsed.search).to.eql({ | ||
foo: 'bar', | ||
search: 'test' | ||
}); | ||
}); | ||
|
||
it('extracts the hash', () => { | ||
expect(parsed).to.have.property('hash', 'hash'); | ||
}); | ||
|
||
it('extracts the host', () => { | ||
expect(parsed).to.have.property('host', 'example.com:3000'); | ||
}); | ||
|
||
}); | ||
|
||
describe('format()', () => { | ||
|
||
it('formats an object in to a URL', () => { | ||
expect(format({ | ||
protocol: 'http', | ||
hostname: 'example.com', | ||
port: 3000, | ||
pathname: '/pathname/', | ||
search: {foo: 'bar', search: 'test'}, | ||
hash: 'hash' | ||
})).to.equal('http://example.com:3000/pathname/?foo=bar&search=test#hash'); | ||
}); | ||
|
||
it('will use defaults for missing properties', () => { | ||
expect(format({ | ||
hostname: 'example.com' | ||
})).to.equal('http://example.com'); | ||
}); | ||
|
||
}); | ||
|
||
}); |