Skip to content

Instantly share code, notes, and snippets.

@stonegray
Last active July 31, 2019 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stonegray/dc030b79957088b6af19d1373f79b056 to your computer and use it in GitHub Desktop.
Save stonegray/dc030b79957088b6af19d1373f79b056 to your computer and use it in GitHub Desktop.
Obfuscated JS practice: Matching bracket pairs
/* 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