Skip to content

Instantly share code, notes, and snippets.

View byF's full-sized avatar
👽
This is the way

Zdenek Farana byF

👽
This is the way
View GitHub Profile
@byF
byF / global.d.ts
Last active April 8, 2024 16:14
more useful TS types
declare global {
type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n ? never : T
interface Array<T> {
filter(predicate: BooleanConstructor, thisArg?: any): NonFalsy<T>[]
includes<S, R extends `${Extract<S, string>}`>(
this: ReadonlyArray<R>,
searchElement: S,
fromIndex?: number,
): searchElement is R & S
@byF
byF / gen.js
Created October 10, 2023 08:30
limiting JS promise concurrency
async function* mapWithLimitedConcurrency(arr, mapFn, limit = 8) {
for (let i = 0; i < arr.length; i += limit) {
yield await Promise.all(arr.slice(i, i + limit).map(mapFn));
}
}
const items = [...]
const iterator = mapWithLimitedConcurrency(items, async x => ...);
@byF
byF / git_cheatsheet.md
Last active August 8, 2021 09:43
Git Cheatsheet
  • reword: git commit --amend or git commit --amend -m "New message"
  • edit last commit:
    1. git reset HEAD~
    2. make your changes
    3. git add your changes
    4. git commit -c ORIG_HEAD
  • git force-pull
  • git prune-local
  • git remove tags:
  • remote: git push --delete origin
@byF
byF / git-force-pull
Last active October 28, 2020 13:03
Put the git-force-pull script on your PATH and voi-la, you can use git force-pull command
#!/bin/sh
. "$(git --exec-path)/git-sh-setup"
USAGE="BRANCH"
_force_pull() {
if [ $# = 1 ]; then
git ls-remote -q --exit-code --heads origin $1 2>&1 1>/dev/null
if [ $? = 0 ]; then
git fetch origin $1
@byF
byF / digestfile.scala
Created October 8, 2020 12:13
Calculate md5 file checksum
Using.Manager { use =>
val md = MessageDigest.getInstance("MD5")
val fis = use(Files.newInputStream(filePath))
val dos = use(new DigestOutputStream(OutputStream.nullOutputStream(), md))
fis.transferTo(dos)
BigInt(1, md.digest()).formatted(s"%0${md.getDigestLength * 2}x")
}
@byF
byF / git-prune-local
Last active November 9, 2020 11:32
Put the git-prune-local script on your PATH: git prune-local
#!/bin/sh
. "$(git --exec-path)/git-sh-setup"
USAGE=""
_prune_local() {
if [ $# = 0 ]; then
# git 2.21+
# -D is aggressive, beware
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
@byF
byF / RichTry.scala
Created October 1, 2020 22:37
Oh Scala, I wish Try's getOrElse could access the throwable in the "else" block by default
implicit class RichTry[T](val poorTry: Try[T]) extends AnyVal {
def getOrElseT[U >: T](default: Throwable => U): U = poorTry.fold(default, x => x)
}
object LookBehindIterator {
trait LookBehindIterator[+A] extends Iterator[A] {
def last: A
def lastOption: Option[A] = Option(last)
}
implicit class ImplicitLookBehindIterator[T](val it: Iterator[T]) extends AnyVal {
def lookBehind: LookBehindIterator[T] =
new scala.collection.AbstractIterator[T] with LookBehindIterator[T] {
@byF
byF / wait-for-pg.sh
Created June 1, 2020 10:21
wait-for-pg script which can be used as Docker entrypoint
#!/usr/bin/env bash
set -e
if [ -z ${DATABASE_HOST+x} ]
then
echo "$0: Provide DATABASE_HOST environment variable" >&2
exit 1
fi
attempt=1
@byF
byF / lambda.js
Created June 5, 2019 13:05
deployer wait for code
return new Promise(
(resolve, reject) => {
const waitForParams = {
tasks: [taskArn],
cluster: ecs_cluster,
}
//provisioning takes time as well
ecs.waitFor('tasksRunning', waitForParams, (err, data) => {
if (err) {
ecs.describeTasks(waitForParams, (errDt, data) => {