Skip to content

Instantly share code, notes, and snippets.

View sematgt's full-sized avatar
🏘️
wip

Semyon Bliznyuk sematgt

🏘️
wip
View GitHub Profile
@sematgt
sematgt / ComboBox.js
Created March 30, 2020 10:12
Test syntax highlighting
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
export default function ComboBox(props) {
return (
<Autocomplete
autoHighlight={props.autoHighlight}
onChange={props.handleChange}
clearText={props.clearText}
@sematgt
sematgt / iterator.js
Created May 8, 2020 15:45
JS iterators tutorial source code simonbliznyuk.com
class Group {
constructor() {
this.elements = [];
}
add(element) {
if (!this.has(element)) {
this.elements.push(element);
}
}
@sematgt
sematgt / calc.js
Created July 18, 2020 08:43
Editable calculator on plain JS
function Calculator() {
this.operations = {
'+': (a, b) => +a + +b,
'-': (a, b) => a - b,
};
this.validateInputNumbers = function(arr) {
return !(isNaN(arr[0]) || isNaN(arr[2]))
};
@sematgt
sematgt / bench.js
Created July 22, 2020 13:40
benchmark function js
function loopSumTo(n) {
let sum = 0;
for (let i = n; i >= 1; i--) {
sum += i;
}
return sum;
}
@sematgt
sematgt / spy.js
Created July 24, 2020 06:51
js spy decorator
function spy(func) {
wrapper.calls = [];
function wrapper() {
wrapper.calls.push([...arguments]);
func(...arguments);
}
return wrapper;
}
@sematgt
sematgt / throttle.js
Last active July 24, 2020 07:45
throttling decorator js
function throttle(f, ms) {
return function updater() {
if (updater.ready === false) {
updater.args = arguments;
return;
}
updater.ready = false;
f.apply(this, arguments);
@sematgt
sematgt / clock.js
Created July 27, 2020 06:39
js clock
class Clock {
constructor({ template }) {
this.template = template;
}
stop() {
clearInterval(this.timer);
};
@sematgt
sematgt / arrayCreate.js
Created January 9, 2021 09:09
array creation via mapping function
const obj = {
length: 4,
0: 'one',
1: 'two',
2: 'three',
};
console.log(Array.from(obj, (v, i) => {
if (v === undefined) {
return 'no element';
@sematgt
sematgt / curry.js
Created January 9, 2021 09:09
curry function
function sum(a, b, c) {
return a + b + c;
}
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
// если в колбеке setTimeout используем this, то нужно либо привязка bind, либо стрелочная функция,
// так как setTimeout вызывает колбек в другом контексте и this внутри функции будет глобальным объектом
// wrong
var obj = {
count : 10,
doSomethingLater : function (){
setTimeout(function(){ // the function executes on the window scope
this.count++;