Skip to content

Instantly share code, notes, and snippets.

View mzahor's full-sized avatar

Marian Zagoruiko mzahor

  • LeanyLabs
  • Lviv, Ukraine
  • X @mzahor
View GitHub Profile
import { useCallback, useEffect } from 'react';
export function useClickOutside(isOpen, ref, onClose) {
const handleOutsideClick = useCallback(
(e) => {
if (!isOpen) return;
const path = e.path || (e.composedPath && e.composedPath());
if (!path.includes(ref.current)) {
onClose();
}
@mzahor
mzahor / sqs-producer-send-json.ts
Created March 26, 2020 13:04
sendJSON method of SqsProducer
async sendJSON(message: object, options: SqsMessageOptions = {}): Promise<any> {
const messageBody = JSON.stringify(message);
const msgSize = Buffer.byteLength(messageBody, 'utf-8');
if ((msgSize > MAX_SQS_MESSAGE_SIZE && this.largePayloadThoughS3) || this.allPayloadThoughS3) {
const payloadId = uuid();
const payloadKey = `${payloadId}.json`;
const s3Response = await this.s3
.upload({
Bucket: this.s3Bucket,
@mzahor
mzahor / wait-for-url.js
Created March 25, 2020 11:58
Wait for url to become available. No dependencies.
#!/usr/bin/env node
const http = require('http');
let timeoutFired = false;
const urlReady = new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
timeoutFired = true;
reject(new Error('Timeout'));
}, 60000);
@mzahor
mzahor / StateProvider.js
Created March 14, 2019 22:59
Super-simple react state management
import React, { createContext, useReducer, useContext } from "react";
export const StateContext = createContext();
export const useStateContext = () => useContext(StateContext);
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
@mzahor
mzahor / webpack.config.js
Last active June 11, 2018 19:47
SourceMapDevToolPlugin demo
const webpack = require('webpack');
const path = require('path');
const isProd = NODE_ENV === 'production';
const ifProd = x => isProd && x;
const removeEmpty = arr => arr.filter(Boolean);
module.exports = {
entry: './index.js',
output: {
@mzahor
mzahor / brute.py
Created December 10, 2017 13:28
RFID bruteforce
import itertools
import re
alphabet = '0123456789abcdef'
def crack():
for combination in itertools.combinations_with_replacement(alphabet, 12):
combostring = ''.join((combination))
bites = [int(bite, 16) for bite in re.findall('..', combostring)]
@mzahor
mzahor / click-outside.directive.js
Created May 14, 2016 10:16
Click outside angular directive
let hookInstalled = false;
function ClickOutsideDirective($window, $rootScope, $parse) {
'ngInject';
return {
restrict: 'A',
link: function(scope, elem, attr) {
if (!hookInstalled) {
$window.document.addEventListener('click', function(event) {
table { display: table }
tr { display: table-row }
thead { display: table-header-group }
tbody { display: table-row-group }
tfoot { display: table-footer-group }
col { display: table-column }
colgroup { display: table-column-group }
td, th { display: table-cell }
caption { display: table-caption }
@mzahor
mzahor / rx_stream_control.html
Last active March 1, 2016 13:18
Controlling one stream with another stream in RxJS
<!DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
</head>
<body>
<label><input id="chb" type="checkbox">Run</label>
<div id="output"></div>
</body>