Skip to content

Instantly share code, notes, and snippets.

View mistadikay's full-sized avatar

Denis Koltsov mistadikay

View GitHub Profile
@mistadikay
mistadikay / index
Last active August 29, 2015 14:05
focusin/focusout polyfill
/**
* usage: NAMESPACE.addFocusInEvent($node, listener);
* replace comments containing [code] with your code
*/
(function(NAMESPACE, global){
var document = global.document,
$body = document.body,
focusInEvent = 'focusin',
focusOutEvent = 'focusout',
$testInputContainer,
function checkControlSumJuridical(value){
var code = value.split(''),
numericValue = +value,
x;
if ((numericValue < 30000000) || (numericValue > 60000000)){
x = code[0] + code[1] * 2 + code[2] * 3 + code[3] * 4 + code[4] * 5 + code[5] * 6 + code[6] * 7;
} else{
x = code[0] * 7 + code[1] + code[2] * 2 + code[3] * 3 + code[4] * 4 + code[5] * 5 + code[6] * 6;
}
function checkControlSumPhysical(value){
var code = value.split(''),
x;
x = code[0] * (-1) + code[1] * 5 + code[2] * 7 + code[3] * 9 + code[4] * 4 + code[5] * 6 + code[6] * 10 + code[7] * 5 + code[8] * 7;
if (x % 11 === 10){
x = (x % 11) % 10;
} else{
x = x - (11 * Math.floor(x / 11));
}
return +code[9] === x;
@mistadikay
mistadikay / bind.js
Created March 25, 2015 08:14
bind polyfill
/**
* bind polyfill by Mozilla
* from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
@mistadikay
mistadikay / index.js
Created March 25, 2015 08:16
shuffle
/* Fisher–Yates shuffle */
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
/**
* @param {object} NS namespace (window by default)
*/
(function(NS){
NS = NS || window;
NS.getRandom = function(){
return Math.random();
};
/**
* @param {object} NS namespace (window by default)
*/
(function(NS){
NS = NS || window;
/**
* Returns option from cases with the correct ending according to passed number
*
* @param {Number} num number
let compare = (n1, n2) => n1 - n2;
let bubbleSort = (arr, cmp = compare) => {
for (let i = 0; i < arr.length; i++) {
for (var j = i; j > 0; j--) {
if (cmp(arr[j], arr[j - 1]) < 0) {
arr[j], arr[j - 1] = arr[j - 1], arr[j];
}
}
}
let insertionSort = (arr) => {
for (let i = 0; i < a.length; i++) {
let toCmp = arr[i];
for (let j = i; j > 0 && toCmp < a[j - 1]; j--)
arr[j] = a[j - 1];
arr[j] = toCmp;
}
return arr;
}
var selectionSort = function (a) {
for (let i = -1; ++i < a.length;) {
for (let m = j = i; ++j < a.length;) {
if (a[m] > a[j]) m = j;
}
a[m], a[i] = a[i], a[m];
}
return a;
}