Skip to content

Commit

Permalink
Extract browser-only helper functions from shared utils file to their…
Browse files Browse the repository at this point in the history
… own files
  • Loading branch information
Munter committed Jun 12, 2020
1 parent 0adda84 commit ca71ddc
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 84 deletions.
6 changes: 4 additions & 2 deletions browser-entry.js
Expand Up @@ -9,6 +9,8 @@

process.stdout = require('browser-stdout')({label: false});

var parseQuery = require('./lib/browser/parseQuery');
var highlightTags = require('./lib/browser/highlightTags');
var Mocha = require('./lib/mocha');

/**
Expand Down Expand Up @@ -162,7 +164,7 @@ mocha.run = function(fn) {
var options = mocha.options;
mocha.globals('location');

var query = Mocha.utils.parseQuery(global.location.search || '');
var query = parseQuery(global.location.search || '');
if (query.grep) {
mocha.grep(query.grep);
}
Expand All @@ -181,7 +183,7 @@ mocha.run = function(fn) {
document.getElementById('mocha') &&
options.noHighlighting !== true
) {
Mocha.utils.highlightTags('code');
highlightTags('code');
}
if (fn) {
fn(err);
Expand Down
39 changes: 39 additions & 0 deletions lib/browser/highlightTags.js
@@ -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, '&lt;')
.replace(/>/g, '&gt;')
.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);
}
};
24 changes: 24 additions & 0 deletions lib/browser/parseQuery.js
@@ -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;
}, {});
};
61 changes: 0 additions & 61 deletions lib/utils.js
Expand Up @@ -91,67 +91,6 @@ exports.clean = function(str) {
return str.trim();
};

/**
* Parse the given `qs`.
*
* @private
* @param {string} qs
* @return {Object}
*/
exports.parseQuery = function(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;
}, {});
};

/**
* Highlight the given string of `js`.
*
* @private
* @param {string} js
* @return {string}
*/
function highlight(js) {
return js
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.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
*/
exports.highlightTags = function(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);
}
};

/**
* If a value could have properties, and has none, this function is called,
* which returns a string representation of the empty value.
Expand Down
23 changes: 23 additions & 0 deletions test/unit/parseQuery.spec.js
@@ -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'});
});
});
21 changes: 0 additions & 21 deletions test/unit/utils.spec.js
Expand Up @@ -567,27 +567,6 @@ describe('lib/utils', function() {
});
});

describe('parseQuery()', function() {
var parseQuery = utils.parseQuery;
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'});
});
});

describe('isPromise()', function() {
it('should return true if the value is Promise-ish', function() {
expect(
Expand Down

0 comments on commit ca71ddc

Please sign in to comment.