Skip to content

Instantly share code, notes, and snippets.

View hackjutsu's full-sized avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / example.sh
Last active February 2, 2017 06:00
Configure hostname in macOS #tags: shell,macOS,hostname
# http://osxdaily.com/2010/09/06/change-your-mac-hostname-via-terminal/
# check hostname
hostname
# change hostname
sudo hostname -s YourHostName
@hackjutsu
hackjutsu / example.js
Last active February 2, 2017 06:06
[Example to specify the gist language] This is a demo about specifying the language for a gist if the GitHub API fails to do so. #tags: example
// vim: syntax=javascript
let test = 'This is a javascript file'
@hackjutsu
hackjutsu / example.js
Last active February 2, 2017 06:44
[Show/Hide element in ReactJS] This is a quick demo about how to show/hide an element in React based on the component's state. #tags: react
var Child = React.createClass({
render: function() {
return (<div>I'm the child</div>);
}
});
var ShowHide = React.createClass({
getInitialState: function () {
return { childVisible: false };
},
@hackjutsu
hackjutsu / includes_startsWidth_endsWith.js
Last active February 2, 2017 06:44
ES6 String #tags: string,es6
// includes() startsWith() endsWith()
s = 'Hello world!'
s.includes('o') // true
s.startsWith('Hello') // true
s.endsWith('!') // true
// 这三个方法都支持第二个参数,表示开始搜索的位置。
s.includes('Hello', 6) // false
s.startsWith('world', 6) // true
@hackjutsu
hackjutsu / array_from.js
Last active February 2, 2017 06:44
ES6 Array #tags: es6,array
/* Array.from方法用于将两类对象转为真正的数组:
1. 类似数组的对象(array-like object)
2. 可遍历(iterable)的对象(包括ES6新增的数据结构Set和Map)*/
// example1
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
// example 2
Array.from('hello') // ['h', 'e', 'l', 'l', 'o']
@hackjutsu
hackjutsu / map_example.js
Last active February 2, 2017 06:45
[ES6 Map] ES6提供了Map数据结构。它类似于对象,也是键值对的集合,但是“键”的范围不限于字符串,各种类型的值(包括对象)都可以当作键。也就是说,Object结构提供了“字符串—值”的对应,Map结构提供了“值—值”的对应,是一种更完善的Hash结构实现。 #tags: es6,map
let m = new Map()
let o = {p: 'Hello World'}
m.set(o, 'content')
m.get(o) // "content"
m.has(o) // true
m.delete(o) // true
m.has(o) // false
@hackjutsu
hackjutsu / basic_syntax.js
Last active February 2, 2017 06:45
[ES6 spread syntax] 扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。 #tags: es6,spread
// 基本用法
console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5
// 该运算符主要用于函数调用
// 例子1:函数定义
function push(array, ...items) {
array.push(...items)
}
// 例子2:函数调用
@hackjutsu
hackjutsu / index.html
Last active February 2, 2017 06:45
[Typewriter Effect] Typing animation by css. https://css-tricks.com/snippets/css/typewriter-effect/ #tags: animation
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
@hackjutsu
hackjutsu / main.js
Last active February 2, 2017 08:35
[Electron keyboard shortcut binding] Register/unregister a keyboard shortcut locally to a BrowserWindow instance. Pay attention, this module will overwrite the default functionality of the original shortcut. #tags: electron,keyboard,lepton
// npm install --save electron-localshortcut
const electronLocalshortcut = require('electron-localshortcut');
const BrowserWindow = require('electron').BrowserWindow;
const win = new BrowserWindow();
win.loadUrl('https://github.com');
win.show();
electronLocalshortcut.register(win, 'Ctrl+A', () => {
@hackjutsu
hackjutsu / IPC_electron.js
Last active February 2, 2017 08:35
[Sending IPC events from main to renderer in Electron] (http://stackoverflow.com/a/36809014/3697757) #tags: electron,lepton
// To send events to particular window you can use webContents.send(EVENT_NAME, ARGS) (see docs). webContents is a property of a window instance:
// main process
storeWindow.webContents.send('store-data', store);
// To listen for this event being sent, you need a listener in a window process (renderer):
// renderer process
// ipcRenderer is provided by electron package. You can import it like this: var ipcRenderer = require('electron').ipcRenderer; or es6 import { ipcRenderer } from 'electron';
import { ipcRenderer } from 'electron';
ipcRenderer.on('store-data', function (store) {