Skip to content

Instantly share code, notes, and snippets.

View huruji's full-sized avatar
☂️
被社会毒打的频率奇高

忽如寄 huruji

☂️
被社会毒打的频率奇高
View GitHub Profile
(function(global){
function generateJsonpCallback() {
return `jsonpcallback_${Date.now()}_${Math.floor(Math.random() * 100000)}`;
}
function removeScript(id) {
document.body.removeChild(document.getElementById(id));
}
function removeFunc(name) {
@huruji
huruji / load.js
Created November 4, 2017 17:55
demo for amd
(function(root){
var modMap = {};
var moduleMap = {};
var cfg = {
baseUrl: location.href.replace(/(\/)[^\/]+$/g, function(s, s1){
return s1
}),
path: {
@huruji
huruji / rem.js
Created April 9, 2018 14:55
set fontsize of root for rem
(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
@huruji
huruji / miniPromise.js
Created July 13, 2018 17:51
a deom for Promise
function Promise(excutor) {
let self = this;
self.status = 'pending';
self.value = null;
self.reason = null;
self.onFulfilledCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value) {
if(self.status === 'pending') {
@huruji
huruji / validator-proxy.js
Last active October 8, 2018 18:17
A simple fluent validator using Proxy
var proxyContext = function(ctx) {
return new Proxy(ctx, {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
const newCtx = proxyContext(ctx.clone());
if (prop in rules) {
let re = newCtx.addRule(rules[prop]);
return re;
@huruji
huruji / reset.js
Created February 23, 2019 07:57 — forked from 19h/reset.js
Node.js — Clear Terminal / Console. Reset to initial state.
console.reset = function () {
return process.stdout.write('\033c');
}
@huruji
huruji / async-foreach.js
Created April 30, 2019 08:08 — forked from Atinux/async-foreach.js
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)
@huruji
huruji / install_pipenv.sh
Created February 23, 2020 18:37 — forked from samredai/install_pipenv.sh
Python: Solution to 'Pipenv: Command Not Found' After 'pip install pipenv'
sudo -H pip install -U pipenv
# If you did a user install because you do not have sudo access, you have to modify your path to add your user folder
# The command on the next line tells you where your user folder is
# python3 -m site --user-base
PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
PATH="$PATH:$PYTHON_BIN_PATH"
@huruji
huruji / cloudSettings
Last active June 2, 2020 10:42
code_settings_2020
{"lastUpload":"2020-06-02T09:16:19.677Z","extensionVersion":"v3.4.3"}
@huruji
huruji / confirm.go
Created June 29, 2021 17:14 — forked from albrow/confirm.go
Go (golang): How to ask for user confirmation via command line
import (
"fmt"
"log"
"os"
"sort"
)
// askForConfirmation uses Scanln to parse user input. A user must type in "yes" or "no" and
// then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as
// confirmations. If the input is not recognized, it will ask again. The function does not return