Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dmonad/lib0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 26, 2020
2 parents 44442e1 + 1933d34 commit a1a15ee
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lib0",
"version": "0.2.14",
"version": "0.2.16",
"description": "",
"sideEffects": false,
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const until = (timeout, check) => create((resolve, reject) => {
clearInterval(intervalHandle)
resolve()
} else if (hasTimeout) {
/* istanbul ignore else */
if (time.getUnixTime() - startTime > timeout) {
clearInterval(intervalHandle)
reject(new Error('Timeout'))
Expand Down
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as math from './math.test.js'
import * as number from './number.test.js'
import * as buffer from './buffer.test.js'
import * as sort from './sort.test.js'
import * as url from './url.test.js'

import { isBrowser, isNode } from './environment.js'

Expand Down Expand Up @@ -50,7 +51,8 @@ runTests({
math,
number,
buffer,
sort
sort,
url
}).then(success => {
/* istanbul ignore next */
if (isNode) {
Expand Down
2 changes: 1 addition & 1 deletion testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const run = async (moduleName, name, f, i, numberOfTests) => {
times.sort((a, b) => a - b)
/* istanbul ignore next */
const againMessage = env.isBrowser
? ` - ${window.location.protocol}//${window.location.host}?filter=\\[${i + 1}/${tc._seed === null ? '' : `&seed=${tc._seed}`}`
? ` - ${window.location.href}?filter=\\[${i + 1}/${tc._seed === null ? '' : `&seed=${tc._seed}`}`
: `\nrepeat: npm run test -- --filter "\\[${i + 1}/" ${tc._seed === null ? '' : `--seed ${tc._seed}`}`
const timeInfo = (repeat && err === null)
? ` - ${times.length} repititions in ${duration.toFixed(2)}ms (best: ${times[0].toFixed(2)}ms, worst: ${array.last(times).toFixed(2)}ms, median: ${statistics.median(times).toFixed(2)}ms, average: ${statistics.average(times).toFixed(2)}ms)`
Expand Down
31 changes: 31 additions & 0 deletions url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as object from './object.js'

/**
* Parse query parameters from an url.
*
* @param {string} url
* @return {Object<string,string>}
*/
export const decodeQueryParams = url => {
/**
* @type {Object<string,string>}
*/
const query = {}
const urlQuerySplit = url.split('?')
const pairs = urlQuerySplit[urlQuerySplit.length - 1].split('&')
for (var i = 0; i < pairs.length; i++) {
const item = pairs[i]
if (item.length > 0) {
const pair = item.split('=')
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '')
}
}
return query
}

/**
* @param {Object<string,string>} params
* @return {string}
*/
export const encodeQueryParams = params =>
object.map(params, (val, key) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`).join('&')
26 changes: 26 additions & 0 deletions url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as t from './testing.js'
import * as url from './url.js'

/**
* @param {Object<string,any>} params
*/
const paramTest = params => {
const out = url.decodeQueryParams(url.encodeQueryParams(params))
t.compareObjects(params, out, 'Compare params')
}

/**
* @param {t.TestCase} tc
*/
export const testUrlParamQuery = tc => {
paramTest({})
paramTest({ a: '4' })
paramTest({ a: 'dtrn', b: '0x0' })

t.compareObjects({ }, url.decodeQueryParams('http://localhost:8080/dtrn?'))
t.compareObjects({ a: 'ay' }, url.decodeQueryParams('http://localhost:8080/dtrn?a=ay'))
t.compareObjects({ a: '' }, url.decodeQueryParams('http://localhost:8080/dtrn?a='))
t.compareObjects({ a: '' }, url.decodeQueryParams('http://localhost:8080/dtrn?a'))
t.compareObjects({ a: 'ay' }, url.decodeQueryParams('http://localhost:8080/dtrn?a=ay&'))
t.compareObjects({ a: 'ay', b: 'bey' }, url.decodeQueryParams('http://localhost:8080/dtrn?a=ay&b=bey'))
}

0 comments on commit a1a15ee

Please sign in to comment.