Skip to content

Commit

Permalink
fix: #453 param parser
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWells committed Dec 11, 2024
1 parent 505f077 commit 18c3671
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
3 changes: 3 additions & 0 deletions packages/analytics-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"unpkg": "dist/analytics-utils.umd.js",
"sideEffects": false,
"scripts": {
"test": "uvu tests -r esm",
"start": "npm run sync && concurrently 'npm:watch:dev' 'npm:copy' 'npm:serve'",
"serve": "servor dist index.html 8081 --reload --browse",
"copy": "watchlist examples -- npm run sync",
Expand All @@ -43,8 +44,10 @@
"devDependencies": {
"concurrently": "^6.1.0",
"microbundle": "^0.13.0",
"esm": "^3.2.25",
"servor": "^4.0.2",
"typescript": "^4.2.3",
"uvu": "^0.5.1",
"watchlist": "^0.2.3"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics-utils/src/paramsParse.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function getParamsAsObject(query) {
var v = decodeUri(temp[2])
if (k.substring(k.length - 2) === '[]') {
k = k.substring(0, k.length - 2);
(params[k] || (params[k] = [])).push(v)
var arrVal = params[k] || (params[k] = [])
params[k] = Array.isArray(arrVal) ? arrVal : []
params[k].push(v)
} else {
params[k] = (v === '') ? true : v
}
Expand Down
47 changes: 33 additions & 14 deletions packages/analytics-utils/tests/paramParse.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import test from 'ava'
import {parseParam} from '../dist/paramsParse'
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { paramsParse } from '../src/paramsParse'

test('test simple params', t => {
function toPlainObject(obj) {
return JSON.parse(JSON.stringify(obj))
}

function _paramsParse(url) {
return toPlainObject(paramsParse(url))
}

test('test simple params', () => {
const url = 'http://localhost:3000/?hi=there&wow=lol'
const parsed = parseParam(url)
t.deepEqual(parsed, {
const parsed = _paramsParse(url)
assert.equal(parsed, {
hi: 'there',
wow: 'lol',
})
})

test('test boolean params', t => {
test('test boolean params', () => {
const url = 'http://localhost:3000/?hi&no=false'
const parsed = parseParam(url)
t.deepEqual(parsed, {
const parsed = _paramsParse(url)
assert.equal(parsed, {
hi: true,
no: 'false',
})
})

test('test utm params', t => {
test('Duplicate param keys', () => {
const url = 'http://localhost:3000/?foo=&foo[]='
const parsed = _paramsParse(url)
assert.equal(parsed, {
foo: [''],
})
})

test('test utm params', () => {
const url = 'http://localhost:3000/http://glocal.dev/?utm_source=the_source&utm_medium=camp%20med&utm_term=Bought%20keyword&utm_content=Funny%20Text&utm_campaign=400kpromo'
const parsed = parseParam(url)
t.deepEqual(parsed, {
const parsed = _paramsParse(url)
assert.equal(parsed, {
utm_campaign: '400kpromo',
utm_content: 'Funny Text',
utm_medium: 'camp med',
Expand All @@ -31,12 +48,12 @@ test('test utm params', t => {
})
})

test('test deeply nested url values', t => {
test('test deeply nested url values', () => {
const url =
'http://localhost:3000/?Target=Offer&Method=findAll&filters[has_goals_enabled][TRUE]=1&filters[status]=active&filters[otherthing]&filters[wow]arr[]=yaz&filters[wow]arr[]=naz&filters[wow]arr[]=[other]&fields[]=id&fields[]=name&fields[]=default_goal_name&yes'

const parsed = parseParam(url)
t.deepEqual(parsed, {
const parsed = _paramsParse(url)
assert.equal(parsed, {
Target: 'Offer',
Method: 'findAll',
fields: ['id', 'name', 'default_goal_name'],
Expand All @@ -49,3 +66,5 @@ test('test deeply nested url values', t => {
},
})
})

test.run()
22 changes: 16 additions & 6 deletions pnpm-lock.yaml

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

0 comments on commit 18c3671

Please sign in to comment.