Skip to content

Instantly share code, notes, and snippets.

View ducdhm's full-sized avatar
:electron:
Keep Moving Forward

Duc Doan (Bobby) ducdhm

:electron:
Keep Moving Forward
View GitHub Profile
var redirectTo = function (url) {
var anchor = document.createElement('a');
anchor.href = url;
document.body.appendChild(anchor);
anchor.click();
};
redirectTo('http://coccoc.com');
@ducdhm
ducdhm / addNumber.js
Last active September 27, 2016 03:19
Add multiple floats as well as numbers
/**
* Bob Math utilities
* @author: ducdhm
* @created: Tue, Feb 11th, 2014 (GTM+7)
*/
(function(Math) {
/**
* Find the number of decimal places
* @method findDec
* @param {Float|Number} dec
@ducdhm
ducdhm / example.js
Last active August 29, 2015 13:56
Get value when press key down with keydown2
$('#query').keydown2(function () {
console.log($(this).val());
});
@ducdhm
ducdhm / generatePasswordRegex.js
Last active August 29, 2015 13:59
Generate password regexp for validating purpose
/**
* Generate password regex
* @method generatePasswordRegex
* @param {Number} min Minimum length of password
* @param {Number} max Maximum length of password
* @param {String} specialCharacter List of special characters
* @param {Number} specialLength Number of required special character in password
* @param {Number} uppercaseLength Number of required uppercase character in password
* @param {Number} numberLength Number of digit character in password *
*/
@ducdhm
ducdhm / Get next birthday in MVEL
Last active August 31, 2017 05:30
Creating example data in Kademi
birthYear = formatter.getYear(profile.birthDate); currentYear = formatter.getYear(formatter.now); age = currentYear - birthYear; nextBirthDay = formatter.addYears(profile.birthDate, age); return nextBirthDay;
@ducdhm
ducdhm / downloadFile.js
Created October 4, 2019 05:45
Download file from url
function downloadFile(url, filename) {
fetch(url)
.then(resp => resp.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
@ducdhm
ducdhm / removeTone.js
Created October 5, 2019 01:56
Remove Vietnamese tones
function removeTone(str, isCaseSensitive = false) {
let formatted = isCaseSensitive ? str : str.toLowerCase();
formatted = formatted.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, 'a');
formatted = formatted.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, 'e');
formatted = formatted.replace(/ì|í|ị|ỉ|ĩ/g, 'i');
formatted = formatted.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, 'o');
formatted = formatted.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, 'u');
formatted = formatted.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, 'y');
formatted = formatted.replace(/đ/g, 'd');
const randomString = (length) => {
const letters = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '1234567890';
const charset = `${letters}${letters.toUpperCase()}${numbers}`;
const randomCharacter = (character) => character[Math.floor(Math.random() * character.length)];
let result = '';
for (let i = 0; i < length; i++) {
result += randomCharacter(charset);
@ducdhm
ducdhm / index.html
Created September 27, 2021 09:24
console.log to HTML
<style>
#log {
position: fixed;
z-index: 9999999999999999999999;
bottom: 5px;
right: 5px;
left: 5px;
padding: 10px;
box-shadow: 0 0 3px rgba(0, 0,0, .2);
background: #333;