Skip to content

Instantly share code, notes, and snippets.

View bluzky's full-sized avatar
🎯
Focusing

Dung Nguyen bluzky

🎯
Focusing
View GitHub Profile
@bluzky
bluzky / string_convert.cpp
Created November 12, 2014 08:59
Convert UTF-8 to UTF-16 and vice versa
wstring utf8_to_utf16(string utf8)
{
std::vector<unsigned long> unicode;
size_t i = 0;
while (i < utf8.size())
{
unsigned long uni;
size_t todo;
bool error = false;
unsigned char ch = utf8[i++];
@bluzky
bluzky / decorator don gian.py
Last active August 29, 2015 14:23
Python decorator introduction
__author__ = 'bluzky'
def name_decorator(f):
def wrapper(ten):
chuoi_moi = "Ten tui la %s" % ten
return f(chuoi_moi)
return wrapper
# su dung decorator
@name_decorator
@bluzky
bluzky / ribbon.css
Created January 10, 2016 09:01
CSS ribbon
.ribbon-container{
width: 300px;
height: 300px;
background: #FFF;
}
.ribbon-container .ribbon{
position:relative;
color:#fff;
@bluzky
bluzky / slug.js
Last active April 8, 2024 05:01
Remove vietnamese accent javascript / Bỏ dấu tiếng Việt
function stringToSlug(str) {
// remove accents
var from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñçýỳỹỵỷ",
to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouuncyyyyy";
for (var i=0, l=from.length ; i < l ; i++) {
str = str.replace(RegExp(from[i], "gi"), to[i]);
}
str = str.toLowerCase()
.trim()
defmodule Infra.Paginator do
import Ecto.Query
alias Infra.Repo
defstruct [:entries, :page, :size, :total]
def new(query, params) do
page_number = params |> Map.get("page", 1) |> to_int
page_size = params |> Map.get("size", 10) |> to_int
%Infra.Paginator{
@bluzky
bluzky / bucket.ex
Last active May 18, 2018 01:58
Simple ETS cache for Phoenix
defmodule PhoenixCache.Bucket do
use GenServer
alias :ets, as: Ets
@expired_after 6 * 60
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
defmodule PhoenixCache.Plug.Cache doimport Plug.Conn
# 6 minute
@default_ttl 6 * 60
def init(ttl \\ nil), do: ttl
def call(conn, ttl \\ nil) do
ttl = ttl || @default_ttl
# Chỉ cache với GET requestif conn.method == "GET" do# tạo key từ request path và query param, thông thường# thì cùng path và cùng param thì kết quả là giống nhau
key = "#{conn.request_path}-#{conn.query_string}"
case PhoenixCache.Bucket.get(key) do
{:ok, body} ->
@bluzky
bluzky / Box.js
Last active November 9, 2018 06:33
Bootstrap alert/confirm/promt
window.Box = (function(){
var template = `
<div tabindex="-1" role="dialog" class="modal modal-{type} {class-modal} fade">
<div class="modal-dialog modal-{size}">
<div class="modal-content">
<div class="modal-header text-inverse {class-header}">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h5 class="modal-title" >{title}</h5>
</div>
@bluzky
bluzky / json_view.ex
Last active December 8, 2020 09:56
Phoenix json render helper
defmodule MyApp.JsonView do
defmacro __using__(_) do
quote do
def render_json(struct, fields, custom_fields, relationships) do
MyApp.JsonView.render_json(struct, __MODULE__,
fields: fields,
custom_fields: custom_fields,
relationships: relationships
)
end
@bluzky
bluzky / crypto.ex
Created August 20, 2021 03:32
Elixir encrypt/decrypt using Erlang :crypto module
defmodule Magik.Crypto do
@moduledoc """
Provide some basic encrypt/decrypt function
"""
@aad "AES256GCM"
@block_size 16
@doc """
`generate_secret`