Skip to content

Instantly share code, notes, and snippets.

View st98's full-sized avatar

st98 st98

View GitHub Profile
@st98
st98 / hex.js
Last active August 29, 2015 14:06
function _repeat(str, n) {
return Array(n + 1).join(str);
}
function _zfill(str, n) {
return (_repeat('0', n) + str).slice(-n);
}
function hex(str) {
return str.split('').map(function (c) {
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Icon</title>
<style>
body {
width: 30em;
margin: 0 auto;
font-family: Consolas, Meiryo, monospace;
@st98
st98 / Number#length.js
Last active August 29, 2015 14:05
Number#length。何桁か調べます。
function log10(x) {
return Math.log(x) / Math.LN10;
}
Object.defineProperty(Number.prototype, 'length', {
get: function () {
return Math.floor(log10(Math.abs(this))) + 1;
},
set: function (value) {
return value;
function isStrict() {
try {
arguments.callee;
} catch (_) {
return true;
}
return false;
}
@st98
st98 / random.bat
Last active August 29, 2015 14:05
自分用。適当に選んで開くヤツ。
@echo off
python -c "import os, random, sys; os.popen('explorer ' + ' & explorer '.join(random.sample(os.listdir('.'), int(sys.argv[1]) if len(sys.argv) > 1 else 1)))" %1
@st98
st98 / rot-n.js
Last active August 29, 2015 14:03
ROT(13|47) の実装、変数の名前とかてけとー。
var rot = (function () {
function tr(str, from, to) {
return str.replace(new RegExp('[' + from + ']', 'g'), function (key) {
return to[from.indexOf(key)] || key;
});
}
function range(min, max) {
var result = '';
var c1, c2;
c1 = console.log;
c2 = console.log.bind(console);
c1('hoge'); // => TypeError: Illegal invocation
c1.call(console, 'hoge'); // => hoge
c2('hoge'); // => hoge
@st98
st98 / gist:9f98e808d4aadd2af5fa
Created June 29, 2014 16:40
'hoge'.charAt(n) と 'hoge'[n] の違い。
'hoge'.charAt(3); // => 'e'
'hoge'[3]; // => 'e'
'hoge'.charAt(4); // => ''
'hoge'[4]; // => undefined
@st98
st98 / String#title.js
Last active August 29, 2015 14:03
Python の str#title みたいな感じで。
String.prototype.title = function () {
return this.replace(/[A-Za-z]+/g, function (m) {
return m[0].toUpperCase() + m.slice(1).toLowerCase();
});
};
@st98
st98 / n文字ごとに文字列を挿入するヤツ.js
Last active August 29, 2015 14:02
n 文字ごとに文字列を挿入するヤツ。
String.prototype.n文字ごとに文字列を挿入する = function (n, str) {
if (n == null || str == null) {
return String(this);
}
return this.replace(new RegExp('.{' + n + '}', 'g'), function (m) {
return m + str;
});
};