Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract browser-only helper functions from shared utils file to their…
… own files
- Loading branch information
Showing
6 changed files
with
90 additions
and
84 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,39 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Highlight the given string of `js`. | ||
* | ||
* @private | ||
* @param {string} js | ||
* @return {string} | ||
*/ | ||
function highlight(js) { | ||
return js | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>') | ||
.replace(/('.*?')/gm, '<span class="string">$1</span>') | ||
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>') | ||
.replace(/(\d+)/gm, '<span class="number">$1</span>') | ||
.replace( | ||
/\bnew[ \t]+(\w+)/gm, | ||
'<span class="keyword">new</span> <span class="init">$1</span>' | ||
) | ||
.replace( | ||
/\b(function|new|throw|return|var|if|else)\b/gm, | ||
'<span class="keyword">$1</span>' | ||
); | ||
} | ||
|
||
/** | ||
* Highlight the contents of tag `name`. | ||
* | ||
* @private | ||
* @param {string} name | ||
*/ | ||
module.exports = function highlightTags(name) { | ||
var code = document.getElementById('mocha').getElementsByTagName(name); | ||
for (var i = 0, len = code.length; i < len; ++i) { | ||
code[i].innerHTML = highlight(code[i].innerHTML); | ||
} | ||
}; |
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,24 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Parse the given `qs`. | ||
* | ||
* @private | ||
* @param {string} qs | ||
* @return {Object<string, string>} | ||
*/ | ||
module.exports = function parseQuery(qs) { | ||
return qs | ||
.replace('?', '') | ||
.split('&') | ||
.reduce(function(obj, pair) { | ||
var i = pair.indexOf('='); | ||
var key = pair.slice(0, i); | ||
var val = pair.slice(++i); | ||
|
||
// Due to how the URLSearchParams API treats spaces | ||
obj[key] = decodeURIComponent(val.replace(/\+/g, '%20')); | ||
|
||
return obj; | ||
}, {}); | ||
}; |
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,23 @@ | ||
'use strict'; | ||
|
||
var parseQuery = require('../../lib/browser/parseQuery'); | ||
|
||
describe('parseQuery()', function() { | ||
it('should get queryString and return key-value object', function() { | ||
expect(parseQuery('?foo=1&bar=2&baz=3'), 'to equal', { | ||
foo: '1', | ||
bar: '2', | ||
baz: '3' | ||
}); | ||
|
||
expect(parseQuery('?r1=^@(?!.*\\)$)&r2=m{2}&r3=^co.*'), 'to equal', { | ||
r1: '^@(?!.*\\)$)', | ||
r2: 'm{2}', | ||
r3: '^co.*' | ||
}); | ||
}); | ||
|
||
it('should parse "+" as a space', function() { | ||
expect(parseQuery('?grep=foo+bar'), 'to equal', {grep: 'foo bar'}); | ||
}); | ||
}); |
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