Skip to content

Instantly share code, notes, and snippets.

View mytholog's full-sized avatar

Igor Gavrilov mytholog

  • 2Gis
  • Russia
View GitHub Profile
@mytholog
mytholog / js: factorial
Last active July 21, 2016 03:23
Factorial
function factorial(n) {
return (n != 1) ? n * factorial(n - 1) : 1;
}
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
@mytholog
mytholog / custom-error.js
Last active August 9, 2016 15:16 — forked from justmoon/custom-error.js
Creating custom Error classes in Node.js
'use strict';
module.exports = function CustomError(message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
require('util').inherits(module.exports, Error);
[1, 2, 3, 4, 5].sort(() => 0.5 - Math.random())
@mytholog
mytholog / transliterate.js
Created October 25, 2016 13:50
transliterate
a = {"Ё":"YO","Й":"I","Ц":"TS","У":"U","К":"K","Е":"E","Н":"N","Г":"G","Ш":"SH","Щ":"SCH","З":"Z","Х":"H","Ъ":"","ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"","Ф":"F","Ы":"I","В":"V","А":"a","П":"P","Р":"R","О":"O","Л":"L","Д":"D","Ж":"ZH","Э":"E","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","Я":"Ya","Ч":"CH","С":"S","М":"M","И":"I","Т":"T","Ь":"","Б":"B","Ю":"YU","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"","б":"b","ю":"yu"};
transliterate = function(word){
return word.split('').map(function(char){
return typeof a[char] !== 'undefined' ? a[char] : char;
}).join("");
};
module.exports = transliterate
// O(2^N)
const fibonacci = function(n){
return n < 2 ? n : fibonacci(n-1) + fibonacci(n-2);
}
// to speed it up - O(2N)
function fibMemo(num, memo = {}) {
if (memo[num]) return memo[num];
if (num <= 1) return 1;
var url = 'https://ronreiter-meme-generator.p.mashape.com/meme?meme=Baby+Godfather&font_size=50&font=Impact&top=Thanks+m&bottom=Later';
var headers = new Headers({'X-Mashape-Key': 'API_KEY'});
var options = {
method: 'GET',
headers: headers,
mode: 'cors',
cache: 'default'
};
var request = new Request(url);
@mytholog
mytholog / LazyLoad.js
Created February 9, 2017 12:40 — forked from neilsoult/LazyLoad.js
LazyLoad directive for loading external javascript for AngularJs. In this example, I use google maps' API as the external library being loaded
angular.module('testApp', []).
directive('lazyLoad', ['$window', '$q', function ($window, $q) {
function load_script() {
var s = document.createElement('script'); // use global document since Angular's $document is weak
s.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize';
document.body.appendChild(s);
}
function lazyLoadApi(key) {
var deferred = $q.defer();
$window.initialize = function () {
@mytholog
mytholog / deepClone.js
Created February 22, 2017 05:06
deep clone JS object
function deepClone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
@mytholog
mytholog / closest.js
Created March 6, 2017 01:52
closest number out of array
const counts = [4, 9, 15, 6, 2];
const goal = 5;
counts
.reduce((prev, curr) => Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);