codecamp

IO.js Query Strings

稳定度: 2 - 稳定

这个模块提供了处理 查询字符串 的工具。它提供了以下方法:

querystring.stringify(obj[, sep][, eq][, options])

序列化一个对象为一个查询字符串。可以可选地覆盖默认的分隔符('&')和赋值符号('=')。

options对象可以包含encodeURIComponent属性(默认为querystring.escape),它被用来在需要时,将字符串编码为非utf-8编码。

例子:

querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='

querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
// returns
'foo:bar;baz:qux'

// Suppose gbkEncodeURIComponent function already exists,
// it can encode string with `gbk` encoding
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
  { encodeURIComponent: gbkEncodeURIComponent })
// returns
'w=%D6%D0%CE%C4&foo=bar'

querystring.parse(str[, sep][, eq][, options])

反序列化一个查询字符串为一个对象。可以可选地覆盖默认的分隔符('&')和赋值符号('=')。

options可以包含maxKeys属性(默认为1000)。它被用来限制被处理的键。将其设置为0会移除限制。

options可以包含decodeURIComponent属性(默认为querystring.unescape),它被用来在需要时,解码非uft8编码字符串。

例子:

querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }

// Suppose gbkDecodeURIComponent function already exists,
// it can decode `gbk` encoding string
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
  { decodeURIComponent: gbkDecodeURIComponent })
// returns
{ w: '中文', foo: 'bar' }

querystring.escape

querystring.stringify使用的转义函数,在需要时可以被覆盖。

querystring.unescape

querystring.parse使用的反转义函数,在需要时可以被覆盖。

首先它会尝试使用decodeURIComponent,但是如果失败了,它就转而使用一个不会在畸形URL上抛出错误的更安全的等价方法。

IO.js Punycode
IO.js Readline
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }