-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mbrandau/babel
Use babel
- Loading branch information
Showing
9 changed files
with
5,173 additions
and
1,780 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
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,42 @@ | ||
"use strict"; | ||
|
||
// Leaving 'd' out to avoid generating the word 'ad' | ||
var defaultAlphabet = 'abcefghijklmnopqrstuvwxyz0123456789_-'; | ||
|
||
var IdGenerator = function IdGenerator(alphabet) { | ||
var options = { | ||
alphabet: alphabet || defaultAlphabet, | ||
length: 1, | ||
index: 0 | ||
}; | ||
return function () { | ||
var res = generateId(options); | ||
|
||
while (/^[0-9-].*$/.test(res)) { | ||
res = generateId(options); | ||
} | ||
|
||
return res; | ||
}; | ||
}; | ||
|
||
var generateId = function generateId(options) { | ||
var res = ''; | ||
|
||
for (var i = options.length - 1; i >= 0; i--) { | ||
var x = Math.pow(options.alphabet.length, i); | ||
var n = Math.floor(options.index / x); | ||
res += options.alphabet[n % options.alphabet.length]; | ||
} | ||
|
||
options.index++; | ||
|
||
if (options.index > Math.pow(options.alphabet.length, options.length) - 1) { | ||
options.length++; | ||
options.index = 0; | ||
} | ||
|
||
return res; | ||
}; | ||
|
||
module.exports = IdGenerator; |
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,78 @@ | ||
"use strict"; | ||
|
||
var replaceStream = require('replacestream'); | ||
|
||
var IdGenerator = require('./idGenerator'); | ||
|
||
var CLASS_NAME_REGEX = /\/\*[\s\S]*?\*\/|(\.[a-z_-][\w-]*)(?=[^{}]*{)/g; // https://stackoverflow.com/a/48962872/5133130 | ||
|
||
var HTML_CLASS_REGEX = /class="([\w-\s]*)"/g; | ||
|
||
var CssShortener = function CssShortener(options) { | ||
if (!options) options = {}; | ||
this._options = options; | ||
if (!this._options.hasOwnProperty('ignorePrefix')) this._options.ignorePrefix = 'ignore-'; | ||
if (!this._options.hasOwnProperty('trimIgnorePrefix')) this._options.trimIgnorePrefix = true; | ||
this._idGenerator = new IdGenerator(this._options.alphabet); | ||
this._classNameMap = {}; | ||
var t = this; | ||
|
||
var replaceCss = function replaceCss(match, capturingGroup) { | ||
if (!capturingGroup) return match; | ||
var id, | ||
orig = capturingGroup.substr(1); // Remove dot infront of class name | ||
// If the ignorePrefix option is set and the current class starts with the prefix, trim the prefix off and ignore the class. | ||
|
||
if (t._options.ignorePrefix && orig.startsWith(t._options.ignorePrefix)) return t._options.trimIgnorePrefix ? ".".concat(orig.substr(t._options.ignorePrefix.length)) : ".".concat(orig); | ||
if (t._classNameMap[orig] != null) id = t._classNameMap[orig]; // Use mapped class name | ||
else id = t._classNameMap[orig] = t._idGenerator(); // Generate and map new class name | ||
|
||
return ".".concat(id); | ||
}; | ||
|
||
var replaceHtml = function replaceHtml(match, capturingGroup) { | ||
if (!capturingGroup) return match; | ||
var classes = capturingGroup.trim().split(' '); | ||
var classCount = classes.length; | ||
var result = ''; | ||
|
||
for (var i = 0; i < classCount; i++) { | ||
// Check if class is mapped and add it to the result | ||
result += t._classNameMap[classes[i]] != null ? t._classNameMap[classes[i]] : classes[i]; | ||
if (i < classCount - 1) result += ' '; | ||
} | ||
|
||
return "class=\"".concat(result, "\""); | ||
}; | ||
|
||
this.getMap = function () { | ||
return this._classNameMap; | ||
}; | ||
|
||
this.importMap = function (map, override) { | ||
for (var orig in map) { | ||
if (this._classNameMap[orig] != null) { | ||
if (override === true) this._classNameMap[orig] = map[orig]; // Override mapped class name | ||
} else this._classNameMap[orig] = map[orig]; // Import class name | ||
|
||
} | ||
}; | ||
|
||
this.cssStream = function () { | ||
return replaceStream(CLASS_NAME_REGEX, replaceCss); | ||
}; | ||
|
||
this.replaceCss = function (css) { | ||
return css.replace(CLASS_NAME_REGEX, replaceCss); | ||
}; | ||
|
||
this.htmlStream = function () { | ||
return replaceStream(HTML_CLASS_REGEX, replaceHtml); | ||
}; | ||
|
||
this.replaceHtml = function (html) { | ||
return html.replace(HTML_CLASS_REGEX, replaceHtml); | ||
}; | ||
}; | ||
|
||
module.exports = CssShortener; |
Oops, something went wrong.