Skip to content

Instantly share code, notes, and snippets.

View NguyenTungs's full-sized avatar

Nguyen Tung NguyenTungs

View GitHub Profile
// Ví dụ về callback truyền thống, nỗi kinh hoàng của developer.
// link ref: https://anonystick.com/blog-developer/series-callback-javascript-phan-3-async-await-la-gi-va-su-khac-nhau-giua-promise-trong-javascript-vRwGny6a.jsx
const verifyUser = function(username, password, callback){
dataBase.verifyUser(username, password, (error, userInfo) => {
if (error) {
callback(error)
}else{
dataBase.getRoles(username, (error, roles) => {
if (error){
callback(error)
// Date Object CheatSheet
// The Date object is used to work with dates and times.
// More: http://www.w3schools.com/jsref/jsref_obj_date.asp
// 1. Instantiating a Date.
var date = new Date();
var date = new Date(milliseconds);
@NguyenTungs
NguyenTungs / regexCheatsheet.js
Created March 7, 2019 10:22 — forked from sarthology/regexCheatsheet.js
A regex cheatsheet 👩🏻‍💻 (by Catherine)
let regex;
/* matching a specific string */
regex = /hello/; // looks for the string between the forward slashes (case-sensitive)... matches "hello", "hello123", "123hello123", "123hello"; doesn't match for "hell0", "Hello"
regex = /hello/i; // looks for the string between the forward slashes (case-insensitive)... matches "hello", "HelLo", "123HelLO"
regex = /hello/g; // looks for multiple occurrences of string between the forward slashes...
/* wildcards */
regex = /h.llo/; // the "." matches any one character other than a new line character... matches "hello", "hallo" but not "h\nllo"
regex = /h.*llo/; // the "*" matches any character(s) zero or more times... matches "hello", "heeeeeello", "hllo", "hwarwareallo"
Hẳn là trong mỗi lập trình viên Javascipt đều có cho mình một lib về các func hữu ích, và nó sẽ đi theo mình suốt một dự án hay dài hơn nữa là cả cuộc đời thăng trầm.
Và đây tôi sẽ chia sẻ cho các bạn lib của tôi, lúc đầu tôi làm es5 nhưng sau này tôi đã chuyển về es6 để thuận tiện phù hợp cho các dự án tiếp theo.
Các bạn nào chưa hiểu về cấu trúc của es5 khác với es6 như thế nào thì tôi khuyên hãy đọc qua bài này.
Sự khác biệt cấu trúc ES5 và ES6...
https://anonystick.com/blog-developer/hoc-javascript-su-khac-biet-cau-truc-class-giua-es5-va-es6-sTuezjDk.jsx
@NguyenTungs
NguyenTungs / functional-utils.js
Created March 6, 2019 08:55 — forked from bendc/functional-utils.js
A set of pure ES2015 functions aimed to make functional JavaScript more idiomatic.
// array utils
// =================================================================================================
const combine = (...arrays) => [].concat(...arrays);
const compact = arr => arr.filter(Boolean);
const contains = (() => Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
@NguyenTungs
NguyenTungs / mongoToES.js
Created September 30, 2018 09:04 — forked from derickson/mongoToES.js
Example of NodeJS Loop of Mongo to Elasticsearch
// npm install elasticsearch
// setup nodejs client for elasticsearch
// documentation: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html
var elasticsearch = require('elasticsearch');
var EsClient = new elasticsearch.Client({
host: 'localhost:9200',
log: 'info'
});
@NguyenTungs
NguyenTungs / index.js
Created August 9, 2018 04:29 — forked from JonathanMH/index.js
JSON Web Token Tutorial: Express
// file: index.js
var _ = require("lodash");
var express = require("express");
var bodyParser = require("body-parser");
var jwt = require('jsonwebtoken');
var passport = require("passport");
var passportJWT = require("passport-jwt");
@NguyenTungs
NguyenTungs / index.html
Created July 5, 2018 04:44
Firebase event is writing.. Firebase event is writing.. // source https://jsbin.com/mesuzuz
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="Firebase event is writing..">
<meta name="keywords" content="Firebase event is writing..">
<meta name="author" content="anonystick">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@NguyenTungs
NguyenTungs / base64-image-upload.js
Created June 12, 2018 08:57 — forked from madhums/base64-image-upload.js
save base64 encoded image
/*
* Taken from http://stackoverflow.com/questions/5867534/how-to-save-canvas-data-to-file/5971674#5971674
*/
var fs = require('fs');
// string generated by canvas.toDataURL()
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
+ "3gAAAABJRU5ErkJggg==";
// strip off the data: url prefix to get just the base64-encoded bytes
const people = Array.from(document.querySelectorAll('.people p'));
const names = peopleArray.map(person => person.textContent);
// ---------------------------------------------------------------
const names = Array.from(document.querySelectorAll('.people p'), person => {
return person.textContent
});