Skip to content

Commit

Permalink
Add querystringify test
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Sep 10, 2024
1 parent 2d96215 commit 3e5f7e2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 24 deletions.
1 change: 1 addition & 0 deletions .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ disable-coverage: true
coverage-report:
- none
reporter: terse
passes: true
plugin:
- "!@tapjs/spawn"
- "!@tapjs/stdin"
35 changes: 28 additions & 7 deletions packages/npm/querystringify/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
'use strict'

function decode(encoded) {
try {
return decodeURIComponent(encoded)
} catch {}
return undefined
}

function parse(query) {
if (typeof query !== 'string' || query.length === 0) {
return {}
}
return Object.fromEntries(
const result = {}
if (typeof query === 'string' && query.length > 0) {
new URLSearchParams(
query.charCodeAt(0) === 35 /*'#'*/ ? query.slice(1) : query
).entries()
)
).forEach((value, key_) => {
const key = decode(key_)
if (key === undefined || key in result) return
result[key] = decode(value)
})
}
return result
}

function stringify(obj, prefix = '') {
const params =
obj !== null && typeof obj === 'object'
? new URLSearchParams(obj).toString()
? new URLSearchParams(
Object.fromEntries(
Object.entries(obj).map(pairs => {
const { 1: value } = pairs
return value === null ||
value === undefined ||
(!value && isNaN(value))
? [pairs[0], '']
: pairs
})
)
).toString()
: ''
if (params.length === 0) {
return ''
Expand Down
2 changes: 1 addition & 1 deletion test/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const tapeBinPath = fs.realpathSync(path.join(binPath, 'tape'))
scripts: { test }
} = pkgJson

describe(pkgName, async () => {
describe(pkgName, () => {
it('should pass all unit tests', async () => {
const args = yargsParser(test)._.map(n =>
n === 'tape' ? tapeBinPath : stripQuotes(`${n}`)
Expand Down
17 changes: 3 additions & 14 deletions test/npm/define-properties/package-lock.json

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

4 changes: 2 additions & 2 deletions test/npm/define-properties/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"private": true,
"scripts": {
"prepare": "patch-package",
"prepare": "patch-package && npm-recursive-install --skip-root --includeDirectories 'node_modules/define-properties'",
"test": "tape 'node_modules/define-properties/test/**/*.js'"
},
"devDependencies": {
"define-properties": "git://github.com/ljharb/define-properties.git#v1.2.1"
"define-properties": "https://github.com/ljharb/define-properties/archive/d7a4db30214eb06d3997dc3e662c11dfe95b25bd.tar.gz"
}
}
3 changes: 3 additions & 0 deletions test/npm/querystringify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('@socketregistry/monorepo/packages/npm/querystringify')
19 changes: 19 additions & 0 deletions test/npm/querystringify/package-lock.json

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

10 changes: 10 additions & 0 deletions test/npm/querystringify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"private": true,
"scripts": {
"prepare": "patch-package && npm-recursive-install --skip-root --includeDirectories 'node_modules/querystringify'",
"test": "node_modules/querystringify/node_modules/.bin/mocha 'node_modules/querystringify/test.js'"
},
"devDependencies": {
"querystringify": "https://github.com/unshiftio/querystringify/archive/73db95a504f988dce3f790e174e298ceb2b46a8e.tar.gz"
}
}
12 changes: 12 additions & 0 deletions test/npm/querystringify/patches/querystringify+2.2.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/node_modules/querystringify/index.js b/node_modules/querystringify/index.js
index 58c9808..eee9b3b 100644
--- a/node_modules/querystringify/index.js
+++ b/node_modules/querystringify/index.js
@@ -1,5 +1,7 @@
'use strict';

+module.exports = require(require('node:path').resolve(`${__dirname}/../../`));
+return
var has = Object.prototype.hasOwnProperty
, undef;

0 comments on commit 3e5f7e2

Please sign in to comment.