Skip to content

Instantly share code, notes, and snippets.

@chernomyrdin
Created February 21, 2014 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernomyrdin/9135130 to your computer and use it in GitHub Desktop.
Save chernomyrdin/9135130 to your computer and use it in GitHub Desktop.
/**
* @output_file_name default.js
* @compilation_level ADVANCED_OPTIMIZATIONS
* @formatting pretty_print
*/
/**
* Sample location parameter access classs
*/
/**
* @constructor
*/
function zParam (id) {
this.id = id;
}
/**
* Установить параметр QueryString
*
* @param {string} str Query string
* @expose
*/
zParam.prototype.setQueryString = function (str) {
if (str.length) {
this.qs = ("?" == str.substr(1,1)) ? "" : "?";
this.qs += str;
}
else {
this.qs = str;
}
return this;
};
/**
* Установить параметр CookieString
*
* @param {string} str Cookie string
* @expose
*/
zParam.prototype.setCookieString = function (str) {
this.cs = str;
return this;
};
/**
* Разделить параметры на части
*
* @protected
* @param {RegExp} re Text to split
* @param {string} text Text to split
* @return {Object.<string, string>} The hex ID
*/
zParam.prototype.keyValue = function (re, text) {
var rc = {};
text.replace(
re,
function (match, name, value /*, offset, text */ ) {
rc[name] = value;
}
);
return rc;
}
/**
* Получить параметр по имени
*
* @param {string} name Parameter name
* @return {string|boolean}
* @expose
*/
zParam.prototype.getParam = function (name) {
if (typeof this._param === "undefined") {
this._param = this.keyValue(
/[?&]([^=&]+)=?([^&]*)/g,
(typeof this.qs === "undefined") ? location.search : this.qs
);
}
return (this._param.hasOwnProperty(name)
? this._param[name]
: false
);
};
/**
* Получить отдельную cookie по ее имени
*
* @param {string} name Cookie name
* @return {string|boolean}
* %expose
*/
zParam.prototype.getCookie = function (name) {
if (typeof this._cookie == "undefined") {
this._cookie = this.keyValue(
/\b([^=]+)=([^;]*)/g, // @todo token=( token | quoted-string )
(typeof this.cs === "undefined") ? document.cookie : this.cs
);
}
return (this._cookie.hasOwnProperty(name)
? this._cookie[name]
: false
);
};
/*
* Тут мы начинаем тестировать все этой хозяйство
*
*/
var app1 = new zParam(1),
app2 = new zParam(2),
qs = location.search.length ? location.search.substr(1) : "order=42&status=ok",
cs = document.cookie.length ? document.cookie : "lang=ua; city=Kiev";
app1.setQueryString(qs).setCookieString(cs);
app2.setQueryString(qs).setCookieString(cs);
alert(
[
"Parameter[order]=" + app1.getParam("order"),
"Parameter[status]=" + app2.getParam("status"),
"Cookie[lang]=" + app1.getCookie("lang"),
"Cookie[city]=" + app2.getCookie("city")
].join("\n")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment