es-toolkit 1.52.0-dev.2130 → 1.52.0-dev.2131

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.
@@ -0,0 +1,2 @@
1
+ import { stringify } from "./stringify.mjs";
2
+ export { stringify };
@@ -0,0 +1,2 @@
1
+ import { stringify } from "./stringify.js";
2
+ export { stringify };
@@ -0,0 +1,3 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_stringify = require("./stringify.js");
3
+ exports.stringify = require_stringify.stringify;
@@ -0,0 +1,2 @@
1
+ import { stringify } from "./stringify.mjs";
2
+ export { stringify };
@@ -0,0 +1,25 @@
1
+ //#region src/query-string/stringify.d.ts
2
+ type QueryValue = string | number | boolean | bigint | null | undefined;
3
+ /**
4
+ * Converts a flat object into a URL search string, such as `?a=1&b=2`.
5
+ *
6
+ * - `null` and `undefined` values are omitted, including inside arrays.
7
+ * - Arrays are serialized as repeated keys, such as `a=1&a=2`.
8
+ * - Other values are converted with `String()`.
9
+ * - Keys and values are encoded with `encodeURIComponent`, so spaces become `%20` and `+` becomes `%2B`.
10
+ *
11
+ * @template T - The type of the object to serialize.
12
+ * @param query - The object to serialize.
13
+ * @returns The search string starting with `?`, or an empty string when there is nothing to serialize.
14
+ * @throws {URIError} Throws when a key or value contains an unpaired surrogate.
15
+ *
16
+ * @example
17
+ * stringify({ page: 1, tags: ['a', 'b'], q: 'hello world', ref: undefined });
18
+ * // => '?page=1&tags=a&tags=b&q=hello%20world'
19
+ *
20
+ * stringify({ ref: undefined });
21
+ * // => ''
22
+ */
23
+ declare function stringify<T extends object & { [K in keyof T]: QueryValue | readonly QueryValue[] }>(query: T): string;
24
+ //#endregion
25
+ export { stringify };
@@ -0,0 +1,25 @@
1
+ //#region src/query-string/stringify.d.ts
2
+ type QueryValue = string | number | boolean | bigint | null | undefined;
3
+ /**
4
+ * Converts a flat object into a URL search string, such as `?a=1&b=2`.
5
+ *
6
+ * - `null` and `undefined` values are omitted, including inside arrays.
7
+ * - Arrays are serialized as repeated keys, such as `a=1&a=2`.
8
+ * - Other values are converted with `String()`.
9
+ * - Keys and values are encoded with `encodeURIComponent`, so spaces become `%20` and `+` becomes `%2B`.
10
+ *
11
+ * @template T - The type of the object to serialize.
12
+ * @param query - The object to serialize.
13
+ * @returns The search string starting with `?`, or an empty string when there is nothing to serialize.
14
+ * @throws {URIError} Throws when a key or value contains an unpaired surrogate.
15
+ *
16
+ * @example
17
+ * stringify({ page: 1, tags: ['a', 'b'], q: 'hello world', ref: undefined });
18
+ * // => '?page=1&tags=a&tags=b&q=hello%20world'
19
+ *
20
+ * stringify({ ref: undefined });
21
+ * // => ''
22
+ */
23
+ declare function stringify<T extends object & { [K in keyof T]: QueryValue | readonly QueryValue[] }>(query: T): string;
24
+ //#endregion
25
+ export { stringify };
@@ -0,0 +1,37 @@
1
+ //#region src/query-string/stringify.ts
2
+ /**
3
+ * Converts a flat object into a URL search string, such as `?a=1&b=2`.
4
+ *
5
+ * - `null` and `undefined` values are omitted, including inside arrays.
6
+ * - Arrays are serialized as repeated keys, such as `a=1&a=2`.
7
+ * - Other values are converted with `String()`.
8
+ * - Keys and values are encoded with `encodeURIComponent`, so spaces become `%20` and `+` becomes `%2B`.
9
+ *
10
+ * @template T - The type of the object to serialize.
11
+ * @param query - The object to serialize.
12
+ * @returns The search string starting with `?`, or an empty string when there is nothing to serialize.
13
+ * @throws {URIError} Throws when a key or value contains an unpaired surrogate.
14
+ *
15
+ * @example
16
+ * stringify({ page: 1, tags: ['a', 'b'], q: 'hello world', ref: undefined });
17
+ * // => '?page=1&tags=a&tags=b&q=hello%20world'
18
+ *
19
+ * stringify({ ref: undefined });
20
+ * // => ''
21
+ */
22
+ function stringify(query) {
23
+ const parts = [];
24
+ const keys = Object.keys(query);
25
+ for (let i = 0; i < keys.length; i++) {
26
+ const key = keys[i];
27
+ const value = query[key];
28
+ const values = Array.isArray(value) ? value : [value];
29
+ for (let j = 0; j < values.length; j++) {
30
+ const item = values[j];
31
+ if (item != null) parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(item))}`);
32
+ }
33
+ }
34
+ return parts.length === 0 ? "" : `?${parts.join("&")}`;
35
+ }
36
+ //#endregion
37
+ exports.stringify = stringify;
@@ -0,0 +1,37 @@
1
+ //#region src/query-string/stringify.ts
2
+ /**
3
+ * Converts a flat object into a URL search string, such as `?a=1&b=2`.
4
+ *
5
+ * - `null` and `undefined` values are omitted, including inside arrays.
6
+ * - Arrays are serialized as repeated keys, such as `a=1&a=2`.
7
+ * - Other values are converted with `String()`.
8
+ * - Keys and values are encoded with `encodeURIComponent`, so spaces become `%20` and `+` becomes `%2B`.
9
+ *
10
+ * @template T - The type of the object to serialize.
11
+ * @param query - The object to serialize.
12
+ * @returns The search string starting with `?`, or an empty string when there is nothing to serialize.
13
+ * @throws {URIError} Throws when a key or value contains an unpaired surrogate.
14
+ *
15
+ * @example
16
+ * stringify({ page: 1, tags: ['a', 'b'], q: 'hello world', ref: undefined });
17
+ * // => '?page=1&tags=a&tags=b&q=hello%20world'
18
+ *
19
+ * stringify({ ref: undefined });
20
+ * // => ''
21
+ */
22
+ function stringify(query) {
23
+ const parts = [];
24
+ const keys = Object.keys(query);
25
+ for (let i = 0; i < keys.length; i++) {
26
+ const key = keys[i];
27
+ const value = query[key];
28
+ const values = Array.isArray(value) ? value : [value];
29
+ for (let j = 0; j < values.length; j++) {
30
+ const item = values[j];
31
+ if (item != null) parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(item))}`);
32
+ }
33
+ }
34
+ return parts.length === 0 ? "" : `?${parts.join("&")}`;
35
+ }
36
+ //#endregion
37
+ export { stringify };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.52.0-dev.2130+5f7c64c3",
3
+ "version": "1.52.0-dev.2131+5f0e6b21",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
@@ -174,6 +174,16 @@
174
174
  "default": "./dist/promise/index.js"
