Created
December 30, 2017 13:16
-
-
Save GNURub/bf79fbf826d4dec12826e90b093d438f to your computer and use it in GitHub Desktop.
Find characterds
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
| 'use strict'; | |
| const alphbet = "aáàbcdeéèfghiíìjklmnñoóòpqrstuúùvwxyz".split(''); | |
| class Matrix { | |
| constructor(element) { | |
| if (typeof element === 'string') { | |
| this.__element = document.querySelectorAll(element); | |
| return; | |
| } | |
| this.__element = element; | |
| } | |
| start() { | |
| if (!this.__element) { | |
| return; | |
| } | |
| if ('length' in this.__element) { | |
| Array.from(this.__element).forEach((e) => { | |
| this.__showBuidlContainers(e); | |
| }); | |
| return; | |
| } | |
| return this.__showBuidlContainers(this.__element); | |
| } | |
| __showBuidlContainers(ele) { | |
| let text = ele.innerText; | |
| if (this.__isEmpty(text)) { | |
| return; | |
| } | |
| const letters = text.split(''); | |
| ele.innerHTML = '<span></span>'.repeat(letters.length); | |
| let prom = []; | |
| letters.forEach((currentLetter, i) => { | |
| prom.push(this.__writeLetter(currentLetter, ele.querySelectorAll('span')[i], this.__isUpper(currentLetter))()); | |
| }); | |
| return Promise.all(prom); | |
| } | |
| __isUpper(a) { | |
| return a === a.toUpperCase(); | |
| } | |
| __writeLetter(originalLetter, ele, isUpper) { | |
| let cached = []; | |
| var writeletter = (posLetter, resolv) => { | |
| if (alphbet.indexOf(originalLetter.toLowerCase()) === -1) { | |
| ele.innerText = originalLetter; | |
| return resolv(true); | |
| } | |
| if (!alphbet[posLetter]) { | |
| return resolv(true); | |
| } | |
| let currentLetter = isUpper ? alphbet[posLetter].toUpperCase() : alphbet[posLetter]; | |
| ele.innerText = currentLetter; | |
| if (currentLetter === originalLetter) { | |
| return resolv(true); | |
| } | |
| let random = 0; | |
| do { | |
| random = this.__random(0, alphbet.length - 1); | |
| } while(cached.indexOf(random) > -1); | |
| cached.push(random); | |
| setTimeout(() => writeletter(random, resolv), this.__random(50, 100)); | |
| } | |
| return () => { | |
| return new Promise((resolv) => { | |
| return writeletter(this.__random(0, alphbet.length - 1), resolv); | |
| }) | |
| } | |
| } | |
| __random(min, max) { | |
| return Math.floor(Math.random() * (max - min + 1)) + min; | |
| } | |
| __isEmpty(text) { | |
| return !text || text.trim().length === 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment