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 / Lever.js
Last active March 27, 2017 15:49
Left Lever
!(function () {
class Lever {
constructor(data) {
const lever = this;
lever.canvas = document.getElementById(data.canvasId);
lever.context = lever.canvas.getContext('2d');
lever.canvas.width = data.width;
lever.canvas.height = data.height;
const svg = d3.select("body").append('svg')
.attr('width', 500)
.attr('height', 500);
const data = [
{cx: 10, cy: 10},
{cx: 490, cy: 50},
{cx: 50, cy: 490}
];
@Aleksey-Danchin
Aleksey-Danchin / dollar.tsv
Last active March 21, 2017 12:24
D3 experience: moveable chart of dollar course. https://codepen.io/al/full/evMmEX/
date course
2010.01.11 30,1851
2010.01.12 29,4283
2010.01.13 29,3774
2010.01.14 29,6409
2010.01.15 29,4299
2010.01.16 29,5603
2010.01.19 29,5963
2010.01.20 29,5184
2010.01.21 29,6941
@Aleksey-Danchin
Aleksey-Danchin / example9.js
Created October 21, 2016 10:34
Пример реализации цепного стиля программирования.
class Point {
constructor () {
const point = this;
point.x = 0;
point.y = 0;
point.color = 'red';
}
moveTo (x, y) {
@Aleksey-Danchin
Aleksey-Danchin / example8.js
Last active October 21, 2016 10:36
Улучшенные свойста объектов и деструктурирующее присваивание.
function showPerson1 (name, family, age) {
console.log(name, family, age);
}
function showPerson2 ({name, family, age}) {
showPerson1(name, family, age);
}
const name = 'Aleksey', family = 'Danchin', age = 24;
@Aleksey-Danchin
Aleksey-Danchin / example7.js
Created October 18, 2016 08:43
Проверка массивов на эквивалентность содержимого.
Array.isEqual = function (...arrays) {
if (arrays.length == 0) return false;
if (arrays.length == 1) return true;
const ctrl = arrays[0]
, length = ctrl.length;
for (let i = 1; i < arrays.length; i++) {
const array = arrays[i];
@Aleksey-Danchin
Aleksey-Danchin / example6.js
Last active October 17, 2016 08:56
Делаем коллекцию Map JSON-представляемой.
Object.defineProperty(Map.prototype, 'toJSON', {
enumerable: false
, configurable: true
, get: () => function () {
const obj = {};
for (const key of this.keys())
obj[key] = this.get(key);
return obj;
@Aleksey-Danchin
Aleksey-Danchin / example5.js
Created October 14, 2016 19:37
Синтаксис статического геттера.
class User {
constructor (isAdmin = false) {
const user = this;
user.isAdmin = isAdmin;
User.all.push(user);
}
static get admins () {
return User.all.filter(user => user.isAdmin);
}
@Aleksey-Danchin
Aleksey-Danchin / example4.js
Created October 13, 2016 07:22
Быстро получить все простые числа от 2 до n.
const n = 1000;
const buff = new Int16Array(n + 1);
for (let i = 3; i < buff.length; i += 2) {
buff[i - 1] = 1;
if (buff[i] == 0)
for (let j = i + i; j < buff.length; j += i)
buff[j] = 1;
}
@Aleksey-Danchin
Aleksey-Danchin / example3.js
Created October 12, 2016 07:36
Доступ к первому элементу массива через .first / Доступ к последнему элементу массива через .last
Object.defineProperty(Array.prototype, 'first', {
enumerable: false
, configurable: true
, get: function () { return this[0]; }
, set: function (value) {
return this.length ? this[0] = value : this.push(value) && value;
}
});
Object.defineProperty(Array.prototype, 'last', {