Skip to content

Instantly share code, notes, and snippets.

View namcoder's full-sized avatar
🌴
On vacation "><img src=x onerror=alert(99)>

Nam Le namcoder

🌴
On vacation "><img src=x onerror=alert(99)>
View GitHub Profile
@namcoder
namcoder / number-pad-zero.js
Created June 24, 2018 14:49 — forked from endel/number-pad-zero.js
Simplest way for leading zero padding in JavaScript
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
(1).pad(3) // => "001"
(10).pad(3) // => "010"
(100).pad(3) // => "100"
@namcoder
namcoder / zero-before.js
Created June 24, 2018 14:50
Add Zero before number less than 10
('0' + 11).slice(-2) // '11'
('0' + 4).slice(-2) // '04'
For ease of access, you could of course extract it to a function, or even extend Number with it:
Number.prototype.pad = function(n) {
return new Array(n).join('0').slice((n || 2) * -1) + this;
}
Which will allow you to write:
c += deg.pad() + '° '; // "04° "
@namcoder
namcoder / median.js
Created August 30, 2018 04:19 — forked from caseyjustus/median.js
calculate the median of an array with javascript
function median(values) {
values.sort( function(a,b) {return a - b;} );
var half = Math.floor(values.length/2);
if(values.length % 2)
return values[half];
else
return (values[half-1] + values[half]) / 2.0;
@namcoder
namcoder / test-php-basic-auth.php
Created January 11, 2019 03:52 — forked from rchrd2/test-php-basic-auth.php
PHP basic auth example
<?php
function require_auth() {
$AUTH_USER = 'admin';
$AUTH_PASS = 'admin';
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
@namcoder
namcoder / react-waiting-user-end-typing.js
Created July 18, 2019 16:54
React trick: Waiting for user stop typing then beign search. Does not take search for every key, save performance
searchPost = value =>{
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
if(value.length < 1) return
return console.log('begin search: ', value)
}, 500)
}
@namcoder
namcoder / unaccent.rules
Created July 19, 2019 05:53
unaccent.rules for Vietnamese
© (C)
« <<
­ -
® (R)
» >>
¼ 1/4
½ 1/2
¾ 3/4
À A
Á A
@namcoder
namcoder / postgresql_id.sql
Created August 6, 2019 14:15 — forked from yohangdev/postgresql_id.sql
PostgreSQL Better ID & UUID Generator
create schema shard_1;
create sequence shard_1.global_id_sequence;
CREATE OR REPLACE FUNCTION shard_1.id_generator(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
-- the id of this DB shard, must be set for each
-- schema shard you have - you could pass this as a parameter too
call plug#begin('~/.vim/plugged')
Plug 'junegunn/vim-easy-align'
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'w0rp/ale'
Plug 'itchyny/lightline.vim'
Plug 'tpope/vim-surround'
Plug 'morhetz/gruvbox'
Plug 'pangloss/vim-javascript'
Plug 'scrooloose/nerdcommenter'
Plug 'chemzqm/vim-jsx-improve'
@namcoder
namcoder / Direct Link of YouTube videos.md
Last active September 4, 2020 04:00 — forked from egyjs/Direct Link of YouTube videos.md
PHP API To get Direct Link of YouTube videos
We couldn’t find that file to show.
@namcoder
namcoder / cloudflare-worker-youtube-dl.js
Created September 7, 2020 08:36 — forked from hizkifw/cloudflare-worker-youtube-dl.js
Download YouTube videos with Cloudflare Worker
/**
* cloudflare-worker-youtube-dl.js
* Get direct links to YouTube videos using Cloudflare Workers.
*
* Usage:
* GET /?v=dQw4w9WgXcQ
* -> Returns a JSON list of supported formats
*
* GET /?v=dQw4w9WgXcQ&f=251
* -> Returns a stream of the specified format ID