• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adobe / reactor-turbine / 9668242631

25 Jun 2024 07:10PM UTC coverage: 97.472% (+0.004%) from 97.468%
9668242631

push

github

web-flow
Remove deprecated querystring dependency. (#170)

* Remove deprecated querystring dependency.

* Fix Safari error.

* Try to simplify code.

* Add ignore files.

314 of 336 branches covered (93.45%)

Branch coverage included in aggregate %.

31 of 32 new or added lines in 1 file covered. (96.88%)

804 of 811 relevant lines covered (99.14%)

12.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/coreModulePackages/query-string/index.js
1
/***************************************************************************************
2
 * (c) 2017 Adobe. All rights reserved.
3
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License. You may obtain a copy
5
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
 *
7
 * Unless required by applicable law or agreed to in writing, software distributed under
8
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
 * OF ANY KIND, either express or implied. See the License for the specific language
10
 * governing permissions and limitations under the License.
11
 ****************************************************************************************/
12
'use strict';
13

14
/**
15
 * Parse a query string into an object. Leading `?` or `#` are ignored, so you
16
 * can pass `location.search` or `location.hash` directly.
17
 *
18
 * URL components are decoded with decode-uri-component.
19
 *
20
 * Parse arrays with elements using duplicate keys, e.g. ?a=1&a=2 becomes
21
 * {a: ['1', '2']}.
22
 *
23
 * Query keys are sorted alphabetically.
24
 *
25
 * Numbers and booleans are NOT parsed; they are left as strings.
26
 * @param {string} query the query part of a url
27
 * @returns {{ [key: string]: string | string[] | undefined }} the parsed query string
28
 */
29
var parseQueryString = function (query) {
1✔
30
  /** @type {{[key: string]: string | string[] | undefined }} */
31
  var result = {};
6✔
32

33
  if (!query || typeof query !== 'string') {
6!
NEW
34
    return result;
×
35
  }
36

37
  var cleanQuery = query.trim().replace(/^[?#&]/, '');
6✔
38
  var params = new URLSearchParams(cleanQuery);
6✔
39

40
  var iter = params.keys();
6✔
41
  do {
6✔
42
    var v = iter.next();
14✔
43
    var key = v.value;
14✔
44

45
    if (key) {
14✔
46
      var values = params.getAll(key);
8✔
47
      if (values.length === 1) {
8✔
48
        result[key] = values[0];
6✔
49
      } else {
50
        result[key] = values;
2✔
51
      }
52
    }
53
  } while (v.done === false);
54

55
  return result;
6✔
56
};
57

58
/**
59
 * Transform an object into a query string.
60
 *
61
 * Keys having object values are ignored.
62
 *
63
 * @param {object} the object to transform into a query string
64
 * @returns {string} the parsed query string
65
 */
66

67
var stringifyObject = function (object) {
1✔
68
  var spaceToken = '{{space}}';
5✔
69
  var params = new URLSearchParams();
5✔
70

71
  Object.keys(object).forEach(function (key) {
5✔
72
    var value = object[key];
12✔
73

74
    if (typeof object[key] === 'string') {
12✔
75
      value = value.replace(/ /g, spaceToken);
7✔
76
    } else if (
5✔
77
      ['object', 'undefined'].includes(typeof value) &&
9✔
78
      !Array.isArray(value)
79
    ) {
80
      value = '';
3✔
81
    }
82

83
    if (Array.isArray(value)) {
12✔
84
      value.forEach(function (arrayValue) {
1✔
85
        params.append(key, arrayValue);
2✔
86
      });
87
    } else {
88
      params.append(key, value);
11✔
89
    }
90
  });
91

92
  return params
5✔
93
    .toString()
94
    .replace(new RegExp(encodeURIComponent(spaceToken), 'g'), '%20');
95
};
96

97
// We proxy the underlying querystring module so we can limit the API we expose.
98
// This allows us to more easily make changes to the underlying implementation later without
99
// having to worry about breaking extensions. If extensions demand additional functionality, we
100
// can make adjustments as needed.
101
module.exports = {
1✔
102
  parse: function (string) {
103
    return parseQueryString(string);
6✔
104
  },
105
  stringify: function (object) {
106
    return stringifyObject(object);
5✔
107
  }
108
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc