Skip to content

Instantly share code, notes, and snippets.

var _ = {
map: function(arr, fn) {
var r = [];
for (var i = 0, len = arr.length; i < len; i++) {
r.push(fn(arr[i]));
}
return r;
},
filter: function(arr, fn) {
@s-shin
s-shin / scrollfix-at-bottom.js
Last active March 23, 2016 05:58
not sufficiently tested
import _ from "underscore";
export default function scrollfixAtBottom(opts) {
opts = _.assign({
elFixed: document.querySelector(".js-scrollfix-bottom"),
elRelative: document.querySelector(".js-scrollfix-bottom-rel"),
bottom: 10,
throttle: 50,
debug: false
}, opts);
@s-shin
s-shin / child.php
Created February 29, 2016 05:57
IPC example by popen in ruby with timeout.
<?php
$num_iterates = $argv[1];
echo "iterates: $num_iterates\n";
foreach (range(1, $num_iterates) as $i) {
sleep($i);
echo "$i\n";
echo "ping\n";
@s-shin
s-shin / icomoon-selection-json-to-android-string-xml.sh
Last active February 24, 2016 14:13
One-liner to convert icomoon's selection.json to android's string xml.
ruby -rjson -e 'puts "<resources>"; JSON.parse(File.read(ARGV[0]))["icons"].each {|icon| puts " <string name=\"#{ARGV[1]}_#{icon["properties"]["name"]}\">&#x#{icon["properties"]["code"].to_s(16)};</string>"}; puts "</resources>"' path/to/icomoon/selection.json prefix > path/to/values/fonts.xml
@s-shin
s-shin / stacked-graph-transformer.rb
Created February 17, 2016 04:58
car data.tsv | ruby stacked-graph-transformer.rb
class StackedGraphTransformer
# @param [IO] io
def initialize(io)
@io = io
end
def gets_tsv
(line = @io.gets) ? line.chomp.split("\t") : nil
end
_ = require "lodash"
class BrainWrapper
constructor: (@brain, @key, @initialData, @migrator = _.identity) ->
get: (path = null, defaultValue = null) ->
data = @_get()
if path then _.get(data, path, defaultValue) else data
set: (path, value) ->
@s-shin
s-shin / tenuki_validator.rb
Last active February 10, 2016 10:20
Very simple validator.
require 'active_support'
require 'active_support/core_ext'
module Tenuki
class Validator
def initialize(field, value)
@field = field
@value = value
end
@s-shin
s-shin / segv_on_require.rb
Created February 10, 2016 08:00
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
class Foo
def foo(segv?: nil, suruyo: nil)
end
end
@s-shin
s-shin / init.coffee
Last active January 23, 2016 05:36
An example to overwrite environment variables in atom editor.
{spawnSync} = require "child_process"
do ->
ENV = [
"PATH"
"GOPATH"
]
result = spawnSync process.env.SHELL, ["-lc", "printenv"]
envLines = result.stdout.toString().split("\n")
@s-shin
s-shin / spawn-promise.coffee
Created December 22, 2015 11:17
手抜きspawn wrapper
{spawn} = require "child_process"
runCommand = (cmd, args, opts) -> new Promise (resolve, reject) ->
p = spawn cmd, args, opts
buf = []
p.stdout.on "data", (data) -> buf.push data
p.stderr.on "data", (data) -> buf.push data
p.on "error", reject
p.on "close", (code) ->
result = buf.join("").trim()