UNPKG

tastypie

Version:

Tastypie is a webservice API framework for Node.js based on Django's Tastypie Framework. It provides a convenient, yet powerful and highly customizable, abstraction for creating REST-style interfaces

28 lines (24 loc) 838 B
var forOwn = require('../object/forOwn'); var isArray = require('../lang/isArray'); var forEach = require('../array/forEach'); /** * Encode object into a query string. */ function encode(obj){ var query = [], arrValues, reg; forOwn(obj, function (val, key) { if (isArray(val)) { arrValues = key + '='; reg = new RegExp('&'+key+'+=$'); forEach(val, function (aValue) { arrValues += encodeURIComponent(aValue) + '&' + key + '='; }); query.push(arrValues.replace(reg, '')); } else { query.push(key + '=' + encodeURIComponent(val)); } }); return (query.length) ? '?' + query.join('&') : ''; } module.exports = encode;