@soleil-se/app-util 5.1.0 → 5.2.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/CHANGELOG.md CHANGED
@@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [5.1.0] - 2021-05-27
8
+ ## [5.2.0] - 2023-01-31
9
+
10
+ ### Changed
11
+
12
+ - New function `parseParams` to parse query parameters from an URL, URI or query string.
13
+
14
+ ## [5.1.0] - 2022-05-27
9
15
 
10
16
  ### Changed
11
17
 
@@ -14,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
20
  - All common constants and functions are now usable in hooks.
15
21
  - Deprecate `getViewUri` since it's not used in WebApps 2.
16
22
 
17
- ## [5.0.0] - 2021-02-08
23
+ ## [5.0.0] - 2022-02-08
18
24
 
19
25
  ### Changed
20
26
 
package/README.md CHANGED
@@ -29,12 +29,23 @@ Utility functions for WebApps.
29
29
  "type": "WebApp",
30
30
  "bundled": true
31
31
  }
32
-
33
32
  ```
34
33
 
35
34
  ## Install
36
35
 
37
- `yarn add @soleil-se/webapp-util`
36
+ ```sh
37
+ > npm install @soleil-se/webapp-util
38
+ > yarn add @soleil-se/webapp-util
39
+ ```
40
+
41
+ :::details Integrity check failed
42
+ If the following error occurs when installing with Yarn run `yarn install --update-checksums`.
43
+
44
+ ```
45
+ Integrity check failed for "@soleil-se/webapp-util" (computed integrity doesn...
46
+ ```
47
+
48
+ :::
38
49
 
39
50
  ## Migration
40
51
 
@@ -182,6 +193,23 @@ const queryString = stringifyParams({ foo: 'bar', num: 1 }, { addQueryPrefix: tr
182
193
  // ?foo=bar&num=1
183
194
  ```
184
195
 
196
+ ### parseParams(url) ⇒ `Object`
197
+
198
+ Parse an URL, URI or query string to an object containing its query parameters.
199
+
200
+ **Returns**: `Object` - Parsed parameters.
201
+
202
+ | Param | Type | Default | Description |
203
+ | --- | --- | --- | --- |
204
+ | url | `String` | | URL, URI or query string to be parsed, must start with or contain "?" |
205
+
206
+ ```js
207
+ import { parseParams } from '@soleil-se/webapp-util';
208
+
209
+ const params = parseParams('?foo=bar&arr[]=1&arr[]=2');
210
+ // { foo: 'bar', arr: [1, 2] }
211
+ ```
212
+
185
213
  ## Rendering
186
214
 
187
215
  * [Svelte](./docs/1.svelte.md)
package/common/index.js CHANGED
@@ -79,6 +79,37 @@ export function stringifyParams(params = {}, { addQueryPrefix = false } = {}) {
79
79
  return addQueryPrefix ? `?${qs}` : qs;
80
80
  }
81
81
 
82
+ /**
83
+ * Parse an URL or URI to an object containing its query parameters.
84
+ * @param {String} url URL or URI to be parsed, must start with or contain "?".
85
+ * @returns Object with parsed parameters.
86
+ */
87
+ export default function parseParams(url = '') {
88
+ const hasQuestion = url.indexOf('?') > -1;
89
+ if (!hasQuestion) return {};
90
+
91
+ let query = url.split('?')[1];
92
+ const hasHash = url.indexOf('#') > -1;
93
+ query = hasHash ? url.split('#')[0] : query;
94
+
95
+ return query.split('&').reduce((params, part) => {
96
+ let [key, value] = part.split('+').join(' ').split('=');
97
+ key = decodeURIComponent(key);
98
+ value = decodeURIComponent(value);
99
+
100
+ const isArray = key.endsWith('[]');
101
+ if (isArray) {
102
+ key = key.replace('[]', '');
103
+ value = params[key] ? [...params[key], value] : [value];
104
+ }
105
+
106
+ return {
107
+ ...params,
108
+ [key]: value,
109
+ };
110
+ }, {});
111
+ }
112
+
82
113
  /**
83
114
  * Get URI for a route, same as `getStandaloneUrl` in Sitevision router.
84
115
  * @param {string} route A route.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Utility and rendering functions for WebApps.",
5
5
  "main": "./common/index.js",
6
6
  "author": "Soleil AB",
@@ -14,6 +14,6 @@
14
14
  "peerDependencies": {
15
15
  "@sitevision/api": "^1.0.10"
16
16
  },
17
- "gitHead": "3ea46889cfbe3193420d079413e20793cb183252",
17
+ "gitHead": "903884ec52e502707ab62483d5f01b739a8f33c9",
18
18
  "dependencies": {}
19
19
  }