Skip to content

Instantly share code, notes, and snippets.

View kouks's full-sized avatar
🎯
Focusing

Pavel Koch kouks

🎯
Focusing
View GitHub Profile
@kouks
kouks / aws-signature.ts
Created March 19, 2019 21:51
AWS Product API signature script
import { Request } from 'awi'
import { HmacSHA256, enc } from 'crypto-js'
export class SignatureFactory {
/**
* Compute the Amazon request signature.
*
* @param request The request sign
*/
@kouks
kouks / url-safe-base64-encoding.js
Last active August 27, 2024 11:21
Encodes and/or decodes data as base64 url safe string.
// Encodes a js object as a url-safe base64 string.
encodeURIComponent(btoa(JSON.stringify({ ... })))
// Takes the previously generated string and returns a parsed js object.
JSON.parse(atob(decodeURIComponent('...')))
@kouks
kouks / resolve-promise-array-ordered.js
Last active February 24, 2019 17:56
Resolve an array of JS promises one after another - one liner
// Example data.
const data = [1, 2, 3]
const fn = async a => a * a
// If we care about errors.
data.reduce((carry, item) => carry.then(() => fn(item)), Promise.resolve())
.catch(console.error)
.then(() => { ... })
// If we don't care.
@kouks
kouks / redirect.js
Last active February 11, 2019 16:03
Redirection based on popular browsers
const redirect = (urls) => {
const browser = window.chrome ? 'chrome' : window.safari ? 'safari' : 'firefox'
const platform = /(chrome|safari|firefox)/i.test(navigator.userAgent) ? 'supported' : 'unsupported'
const os = /(android|ios|iphone)/i.test(navigator.userAgent) ? 'mobile' : 'desktop'
const parametrize = (url) => {
url = new URL(url)
Array.from(new URL(location.href).searchParams.entries()).forEach(([k, v]) => url.searchParams.set(k, v))
return url.toString()
}
@kouks
kouks / registerControllerRouter.js
Last active July 25, 2018 13:56
Controller & middleware plugin for VueRouter
import router from './router'
/**
* Intercept each route with a controller call if there is an existing one.
*/
router.beforeEach((to, from, next) => {
const controller = to.meta.controller
const middleware = to.meta.middleware || []
const request = { to, from }
@kouks
kouks / subdirectories.sh
Last active December 27, 2018 11:06
Perform commands in all one-level subdirectories.
#!/bin/bash
libs=(support ioc aop events exceptions core storage messaging tabs cache compatibility management)
cd framework/lib
git checkout develop
for lib in ${libs[@]}; do
cd ${lib}
set nocompatible " Latest Vim setting
so ~/.vim/plugins.vim
syntax enable
set backspace=indent,eol,start " Make backspace behave like in any other editor
let mapleader=',' " Set the default leader
set foldcolumn=2
set autowriteall " Write when switching buffers
"---------- Visuals ----------"
set background=dark " Set the background to be dark