Skip to content

Instantly share code, notes, and snippets.

View ivan-demchenko's full-sized avatar

Ivan Demchenko ivan-demchenko

View GitHub Profile
@ivan-demchenko
ivan-demchenko / primeNumbers.js
Last active January 4, 2016 11:09
Prime numbers algorythm
function getPrimeNumbers(n) {
var arr = [];
for(var i = 2; i <= n; i++) {
for(var k = 2; k <= i; k++) {
if (i === k) arr.push(i);
if (i % k === 0) break;
}
}
@ivan-demchenko
ivan-demchenko / gist:8912130
Created February 10, 2014 08:11
Test the value whether it is a number.
function isNumber(value) {
return typeof value === 'number' && isFinite(value);
}
@ivan-demchenko
ivan-demchenko / gist:8912213
Last active August 29, 2015 13:56
Get the real type of a value
/**
* Thanks to Dmitry Baranovsky for his knowledge sharing:
* http://dmitry.baranovskiy.com/post/typeof-and-friends
*/
function is (value) {
return {
aan: function (type) {
type = String(type).toLowerCase();
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase() === type;
}
@ivan-demchenko
ivan-demchenko / gist:9093104
Last active August 29, 2015 13:56
CSS media queries template
/* portrait phone (< 480px) */
@media screen and (max-width:29.9999em) {
}
/* landscape phone and portrait tablet (>= 480px < 960px) */
@media screen and (min-width:30em) and (max-width:59.9999em) {
}
/* landscape tablet and normal monitor (>= 960px < 1440px) */
@media screen and (min-width:60em) and (max-width:89.9999em) {
@ivan-demchenko
ivan-demchenko / gist:10534886
Last active August 29, 2015 13:59
My Ubuntu Tweaks
#
# Ubuntu Tweak
#
sudo add-apt-repository ppa:tualatrix/ppa -y && sudo apt-get update && sudo apt-get install ubuntu-tweak -y
#
# Git
#
sudo apt-get update && sudo apt-get install git giggle -y
@ivan-demchenko
ivan-demchenko / colors.styl
Created March 26, 2015 21:42
Organise colours palette with Stylus
// These are colors in our palette
availableColors = red #F00,
blue #00F,
green #0F0
// Function that can find a color by name
colourByName(name)
return pair[1] if name == pair[0] for pair in availableColors
// Now we can find our color by name
@ivan-demchenko
ivan-demchenko / iFrameContent.js
Created April 7, 2015 12:43
Get the content of an iFrame
// Sometimes it's needed to get access to body or head elements within an iFrame.
// Here is a peace of code that helps to do this without jQuery.
var iFrameContent = window.iFrameContent || {};
iFrameContent.withIn = function(iFrameElement) {
if (!iFrameElement) {
throw 'Please, specify an iFrame to work with';
}
if (iFrameElement.nodeName !== 'IFRAME') {
@ivan-demchenko
ivan-demchenko / directive.spec.js
Created April 15, 2015 12:05
Test directive's controller
// http://stackoverflow.com/a/24761145/993216
describe("myDirective", function() ->
var el, scope, controller;
beforeEach inject(function($compile, $rootScope) {
el = angular.element("<my-directive></my-directive>")
$compile(el)($rootScope.$new())
$rootScope.$digest()
@ivan-demchenko
ivan-demchenko / lazy-take-while-list.js
Created September 20, 2015 16:27
Lazy list comprehension using generator
// Inspired by Haskell's `takeWhile (<10000) $ map (^3) [1..]`
let nextVal = (x) => x.next().value;
function* mList(x = 0) {
while(1) yield x++;
}
function* map(fn, list) {
while(1) {
yield fn(nextVal(list));
// This is the implmenetation of some of the lamdas from lamda calculus
// https://en.wikipedia.org/wiki/Lambda_calculus
console.clear();
const log = function(x) {
console.log('log: ' + x);
return x;
}
// Boolean: