Skip to content

Instantly share code, notes, and snippets.

View sunderls's full-sized avatar
🎯
Focusing

sunderls

🎯
Focusing
View GitHub Profile
@sunderls
sunderls / curry.js
Created June 1, 2020 15:48
curryify
function curry(fn) {
// fn(1,2,3) => fn(1)(2,3) => fn(1,2)(3)
// so it return a function which:
// 1. if enough param, just run it
// 2. if not enough param, return another function with param prefilled
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(null, args)
} else {
return curried.bind(null, ...args)
@sunderls
sunderls / mynew.js
Created June 1, 2020 13:19
implement our own `new` keyword
const myNew = (constructor, ...args) => {
// 1. create an object based on constructor.prototype
const obj = Object.create(constructor.prototype)
// 2. run constructor with this set to the above object
const result = constructor.call(obj, ...args)
// 3. if return value is object, user that. if not use the newly created one
if (result instanceof Object) {
return result
}
@sunderls
sunderls / mycall.js
Created June 1, 2020 13:15
implement Function.prototype.call
Function.prototype.mycall = function(target, ...args) {
const key = Symbol()
target[key] = this
const result = target[key](...args)
delete target[key]
return result
}
@sunderls
sunderls / debounce.js
Last active June 1, 2020 15:13
throttle/debounce in JavaScript
const debounce = (func, wait, options = {
leading: false,
trailing: true
}) => {
// same timer for leading and trailing
let timer = null
// special flag to skip the trailing call if leading is true
let isCalledForLeading = false
@sunderls
sunderls / nuxt_pc_mobile.js
Created January 4, 2019 09:40
use nuxt as a middleware to provide different views according to user-agent
const app = express();
app.use(router);
const configPC = require('../pc/nuxt.config.js');
const configSP = require('../sp/nuxt.config.js');
const nuxtPC = new Nuxt(configPC);
const nuxtSP = new Nuxt(configSP);
// Enable live build & reloading on dev
if (nuxtPC.options.dev) {
@sunderls
sunderls / compilation.assets.json
Created April 15, 2017 04:38
DebugCompilationPlugin for webpack
{
"index.js": {
"_source": {
"children": [
{
"children": [
"/******/ (function(modules) { // webpackBootstrap\n",
{
"_source": {
"_value": " \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// exp
@sunderls
sunderls / fbevents.js
Last active April 26, 2024 10:31
facebook pixel js prettified
/*1482118385,,JIT Construction: v2746767,en_US*/
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*/
try {
(function(win, doc, location, history) {
'use strict';