Skip to content

Instantly share code, notes, and snippets.

View tpai's full-sized avatar
🌴
On vacation in Spain

Tony Pai tpai

🌴
On vacation in Spain
View GitHub Profile
@tpai
tpai / fillParamsIntoPath.js
Last active June 6, 2018 04:14
Replace route placeholder with actual params.
function fillParamsIntoPath (path, ...params) {
let i = -1;
return path.replace(/\/\:\w{1,}/ig, () => {
i += 1;
return `\/${params[i]}`;
});
}
console.log(fillParamsIntoPath('https://localhost:3000/api/members/:id', 1));
console.log(fillParamsIntoPath('/api/members/:member_id/friends/:friend_id', 1, 2));
@tpai
tpai / index.jsx
Created May 27, 2018 16:13
React component with Google API integration
/* global document, gapi, Promise */
import React, { PureComponent } from 'react';
class App extends PureComponent {
constructor(props) {
super(props);
this.state = { gapiReady: false, values: {} };
}
initGoogleAPI = () => {
const mouseDownEvent = document.createEvent('MouseEvents');
mouseDownEvent.initEvent('mousedown', true, true);
let activeSquares = [];
function obsRoot() {
const root = document.getElementsByClassName('squares')[0];
const rootObserver = new MutationObserver((mutations) => mutations.map(mutation => {
obsSquares();
}));
@tpai
tpai / with-without-cors.md
Last active March 23, 2018 07:52
In the present, web services almost build based on microservice architecture, separate front end node app and back end api, this will cause cors issue while developing and distributing production, that's go through some basic solution for dealing wit

CORS solution

In the present, web services almost build based on microservice architecture, separate front end node app and back end api, this will cause cors issue while developing and distributing production, that's go through some basic solution for dealing with cors.

API server CORS enabled

By default json-server is launch with cors enabled, so directly fetch data from API server.

API server CORS disabled

@tpai
tpai / jest-cheatsheet.md
Last active September 28, 2020 12:01
paste jest cheatsheet
@tpai
tpai / parcel_bundle.js
Created January 9, 2018 05:03
parcel_bundle
// modules are defined as an array
// [ module function, map of requires ]
//
// map of requires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the require for previous bundles
// eslint-disable-next-line no-global-assign
require = (function (modules, cache, entry) {
@tpai
tpai / react-dom-umd.js
Created January 5, 2018 04:23
react-dom-umd
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) :
typeof define === 'function' && define.amd ? define(['react'], factory) :
(global.ReactDOM = factory(global.React));
}(this, (function (React) {
@tpai
tpai / ImageProcess.js
Created January 4, 2018 04:32
Lambda function for create thumbnail.
// dependencies
var async = require('async');
var path = require('path');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({
imageMagick: true
});
var util = require('util');
var LambdaWatermark = require('lambda-watermark');
@tpai
tpai / webpackUMD.js
Last active February 12, 2024 00:06
webpackUMD
(function webpackUniversalModuleDefinition(root, factory) {
if (typeof exports === 'object' && typeof module === 'object') // CommonJS(2)
module.exports = factory(require(__require_module__));
else if (typeof define === 'function' && define.amd) // AMD
define([__require_module__], factory);
else if (typeof exports === 'object') // CommonJS(1)
exports[__module_name__] = factory(require(__require_module__));
else // Global Variable
root[__module_name__] = factory(root[__require_module__]);
})(typeof self !== 'undefined' ? self : this, function() {