175
175
  }
176
176
  },
177
+ "./query-string": {
178
+ "import": {
179
+ "types": "./dist/query-string/index.d.mts",
180
+ "default": "./dist/query-string/index.mjs"
181
+ },
182
+ "require": {
183
+ "types": "./dist/query-string/index.d.ts",
184
+ "default": "./dist/query-string/index.js"
185
+ }
186
+ },
177
187
  "./set": {
178
188
  "import": {
179
189
  "types": "./dist/set/index.d.mts",
@@ -257,6 +267,7 @@
257
267
  "object.js",
258
268
  "predicate.js",
259
269
  "promise.js",
270
+ "query-string.js",
260
271
  "set.js",
261
272
  "string.js",
262
273
  "server.js",
@@ -485,6 +496,16 @@
485
496
  "default": "./dist/promise/index.js"
486
497
  }
487
498
  },
499
+ "./query-string": {
500
+ "import": {
501
+ "types": "./dist/query-string/index.d.mts",
502
+ "default": "./dist/query-string/index.mjs"
503
+ },
504
+ "require": {
505
+ "types": "./dist/query-string/index.d.ts",
506
+ "default": "./dist/query-string/index.js"
507
+ }
508
+ },
488
509
  "./set": {
489
510
  "import": {
490
511
  "types": "./dist/set/index.d.mts",
@@ -0,0 +1 @@
1
+ export * from './dist/query-string';
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/query-string');