Skip to content

Instantly share code, notes, and snippets.

View cognitom's full-sized avatar

Tsutomu Kawamura cognitom

View GitHub Profile
@cognitom
cognitom / normalizeDate.js
Created January 30, 2017 02:13
openBDの日付フィールドのノーマライズ
function normalizeDate (raw) {
const patterns = [
// 2017-01-30
{re: /^\d{4}-\d{2}-\d{2}($|,)/, f: m => m[0]},
// 2017-01
{re: /^\d{4}-\d{2}($|,)/, f: m => m[0]},
// 2017
{re: /^\d{4}($|,)/, f: m => m[0]},
// 20170130
{re: /^(\d{4})(\d{2})(\d{2})($|,)/, f: m => `${m[1]}-${m[2]}-${m[3]}`},
@cognitom
cognitom / app.tag
Last active July 3, 2016 05:54
How to use postcss with Riot.js
<app>
<h1>Riot</h1>
<p>A React-like user interface micro-library.</p>
<style scoped type="external">
:scope {
display: block;
}
h1 {
border-bottom: 1px solid black;
@cognitom
cognitom / index.js
Created June 29, 2016 03:39
How to get [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in JavaScript
// ES6
[...Array(10).keys()]
Array(10).map((n, i) => i)
Array.from(Array(10).keys())
Array.from({ length:10 }, (v, i) => i)
// ES5
Array(10).map(function(n, i) { return i })
// Oldies
@cognitom
cognitom / number_format.coffee
Created July 26, 2013 16:14
Format numbers with comma.
numberFormat = (n) ->s = n + ''; s = s.replace(/(\d+)(\d{3})$/, '$1,$2') while /(\d+)(\d{3})$/.test s; s
numberFormat2 = (n) -> (n + '').replace /(\d+)(\d{3})?(\d{3})?(\d{3})?(\d{3})?(\d{3})$/, (match...) -> (match[p] + '' for p in [1..6] when match[p]).join ','
numberFormat3 = (n) -> (n = '00' + n).substr(n.length % 3).match(/\d{3}/g).reduce((x,y) -> x + ',' + y if y).replace /^0*/, ''
numberFormat4 = (n) -> (n + '').split('').reduceRight (x, y) -> if (x.length + 1) % 4 then y + x else y + ',' + x
# usage
s = numberFormat4 1234567 # "1,234,567"
var MyNamespace = {};
MyNamespace.Person = function(){
/* Public Fields */
this.name = '';
this.sex = 'M';
/* Private Fields */
var _age = 0;
/* Constructor */
this.initialize = function(name, sex, age){
@cognitom
cognitom / gist:5500409
Created May 2, 2013 06:00
サイト全体をダウンロードして、相対パスに書き換える。ローカルでも表示可能。
wget -m -k -K -E http://url/of/web/site
var widget;widget={server:"http://librize.com",root:"https://raw.github.com/librize/widgets/master",default_params:{},style_loaded:{},addStyle:function(a){return $("head").append('<link rel="stylesheet" type="text/css" href="'+a+'" />')},getQueryParams:function(a){var b,c,d,e,f,g,h,i;d={};h=a.replace(/^.*\?/,"").split("&");for(f=0,g=h.length;f<g;f++){c=h[f];i=c.split("="),b=i[0],e=i[1];d[b]=e}return d},getDefaultParams:function(a){var b;b=this.getQueryParams(a);return this.default_params={place:b.place||void 0,limit:b.limit||5,height:b.height||75,width:b.width||0,theme:b.theme||"simple"}},show:function(a){var b,c,d,e,f,g,h=this;d=a.attr("data-place")||this.default_params.place;c=a.attr("data-limit")||this.default_params.limit;b=a.attr("data-height")||this.default_params.height;g=a.attr("data-width")||this.default_params.width;e=a.attr("data-theme")||this.default_params.theme;if(d){(this.style_loaded[e]==null?e!=="none":void 0)&&this.addStyle(""+widget.root+"/css/"+e+".css");this.style_loaded[e]=!0;f=""+widget
@cognitom
cognitom / not-pass.html
Created April 25, 2012 18:42
Safari doesn't say "PASS"
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.js"></script>
</head>
<body>
<script>
url = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www.engadget.com/rss.xml&callback=?';
$.getJSON(url, function(){
alert('PASS');
@cognitom
cognitom / level1-challenge5.lolcode
Created April 1, 2012 05:06
LEVEL 1 CHALLENGE 5
HAI
I HAS A ANIMAL
GIMMEH ANIMAL
BOTH SAEM ANIMAL AN "CAT", O RLY?
YA RLY
VISIBLE "U HAS KITTEH"
NO WAI
VISIBLE "KITTEH R 2 GUD 4 U"
OIC
@cognitom
cognitom / gist:2192416
Created March 25, 2012 08:41
Syntax check multiple PHP files in the current directory
find . -name \*.php -exec php -l "{}" \;