Skip to content

Instantly share code, notes, and snippets.

View monjer's full-sized avatar

monjer monjer

  • None
  • Beijing China
  • 19:37 (UTC -12:00)
View GitHub Profile
@monjer
monjer / number-pad.js
Created November 25, 2020 07:00
Number pad left zero
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
/**
* @param {string} dir file or folder path
* @param {function} fileHandler hanlder function used for deal with file
*/
function walk(dir, fileHandler = (filePath) => filePath) {
const fstats = fs.lstatSync(dir);
if (fstats.isDirectory()) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
//
// Simple plugins mode in koajs use JavaScript async await
// https://github.com/senchalabs/connect/blob/master/index.js
//
const plugins = {
stack: [],
use: (plugin) => {
plugins.stack.push(plugin);
},
run: async (done) => {
@monjer
monjer / fillArrayWithIndex.js
Last active October 21, 2019 02:46
Create array fill with index
const fillArrayWithIndex = (length) => {
return Array.from(Array(length).keys());
}
const fillArray = (start, end) => {
return Array.from(Array(end - start + 1).keys()).map((v)=>v+start);
}
const fillArrayWithStep = (start, end, step=1) => {
return Array.from(Array(end - start + step).keys()).map((v)=>v+start);
const schedule = (() => {
const taskQueue = [];
const runTask = () => {
while (taskQueue.length > 0) {
const task = taskQueue.shift();
task();
}
};
const isArray = (obj) => {
return Object.prototype.toString.call(obj) === "[object Array]";
const defProp = (obj, name, value) => {
obj[name] = value;
};
const isThenable = (value) => {
return value && typeof value.then === "function";
};
const State = {
Pending: "Pending",
Fullfill: "Fullfill",
Reject: "Reject",
const memo = (fun) => {
let lastArgs = [];
let lastResult;
return (...args) => {
let argsEqual = true;
for (let i = 0; i < args.length; i++) {
if (args[i] !== lastArgs[i]) {
argsEqual = false;
break;
}
@monjer
monjer / downloadFile.js
Created December 26, 2018 12:34
downloadFile.js
const downloadFile = (data, fileName, type = 'text/plain') => {
const a = document.createElement('a');
a.style.display = 'none';
document.body.appendChild(a);
a.href = window.URL.createObjectURL(new Blob([data], { type }));
a.setAttribute('download', fileName);
a.click();
setTimeout(()=>{
document.body.removeChild(a);
})
@monjer
monjer / middleware.js
Created November 15, 2018 08:31
Javascript middleware demo
function operation(num) {
console.log(num);
}
const applyMiddleware = (...middlewares) => {
return (next) => (...args) => {
(middlewares || []).forEach(function(middleware) {
middleware(next)(...args);
});
};
@monjer
monjer / remove-duplicate.js
Created February 27, 2018 13:39
JS remove duplicate element in array
function removeDup(arr){
return [...new Set(arr)];
}
function removeDup(arr){
return Array.from(new Set(arr));
}
function removeDup(arr){