Skip to content

Instantly share code, notes, and snippets.

View lelinhtinh's full-sized avatar
🤤
Slimming failure

Thành Thân Thiện lelinhtinh

🤤
Slimming failure
  • Da nang, Vietnam
  • 14:25 (UTC +07:00)
View GitHub Profile
@lelinhtinh
lelinhtinh / JavaScript array difference
Last active August 29, 2015 14:05
Lấy ra mảng khác biệt từ 2 mảng, Trừ 2 mảng trong javascript
// http://stackoverflow.com/a/8211012
var array1 = ["test1", "test2", "test3", "test4"];
var array2 = ["test1", "test2", "test3", "test4", "test5", "test6"];
var _array = new Array();
_array = jQuery.grep(array2, function(item) {
return jQuery.inArray(item, array1) < 0;
});
@lelinhtinh
lelinhtinh / Random Hex Color Code Generator in JavaScript
Created August 8, 2014 11:29
Tạo mã màu Hex ngẫu nhiên trong javascript
// http://www.paulirish.com/2009/random-hex-color-code-snippets/
(function(m, s, c) {
return (c ? arguments.callee(m, s, c - 1) : '#') + s[m.floor(m.random() * s.length)]
})(Math, '0123456789ABCDEF', 5);
@lelinhtinh
lelinhtinh / Shuffle array
Created August 8, 2014 11:31
Đảo ngẫu nhiên đối tượng trong mảng
// http://devs.forumvi.com/t571-gist-shuffle-array-dao-ngau-nhien-doi-tuong-trong-mang
var arr = [1, 2, 3, 4, 5];
/**
* Shuffling (randomizing) the order of an array
* http://www.javascriptkit.com/javatutors/arraysort.shtml
*/
arr.sort(function () {
return .5 - Math.random();
});
@lelinhtinh
lelinhtinh / javascript-vietnam-datetime-format.js
Last active February 20, 2025 13:52
Định dạng ngày giờ Việt Nam bằng javascript
// New version, timezone support, modern browsers
(function (dt) {
return new Date(dt)
.toLocaleString('vi-VN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
@lelinhtinh
lelinhtinh / Rounding Decimals in JavaScript
Last active July 1, 2022 07:19
Làm tròn số thập phân với javascript
/* http://www.jacklmoore.com/notes/rounding-in-javascript/ */
function round(value, decimals) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
/* http://stackoverflow.com/a/11832950 */
Math.round(num * 100) / 100;
/* http://stackoverflow.com/a/12830454 */
parseFloat("123.456").toFixed(2);
@lelinhtinh
lelinhtinh / Regex match youtube id
Created September 8, 2014 10:03
Regex lấy id video youtube từ URL
// http://stackoverflow.com/a/8260383
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[7].length==11){
return match[7];
} else {
alert("Url incorrecta");
}
}
@lelinhtinh
lelinhtinh / String to Binary
Created September 9, 2014 13:57
Chuyển chuỗi sang mã nhị phân
// http://www.midnight-coding.com/2014/05/convert-string-to-binary-to-string.html
function stringToBinary(stringValue) {
return stringValue.replace(/.{1}/g, function (matchedString) {
var binString = matchedString.charCodeAt(0).toString(2);
return '00000000'.substring(0, 8 - binString.length) + binString;
});
}
@lelinhtinh
lelinhtinh / Binary to String
Created September 9, 2014 13:59
Chuyển mã nhị phân thành chuỗi
// http://www.midnight-coding.com/2014/05/convert-string-to-binary-to-string.html
function binaryToString(binValue) {
return binValue.replace(/[01]{8}/g, function (matchedString) {
return String.fromCharCode(parseInt(matchedString, 2));
});
}
@lelinhtinh
lelinhtinh / Random value from an array
Created September 13, 2014 20:11
Lấy một đối tượng ngẫu nhiên trong mảng
// http://stackoverflow.com/a/4550514
var rand = myArray[Math.floor(Math.random() * myArray.length)];
@lelinhtinh
lelinhtinh / Check element exists
Created September 15, 2014 06:22
Kiểm tra xem phần tử có tồn tại hay chưa
// Tiny jQuery Plugin
// by Chris Goodchild
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;