Last active
July 31, 2019 05:58
-
-
Save stonegray/dc030b79957088b6af19d1373f79b056 to your computer and use it in GitHub Desktop.
Obfuscated JS practice: Matching bracket pairs
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
/* brackets.js | |
* | |
* Obfuscated JS practice. | |
* | |
* Accepts a string containing Javascirpt code, returns true if the brackets | |
* are balanced. Supports (), {}, and []. | |
*/ | |
const bracketsAreBalanced = n => ( | |
( | |
( | |
f = c = o = !( | |
z = String.fromCharCode | |
) | |
), | |
( | |
a = [ | |
40, 123, 91 | |
] | |
) | |
).forEach(s => | |
n.split("").forEach(h => | |
( | |
q = [ | |
z( | |
s | |
), | |
z( | |
s + 1 + ( | |
s > a[ | |
0 | |
] | |
) | |
) | |
], | |
h == q [ | |
+f | |
] ? o++ : 0, | |
h == [ | |
z( | |
s | |
), | |
z( | |
s + 1 + ( | |
s > a [ | |
0 | |
] | |
) | |
) | |
][ | |
+!f | |
] ? c++ : 0, | |
!( | |
c > o | |
) || f++ | |
) | |
), | |
c = o = 0 | |
), | |
( | |
c !== o | |
) + f == 0 | |
); | |
console.log(bracketsAreBalanced('{{bar;}{}[(23),bar]([/*foo*/]){}}')); // true | |
/* const bracketsAreBalanced = n => ( | |
((f = c = o = !(z = String.fromCharCode)), (a = [40, 123, 91])) | |
.forEach(s => n.split("").forEach(h => ( | |
q = [z(s), z(s + 1 + (s > a[0]))], | |
h == q[+f] ? o++ : 0, h == [z(s), | |
z(s + 1 + (s > a[0])) | |
][+!f] ? c++ | |
: | |
0, !(c > o) || f++)), c = o = 0), | |
(c !== o) + f == 0 | |
); */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment