Skip to content

Instantly share code, notes, and snippets.

View Aleksey-Danchin's full-sized avatar

Алексей Данчин Aleksey-Danchin

View GitHub Profile
@Aleksey-Danchin
Aleksey-Danchin / example2.js
Last active October 11, 2016 06:02
Делаем объекты итерируемыми для for-of по [key, value]
const object = {a: 1, b: 2};
Object.defineProperty(Object.prototype, Symbol.iterator, {
enumirable: false
, configurable: true
, get: () => function * () {
for (const key of Object.keys(this))
yield [key, this[key]];
}
});
@Aleksey-Danchin
Aleksey-Danchin / example1.js
Created October 10, 2016 08:06
Фильтрация массива на уникальные элементов
let array = [1, 'b', 3, 'f', 'f', 'd', 'a', 9, 'b', 8, 'd', 4, 9, 'a', 5, 10, 3, 'c', 8, 7, 7, 6, 1, 'g', 'a', 'g', 4, 'e', 9, 8, 6, 5, 5, 2, 'c', 'd', 'e', 1, 6, 4, 2, 'b', 3, 10, 'e', 'f', 2, 'c', 10, 'g', 7];
array = array.filter((e, i, l) => i == l.lastIndexOf(e));
console.log(array); // => [ 'a', 9, 8, 5, 'd', 1, 6, 4, 'b', 3, 'e', 'f', 2, 'c', 10, 'g', 7 ]
@Aleksey-Danchin
Aleksey-Danchin / watch.js
Created August 20, 2016 20:07
Watch service
function Watch (obj, scheme, handler, pathOriginal = [], _obj = {}) {
scheme.forEach(fieldName => {
const path = pathOriginal.map(e => e);
if (typeof fieldName === 'string') {
path.push(fieldName);
if (['string', 'number', 'boolean'].indexOf(typeof obj[fieldName]) !== -1 || obj[fieldName] === null) {
_obj[fieldName] = obj[fieldName];
delete obj[fieldName];
Object.defineProperty(obj, fieldName, {
enumerable: true
@Aleksey-Danchin
Aleksey-Danchin / cat.js
Created June 27, 2016 11:54
Скрипт поика разреза на одинаковые части.
"use strict";
let res = `
1111110
1111100
1111111
1111111
1111111
0111111
0111111
@Aleksey-Danchin
Aleksey-Danchin / isIntersection.js
Created January 21, 2016 22:49
isIntersection (v2 teory)
function isIntersection (x1, y1, x2, y2, x3, y3, x4, y4) {
var angles = [
getAngleOf(x2, y2, x1, y1, x3, y3) + getAngleOf(x2, y2, x1, y1, x4, y4),
getAngleOf(x4, y4, x3, y3, x1, y1) + getAngleOf(x4, y4, x3, y3, x2, y2),
getAngleOf(x1, y1, x2, y2, x3, y3) + getAngleOf(x1, y1, x2, y2, x4, y4),
getAngleOf(x3, y3, x4, y4, x2, y2) + getAngleOf(x3, y3, x4, y4, x1, y1)
];
var sum = angles[0] + angles[1] + angles[2] + angles[3];
@Aleksey-Danchin
Aleksey-Danchin / formated.js
Last active October 17, 2015 06:31
Форматирование страницы в firefox.
// Удаление шапки в которой написн вариант.
$('div.header').remove();
// Удаление номеров каждого задания.
var elements = $('.nobreak.pbody').find('span');
for (var i = 0; i < elements.length; i++) {
var element = $(elements[i]),
text = element.text(),
index = text.indexOf('.'),
text = '№ ' + text.substr(0, index) + ')';
@Aleksey-Danchin
Aleksey-Danchin / FirstTimeUbuntu
Last active August 29, 2015 14:25
First time Ubuntu softwares install command.
sudo apt-get install thunderbird apache2 mysql-server php5-mysql php5 libapache2-mod-php5 php5-mcrypt git gitk npm vim htop ssh traceroute vlc
@Aleksey-Danchin
Aleksey-Danchin / binSum.js
Created July 15, 2015 09:38
Bitwise suma.
function sum (a, b) {
var c = [], buff = false;
for (var i = a.length - 1; i >= 0; i--)
if ((a[i] && b[i]) || (!a[i] && !b[i])) {
c.unshift(buff);
buff = a[i];
} else c.unshift(!buff);
if (buff) c.unshift(true);
@Aleksey-Danchin
Aleksey-Danchin / insertSort.js
Created July 15, 2015 09:04
Sort by Insert Method.
function insertSort (array) {
for (var i = 1; i < array.length; i++) {
var key = array[i], j = i - 1;
while (j > -1 && array[j] > key)
array[j + 1] = array[j--];
array[j + 1] = key;
}
function setIntervalControlling (callback, time) {
var startMoment = (new Date).getTime(),
number = 0, timeOfLoop = 0, difference = 0,
realTime = time, minStep = Math.pow(2, -4);
var loop;
__restart();
return { stop: function () { clearInterval(loop); } };