Skip to content

Instantly share code, notes, and snippets.

@ajhsu
ajhsu / better-nodejs-require-paths.md
Created October 28, 2015 04:15 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@ajhsu
ajhsu / dive-into-react-set-state.md
Last active May 14, 2018 14:12
Dive into setState method in React

Dive into setState() method in React

起因

與伊神哥在討論其中一段程式碼時,發現某一段程式碼在 setTimeout() 裡面執行結果如預期,但搬出 setTimeout() 之後卻會有問題。

1. 以下僅為示意程式碼,並非案例當事人。
2. 其實 setTimeout() 在原本的案例是 Promise Callback,為了解釋則替換為 setTimeout()

body {
padding: 50px;
font: 200 16px "Helvetica Neue", Helvetica, Arial;
}
h1 {
font-weight: 300;
}
a {
// 1. 打開 Slack 網頁版
// 2. 開啟需要備份的對話視窗,包含 Direct Message 或 Channel 都可以
// 3. 打開 Chrome Dev Tools 的 Console
// 4. 複製貼上以下程式碼
// 5. 執行 JS 並輸出歷史訊息
// 6. 換下一個 Channel 繼續備份
// 7. 重複第4個步驟
// PS. 輸出到 Console 的歷史訊息,可以透過在空白處按右鍵 -> Save as ,另存成文字檔
var historyMessages = `[Slack Export History Messages] SEHM\n`;
$('.navbar-brand').css('position', 'fixed').animate({top: 500}, 5000);
(function(e){function o(){t&&(i(o),jQuery.fx.tick())}var t,n=0,r=["webkit","moz"],i=window.requestAnimationFrame,s=window.cancelAnimationFrame;for(;n<r.length&&!i;n++)i=window[r[n]+"RequestAnimationFrame"],s=s||window[r[n]+"CancelAnimationFrame"]||window[r[n]+"CancelRequestAnimationFrame"];i?(window.requestAnimationFrame=i,window.cancelAnimationFrame=s,jQuery.fx.timer=function(e){e()&&jQuery.timers.push(e)&&!t&&(t=!0,o())},jQuery.fx.stop=function(){t=!1}):(window.requestAnimationFrame=function(e,t){var r=(new Date).getTime(),i=Math.max(0,16-(r-n)),s=window.setTimeout(function(){e(r+i)},i);return n=r+i,s},window.cancelAnimationFrame=function(e){clearTimeout(e)})})(jQuery);
{
"hello": "world two!"
}
// Async function
var getNum = function(){
return new Promise((resolve, reject) => {
// resolve(1);
reject('not found');
});
}
// A function that contains an async-call
async function asyncFun (){
@ajhsu
ajhsu / index.js
Last active August 31, 2016 03:32
json-object-diff
var shallowequal = require('shallowequal');
const oldData = [{
carLow: "",
carNo: "",
isFull: "0",
lastInfo: "",
lat: "24.67831",
lon: "121.77582",
name: "羅東轉運站",
@ajhsu
ajhsu / visual-studio-code-settings.js
Last active October 11, 2016 03:41
Sublime Text settings within Visual Studio Code
// keybinding.json
[
{
"key": "cmd+shift+[",
"command": "workbench.action.previousEditor"
},
{
"key": "cmd+shift+]",
"command": "workbench.action.nextEditor"
},