git ssb

0+

dangerousbeans / yap



forked from Dominic / yap

Commit c8b6c573917bfcfaade1205400b4a5ef965bad06

cleanOpts, and tests. urls shouldn't include undefined fields

Dominic Tarr committed on 2/23/2019, 12:31:23 AM
Parent: 51b1b442305e52aed195c823d4ee96551f8b64cb

Files changed

test/util.jsadded
util.jschanged
test/util.jsView
@@ -1,0 +1,28 @@
1+
2+var tape = require('tape')
3+
4+var u = require('../util')
5+
6+tape('clean', function (t) {
7+ t.deepEqual(u.cleanOpts({
8+ private: undefined,
9+ content: {channel: 'foo'}
10+ }),
11+ {content: {channel: 'foo'}}
12+ )
13+ t.deepEqual(u.cleanOpts({one: 1}),
14+ {one: 1}
15+ )
16+ t.deepEqual(u.cleanOpts({undef: undefined}),
17+ undefined
18+ )
19+
20+ t.end()
21+})
22+
23+
24+
25+
26+
27+
28+
util.jsView
@@ -1,6 +1,6 @@
11 var URL = require('url')
2-var QS = require('querystring')
2+var QS = require('qs')
33 var hyperscript = require('hyperscript')
44 var cpara = require('cont').para
55 var pull = require('pull-stream')
66 var paramap = require('pull-paramap')
@@ -22,14 +22,38 @@
2222 function isString (s) {
2323 return 'string' === typeof s
2424 }
2525
26+function cleanOpts (opts) {
27+ return (function copy (opts) {
28+ var o = undefined
29+ if(opts && 'object' === typeof opts)
30+ for(var k in opts) {
31+ if('undefined' !== typeof opts[k]) {
32+ var v = copy(opts[k])
33+ if('undefined' !== typeof v) {
34+ o = o || {}
35+ o[k] = v
36+ }
37+ }
38+ }
39+ else
40+ return opts //a string, or other value
41+ return o
42+ })(opts)
43+}
44+
45+function encodeOpts (opts) {
46+ opts = cleanOpts(opts)
47+ return opts ? '?'+QS.stringify(opts) : ''
48+}
49+
50+exports.cleanOpts = cleanOpts
51+
2652 exports.toUrl = function toUrl(path, opts) {
2753 return '/'+(
2854 Array.isArray(path) ? path.join('/') : ''+path
29- ) + (
30- !isEmpty(opts) ? '?'+QS.encode(opts) : ''
31- )
55+ ) + encodeOpts(opts)
3256 }
3357 exports.h = function () {
3458 return [].slice.call(arguments)
3559 }
@@ -219,4 +243,11 @@
219243
220244
221245
222246
247+
248+
249+
250+
251+
252+
253+

Built with git-ssb-web