diff --git a/regexpu/README.md b/regexpu/README.md new file mode 100644 index 0000000..058607d --- /dev/null +++ b/regexpu/README.md @@ -0,0 +1,5 @@ +# [ES6 Unicode regular expression transpiler](https://mothereff.in/regexpu) + +This tool uses [_regexpu_](http://mths.be/regexpu) to transpile [ES6 Unicode regular expressions (with a `u` flag)](https://mathiasbynens.be/notes/es6-unicode-regex) to equivalent ES5. + +Made by [Mathias Bynens](https://mathiasbynens.be/). diff --git a/regexpu/eff.css b/regexpu/eff.css new file mode 100644 index 0000000..53a98fd --- /dev/null +++ b/regexpu/eff.css @@ -0,0 +1,87 @@ +html, textarea { + font: .8em/1.6 sans-serif; +} + +body { + max-width: 40em; + padding: 0 1em; +} + +h1 { + text-align: center; + font-size: 1.3em; + margin: 0 0 .5em; + padding-top: 1em; +} + +h2 { + font-size: 1em; +} + +a { + color: #333; + text-decoration: none; + border-bottom: 1px solid #aaa; + padding: .1em .2em; +} + +a:hover, a:focus { + color: #fff; + border-color: #036; + background: #36c; +} + +textarea { + font-family: Monaco, Consolas, monospace; +} + +#footer { + margin-top: 2em; + text-align: center; +} + +textarea { + border: 3px double green; + background: #90ee90; + width: 100%; + -moz-box-sizing: border-box; + -webkit-box-sizing: border-box; + box-sizing: border-box; + display: block; + margin: 1em 0 .5em; + padding: .7em; + resize: vertical; + min-height: 14.5em; +} + +code { + font-family: Monaco, Consolas, monospace; + font-size: .9em; + white-space: pre; + white-space: pre-wrap; + word-wrap: break-word; +} + +.invalid, :invalid { + border-color: red; + background: #ffb6c1; +} + +@media (min-width: 42em) { + + html { + font-size: 1.2em; + background: #c4c4c4; + height: 100%; + } + + body { + margin: 0 auto; + padding: 0 2em; + min-height: 100%; + background: #fff; + border: solid #aaa; + border-width: 0 1px; + } + +} diff --git a/regexpu/eff.js b/regexpu/eff.js new file mode 100644 index 0000000..c183690 --- /dev/null +++ b/regexpu/eff.js @@ -0,0 +1,79 @@ +(function(window, document) { + + var textareas = document.getElementsByTagName('textarea'); + var es6 = textareas[0]; + var es5 = textareas[1]; + var regexpu = require('regexpu'); + var run = document.getElementById('run'); + var permalink = document.getElementById('permalink'); + // https://mathiasbynens.be/notes/localstorage-pattern + var storage = (function() { + var uid = new Date; + var storage; + var result; + try { + (storage = window.localStorage).setItem(uid, uid); + result = storage.getItem(uid) == uid; + storage.removeItem(uid); + return result && storage; + } catch (exception) {} + }()); + + function encode(string) { + // URL-encode some more characters to avoid issues when using permalink URLs in Markdown + return encodeURIComponent(string).replace(/['()_*]/g, function(character) { + return '%' + character.charCodeAt().toString(16); + }); + } + + function update() { + var value = es6.value; + var transpiled; + var isError = false; + try { + transpiled = regexpu.transpileCode(value); + } catch (exception) { + isError = true; + } + if (isError) { + es6.className = es5.className = 'invalid'; + es5.value = '// Error during transpilation.'; + } else { + es6.className = es5.className = ''; + es5.value = transpiled; + } + permalink.hash = encode(value); + storage && (storage.regexpu = value); + }; + + run.onclick = function(event) { + event.preventDefault(); + eval(es5.value); + }; + + es6.oninput = update; + + if (storage) { + storage.regexpu && (es6.value = storage.regexpu); + update(); + } + + window.onhashchange = function() { + es6.value = decodeURIComponent(location.hash.slice(1)); + update(); + }; + + if (location.hash) { + window.onhashchange(); + } + +}(this, document)); + +// Google Analytics +window._gaq = [['_setAccount', 'UA-6065217-60'], ['_trackPageview']]; +(function(d) { + var g = d.createElement('script'); + var s = d.scripts[0]; + g.src = '//www.google-analytics.com/ga.js'; + s.parentNode.insertBefore(g, s); +}(document)); diff --git a/regexpu/index.html b/regexpu/index.html new file mode 100644 index 0000000..704c08c --- /dev/null +++ b/regexpu/index.html @@ -0,0 +1,24 @@ + + + +
This tool uses regexpu to transpile ES6 Unicode regular expressions (with a u
flag) to equivalent ES5.
+