Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // o.hasOwnProperty можно переопределить
- function hasOwn(o, p) {
- return Object.prototype.hasOwnProperty.call(o, p);
- }
- function is(t, o) {
- return Object.prototype.toString.call(o) == '[object ' + t + ']';
- }
- function isObject(o) {
- return is('Object', o);
- }
- // NOTE: undefined, NaN, Infinity можно переопределить
- // typeof 'foo' == 'string', but typeof String('foo') == 'object'
- // Примитивы, которые получают тип 'object' при создании через конструктор
- function isNumber(o) {
- return is('Number', o);
- }
- function isString(o) {
- return is('String', o);
- }
- function isBoolean(o) {
- return is('Boolean');
- }
- // встроенная функция всяко быстрее
- function isArray(o) {
- return Array.isArray(o);
- }
- function isObjectOrArray(o) {
- return isObject(o) || isArray(o);
- }
- function buildQuery(params) {
- if (!isObjectOrArray(params)) {
- throw new TypeError('must be object or array');
- }
- return build(params);
- function build(data, prefix, undef) {
- var r = []
- , k
- , v;
- for (var k in data) {
- if (hasOwn(data, k)) {
- v = data[k];
- k = encodeURIComponent(k);
- k = prefix === undef ? k : prefix + '[' + k + ']';
- r.push(isObjectOrArray(v) ? build(v, k) : k + '=' + encodeURIComponent(v))
- }
- }
- return r.join('&');
- }
- }
- buildQuery({
- q: 'тест',
- __ts: new Date().getTime(),
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement