@oscarpalmer/atoms 0.173.1 → 0.173.3
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/dist/index.mjs +10 -9
- package/dist/internal/number.mjs +1 -3
- package/dist/query.mjs +10 -7
- package/package.json +1 -1
- package/src/internal/number.ts +1 -7
- package/src/query.ts +13 -7
package/dist/index.mjs
CHANGED
|
@@ -1198,12 +1198,10 @@ function getNumber(value) {
|
|
|
1198
1198
|
if (EXPRESSION_ZEROISH.test(parsed)) return 0;
|
|
1199
1199
|
const isBinary = EXPRESSION_BINARY.test(trimmed);
|
|
1200
1200
|
if (isBinary || EXPRESSION_OCTAL.test(trimmed)) return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
|
|
1201
|
-
return Number(
|
|
1201
|
+
return Number(trimmed);
|
|
1202
1202
|
}
|
|
1203
1203
|
const EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
1204
|
-
const EXPRESSION_HEX = /^0x[0-9a-f]+$/i;
|
|
1205
1204
|
const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
1206
|
-
const EXPRESSION_UNDERSCORE = /_/g;
|
|
1207
1205
|
const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
1208
1206
|
const OCTAL_VALUE = 8;
|
|
1209
1207
|
//#endregion
|
|
@@ -4058,11 +4056,11 @@ async function resultPromises(items, signal) {
|
|
|
4058
4056
|
*/
|
|
4059
4057
|
function fromQuery(query) {
|
|
4060
4058
|
if (typeof query !== "string" || query.trim().length === 0) return {};
|
|
4061
|
-
const parts = query.split(
|
|
4059
|
+
const parts = query.split(AMPERSAND);
|
|
4062
4060
|
const { length } = parts;
|
|
4063
4061
|
const parameters = {};
|
|
4064
4062
|
for (let index = 0; index < length; index += 1) {
|
|
4065
|
-
const decoded = parts[index].split(
|
|
4063
|
+
const decoded = parts[index].split(EQUAL).map(tryDecode);
|
|
4066
4064
|
const key = decoded[0].replace(EXPRESSION_ARRAY_SUFFIX, "");
|
|
4067
4065
|
if (!ignoreKey(key)) setQueryValue(parameters, key, decoded[1]);
|
|
4068
4066
|
}
|
|
@@ -4078,8 +4076,8 @@ function getParts(value, fromArray, prefix) {
|
|
|
4078
4076
|
const full = join([prefix, fromArray ? void 0 : key], ".");
|
|
4079
4077
|
if (Array.isArray(val)) parts.push(...getParts(val, true, full));
|
|
4080
4078
|
else if (isPlainObject(val)) parts.push(...getParts(val, false, full));
|
|
4081
|
-
else if (isDecodable(val)) parts.push(`${tryEncode(full)}=${tryEncode(val)}`);
|
|
4082
|
-
else if (val instanceof Date) parts.push(`${tryEncode(full)}=${val.toJSON()}`);
|
|
4079
|
+
else if (isDecodable(val)) parts.push(`${getString(tryEncode(full))}=${getString(tryEncode(val))}`);
|
|
4080
|
+
else if (val instanceof Date) parts.push(`${getString(tryEncode(full))}=${val.toJSON()}`);
|
|
4083
4081
|
}
|
|
4084
4082
|
return parts;
|
|
4085
4083
|
}
|
|
@@ -4096,7 +4094,7 @@ function isDecodable(value) {
|
|
|
4096
4094
|
return TYPES.has(typeof value);
|
|
4097
4095
|
}
|
|
4098
4096
|
function setQueryValue(parameters, key, value) {
|
|
4099
|
-
if (
|
|
4097
|
+
if (EXPRESSION_DOT.test(key)) setValue(parameters, key, getQueryValue(value));
|
|
4100
4098
|
else if (key in parameters) {
|
|
4101
4099
|
if (!Array.isArray(parameters[key])) parameters[key] = [parameters[key]];
|
|
4102
4100
|
parameters[key].push(getQueryValue(value));
|
|
@@ -4108,10 +4106,13 @@ function setQueryValue(parameters, key, value) {
|
|
|
4108
4106
|
* @returns Query string representation of the object
|
|
4109
4107
|
*/
|
|
4110
4108
|
function toQuery(parameters) {
|
|
4111
|
-
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0),
|
|
4109
|
+
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0), AMPERSAND) : "";
|
|
4112
4110
|
}
|
|
4111
|
+
const AMPERSAND = "&";
|
|
4112
|
+
const EQUAL = "=";
|
|
4113
4113
|
const EXPRESSION_ARRAY_SUFFIX = /\[\]$/;
|
|
4114
4114
|
const EXPRESSION_BOOLEAN = /^(false|true)$/;
|
|
4115
|
+
const EXPRESSION_DOT = /\./g;
|
|
4115
4116
|
const TRUE = "true";
|
|
4116
4117
|
const TYPES = new Set([
|
|
4117
4118
|
"boolean",
|
package/dist/internal/number.mjs
CHANGED
|
@@ -51,12 +51,10 @@ function getNumber(value) {
|
|
|
51
51
|
if (EXPRESSION_ZEROISH.test(parsed)) return 0;
|
|
52
52
|
const isBinary = EXPRESSION_BINARY.test(trimmed);
|
|
53
53
|
if (isBinary || EXPRESSION_OCTAL.test(trimmed)) return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
|
|
54
|
-
return Number(
|
|
54
|
+
return Number(trimmed);
|
|
55
55
|
}
|
|
56
56
|
const EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
57
|
-
const EXPRESSION_HEX = /^0x[0-9a-f]+$/i;
|
|
58
57
|
const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
59
|
-
const EXPRESSION_UNDERSCORE = /_/g;
|
|
60
58
|
const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
61
59
|
const OCTAL_VALUE = 8;
|
|
62
60
|
//#endregion
|
package/dist/query.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isPlainObject } from "./internal/is.mjs";
|
|
2
|
-
import { ignoreKey, join, tryDecode, tryEncode } from "./internal/string.mjs";
|
|
2
|
+
import { getString, ignoreKey, join, tryDecode, tryEncode } from "./internal/string.mjs";
|
|
3
3
|
import { getNumber } from "./internal/number.mjs";
|
|
4
4
|
import { setValue } from "./internal/value/set.mjs";
|
|
5
5
|
//#region src/query.ts
|
|
@@ -10,11 +10,11 @@ import { setValue } from "./internal/value/set.mjs";
|
|
|
10
10
|
*/
|
|
11
11
|
function fromQuery(query) {
|
|
12
12
|
if (typeof query !== "string" || query.trim().length === 0) return {};
|
|
13
|
-
const parts = query.split(
|
|
13
|
+
const parts = query.split(AMPERSAND);
|
|
14
14
|
const { length } = parts;
|
|
15
15
|
const parameters = {};
|
|
16
16
|
for (let index = 0; index < length; index += 1) {
|
|
17
|
-
const decoded = parts[index].split(
|
|
17
|
+
const decoded = parts[index].split(EQUAL).map(tryDecode);
|
|
18
18
|
const key = decoded[0].replace(EXPRESSION_ARRAY_SUFFIX, "");
|
|
19
19
|
if (!ignoreKey(key)) setQueryValue(parameters, key, decoded[1]);
|
|
20
20
|
}
|
|
@@ -30,8 +30,8 @@ function getParts(value, fromArray, prefix) {
|
|
|
30
30
|
const full = join([prefix, fromArray ? void 0 : key], ".");
|
|
31
31
|
if (Array.isArray(val)) parts.push(...getParts(val, true, full));
|
|
32
32
|
else if (isPlainObject(val)) parts.push(...getParts(val, false, full));
|
|
33
|
-
else if (isDecodable(val)) parts.push(`${tryEncode(full)}=${tryEncode(val)}`);
|
|
34
|
-
else if (val instanceof Date) parts.push(`${tryEncode(full)}=${val.toJSON()}`);
|
|
33
|
+
else if (isDecodable(val)) parts.push(`${getString(tryEncode(full))}=${getString(tryEncode(val))}`);
|
|
34
|
+
else if (val instanceof Date) parts.push(`${getString(tryEncode(full))}=${val.toJSON()}`);
|
|
35
35
|
}
|
|
36
36
|
return parts;
|
|
37
37
|
}
|
|
@@ -48,7 +48,7 @@ function isDecodable(value) {
|
|
|
48
48
|
return TYPES.has(typeof value);
|
|
49
49
|
}
|
|
50
50
|
function setQueryValue(parameters, key, value) {
|
|
51
|
-
if (
|
|
51
|
+
if (EXPRESSION_DOT.test(key)) setValue(parameters, key, getQueryValue(value));
|
|
52
52
|
else if (key in parameters) {
|
|
53
53
|
if (!Array.isArray(parameters[key])) parameters[key] = [parameters[key]];
|
|
54
54
|
parameters[key].push(getQueryValue(value));
|
|
@@ -60,10 +60,13 @@ function setQueryValue(parameters, key, value) {
|
|
|
60
60
|
* @returns Query string representation of the object
|
|
61
61
|
*/
|
|
62
62
|
function toQuery(parameters) {
|
|
63
|
-
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0),
|
|
63
|
+
return isPlainObject(parameters) ? join(getParts(parameters, false).filter((part) => part.length > 0), AMPERSAND) : "";
|
|
64
64
|
}
|
|
65
|
+
const AMPERSAND = "&";
|
|
66
|
+
const EQUAL = "=";
|
|
65
67
|
const EXPRESSION_ARRAY_SUFFIX = /\[\]$/;
|
|
66
68
|
const EXPRESSION_BOOLEAN = /^(false|true)$/;
|
|
69
|
+
const EXPRESSION_DOT = /\./g;
|
|
67
70
|
const TRUE = "true";
|
|
68
71
|
const TYPES = new Set([
|
|
69
72
|
"boolean",
|
package/package.json
CHANGED
package/src/internal/number.ts
CHANGED
|
@@ -94,9 +94,7 @@ export function getNumber(value: unknown): number {
|
|
|
94
94
|
return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
return Number(
|
|
98
|
-
EXPRESSION_HEX.test(trimmed) ? trimmed : trimmed.replace(EXPRESSION_UNDERSCORE, ''),
|
|
99
|
-
);
|
|
97
|
+
return Number(trimmed);
|
|
100
98
|
}
|
|
101
99
|
|
|
102
100
|
// #endregion
|
|
@@ -105,12 +103,8 @@ export function getNumber(value: unknown): number {
|
|
|
105
103
|
|
|
106
104
|
const EXPRESSION_BINARY = /^0b[01]+$/i;
|
|
107
105
|
|
|
108
|
-
const EXPRESSION_HEX = /^0x[0-9a-f]+$/i;
|
|
109
|
-
|
|
110
106
|
const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
|
|
111
107
|
|
|
112
|
-
const EXPRESSION_UNDERSCORE = /_/g;
|
|
113
|
-
|
|
114
108
|
const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
|
|
115
109
|
|
|
116
110
|
const OCTAL_VALUE = 8;
|
package/src/query.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {isPlainObject} from './internal/is';
|
|
2
2
|
import {getNumber} from './internal/number';
|
|
3
|
-
import {ignoreKey, join, tryDecode, tryEncode} from './internal/string';
|
|
3
|
+
import {getString, ignoreKey, join, tryDecode, tryEncode} from './internal/string';
|
|
4
4
|
import {setValue} from './internal/value/set';
|
|
5
5
|
import type {ArrayOrPlainObject, PlainObject} from './models';
|
|
6
6
|
|
|
@@ -16,13 +16,13 @@ export function fromQuery(query: string): PlainObject {
|
|
|
16
16
|
return {};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
const parts = query.split(
|
|
19
|
+
const parts = query.split(AMPERSAND);
|
|
20
20
|
const {length} = parts;
|
|
21
21
|
|
|
22
22
|
const parameters: PlainObject = {};
|
|
23
23
|
|
|
24
24
|
for (let index = 0; index < length; index += 1) {
|
|
25
|
-
const decoded = parts[index].split(
|
|
25
|
+
const decoded = parts[index].split(EQUAL).map(tryDecode);
|
|
26
26
|
const key = decoded[0].replace(EXPRESSION_ARRAY_SUFFIX, '');
|
|
27
27
|
|
|
28
28
|
if (!ignoreKey(key)) {
|
|
@@ -50,9 +50,9 @@ function getParts(value: ArrayOrPlainObject, fromArray: boolean, prefix?: string
|
|
|
50
50
|
} else if (isPlainObject(val)) {
|
|
51
51
|
parts.push(...getParts(val, false, full));
|
|
52
52
|
} else if (isDecodable(val)) {
|
|
53
|
-
parts.push(`${tryEncode(full)}=${tryEncode(val)}`);
|
|
53
|
+
parts.push(`${getString(tryEncode(full))}=${getString(tryEncode(val))}`);
|
|
54
54
|
} else if (val instanceof Date) {
|
|
55
|
-
parts.push(`${tryEncode(full)}=${val.toJSON()}`);
|
|
55
|
+
parts.push(`${getString(tryEncode(full))}=${val.toJSON()}`);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -86,7 +86,7 @@ function isDecodable(value: unknown): value is boolean | number | string {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
function setQueryValue(parameters: PlainObject, key: string, value: string): void {
|
|
89
|
-
if (
|
|
89
|
+
if (EXPRESSION_DOT.test(key)) {
|
|
90
90
|
setValue(parameters, key, getQueryValue(value));
|
|
91
91
|
} else {
|
|
92
92
|
if (key in parameters) {
|
|
@@ -110,7 +110,7 @@ export function toQuery(parameters: PlainObject): string {
|
|
|
110
110
|
return isPlainObject(parameters)
|
|
111
111
|
? join(
|
|
112
112
|
getParts(parameters, false).filter(part => part.length > 0),
|
|
113
|
-
|
|
113
|
+
AMPERSAND,
|
|
114
114
|
)
|
|
115
115
|
: '';
|
|
116
116
|
}
|
|
@@ -119,10 +119,16 @@ export function toQuery(parameters: PlainObject): string {
|
|
|
119
119
|
|
|
120
120
|
// #region Variables
|
|
121
121
|
|
|
122
|
+
const AMPERSAND = '&';
|
|
123
|
+
|
|
124
|
+
const EQUAL = '=';
|
|
125
|
+
|
|
122
126
|
const EXPRESSION_ARRAY_SUFFIX = /\[\]$/;
|
|
123
127
|
|
|
124
128
|
const EXPRESSION_BOOLEAN = /^(false|true)$/;
|
|
125
129
|
|
|
130
|
+
const EXPRESSION_DOT = /\./g;
|
|
131
|
+
|
|
126
132
|
const TRUE = 'true';
|
|
127
133
|
|
|
128
134
|
const TYPES = new Set(['boolean', 'number', 'string']);
|