Skip to content

Instantly share code, notes, and snippets.

View yesvods's full-sized avatar
💭
Getting things done.

Jogis yesvods

💭
Getting things done.
View GitHub Profile
@yesvods
yesvods / promise-wrap.js
Last active November 19, 2018 23:25
wrap nodejs fs#readFile to be a promise.
let fs = {
readFile: function(filename, cb){
setTimeout(()=>{
cb('hello')
}, 100)
}
}
let readFilePro = function(filename){
return new Promise(function(resolve, reject) {
fs.readFile(filename, (data, err) => {
@yesvods
yesvods / es6-inherit.js
Last active August 29, 2015 14:25
ES6 Class inherit
//ES6 Class inherit example
class Person{
sayName(){
console.log(this.name);
}
}
class Classmate extends Person {
constructor(name){
super()
this.name = name;
@yesvods
yesvods / async-parallel.js
Created July 21, 2015 09:11
ES6 async parallel execution
//mock a fs Object
let fs = {
readFile: function(filename, cb){
let randomTime = 100+Math.random()*1000>>0;
setTimeout(()=>{
cb(`hello ${filename}`)
}, randomTime)
}
}
@yesvods
yesvods / node-folder-structure-options.md
Last active April 2, 2018 15:33 — forked from lancejpollard/node-folder-structure-options.md
What is your folder-structure preference for a large-scale Node.js project?

What is your folder-structure preference for a large-scale Node.js project?

0: Starting from Rails

This is the reference point. All the other options are based off this.

|-- app
|   |-- controllers
|   |   |-- admin
@yesvods
yesvods / gist:4c7251f060029c8a5b07
Last active October 1, 2018 13:44
Merge Object with ES7 object spread
const obj1 = {name:"xiaoming", age: 23}
const obj2 = {age: 33}
const obj3 = {...obj1, ...obj2}
//obj3 ==> {"name":"xiaoming","age":33}
@yesvods
yesvods / gist:51af798dd1e7058625f4
Created August 15, 2015 11:13
Merge Arrays in one with ES6 Array spread
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1, ...arr2] //arr3 ==> [1,2,3,4,5,6]
@yesvods
yesvods / express-lite.js
Last active January 2, 2016 15:46
Express-lite
function server(req, res){
let next = () => {
//deal with ending job
}
middlewares.reduceRight((next, middleware) => {
return () => {
middleware(req, res, next)
}
}, next)();
}
@yesvods
yesvods / ls.js
Created January 21, 2016 09:54
LocalStorage MaxSzie
if (localStorage && !localStorage.getItem('size')) {
var i = 0;
try {
// Test up to 10 MB
for (i = 250; i <= 10000; i += 250) {
localStorage.setItem('test', new Array((i * 1024) + 1).join('a'));
}
} catch (e) {
localStorage.removeItem('test');
localStorage.setItem('size', i - 250);
@yesvods
yesvods / save.js
Created January 22, 2016 03:41
Save variable string to file in browser
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
@yesvods
yesvods / template.js
Created April 4, 2016 08:32
Javascript template engine
function template(html, data){
//process html template
html = html.replace(/&gt;/g, ">")
.replace(/&lt;/g, "<")
.replace(/[\r\t\n]\s+/g, '')
.trim();
var re = /<%([^>]+)?%>/g,
reExp = /(^( )?(if|for|else|switch|case|break|{|}|var|let|const))(.*)?/g,
code = 'var r=[];\n',