Skip to content

Instantly share code, notes, and snippets.

View roalcantara's full-sized avatar
🤖
Don't Panic!

Rogério Rodrigues de Alcântara roalcantara

🤖
Don't Panic!
View GitHub Profile
@roalcantara
roalcantara / HasTail.ts
Last active September 6, 2020 09:09
TypeScript 3.9 | Advanced Types: Returns true when there is more than one parameter
type HasTail<T extends any[]> =
T extends [] | [any]
? false
: true
type Type1 = HasTail<[age: number, single: boolean]> // true
type Type2 = HasTail<[single: boolean]>> // false
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
// https://github.com/millsp/medium/blob/master/types-curry-ramda/src/index.ts
@roalcantara
roalcantara / Tail.ts
Last active September 6, 2020 09:05
TypeScript 3.9 | Advanced Types: Removes the first entry of a tuple
type Tail<T extends any[]> = ((...t: T) => any) extends (
_: any,
...tail: infer TT
) => any
? TT
: []
type Type1 = Tail<[age: number, single: boolean]> // [single: boolean]
type Type2 = Tail<Type1> // []
@roalcantara
roalcantara / Head.ts
Last active September 6, 2020 09:06
TypeScript 3.9 | Advanced Types: Extracts the first param type of a given tuple
type Head<T extends any[]> =
T extends [any, ...any[]]
? T[0]
: never
type Type1 = Head<[name: string, age: number, single: boolean]> // string
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab
// https://github.com/millsp/medium/blob/master/types-curry-ramda/src/index.ts
@roalcantara
roalcantara / Params.ts
Last active September 6, 2020 09:06
TypeScript 3.9 | Advanced Types: Extracts a tuple of param types of a given function
type Params<F extends (...args: any[]) => any> = F extends (
...args: infer A
) => any
? A
: never
const fn00 = (name: string, age: number, single: boolean) => true
type Type1 = Params<typeof fn00> // [name: string, age: number, single: boolean]
@roalcantara
roalcantara / append.ts
Last active September 4, 2020 06:55
Typescript 4.0: Short-Circuiting Assignment Operators
// Then
const append = (values: string[], value: string) => {
(values ?? (values = [])).push(value)
return values
}
// Now
const append = (values: string[], value: string) => {
(values ??= []).push(value)
@roalcantara
roalcantara / docker_setup_build_publish_clean.md
Last active November 23, 2021 06:20
Docker`s Zsh Cheat Sheet-ish

Build, Publish and Clean-Up and Prune Images

1. Setup

  • ~❯ brew update && brew upgrade && brew cask upgrade && brew cleanup && brew doctor to get brew ready
  • ~❯ brew cask install docker-edge
  • ~❯ echo "export DOCKER_CONFIG=$XDG_CONFIG_HOME/docker" >> $ZDOTDIR/.zshenv

2. Dockerfile

@roalcantara
roalcantara / app.module.ts
Last active January 21, 2020 05:32
[Angular 8] Module.forRoot() configuration
import { FooModule } from "@foo/modules"
import { config } from "@foo/config"
@NgModule( {
imports: [
BrowserModule,
FooModule.forRoot(config)
]
bootstrap: [ AppComponent ],
})
@roalcantara
roalcantara / curried_func.ts
Last active September 4, 2020 06:58
Curried Function Sample
interface Todo {
id: number,
text: string,
done: boolean
}
const todo: Todo = {
id: 1,
text: 'learn TS',
done: false
@roalcantara
roalcantara / elasticsearch_6_8_upsert_into_array.painless.es
Created December 6, 2019 10:16
Elasticsearch [6.8] > Painless > Upsert into arrays
POST events/user/:id/_update
{
"script": {
"lang": "painless",
"source": "if (ctx._source.on_screen_view == null) { ctx._source.on_screen_view = [] }\nfor (int i = 0; i < params.events.length; i++) {\nctx._source.on_screen_view.add(params.events[i])\n}\n",
"params": {
"events": [
"2019-07-10T21:57:46.173Z",
"2019-07-10T20:49:34.429Z",
"2019-07-10T20:50:08.135Z"