From fdf428671f4b09fe14d2e7dae16b201c05d47f20 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 18 Feb 2020 14:59:22 -0600 Subject: [PATCH 1/6] add url encoding/decoding --- test.js | 4 +++- url.js | 31 +++++++++++++++++++++++++++++++ url.test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 url.js create mode 100644 url.test.js diff --git a/test.js b/test.js index 04f3d84..ee0ea63 100644 --- a/test.js +++ b/test.js @@ -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' @@ -50,7 +51,8 @@ runTests({ math, number, buffer, - sort + sort, + url }).then(success => { /* istanbul ignore next */ if (isNode) { diff --git a/url.js b/url.js new file mode 100644 index 0000000..70b1901 --- /dev/null +++ b/url.js @@ -0,0 +1,31 @@ +import * as object from './object.js' + +/** + * Parse query parameters from an url. + * + * @param {string} url + * @return {Object} + */ +export const decodeQueryParams = url => { + /** + * @type {Object} + */ + 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} params + * @return {string} + */ +export const encodeQueryParams = params => + object.map(params, (val, key) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`).join('&') diff --git a/url.test.js b/url.test.js new file mode 100644 index 0000000..bd177e1 --- /dev/null +++ b/url.test.js @@ -0,0 +1,26 @@ +import * as t from './testing.js' +import * as url from './url.js' + +/** + * @param {Object} 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')) +} From cf559af4d97ca8b846cbebd605542fbbf91dd5e0 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 18 Feb 2020 15:02:17 -0600 Subject: [PATCH 2/6] 0.2.15 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5b6236d..aa54b80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "lib0", - "version": "0.2.14", + "version": "0.2.15", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2400f72..405cb2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lib0", - "version": "0.2.14", + "version": "0.2.15", "description": "", "sideEffects": false, "type": "module", From 35e8fec40e93d9d95078fb01ad56d579abefbc06 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 19 Feb 2020 21:32:22 -0600 Subject: [PATCH 3/6] fix testing repeat location url --- testing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing.js b/testing.js index 34c3f86..0ec8af7 100644 --- a/testing.js +++ b/testing.js @@ -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)` From 3a73d7ef2d09e706f5a81f711242e6df91679f56 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 19 Feb 2020 21:32:22 -0600 Subject: [PATCH 4/6] fix testing repeat location url --- testing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing.js b/testing.js index 34c3f86..0ec8af7 100644 --- a/testing.js +++ b/testing.js @@ -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)` From d719a4af26a161e4363bd207728d3129e341e7d6 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 19 Feb 2020 21:45:46 -0600 Subject: [PATCH 5/6] dont enforce coverage of slow code :p --- promise.js | 1 + 1 file changed, 1 insertion(+) diff --git a/promise.js b/promise.js index b81fd79..03ab1df 100644 --- a/promise.js +++ b/promise.js @@ -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')) From 1933d34ab5d60b8d59d79e20726f7f9c75e57fc5 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 19 Feb 2020 21:47:54 -0600 Subject: [PATCH 6/6] 0.2.16 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index aa54b80..6bbe31a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "lib0", - "version": "0.2.15", + "version": "0.2.16", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 405cb2d..62dffae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lib0", - "version": "0.2.15", + "version": "0.2.16", "description": "", "sideEffects": false, "type": "module",