Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Yaroslav Surilov zhibirc

🎯
Focusing
View GitHub Profile
@zhibirc
zhibirc / gist:db0baa7ef3f4a7e564a19208ebae9c86
Last active Jul 7, 2017
Protection of the function code from inspection
View gist:db0baa7ef3f4a7e564a19208ebae9c86
// basic implementation
function protect ( fn ) {
fn.toString = fn.toLocaleString = fn.toSource = function () {
return 'function ' + fn.name + '() { [native code] }';
};
}
// but indirect call still available, so fix it
var protect = (function () {
// function can be reassign with different name, so use functions itself instead of string names
@zhibirc
zhibirc / protector.js
Last active Oct 24, 2018
Protect API methods or arbitrary functions from introspection
View protector.js
/* Aim:
"function <function name>() {
[native code]
}"
*/
function protect ( fn ) {
fn.toString = fn.toLocaleString = fn.toSource = function () {
return 'function ' + fn.name + '() { [native code] }';
};
@zhibirc
zhibirc / recursion-examples.js
Last active Mar 19, 2021
Recursive one-liners (or a bit more) for popular tasks
View recursion-examples.js
/** @see {@link https://en.wikipedia.org/wiki/Digit_sum} */
const dsum = n => n < 10 ? n : n % 10 + dsum(n / 10 | 0);
/** @see {@link https://encyclopediaofmath.org/wiki/Factorial} */
const fact = n => n ? n * fact(n - 1) : 1;
/** @see {@link https://encyclopediaofmath.org/wiki/Greatest_common_divisor} */
const gcd = (a, b) => b ? gcd(b, a % b) : a;
/** @see {@link https://encyclopediaofmath.org/wiki/Power|Fast algorithm} */