Skip to content

Instantly share code, notes, and snippets.

View secf4ult's full-sized avatar

Yu Xiao secf4ult

View GitHub Profile
// common code for implementing require()/exports
var dependencies = {} // loaded modules
var modules = {} // code of your dependencies
// require function
var require = function (module) {
if (!dependencies[module]) {
// module not loaded, let’s load it
var exports = {}
modules[module](exports)
// now in `exports` we have the things made “public”
@secf4ult
secf4ult / webpack.config.js
Last active November 23, 2020 04:52
webpack config template
const config = {
entry: {
main: __dirname + '/app/main.js'
},
output: {
path: __dirname + '/public',
filename: 'bundle.js'
},
module: {
rules: [
@secf4ult
secf4ult / meteor-memory-leak.js
Created September 11, 2019 16:46
The memory leak meteor bumped into.
// From the Meteor blog: https://blog.meteor.com/an-interesting-kind-of-javascript-memory-leak-8b47d2e7f156.
// TL;DR
// If a variable is used by a closure, it ends up in the lexical enviroment shared by all closures in that scope,
// which can lead to memory leaks.
// Even though function unused is never called, originalThing is refered in its body, so originalThing is shared
// by the lexical environment between unused and someMethod, which is also a closure, so someMethod holding the
// lexical environment prevents the originalThing from being GCed, because it's important that all closures in
// that scope get the same variable.
// This can be fixed in the JS engine if each closure has its own lexical environment (a dictionary containing
@secf4ult
secf4ult / class.babel.js
Created July 22, 2020 09:39
Classes and prototypes in JavaScript.
"use strict";
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
@secf4ult
secf4ult / set_npm_mirror
Created July 28, 2020 13:02
Setup npm mirror in China
# taobao mirror
npm config set registry https://registry.npm.taobao.org/
npm config set disturl https://npm.taobao.org/mirrors/node/
# thrid-party module
MIRROR_HOST=https://npm.taobao.org/mirrors
npm config set sass_binary_site $MIRROR_HOST/node-sass/
npm config set sharp_dist_base_url $MIRROR_HOST/sharp-libvips/
npm config set electron_mirror $MIRROR_HOST/electron/
npm config set puppeteer_download_host $MIRROR_HOST/puppeteer/
@secf4ult
secf4ult / downloader.js
Last active December 8, 2020 08:37
Download file using JavaScript
const downloader = function (url, filename) {
// change these two variables
if (!url) throw new Error('url is needed!')
filename = filename || 'newfile'
fetch(url)
.then(res => res.blob())
.then(blob => {
const url = URL.createObjectURL(blob)
const anchor = document.createElement('a')
@secf4ult
secf4ult / y.js
Last active February 9, 2021 08:34
Y Combinator in JavaScript
// from Crockford's talk: https://www.youtube.com/watch?v=ya4UHuXNygM
function Y(le) {
return (function (f) {
return f(f)
}(function (f) {
return le(function (x) {
return f(f)(x)
})
}))
}
@secf4ult
secf4ult / setup_linux
Created February 23, 2021 07:22
Environment setup scripts.
#!/bin/sh
# Getting Ready
LOGDIR="~/log"
DOTFILES="~/.dotfiles"
# Update & Upgrade
sudo apt-get update -y
sudo apt-get upgrade -y
# Setup Prerequisite
@secf4ult
secf4ult / vue_reactivity.js
Last active February 23, 2021 10:32
Simple Vue reactivity.
const targetMap = new WeakMap()
let activeEffect = null
function track(target, key) {
let depsMap = targetMap.get(target)
if (!depsMap) {
targetMap.set(target, (depsMap = new Map()))
}
@secf4ult
secf4ult / ackmann.c
Created February 23, 2021 10:30
Ackermann Function
#include <stdio.h>
int ack(int m, int n)
{
int ans;
if (m == 0) ans = n + 1;
else if (n == 0) ans = ack(m - 1, 1);
else ans = ack(m - 1, ack(m, n - 1));
return (ans);
}