Created
November 18, 2015 01:50
-
-
Save ryu1/2c49d684588738769a5e to your computer and use it in GitHub Desktop.
This is generic library of javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // String.prototype.format() - 文字列フォーマット | |
| // | |
| // Example: | |
| // var hello = 'Hello, {0}!'.format('world'); | |
| // console.log(hello); // Hello, world! | |
| // | |
| // var profile = 'name: {name}, age: {age}'.format({ | |
| // name: 'phi', | |
| // age: '27', | |
| // }); | |
| // console.log(profile); // name: phi, age: 27 | |
| Object.defineProperty(String.prototype, 'format', { | |
| value: function(arg) { | |
| var fn = null; | |
| if (typeof arg === 'object') { | |
| fn = function(m, k) { return arg[k]; }; | |
| } | |
| else { | |
| var args = arguments; | |
| fn = function(m, k) { return args[+k]; }; | |
| } | |
| return this.replace( /\{(\w+)\}/g, fn ); | |
| }, | |
| }); | |
| // randint(min, max) - 範囲指定でランダム整数を生成 | |
| // | |
| // Example: | |
| // | |
| // for (var i=0; i<10; ++i) { | |
| // console.log(randint(0, 10)); // 0 ~ 10 の範囲でランダムに表示 | |
| // } | |
| var randint = function(min, max) { | |
| return Math.floor(Math.random() * (max - min + 1)) + min; | |
| }; | |
| // $ - document.querySelector のショートハンド | |
| // Example | |
| // | |
| // var h1 = $('h1'); | |
| // h1.innerHTML = 'Hello, world!'; | |
| var $ = function(q) { | |
| return document.querySelector(q); | |
| }; | |
| // toArray() - 全てを Array(配列) に変換 | |
| // | |
| // Example: | |
| // var hello = function() { | |
| // toArray(arguments).forEach(function(name) { | |
| // console.log('Hello, ' + name + '!'); | |
| // }); | |
| // }; | |
| // | |
| // 全ての名前が出力される | |
| // hello('phi', 'omatoro', 'daishi', 'minimo', 'simiraaaa', 'alkn', 'utyo', 'emadurandal'); | |
| var toArray = function(arr) { | |
| return Array.prototype.slice.call(arr); | |
| }; | |
| // GET ... 簡易非同期通信 | |
| // | |
| // Example: | |
| // // cdnjs から material を含むライブラリの一覧を取得 | |
| // GET('https://api.cdnjs.com/libraries?search=material', function(res) { | |
| // var data = JSON.parse(res); | |
| // data.results.forEach(function(item) { | |
| // console.log(item.name); | |
| // }); | |
| // }); | |
| var GET = function(url, fn) { | |
| var req = new XMLHttpRequest(); | |
| req.onreadystatechange = function() { | |
| if (req.readyState === 4) { | |
| if (req.status === 0 || req.status === 200) { | |
| fn(req.responseText); | |
| } | |
| } | |
| }; | |
| req.open('GET', url, true); | |
| req.send(''); | |
| }; | |
| // QueryString ... クエリパラメータを取得 | |
| // Example: | |
| // var obj = QueryString.parse('hoge=100&foo=bar'); | |
| // // parse | |
| // console.log(JSON.stringify(obj)); // {"hoge":"100","foo":"bar"} | |
| // // stringify | |
| // console.log(QueryString.stringify(obj)); // hoge=100&foo=bar | |
| var QueryString = { | |
| parse: function(text, sep, eq, isDecode) { | |
| text = text || location.search.substr(1); | |
| sep = sep || '&'; | |
| eq = eq || '='; | |
| var decode = (isDecode) ? decodeURIComponent : function(a) { return a; }; | |
| return text.split(sep).reduce(function(obj, v) { | |
| var pair = v.split(eq); | |
| obj[pair[0]] = decode(pair[1]); | |
| return obj; | |
| }, {}); | |
| }, | |
| stringify: function(value, sep, eq, isEncode) { | |
| sep = sep || '&'; | |
| eq = eq || '='; | |
| var encode = (isEncode) ? encodeURIComponent : function(a) { return a; }; | |
| return Object.keys(value).map(function(key) { | |
| return key + eq + encode(value[key]); | |
| }).join(sep); | |
| }, | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment