Skip to content

Commit

Permalink
regexpu: Update to v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Jun 11, 2016
1 parent 3303bf0 commit af6516c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions regexpu/eff.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ code {
background: #ffb6c1;
}

.hide {
display: none;
}

@media (min-width: 42em) {

html {
Expand Down
38 changes: 33 additions & 5 deletions regexpu/eff.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
var es6 = textareas[0];
var es5 = textareas[1];
var regexpu = window.regexpu = require('regexpu');
var inputs = document.getElementsByTagName('input');
var checkboxUnicodePropertyEscapes = inputs[0];
var checkboxUseUnicodeFlag = inputs[1];
var targetLanguage = document.getElementById('target-language');
var divUseUnicodeFlag = document.getElementsByTagName('div')[1];
var run = document.getElementById('run');
var permalink = document.getElementById('permalink');
// https://mathiasbynens.be/notes/localstorage-pattern
Expand All @@ -27,11 +32,19 @@
}

function update() {
var showUnicodeFlagCheckbox = checkboxUnicodePropertyEscapes.checked;
checkboxUseUnicodeFlag.disabled = !showUnicodeFlagCheckbox;
var useES6 = showUnicodeFlagCheckbox && checkboxUseUnicodeFlag.checked;
targetLanguage.textContent = useES6 ? 'ES6' : 'ES5';
divUseUnicodeFlag.classList.toggle('hide', !showUnicodeFlagCheckbox);
var value = es6.value;
var transpiled;
var isError = false;
try {
transpiled = regexpu.transpileCode(value);
transpiled = regexpu.transpileCode(value, {
'unicodePropertyEscape': checkboxUnicodePropertyEscapes.checked,
'useUnicodeFlag': useES6
});
} catch (exception) {
isError = true;
}
Expand All @@ -42,7 +55,15 @@
es6.className = es5.className = '';
es5.value = transpiled;
}
permalink.hash = encode(value);
var params = new URLSearchParams();
params.set('input', value);
if (checkboxUnicodePropertyEscapes.checked) {
params.set('unicodePropertyEscape', '1');
}
if (showUnicodeFlagCheckbox && checkboxUseUnicodeFlag.checked) {
params.set('useUnicodeFlag', '1');
}
permalink.hash = params.toString();
storage && (storage.regexpu = value);
};

Expand All @@ -51,15 +72,22 @@
eval(es5.value);
};

es6.oninput = update;
es6.oninput = checkboxUnicodePropertyEscapes.onchange = checkboxUseUnicodeFlag.onchange = update;

if (storage) {
storage.regexpu && (es6.value = storage.regexpu);
update();
}

window.onhashchange = function() {
es6.value = decodeURIComponent(location.hash.slice(1));
var params = new URLSearchParams(location.hash.slice(1));
checkboxUnicodePropertyEscapes.checked = JSON.parse(
params.get('unicodePropertyEscape')
);
checkboxUseUnicodeFlag.checked = JSON.parse(
params.get('useUnicodeFlag')
);
es6.value = params.get('input');
update();
};

Expand All @@ -74,6 +102,6 @@ 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';
g.src = 'https://www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document));
8 changes: 6 additions & 2 deletions regexpu/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ <h2>ES6:</h2>
var match = string.match(/foo(.)bar/u);
console.log(match[1]);
// → '&#x1D306;'</textarea>
<h2>Transpiled to ES5: (<a href=#run-code id=run>run code</a>, <a href=#var%20string%20%3D%20%27foo%F0%9D%8C%86bar%27%3B%0Avar%20match%20%3D%20string.match%28%2Ffoo%28.%29bar%2Fu%29%3B%0Aconsole.log%28match%5B1%5D%29%3B%0A%2F%2F%20%E2%86%92%20%27%F0%9D%8C%86%27 id=permalink>permalink</a>)</h2>
<div><label><input type=checkbox> enable Unicode property escapes (<code>\p{…}</code> and <code>\P{…}</code></label></div>
<div class=hide><label><input type=checkbox disabled> use ES6 <code>u</code> flag in output</label></div>
<h2>Transpiled to <span id=target-language>ES5</span>: (<a href=#run-code id=run>run code</a>, <a href="#input=var+string+%3D+%27foo%F0%9D%8C%86bar%27%3B%0Avar+match+%3D+string.match(/foo(.)bar/u)%3B%0Aconsole.log(match%5B1%5D)%3B%0A//+%E2%86%92+%27%F0%9D%8C%86%27" id=permalink>permalink</a>)</h2>
<textarea readonly>var string = 'foo&#x1D306;bar';
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))bar/);
console.log(match[1]);
// → '&#x1D306;'</textarea>
<h2>About this tool</h2>
<p>This tool uses <a href=https://mths.be/regexpu><i>regexpu</i></a> to transpile <a href=https://mathiasbynens.be/notes/es6-unicode-regex>ES6 Unicode regular expressions (with a <code>u</code> flag)</a> to equivalent ES5.
<p>The <a href="https://github.com/mathiasbynens/regexpu-core/blob/master/property-escapes.md">Unicode property escapes functionality</a> is <a href="https://github.com/mathiasbynens/es-regexp-unicode-property-escapes">based on a proposal</a> and may or may not reflect what eventually gets specified.
<p id=footer>Made by <a href=https://mathiasbynens.be/>@mathias</a><a href=https://github.com/mathiasbynens/mothereff.in/tree/master/regexpu>fork this on GitHub!</a></p>
<script src=vendor/regexpu-bundle.min.js></script>
<script src=vendor/regexpu-browser.js></script>
<script src=vendor/url-search-params.js></script>
<script src=eff.js></script>

0 comments on commit af6516c

Please sign in to comment.