Skip to content

Instantly share code, notes, and snippets.

View hoangbits's full-sized avatar
🏝️
Working from earth

Le Gia Hoang hoangbits

🏝️
Working from earth
View GitHub Profile
@hoangbits
hoangbits / git-pull-all
Created June 5, 2021 10:36 — forked from grimzy/git-pull-all
Git pull all remote branches
#!/usr/bin/env bash
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
@hoangbits
hoangbits / package.json
Created June 6, 2021 07:14
liveview_package.json
{
"repository": {},
"description": " ",
"license": "MIT",
"scripts": {
"deploy": "webpack --mode production",
"watch": "webpack --mode development --watch"
},
"dependencies": {
"@tailwindcss/forms": "^0.2.1",
from sqlalchemy import (String,
Integer,
engine_from_config,
Column)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base_1 = declarative_base()
Base_2 = declarative_base()
@hoangbits
hoangbits / init_db.sh
Last active July 5, 2021 14:29
wait for mysql and create schemas
#!/bin/bash
# Initialize MySQL database.
# ADD this file into the container via Dockerfile.
# Assuming you specify a VOLUME ["/var/lib/mysql"] or `-v /var/lib/mysql` on the `docker run` command…
# Once built, do e.g. `docker run your_image /path/to/docker-mysql-initialize.sh`
# Again, make sure MySQL is persisting data outside the container for this to have any effect.
set -e
set -x
defmodule ModuleToBeUsed do
defmacro __using__(_) do
IO.puts __MODULE__ # COMPILATION STAGE
quote do # needed to prevent execution on compilation stage
IO.inspect(__MODULE__, label: "__MODULE__")
IO.inspect(unquote(__MODULE__), label: "unquote(__MODULE__)")
import unquote(__MODULE__)
def test, do: IO.puts "I am test" # EXECUTION STAGE
end
end
@hoangbits
hoangbits / catch-promise
Last active September 13, 2021 09:24
handle UnhandledPromiseRejection
/**
Ref: https://jonasjancarik.medium.com/handling-those-unhandled-promise-rejections-when-using-javascript-async-await-and-ifee-5bac52a0b29f
Placement of catch BEFORE and AFTER then: https://stackoverflow.com/questions/42013104/placement-of-catch-before-and-after-then
*/
// 1. Work
(async()=>{
return Promise.reject("a").catch((_error) =>{})
@hoangbits
hoangbits / snippets.json
Last active September 20, 2021 11:11
Liveview snippets for VS-code
//
// To install these snippets, select "User Snippets" under "File >
// Preferences" ("Code > Preferences" on macOS), and then select
// elixir.json. Paste the snippets below into that file. If you already
// have snippets in that file, you'll need to put all the snippets in one
// top-level JavaScript object, so remove the outer braces in the code
// below.
//
// JSON doesn't allow comments, so you must remove these comment lines! :-)
//
@hoangbits
hoangbits / model.ex
Created April 12, 2022 08:51
implicit encode Ecto schema with Jason
# inside an ecto Schema, adding this:
defimpl Jason.Encoder, for: [__MODULE__] do
def encode(struct, opts) do
Enum.reduce(Map.from_struct(struct), %{}, fn
({k, %Ecto.Association.NotLoaded{}}, acc) -> acc
({k, v}, acc) -> Map.reject(acc, fn {k, v} -> k === :__meta__ end) |> Map.put(k, v)
end)
|> Jason.Encode.map(opts)
end
dfdfdfd
irb(main):015:0> ["1",2,3,4,5].select {|val| val.is_a? Numeric }
=> [2, 3, 4, 5]
irb(main):016:0> ["X","-",3,4,5].select {|val| val.is_a? Numeric or val == "X"}
=> ["X", 3, 4, 5]