Skip to content

Instantly share code, notes, and snippets.

View Shuumatsu's full-sized avatar

为世人降下祝福 Shuumatsu

View GitHub Profile
import { adjust, curryN, length, map } from 'ramda'
const globalize = transform => selector =>
curryN(length(selector),
(...args) => selector(...adjust(transform, -1, args))
)
export default (localStateTransform, selectors) =>
map(globalize(localStateTransform), selectors)
@Shuumatsu
Shuumatsu / parse-es6-template.js
Created July 24, 2017 15:19 — forked from smeijer/parse-es6-template.js
ES6 template string parser
function get(path, obj, fb = `$\{${path}}`) {
return path.split('.').reduce((res, key) => res[key] || fb, obj);
}
function parseTpl(template, map, fallback) {
return template.replace(/\$\{.+?}/g, (match) => {
const path = match.substr(2, match.length - 3).trim();
return get(path, map, fallback);
});
}
<div id="data"></div>
<body>
<script>
const div = document.querySelector('div')
const print = arg => div.insertAdjacentText('beforeend', arg)
</script>
<script src="https://en.wikipedia.org/w/api.php?action=opensearch&format=json&callback=print&search=google"></script>
@Shuumatsu
Shuumatsu / alibaba_online_test_nodejs.js
Created March 2, 2017 10:20 — forked from MrRaindrop/alibaba_online_test_nodejs.js
阿里校招在线笔试nodejs题
/**
* 实现一个nodejs的Master-Worker的小程序,要求:
* 1. Master维护与cpu核数相同的Worker的数量;
* 2. Master接收到Worker的disconnect消息时,重启新的Worker进程;
* 3. Worker监听1024端口并输出“Hello World”;
* 4. 在Worker遇到uncaughtException时,通知Master进程并等待3s后退出
*/
var os = require('os'),
cluster = require('cluster'),
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@Shuumatsu
Shuumatsu / a stack that supports getMin() in O(1) time and O(1) extra space
Last active September 25, 2016 05:57
a stack that supports getMin() in O(1) time and O(1) extra space
export class MinStack {
stack: number[] = [];
num: number = 0;
private _min: number;
get min(): number {
if (this.num > 0) {
return this._min;
}
}
@Shuumatsu
Shuumatsu / API.md
Created September 19, 2016 15:34 — forked from anonymous/API.md
// 1000->1020->1040->1050(100)
let read_line = (() => {
let arr = ['1000 1050', '1000 1020 50', '1000 1030 70', '1020 1030 15', '1020 1040 30', '1030 1050 40', '1040 1050 20'];
let i = 0;
return _ => {
return arr[i++];
}
})();
@Shuumatsu
Shuumatsu / gist:836024d8bc41318516d4d4f6a666afc6
Created September 12, 2016 11:54
Triggers the next round of Angular change detection after one turn of the browser event loop ensuring display of msg added in onDestroy
```
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
let nextId = 1;
@Component({
selector: 'heavy-loader',
template: '<span>heavy loader #{{id}} on duty!</span>'
})
export class HeavyLoaderComponent implements OnDestroy, OnInit {
id = nextId++;
@Input() logs: string[];
@Shuumatsu
Shuumatsu / co.js
Created June 3, 2016 10:37 — forked from iwillwen/co.js
A simple version of co
/**
* A simple co
* @param {Function} fn Generator Function
* @return {Function} callback
*/
function co(fn) {
return function(done) {
done = done || function() {};
var gen = fn();