Skip to content

Commit

Permalink
Refactoring es2016nify
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Oct 18, 2018
1 parent a1c9e11 commit ed5cb6e
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 172 deletions.
4 changes: 2 additions & 2 deletions lib/cache_stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Transform = require('stream').Transform;
const { Transform } = require('stream');

function CacheStream() {
Transform.call(this);
Expand All @@ -11,7 +11,7 @@ function CacheStream() {
require('util').inherits(CacheStream, Transform);

CacheStream.prototype._transform = function(chunk, enc, callback) {
var buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);
const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);

this._cache.push(buf);
this.push(buf);
Expand Down
28 changes: 16 additions & 12 deletions lib/camel_case_keys.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var camelCase = require('camel-case');
const camelCase = require('camel-case');

var rPrefixUnderscore = /^(_+)/;
const rPrefixUnderscore = /^(_+)/;

function getter(key) {
return function() {
Expand All @@ -19,17 +19,17 @@ function setter(key) {
function camelCaseKeys(obj) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');

var keys = Object.keys(obj);
var result = {};
const keys = Object.keys(obj);
const result = {};

for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
var value = obj[key];
var match = key.match(rPrefixUnderscore);
var newKey;
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
const value = obj[key];
const match = key.match(rPrefixUnderscore);
let newKey;

if (match) {
var underscore = match[1];
const underscore = match[1];
newKey = underscore + camelCase(key.substring(underscore.length));
} else {
newKey = camelCase(key);
Expand All @@ -39,8 +39,12 @@ function camelCaseKeys(obj) {
result[key] = value;
} else {
result[newKey] = value;
result.__defineGetter__(key, getter(newKey));
result.__defineSetter__(key, setter(newKey));
Object.defineProperty(result, key, {
get: getter(newKey),
set: setter(newKey),
configurable: true,
enumerable: true
});
}
}

Expand Down
14 changes: 6 additions & 8 deletions lib/escape_diacritic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var defaultDiacriticsRemovalap = [
const defaultDiacriticsRemovalap = [
{'base': 'A', 'letters': '\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F'},
{'base': 'AA', 'letters': '\uA732'},
{'base': 'AE', 'letters': '\u00C6\u01FC\u01E2'},
Expand Down Expand Up @@ -89,12 +89,12 @@ var defaultDiacriticsRemovalap = [
{'base': 'z', 'letters': '\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763'}
];

var diacriticsMap = {};
const diacriticsMap = {};

for (var i = 0; i < defaultDiacriticsRemovalap.length; i++) {
var letters = defaultDiacriticsRemovalap[i].letters.split('');
for (let i = 0; i < defaultDiacriticsRemovalap.length; i++) {
const letters = defaultDiacriticsRemovalap[i].letters.split('');

for (var j = 0; j < letters.length; j++) {
for (let j = 0; j < letters.length; j++) {
diacriticsMap[letters[j]] = defaultDiacriticsRemovalap[i].base;
}
}
Expand All @@ -104,9 +104,7 @@ function escapeDiacritic(str) {

// http://stackoverflow.com/a/18391901
// eslint-disable-next-line no-control-regex
return str.replace(/[^\u0000-\u007E]/g, function(a) {
return diacriticsMap[a] || a;
});
return str.replace(/[^\u0000-\u007E]/g, a => diacriticsMap[a] || a);
}

module.exports = escapeDiacritic;
6 changes: 2 additions & 4 deletions lib/escape_html.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var htmlEntityMap = {
const htmlEntityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
Expand All @@ -13,9 +13,7 @@ function escapeHTML(str) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');

// http://stackoverflow.com/a/12034334
return str.replace(/[&<>"'/]/g, function(a) {
return htmlEntityMap[a];
});
return str.replace(/[&<>"'/]/g, a => htmlEntityMap[a]);
}

module.exports = escapeHTML;
10 changes: 5 additions & 5 deletions lib/hash.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var Transform = require('stream').Transform;
var crypto = require('crypto');
const { Transform } = require('stream');
const crypto = require('crypto');

var ALGORITHM = 'sha1';
const ALGORITHM = 'sha1';

function HashStream() {
Transform.call(this);
Expand All @@ -23,8 +23,8 @@ HashStream.prototype._flush = function(callback) {
callback();
};

exports.hash = function(content) {
var hash = crypto.createHash(ALGORITHM);
exports.hash = content => {
const hash = crypto.createHash(ALGORITHM);
hash.update(content);
return hash.digest();
};
Expand Down
99 changes: 47 additions & 52 deletions lib/highlight.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,71 @@
'use strict';

var hljs = require('highlight.js/lib/highlight');
var Entities = require('html-entities').XmlEntities;
var entities = new Entities();
var alias = require('../highlight_alias.json');
const hljs = require('highlight.js/lib/highlight');
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
const alias = require('../highlight_alias.json');

function highlightUtil(str, options) {
function highlightUtil(str, options = {}) {
if (typeof str !== 'string') throw new TypeError('str must be a string!');
options = options || {};

var useHljs = options.hasOwnProperty('hljs') ? options.hljs : false;
var gutter = options.hasOwnProperty('gutter') ? options.gutter : true;
var wrap = options.hasOwnProperty('wrap') ? options.wrap : true;
var firstLine = options.hasOwnProperty('firstLine') ? +options.firstLine : 1;
var caption = options.caption;
var mark = options.hasOwnProperty('mark') ? options.mark : [];
var tab = options.tab;
const useHljs = options.hasOwnProperty('hljs') ? options.hljs : false;
const {
gutter = true,
firstLine = 1,
caption,
mark = [],
tab
} = options;
let { wrap = true } = options;

hljs.configure({ classPrefix: useHljs ? 'hljs-' : ''});

var data = highlight(str, options);
const data = highlight(str, options);

if (useHljs && !gutter) wrap = false;

var before = useHljs ? '<pre><code class="hljs ' + options.lang + '">' : '<pre>';
var after = useHljs ? '</code></pre>' : '</pre>';
const before = useHljs ? `<pre><code class="hljs ${options.lang}">` : '<pre>';
const after = useHljs ? '</code></pre>' : '</pre>';

if (!wrap) return useHljs ? before + data.value + after : data.value;

var lines = data.value.split('\n');
var numbers = '';
var content = '';
var result = '';
var line;
const lines = data.value.split('\n');
let numbers = '';
let content = '';

for (var i = 0, len = lines.length; i < len; i++) {
line = lines[i];
for (let i = 0, len = lines.length; i < len; i++) {
let line = lines[i];
if (tab) line = replaceTabs(line, tab);
numbers += '<span class="line">' + (firstLine + i) + '</span><br>';
numbers += `<span class="line">${firstLine + i}</span><br>`;
content += formatLine(line, firstLine + i, mark, options);
}

result += '<figure class="highlight' + (data.language ? ' ' + data.language : '') + '">';
let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}">`;

if (caption) {
result += '<figcaption>' + caption + '</figcaption>';
result += `<figcaption>${caption}</figcaption>`;
}

result += '<table><tr>';

if (gutter) {
result += '<td class="gutter"><pre>' + numbers + '</pre></td>';
result += `<td class="gutter"><pre>${numbers}</pre></td>`;
}

result += '<td class="code">' + before + content + after + '</td>';
result += `<td class="code">${before}${content}${after}</td>`;
result += '</tr></table></figure>';

return result;
}

function formatLine(line, lineno, marked, options) {
var useHljs = options.hljs || false;
var res = useHljs ? '' : '<span class="line';
if (marked.indexOf(lineno) !== -1) {
const useHljs = options.hljs || false;
let res = useHljs ? '' : '<span class="line';
if (marked.includes(lineno)) {
// Handle marked lines.
res += useHljs ? '<mark>' + line + '</mark>' : ' marked">' + line + '</span>';
res += useHljs ? `<mark>${line}</mark>` : ` marked">${line}</span>`;
} else {
res += useHljs ? line : '">' + line + '</span>';
res += useHljs ? line : `">${line}</span>`;
}

res += '<br>';
Expand All @@ -78,10 +77,10 @@ function encodePlainString(str) {
}

function replaceTabs(str, tab) {
return str.replace(/^\t+/, function(match) {
var result = '';
return str.replace(/^\t+/, match => {
let result = '';

for (var i = 0, len = match.length; i < len; i++) {
for (let i = 0, len = match.length; i < len; i++) {
result += tab;
}

Expand All @@ -90,7 +89,7 @@ function replaceTabs(str, tab) {
}

function loadLanguage(lang) {
hljs.registerLanguage(lang, require('highlight.js/lib/languages/' + lang));
hljs.registerLanguage(lang, require(`highlight.js/lib/languages/${lang}`));
}

function tryLanguage(lang) {
Expand All @@ -102,29 +101,25 @@ function tryLanguage(lang) {
}

function loadAllLanguages() {
alias.languages.filter(function(lang) {
return !hljs.getLanguage(lang);
}).forEach(loadLanguage);
alias.languages.filter(lang => !hljs.getLanguage(lang)).forEach(loadLanguage);
}

function highlight(str, options) {
var lang = options.lang;
var autoDetect = options.hasOwnProperty('autoDetect') ? options.autoDetect : false;
let { lang } = options;
const { autoDetect = false } = options;

if (!lang && autoDetect) {
loadAllLanguages();
lang = (function() {
var result = hljs.highlightAuto(str);
if (result.relevance > 0 && result.language) return result.language;
const result = hljs.highlightAuto(str);
if (result.relevance > 0 && result.language) lang = result.language;

}());
}

if (!lang) {
lang = 'plain';
}

var result = {
const result = {
value: encodePlainString(str),
language: lang.toLowerCase()
};
Expand All @@ -145,11 +140,11 @@ function highlight(str, options) {

function tryHighlight(str, lang) {
try {
var matching = str.match(/(\r?\n)/);
var separator = matching ? matching[1] : '';
var lines = matching ? str.split(separator) : [str];
var result = hljs.highlight(lang, lines.shift());
var html = result.value;
const matching = str.match(/(\r?\n)/);
const separator = matching ? matching[1] : '';
const lines = matching ? str.split(separator) : [str];
let result = hljs.highlight(lang, lines.shift());
let html = result.value;
while (lines.length > 0) {
result = hljs.highlight(lang, lines.shift(), false, result.top);
html += separator + result.value;
Expand Down
11 changes: 4 additions & 7 deletions lib/html_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
function htmlTag(tag, attrs, text) {
if (!tag) throw new TypeError('tag is required!');

var result = '<' + tag;
let result = `<${tag}`;

for (var i in attrs) {
if (
attrs[i] !== undefined
&& attrs[i] !== null
) result += ' ' + i + '="' + attrs[i] + '"';
for (const i in attrs) {
if (attrs[i] != null) result += ` ${i}="${attrs[i]}"`;
}

result += text == null ? '>' : '>' + text + '</' + tag + '>';
result += text == null ? '>' : `>${text}</${tag}>`;

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var hash = require('./hash');
const hash = require('./hash');

exports.escapeDiacritic = require('./escape_diacritic');
exports.escapeHTML = require('./escape_html');
Expand Down
Loading

0 comments on commit ed5cb6e

Please sign in to comment.