@vbarbarosh/node-helpers 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/redis_poll_zshift_spawn_rpush.js +1 -1
- package/src/urlmod.js +35 -0
- package/src/urlmod.test.js +35 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const Promise = require('bluebird');
|
|
2
2
|
const child_process = require('child_process');
|
|
3
3
|
const fs_path_resolve = require('./fs_path_resolve');
|
|
4
|
-
const fs_tempdir = require('./
|
|
4
|
+
const fs_tempdir = require('./fs_tempdir');
|
|
5
5
|
const fs_write_json = require('./fs_write_json');
|
|
6
6
|
const random_int = require('./random_int');
|
|
7
7
|
const redis_zshift = require('./redis_zshift');
|
package/src/urlmod.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set, change, or remove query string parameters.
|
|
3
|
+
*
|
|
4
|
+
* urlmod('', {a: 1}) '?a=1' set
|
|
5
|
+
* urlmod('?a=1', {a: 2}) '?a=2' change
|
|
6
|
+
* urlmod('?a=1', {a: null}) '' remove
|
|
7
|
+
*/
|
|
8
|
+
function urlmod(url, params)
|
|
9
|
+
{
|
|
10
|
+
const tmp_url = new URL(url||'', 'xxx://___base___/');
|
|
11
|
+
const tmp_search = tmp_url.searchParams;
|
|
12
|
+
Object.entries(params || {}).forEach(function ([key, value]) {
|
|
13
|
+
switch (value) {
|
|
14
|
+
case null:
|
|
15
|
+
case undefined:
|
|
16
|
+
tmp_search.delete(key);
|
|
17
|
+
break;
|
|
18
|
+
case true:
|
|
19
|
+
tmp_search.set(key, 1);
|
|
20
|
+
break;
|
|
21
|
+
case false:
|
|
22
|
+
tmp_search.set(key, 0);
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
tmp_search.set(key, value);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
if (url && url[0] === '/') {
|
|
30
|
+
return tmp_url.toString().replace(/^xxx:\/\/___base___/, '');
|
|
31
|
+
}
|
|
32
|
+
return tmp_url.toString().replace(/^xxx:\/\/___base___\//, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = urlmod;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const urlmod = require('./urlmod');
|
|
3
|
+
|
|
4
|
+
describe('urlmod', function () {
|
|
5
|
+
it('should take no args', function () {
|
|
6
|
+
assert.deepEqual(urlmod(), '');
|
|
7
|
+
});
|
|
8
|
+
it('should take basic args', function () {
|
|
9
|
+
assert.deepEqual(urlmod('?a=1'), '?a=1');
|
|
10
|
+
assert.deepEqual(urlmod('?a=1', {a: 2}), '?a=2');
|
|
11
|
+
assert.deepEqual(urlmod('?a=1', {a: null}), '');
|
|
12
|
+
assert.deepEqual(urlmod('?a=1', {b: 1}), '?a=1&b=1');
|
|
13
|
+
assert.deepEqual(urlmod('?a=1', {b: 1, a: 2}), '?a=2&b=1');
|
|
14
|
+
assert.deepEqual(urlmod('?a=1', {a: true}), '?a=1');
|
|
15
|
+
assert.deepEqual(urlmod('?a=1', {a: false, b: true}), '?a=0&b=1');
|
|
16
|
+
});
|
|
17
|
+
it('should take `location`', function () {
|
|
18
|
+
// const elem = document.createElement('A');
|
|
19
|
+
// elem.href = 'http://www.example.com/some/path?a=1';
|
|
20
|
+
// const location = elem.href;
|
|
21
|
+
const location = {
|
|
22
|
+
toString: function () {
|
|
23
|
+
return 'http://www.example.com/some/path?a=1';
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
assert.deepEqual(urlmod(location, {b: 2}), 'http://www.example.com/some/path?a=1&b=2');
|
|
27
|
+
});
|
|
28
|
+
it('should keep hash part', function () {
|
|
29
|
+
assert.deepEqual(urlmod('#ex', {b: 2}), '?b=2#ex');
|
|
30
|
+
assert.deepEqual(urlmod('#ex?a=1#ex', {b: 2}), '?b=2#ex?a=1#ex');
|
|
31
|
+
assert.deepEqual(urlmod('?a=1#ex', {b: 2}), '?a=1&b=2#ex');
|
|
32
|
+
assert.deepEqual(urlmod('/#ex', {b: 2}), '/?b=2#ex');
|
|
33
|
+
assert.deepEqual(urlmod('http://www.example.com/some/path?a=1#ex', {b: 2}), 'http://www.example.com/some/path?a=1&b=2#ex');
|
|
34
|
+
});
|
|
35
|
+
});
|