Created
September 5, 2017 12:52
Star
You must be signed in to star a gist
tost.js
This file contains 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
/** | |
* @module tost | |
* @author eray arslan | |
* @date may 2015 | |
* | |
* var sucuklu = new tost([]); | |
* | |
*/ | |
(function () { | |
// constants | |
var root = this; | |
var splitter = "-"; | |
var errors = { | |
"undefined_data": "undefined data", | |
"key_not_found": "key not found" | |
}; | |
var strip_comments = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
var argument_names = /([^\s,]+)/g; | |
// undefined check | |
var isUndefined = function (o) { | |
return typeof o === "undefined"; | |
}; | |
// constructor | |
var tost = function (o) { | |
this.o = o; | |
}; | |
// node.js or commonjs support | |
if (typeof exports !== "undefined") { | |
if (typeof module !== "undefined" && module.exports) { | |
exports = module.exports = tost; | |
} | |
exports.tost = tost; | |
} else { | |
root.tost = tost; | |
} | |
// text to base64 | |
var utf8_to_b64 = function (str) { | |
if (typeof window === "undefined") { | |
return new Buffer(str.toString()).toString('base64'); | |
} else { | |
return window.btoa(str); | |
} | |
}; | |
// base64 to text | |
var b64_to_utf8 = function (str) { | |
if (typeof window === "undefined") { | |
return new Buffer(str.toString(), 'base64').toString('ascii'); | |
} else { | |
return window.atob(str); | |
} | |
}; | |
// function str params parser | |
var getParamNames = function (func) { | |
var fnStr = func.toString().replace(strip_comments, ''); | |
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(argument_names); | |
if (result === null) { | |
result = []; | |
} | |
return result; | |
}; | |
var readData = function (obj, key) { | |
var result = obj[key]; | |
if (isUndefined(result)) { | |
throw new Error(errors.key_not_found); | |
} | |
var value = b64_to_utf8(result); | |
var arr = value.split(splitter); | |
var data = arr[0]; | |
var type = arr[1]; | |
var maybe = arr[2] || "[]"; | |
if (type === 'number') { | |
return parseFloat(data); | |
} else if (type === 'boolean' || type === 'object') { | |
return JSON.parse(data); | |
} else if (type === 'function') { | |
return new Function(JSON.parse(maybe), data.substring(data.indexOf("{") + 1, data.lastIndexOf("}"))); | |
} else if (type === 'undefined') { | |
throw new Error("undefined data"); | |
} else { | |
return data; | |
} | |
}; | |
var writeDate = function (obj, key, value) { | |
if (typeof value === 'object') { | |
obj[key] = utf8_to_b64(JSON.stringify(value) + "-" + typeof value); | |
} else if (typeof value === 'function') { | |
obj[key] = utf8_to_b64(value.toString() + "-" + typeof value + "-" + JSON.stringify(getParamNames(value))); | |
} else if (isUndefined(value)) { | |
throw new Error(errors.undefined_data); | |
} else { | |
obj[key] = utf8_to_b64(value + "-" + typeof value); | |
} | |
}; | |
tost.prototype.get = function (key, callback) { | |
if (!isUndefined(callback)) { | |
callback(readData(this.o, key)); | |
} else { | |
return readData(this.o, key); | |
} | |
}; | |
tost.prototype.set = function (key, value, callback) { | |
writeDate(this.o, key, value); | |
if (!isUndefined(callback)) { | |
callback(); | |
} | |
return this; | |
}; | |
// amd support | |
if (typeof define === 'function' && define.amd) { | |
define('tost', [], function () { | |
return tost; | |
}); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment