Skip to content

Instantly share code, notes, and snippets.

View WebTarantul's full-sized avatar

Taras Kapusta WebTarantul

View GitHub Profile
@WebTarantul
WebTarantul / middlewere.js
Last active August 12, 2019 12:40
Sample middlewere
import { createStore, applyMiddleware } from 'redux'
import reducer from './reducers'
const middlewere = (store) => (dispatch) => (action) => {
if (typeof action === 'string') {
console.log(store.getState());
return dispatch({
type: action
})
@WebTarantul
WebTarantul / enhancer.js
Created August 12, 2019 12:22
Sample to enhancer function
const enhancer = (createStore) => (...args) => {
const store = createStore(...args);
const orDispatch = store.dispatch;
store.dispatch = (action) => {
if (typeof action === 'string') {
return orDispatch({
type: action
})
} else {
return orDispatch(action)
@WebTarantul
WebTarantul / React-Redux__update-cart-functions.js
Last active August 12, 2019 08:45
React-Redux__update-cart-functions(updateCartItems, updateCartItem, updateOrder)
const updateCartItems = (cartItems, item, idx) => {
if (item.count === 0) {
return [
...cartItems.slice(0,idx),
...cartItems.slice(idx + 1)
]
}
if (idx > -1) {
return [
const getResource = async (url) => {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Could not fetch ${url}`, `received ${res.status}`)
}
const body = await res.json();
return body;
}
@WebTarantul
WebTarantul / Clases.js
Created July 11, 2019 09:10
Creating class using old syntax and new syntax
// function Animal (name, voice) {
// this.name = name;
// this.voice = voice;
// }
// Animal.prototype.say = function () {
// console.log(`${this.name} says: ${this.voice}`);
// }
// const dog = new Animal('Dog', 'Woof');
// const cat = new Animal('Cat', 'Myau');
@WebTarantul
WebTarantul / trycatch.js
Last active April 17, 2019 11:49
try...catch exsmples
try {
// потенциально опасный код
// потенциально опасный код
} catch (error) {
// обработка
// обработка
}
function div(a, b) {
try {
@WebTarantul
WebTarantul / console-methods.js
Last active April 17, 2019 11:50
console methods
console.log('Простое сообщение с форматированием %s там там %s', 'первая %s','вторая %s');
console.error();
console.info();
console.warn();
console.trace(); // путь к вызову этой функции
// Groups
console.group('Имя группы')ж
@WebTarantul
WebTarantul / fetch.js
Created April 16, 2019 15:08
description of Fetch method
// https://codeguida.com/post/756
fetch('some-url', options);
fetch('some-url')
.then(handleResponse)
.then(data => console.log(data))
.then(error => console.log(error))
@WebTarantul
WebTarantul / ES6 шпаргалка.js
Last active April 4, 2019 19:12
ES6 шпаргалка.js
'use strict';
// TODO Destructurization ДЕСТРУКТУРИЗАЦИЯ
// STUB Syntax Синтаксис:
let {prop : varName = default, ...} = object
// Здесь двоеточие : задаёт отображение свойства prop в переменную varName, а равенство =default задаёт выражение, которое будет использовано, если значение отсутствует (не указано или undefined).
// Для массивов имеет значение порядок, поэтому нельзя использовать :, но значение по умолчанию – можно:
let [var1 = default, var2, ...rest] = array
// ANCHOR Array Destructurization
@WebTarantul
WebTarantul / DOM методи.js
Created April 1, 2019 19:42
DOM методи(шпаргалка)
location.href = "https://hexlet.io";
history
$0 // - якшо ввечти в конслоь, можна отримати доступ до вибраного елемента
console.dir //выводит узлы DOM в формате, удобном для интроспекции.
// Создаем текстовый узел
const textNode = document.createTextNode('life is life');
// ========ВСТАВКА=========
node.prepend(...nodes) // – вставляет nodes в начало node,
node.after(...nodes) // – вставляет nodes после узла node,
node.before(...nodes) // – вставляет nodes перед узлом node,