/
util.js
30 lines (26 loc) · 798 Bytes
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const extend = require('xtend/mutable')
const url = require('url')
module.exports = exports = require('./util-node')
exports.addCommas = require('add-commas')
/**
* Return a random number between 0 (inclusive) and `max` (exclusive).
* @param {number} max defaults to 100
* @return {number}
*/
exports.randomInt = function (max) {
if (max === undefined) max = 100
return Math.floor(Math.random() * max)
}
/**
* Add query parameters to a URL.
* Example: addQueryParams('http://host.com/?a=1', { b: 2 })
* Returns: 'http://host.com/?a=1&b=2'
* @param {string} u
* @param {Object} params
*/
exports.addQueryParams = function (u, params) {
u = url.parse(u, true) // eslint-disable-line node/no-deprecated-api
extend(u.query, params)
u.search = null
return url.format(u)
}