-
-
Save kaanuki/ff75ece93feb994315d65ff62acd6b6d to your computer and use it in GitHub Desktop.
JavaScript: Text Spinner Function
This file contains hidden or 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
| /* ============================================================ | |
| | :=Text Spinner | |
| =============================================================== */ | |
| function preg_quote(str, delimiter) { | |
| return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); | |
| } | |
| function spin(text) { | |
| var matches = text.match(/{[^<]+/gi); | |
| if (matches === null) { | |
| return text; | |
| } | |
| if (matches[0].indexOf('{') != -1) { | |
| matches[0] = matches[0].substr(matches[0].indexOf('{') + 1); | |
| } | |
| if (matches[0].indexOf('}') != -1) { | |
| matches[0] = matches[0].substr(0, matches[0].indexOf('}')); | |
| } | |
| var parts = matches[0].split('|'); | |
| var t = preg_quote(matches[0]); | |
| e_v = new RegExp('{' + t + '}', 'g'); | |
| text = text.replace(e_v, parts[Math.floor(Math.random()*parts.length)]); | |
| return spin(text); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment