-
Notifications
You must be signed in to change notification settings - Fork 6
/
rockstage.js
199 lines (179 loc) · 5.97 KB
/
rockstage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*! rockstage.js - v0.0.5 - 2012-12-14
* http://layzie.github.com/rockstage_js
* Copyright (c) 2012 HIRAKI Satoru; Licensed MIT */
(function(window, undefined) {
'use strict';
/**
* This function check the type. (This is private)
* @private
* @param {String} type String which you want to check type.
* @param argument Any type want to compare to type.
* @return {Boolean} Return type and argument is equal then return true.
*/
function _checkArgument(type, argument) {
var object = Object.prototype.toString.call(argument).slice(8, -1);
return argument !== undefined && argument !== null && object === type;
}
/**
* This function is select the storage. True or undefined is
* 'localStorage', false is 'sessionStorage'. (This is private)
* @private
* @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
* @param {String} fun String which want to retun error.
* @return {Object} Return localStorage or sessionStorage.
*/
function _selectStorage(flag, fun) {
var order = (fun !== 'clear()') ? '2nd ' : '',
storage;
if (_checkArgument('Boolean', flag) || flag === undefined) {
storage = (flag === true || flag === undefined) ? localStorage : sessionStorage;
return storage;
} else {
/**
* @throws {Error} If second argument(only argyment in RS.clear()) isn't boolean.
*/
throw new Error('RS.' + fun + ': ' + order + 'argument should be boolean');
}
}
/**
* 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()');
if (_checkArgument('Object', obj) && !_checkArgument('Array', obj)) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
storage.setItem(i, JSON.stringify(obj[i]));
}
}
} 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);
if (_checkArgument('String', key)) {
if (selectKey) {
return JSON.parse(selectKey);
} else {
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);
if (_checkArgument('String', key)) {
if (selectKey) {
storage.removeItem(key);
} else {
console.log('RS.remove(): This key is not in storage');
}
}
}
/**
* 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);
if (_checkArgument('String', key)) {
if (selectKey) {
return true;
} else {
return false;
}
} else {
/**
* @throws {Error} If first argument isn't string.
*/
throw new Error('RS.is(): 1st argument should be strings');
}
}
/**
* You can check existance of a object in the storage.
* @param {Boolean} flag Boolean which the storage you want to use. Default is 'true'.
* @return {Number} Return object's length in storage
* @example When you'll check object's length in localStorage.
* RS.length();
*/
function length(flag) {
var storage = _selectStorage(flag, 'length()');
return storage.length;
}
/**
* This function is called when 'RS' will be loaded. Then check the storageprivate
* and set 'RS' name space in 'window'. (This is private)
* @private
* @return {Void} Return nothing
*/
(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,
remove: remove,
clear: clear,
is: is,
length: length
};
} else {
/**
* @throws {Error} If a browser have no storage.
*/
throw new Error('RS: This browser have no storage.');
}
}());
}(this));