@igorkosta/urlparams 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.README.md.un~ +0 -0
- package/.index.js.un~ +0 -0
- package/.package.json.un~ +0 -0
- package/.test.js.un~ +0 -0
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/index.js +11 -0
- package/package.json +18 -0
- package/test.js +46 -0
package/.README.md.un~
ADDED
|
Binary file
|
package/.index.js.un~
ADDED
|
Binary file
|
|
Binary file
|
package/.test.js.un~
ADDED
|
Binary file
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Igor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @igorkosta/urlparams
|
|
2
|
+
|
|
3
|
+
A tiny utility that takes a URL and returns its search (query) parameters as a plain JavaScript object.
|
|
4
|
+
|
|
5
|
+
Useful for parsing URLs, handling query strings, or normalizing request parameters in Node.js and browser environments.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @igorkosta/urlparams
|
|
13
|
+
|
|
14
|
+
## Basic Example
|
|
15
|
+
|
|
16
|
+
```node
|
|
17
|
+
import urlparams from "@igorkosta/urlparams";
|
|
18
|
+
|
|
19
|
+
const params = urlparams("https://example.com/page?foo=bar&baz=123");
|
|
20
|
+
|
|
21
|
+
console.log(params);
|
|
22
|
+
// {
|
|
23
|
+
// foo: "bar",
|
|
24
|
+
// baz: "123"
|
|
25
|
+
// }
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## With encoded values
|
|
29
|
+
|
|
30
|
+
```node
|
|
31
|
+
import urlparams from "@igorkosta/urlparams";
|
|
32
|
+
|
|
33
|
+
const params = urlparams("https://example.com/?q=hello%20world&lang=en");
|
|
34
|
+
|
|
35
|
+
console.log(params);
|
|
36
|
+
// {
|
|
37
|
+
// q: "hello world",
|
|
38
|
+
// lang: "en"
|
|
39
|
+
// }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Repeated parameters
|
|
43
|
+
|
|
44
|
+
```node
|
|
45
|
+
import urlparams from "@igorkosta/urlparams";
|
|
46
|
+
|
|
47
|
+
const params = urlparams("https://example.com/?tag=js&tag=node&tag=web");
|
|
48
|
+
|
|
49
|
+
console.log(params);
|
|
50
|
+
// {
|
|
51
|
+
// tag: ["js", "node", "web"]
|
|
52
|
+
// }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Without parameters
|
|
56
|
+
|
|
57
|
+
```node
|
|
58
|
+
import urlparams from "@igorkosta/urlparams";
|
|
59
|
+
|
|
60
|
+
const params = urlparams("https://example.com/");
|
|
61
|
+
|
|
62
|
+
console.log(params);
|
|
63
|
+
// {}
|
|
64
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function urlparams(url) {
|
|
2
|
+
const params = new URL(url).searchParams;
|
|
3
|
+
const result = {};
|
|
4
|
+
|
|
5
|
+
for (const key of params.keys()) {
|
|
6
|
+
const values = params.getAll(key);
|
|
7
|
+
result[key] = values.length > 1 ? values : values[0];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return result;
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@igorkosta/urlparams",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "URL Params as JSON Object",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"url",
|
|
7
|
+
"params",
|
|
8
|
+
"json",
|
|
9
|
+
"object"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Igor Kostyuchenok <igor@kostyuchenok.com>",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "index.js",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "node test.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// test/index.test.js
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
|
|
5
|
+
import searchParams from "./index.js";
|
|
6
|
+
|
|
7
|
+
test("Get URL Params as JSON Object", () => {
|
|
8
|
+
const url = "https://example.com?foo=bar&baz=qux";
|
|
9
|
+
const result = searchParams(url);
|
|
10
|
+
assert.equal(typeof result, "object");
|
|
11
|
+
assert.deepStrictEqual({
|
|
12
|
+
foo: "bar",
|
|
13
|
+
baz: "qux"
|
|
14
|
+
}, result);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("With encoded values", () => {
|
|
18
|
+
const url = "https://example.com/?q=hello%20world&lang=en";
|
|
19
|
+
const result = searchParams(url);
|
|
20
|
+
console.log(result);
|
|
21
|
+
assert.equal(typeof result, "object");
|
|
22
|
+
assert.deepStrictEqual({
|
|
23
|
+
q: "hello world",
|
|
24
|
+
lang: "en"
|
|
25
|
+
}, result);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
test("Wit Repeated Parameters", () => {
|
|
30
|
+
const url = "https://example.com/?tag=js&tag=node&tag=web";
|
|
31
|
+
const result = searchParams(url);
|
|
32
|
+
console.log(result);
|
|
33
|
+
assert.equal(typeof result, "object");
|
|
34
|
+
assert.deepStrictEqual({
|
|
35
|
+
tag: ["js", "node", "web"]
|
|
36
|
+
}, result);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("Without Parameters", () => {
|
|
40
|
+
const url = "https://example.com/";
|
|
41
|
+
const result = searchParams(url);
|
|
42
|
+
console.log(result);
|
|
43
|
+
assert.equal(typeof result, "object");
|
|
44
|
+
assert.deepStrictEqual({
|
|
45
|
+
}, result);
|
|
46
|
+
});
|