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 / 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 () {
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 / 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
@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);
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
@mytholog
mytholog / js: factorial
Last active July 21, 2016 03:23
Factorial
function factorial(n) {
return (n != 1) ? n * factorial(n - 1) : 1;
}
[1, 2, 3, 4, 5].sort(() => 0.5 - Math.random())
// 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 counts = {};
array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
arr1
.filter(function (x) { return arr2.indexOf(x) === -1;})
.concat(arr2.filter(function (x) {return arr1.indexOf(x) === -1;}));