Skip to content

Commit

Permalink
feat: delete storage.js
Browse files Browse the repository at this point in the history
  • Loading branch information
halldwang committed Mar 2, 2021
1 parent abea2c2 commit ca13695
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 375 deletions.
161 changes: 13 additions & 148 deletions dist/bbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* bbo is a utility library of zero dependencies for javascript.
* (c) 2011 - 2021
* https://github.com/tnfe/bbo.git
* version 1.1.25
* version 1.1.26
*/

(function (global, factory) {
Expand Down Expand Up @@ -118,7 +118,7 @@
return getTag(func) === '[object Function]';
}

var version = '1.1.25';
var version = '1.1.26';

var globalObject = null;

Expand Down Expand Up @@ -1014,150 +1014,6 @@
return acc;
}, {});

/**
* Whether a string contains another string
*/
function containsWith(target, item) {
// discuss at: https://locutus.io/golang/strings/Contains
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: bbo.contains('Kevin', 'K')
// returns 1: true
return String(target).indexOf(item) !== -1;
}

/************************************************************************
* localStorage && sessionStorage
* Method for safely supporting localStorage sessionStorage 'setItem' 'getItem' 'removeItem' 'removeAll',
* Some extension method 'has' 'get' adn Store prefix
*************************************************************************/
var storage;

try {
var ulocalStorage = window.localStorage;
var ussesionStorage = window.sessionStorage;

class Storage {
constructor(options) {
var _options$type = options.type,
type = _options$type === void 0 ? 'local' : _options$type,
_options$prefix = options.prefix,
prefix = _options$prefix === void 0 ? 'bbo.storage' : _options$prefix,
_options$message = options.message,
message = _options$message === void 0 ? {
setItem: 'write in',
getItem: 'read',
removeAll: 'remove all',
removeItem: 'remove item'
} : _options$message;
this.prefix = prefix;
this.type = type;
this.message = message;

if (type === 'local') {
this._storage = ulocalStorage;
} else if (type === 'session') {
this._storage = ussesionStorage;
}
}

doItem(func, action) {
try {
if (isFunction(func)) {
return func();
}
} catch (err) {
this._warn(action);

return null;
}

return true;
}

setItem(key, value) {
if (isObject(key)) {
Object.keys(key).forEach((k, index) => {
this.doItem(() => this._storage.setItem(`${this.prefix}.${k}`, JSON.stringify(key[k])), 'setItem');
});
} else {
this.doItem(() => this._storage.setItem(`${this.prefix}.${key}`, JSON.stringify(value)), 'setItem');
}
}

has() {
for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) {
keys[_key] = arguments[_key];
}

return keys.every((key, index) => this._storage.getItem(`${this.prefix}.${key}`));
}

get() {
var result = {};

for (var _len2 = arguments.length, keys = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
keys[_key2] = arguments[_key2];
}

keys.forEach((key, index) => {
if (`${this._storage.getItem(`${this.prefix}.${key}`)}` !== 'null') {
try {
result[key] = JSON.parse(this._storage.getItem(`${this.prefix}.${key}`));
} catch (err) {
console.warn(this._warn('getItem'));
}
}
});
return result;
}

getItem(key) {
return this.doItem(() => JSON.parse(this._storage.getItem(`${this.prefix}.${key}`)), 'getItem');
}

removeAll() {
Object.keys(this._storage).forEach(k => {
if (containsWith(k, this.prefix)) {
this._remove(`${k}`);
}
});
}

removeItem() {
for (var _len3 = arguments.length, keys = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
keys[_key3] = arguments[_key3];
}

console.log(keys);
keys.forEach((key, index) => this.doItem(() => this._storage.removeItem(`${this.prefix}.${key}`), 'removeItem'));
}

_warn(action) {
var message = this.message;
console.warn(`Unable to ${message[action] || ''} ${this.type} Storage`);
}

_remove(keys) {
this.doItem(() => this._storage.removeItem(`${keys}`), 'removeItem');
}

}

storage = (_ref) => {
var type = _ref.type,
prefix = _ref.prefix;
return new Storage({
type: type,
prefix: prefix
});
};
} catch (e) {
storage = noop;
console.error(e);
}

var storage$1 = storage;

/**
* getUrlParam / deleteUrlParam
* From https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
Expand Down Expand Up @@ -2560,6 +2416,17 @@
return ignore ? str.toLowerCase() === item.toLowerCase() : str === item;
}

/**
* Whether a string contains another string
*/
function containsWith(target, item) {
// discuss at: https://locutus.io/golang/strings/Contains
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: bbo.contains('Kevin', 'K')
// returns 1: true
return String(target).indexOf(item) !== -1;
}

/**
* XSS string filtering
*/
Expand Down Expand Up @@ -3096,8 +2963,6 @@
deleteCookie: deleteCookie,
delCookie: deleteCookie,
parseCookie: parseCookie,
// storage
storage: storage$1,
// http
open: open,
getUrlParam: getUrlParam,
Expand Down
4 changes: 2 additions & 2 deletions dist/bbo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bbo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bbo",
"version": "1.1.25",
"version": "1.1.26",
"description": "bbo is a utility library of zero dependencies for javascript.",
"homepage": "https://tnfe.github.io/bbo",
"author": "halldwang",
Expand Down
5 changes: 0 additions & 5 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ import getCookie from './cookie/get_cookie';
import deleteCookie from './cookie/delete_cookie';
import parseCookie from './cookie/parse_cookie';

// storage
import storage from './storage/storage';

// http
import getUrlParam from './http/get_url_param';
import setUrlParam from './http/set_url_param';
Expand Down Expand Up @@ -284,8 +281,6 @@ export default {
deleteCookie,
delCookie: deleteCookie,
parseCookie,
// storage
storage,
// http
open,
getUrlParam,
Expand Down
124 changes: 0 additions & 124 deletions src/storage/storage.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version = '1.1.25';
const version = '1.1.26';

export default version;
Loading

0 comments on commit ca13695

Please sign in to comment.