Skip to content

Instantly share code, notes, and snippets.

@xulapp
xulapp / trim_comment.js
Created March 28, 2010 04:50
JS のコメントを除去
// できないこと
// ・演算子(/, /=) の後ろにあるコメントを除去できない
// ・演算子(/, /=) の後ろにある正規表現リテラルを無視できない
// ・<xml>{0/*消えて*/}/*消えないで*/</xml>
function trimComment(str) {
// コメント___________________ 文字列リテラル(")_______ 文字列リテラル(')_______ CDATA セクション________ 正規表現リテラル___________________________
return str.replace(/(\/)(?:\*[\s\S]*?\*\/|\/.*)|"(?:\\[\s\S]|[^\\\n"])*"|'(?:\\[\s\S]|[^\\\n'])*'|<!\[CDATA\[[\s\S]*?\]\]>|\/(?:\\.|\[(?:\\.|[^\n\]])*\]|[^\n/])+\/\w*/g, function($0, $1) {
return $1 ? '' : $0;
});
@xulapp
xulapp / es3bind.js
Created July 31, 2011 17:33
bind ES3
Function.prototype.bind = Function.prototype.bind || function bind(thisArg) {
var method = this;
var args = Array.prototype.slice.call(arguments, 1);
return function bound() {
var _args = args.concat(Array.prototype.slice.call(arguments));
if (!(this instanceof bound))
return method.apply(thisArg, _args);
var __args = [];
@xulapp
xulapp / callable.js
Created March 12, 2010 07:51
__call__ in JavaScript
function Callable() {
var self = function Callable()
this.__call__.apply(this, arguments);
self.__proto__ = Callable.prototype;
return self;
}
Callable.prototype = {
constructor: Callable,
__proto__: Function.prototype,
__call__: function __call__() {
@xulapp
xulapp / class.js
Created March 12, 2010 14:08
Class
function Class(sup, pro) {
if (sup && typeof sup === 'object')
pro = sup, sup = Object;
var con = Object.getOwnPropertyDescriptor(pro, 'constructor');
if (!con)
con = {value: Function(), writable: true, configurable: true};
if (con.configurable) {
con.enumerable = false;
/* @require Prototype.js
var src = new WaveFile(properties).toDataURI();
// properties は WaveFile.prototype を参考に。
*/
Object.extend(Number.prototype, {
toLEArray: function toLEArray(length) {
var num = this | 0, length = +length, result = [];
if (0 < arguments.length && !length) return result;
@xulapp
xulapp / symbol.js
Created March 24, 2010 05:11
symbol
[p for(p in Iterator(#1={
0: +'',
1: -~'',
2: -~-~'',
3: -~-~-~'',
4: -~-~-~-~'',
5: -~-~-~-~-~'',
6: '$$$$$$'[~''],
7: '$$$$$$$'[~''],
8: -~''<<-~-~-~'',
@xulapp
xulapp / prediction.js
Created March 31, 2010 18:18
PPM 法
function Prediction(level) {
this.level = +level || 5;
this.history = '';
this.recent = '';
this.accum = {};
this.tokens = {};
}
Prediction.prototype.add = function add(c) {
c = ('' + c).charAt(0);
this.tokens[c] = true;
@xulapp
xulapp / innocencer.js1k.htm
Created September 10, 2010 11:50
innocencer.js1k
<!doctype html>
<html>
<head>
<title>JS1k, 1k demo submission</title>
<meta charset="utf-8" />
</head>
<body>
<canvas id="c"></canvas>
<script type="text/javascript" src="innocencer.js1k.js"></script>
</body>
@xulapp
xulapp / Hatebu_count_on_Google_Reader.user.js
Created October 11, 2010 06:52
Hatebu count on Google Reader
// ==UserScript==
// @name Hatebu count on Google Reader
// @namespace http://twitter.com/xulapp
// @include http://www.google.tld/reader/view/*
// @include https://www.google.tld/reader/view/*
// @author xulapp
// @license MIT License
// @version 2010/10/11 15:50 +09:00
// ==/UserScript==
@xulapp
xulapp / jstmpl.js
Created October 27, 2010 17:14
JS テンプレートエンジン
(function() {
var quote = (function() {
var specialCharReg = /[\n\r'\\]/g;
var specialCharMap = {'\n': '\\n', '\r': '\\r', '\'': '\\\'', '\\': '\\\\'};
var specialCharRegCb = function(c) { return specialCharMap[c]; };
return function(str) {
return '\'' + str.replace(specialCharReg, specialCharRegCb) + '\'';
};
})();