Skip to content

Instantly share code, notes, and snippets.

View googlicius's full-sized avatar
🎯
Focusing

Dang Nguyen googlicius

🎯
Focusing
  • Ho Chi Minh City
View GitHub Profile
@googlicius
googlicius / main.dart
Last active May 5, 2019 18:43
Dart factory
class Logger {
final String name;
bool mute = false;
static final Map<String, Logger> _cache = <String, Logger>{};
factory Logger(String name) {
if(_cache.containsKey(name)) {
return _cache[name];
}
@googlicius
googlicius / timeIn.js
Last active June 1, 2018 06:50
Declarative ways to get time in months|days|hours|seconds|miliseconds later (or of) months|days|minutes|seconds
/**
* Get time in months|days|hours|seconds|miliseconds later (or of) months|days|hours|seconds|miliseconds
*
* Examples:
* timeIn('second').later(30).minutes();
* timeIn('second').of(2).days();
*
* @param {string} name
* @return {object} { later, of }
*/
// Load CSS file async
export const loadAsyncCss = (href: string | Array<string>, id, cb: () => void = null) => {
function createElement(d, s, href) {
var link = d.createElement(s);
link.href = href;
link.rel = 'stylesheet';
return link;
}
(function (d, id, href, r) {
var fjs = d.getElementsByTagName('link')[0];
/**
* Load JS file async
*
* @param url url of JS file
* @param cb Callback after file has loaded.
* @param wait_id id of element that being waited to load.
* @returns Promise<any>
*/
export const loadAsyncJS = (url: string | Array<string>, cb?: (error?: any) => void, wait_src: string = null): Promise<any> => {
const createElement = (d, s, url) => {
@googlicius
googlicius / addOnloadHandler.js
Last active November 23, 2017 07:43
Method to add one or more function handler on an element's onload event...
/**
* Method to add one or more function handler on an element's onload event
* This is because onload cannot carry out many separate functions.
*
* @param {any} element DOM element
* @param {any} handler function
*/
const addOnloadHandler = (element, handler) => {
if (typeof element.onload === 'function') {
const pre_func = element.onload;
@googlicius
googlicius / bodauTiengViet.js
Created November 23, 2017 07:37
Function to remove Vietnamese's diacritic marks.
const bodauTiengViet = str => {
str = str.toLowerCase();
str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, 'a');
str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, 'e');
str = str.replace(/ì|í|ị|ỉ|ĩ/g, 'i');
str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, 'o');
str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, 'u');
str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, 'y');
str = str.replace(/đ/g, 'd');
// 2 lines below to replace spaces to '-', uncomment if you need.
@googlicius
googlicius / similarity.js
Last active November 26, 2018 01:07
Function to compare 2 string by percentage.
/**
* Function to compare 2 string by percentage.
*
* @see https://stackoverflow.com/a/36566052/1427748
* @param {string} s1
* @param {string} s2
*/
const similarity = (s1, s2) => {
function editDistance(s1, s2) {
s1 = s1.toLowerCase();