Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

close #8 add JsDoc comment

  • Loading branch information...
commit 25e63c27a17dd9a5a5c1b90920d14ebb74e0628f 1 parent d03cc5d
@Layzie authored
Showing with 77 additions and 0 deletions.
  1. +77 −0 lib/rockstage.js
View
77 lib/rockstage.js
@@ -19,15 +19,29 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+/**
+ * @fileOverView The library of 'localStorage' and 'sessionStorage'
+ * @author HIRAKI Satoru@Layzie
+ * @version 0.0.2
+ */
+
(function(window, undefined) {
'use strict';
+ // This function is called when 'RS' will be loaded. Then check the storage
+ // and set 'RS' name space in 'window'. (This is private)
+ /**
+ * @private
+ */
(function _init() {
if (window.localStorage && window.sessionStorage) {
if (_checkArgument('Undefined', window.RS)) {
window.RS = {};
}
+ /**
+ * @namespace RS This library's namespace is 'RS'
+ */
window.RS = {
put: put,
get: get,
@@ -36,14 +50,25 @@
is: is
};
} else {
+ /**
+ * @throws {Error} If a browser have no storage.
+ */
throw new Error('RS: This browser have no storage.');
}
})();
+ // This function is select the storage. True or undefined is
+ // 'localStorage', false is 'sessionStorage'. (This is private)
+ /**
+ * @private
+ */
function _selectStorage(flag, fun) {
var storage;
if (!_checkArgument('Boolean', flag) && flag !== undefined) {
+ /**
+ * @throws {Error} If second argument isn't boolean.
+ */
throw new Error('RS.' + fun + ': 2nd argument should be boolean');
} else {
storage = (flag === true || flag === undefined) ? localStorage : sessionStorage;
@@ -52,12 +77,24 @@
}
}
+ // This function check the type. (This is private)
+ /**
+ * @private
+ */
function _checkArgument(type, argument) {
var object = Object.prototype.toString.call(argument).slice(8, -1);
return argument !== undefined && argument !== null && object === type;
}
+ /**
+ * You can set object into the storage.
+ * @param {Object} obj Object which you want to save the storage.
+ * @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
+ * @return {Void} Return nothing.
+ * @example When you'll save object in localStorage.
+ * RS.put({foo: 'hoge', bar: 'fuga', baz: 'hogera'});
+ */
function put(obj, flag) {
var storage = _selectStorage(flag, 'put()');
@@ -68,10 +105,21 @@
}
}
} else {
+ /**
+ * @throws {Error} If first argument isn't object.
+ */
throw new Error('RS.put(): 1st argument should be object');
}
}
+ /**
+ * You can get string from the storage.
+ * @param {String} key String which you want to get from the storage.
+ * @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
+ * @return {String} Return value of matched in storage.
+ * @example When you'll get value in localStorage.
+ * RS.get('foo');
+ */
function get(key, flag) {
var storage = _selectStorage(flag, 'get()'),
selectKey = storage.getItem(key);
@@ -83,10 +131,21 @@
return console.log('RS.get(): This key is not in storage');
}
} else {
+ /**
+ * @throws {Error} If first argument isn't string.
+ */
throw new Error('RS.get(): 1st argument should be string');
}
}
+ /**
+ * You can remove object from the storage.
+ * @param {String} key String which you want to remove from storage.
+ * @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
+ * @return {Void} Return nothing.
+ * @example When you'll remove key & value in localStorage.
+ * RS.remove('foo');
+ */
function remove(key, flag) {
var storage = _selectStorage(flag, 'remove()'),
selectKey = storage.getItem(key);
@@ -100,12 +159,27 @@
}
}
+ /**
+ * You can remove all object in the storage.
+ * @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
+ * @return {Void} Return nothing.
+ * @example When you'll remove all of key & value in localStorage.
+ * RS.clear();
+ */
function clear(flag) {
var storage = _selectStorage(flag, 'clear()');
storage.clear();
}
+ /**
+ * You can check existance of a object in the storage.
+ * @param {String} key String which you want to know in storage.
+ * @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
+ * @return {Boolean} Return true or false.
+ * @example When you'll check a key in localStorage.
+ * RS.is('foo');
+ */
function is(key, flag) {
var storage = _selectStorage(flag, 'is()'),
selectKey = storage.getItem(key);
@@ -117,6 +191,9 @@
return false;
}
} else {
+ /**
+ * @throws {Error} If first argument isn't string.
+ */
throw new Error('RS.is(): 1st argument should be strings');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.