@koine/utils 1.0.26 → 1.0.29
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/{async.d.ts → Defer.d.ts} +5 -5
- package/{async.js → Defer.js} +2 -7
- package/{emitter.d.ts → Emitter.d.ts} +1 -0
- package/{emitter.js → Emitter.js} +1 -0
- package/accentSets.d.ts +16 -0
- package/accentSets.js +38 -0
- package/addOrReplaceAtIdx.d.ts +5 -0
- package/addOrReplaceAtIdx.js +19 -0
- package/arrayToLookup.d.ts +7 -0
- package/arrayToLookup.js +13 -0
- package/buildUrlQueryString.d.ts +12 -0
- package/buildUrlQueryString.js +24 -0
- package/changeUrlPath.d.ts +10 -0
- package/changeUrlPath.js +18 -0
- package/chunkByChunks.d.ts +7 -0
- package/chunkByChunks.js +37 -0
- package/chunkBySize.d.ts +6 -0
- package/chunkBySize.js +12 -0
- package/clamp.d.ts +8 -0
- package/clamp.js +10 -0
- package/clsx.d.ts +9 -0
- package/clsx.js +49 -0
- package/convertRange.d.ts +14 -0
- package/convertRange.js +16 -0
- package/cookie.d.ts +7 -26
- package/cookie.js +3 -221
- package/decode.d.ts +6 -0
- package/decode.js +10 -0
- package/encode.d.ts +6 -0
- package/encode.js +10 -0
- package/{cast.d.ts → ensureInt.d.ts} +3 -1
- package/{cast.js → ensureInt.js} +3 -4
- package/findDuplicatedIndexes.d.ts +5 -0
- package/findDuplicatedIndexes.js +14 -0
- package/getKeys.d.ts +9 -0
- package/getKeys.js +9 -0
- package/getNonce.d.ts +6 -0
- package/getNonce.js +9 -0
- package/getType.d.ts +22 -0
- package/getType.js +19 -0
- package/getUrlHashParams.d.ts +9 -0
- package/getUrlHashParams.js +16 -0
- package/getUrlHashPathname.d.ts +9 -0
- package/getUrlHashPathname.js +13 -0
- package/getUrlPathnameParts.d.ts +12 -0
- package/getUrlPathnameParts.js +20 -0
- package/getUrlQueryParams.d.ts +14 -0
- package/getUrlQueryParams.js +38 -0
- package/index.d.ts +107 -15
- package/index.js +108 -16
- package/isAnyObject.d.ts +8 -0
- package/isAnyObject.js +10 -0
- package/isArray.d.ts +7 -0
- package/isArray.js +10 -0
- package/isBlob.d.ts +7 -0
- package/isBlob.js +10 -0
- package/isBoolean.d.ts +7 -0
- package/isBoolean.js +10 -0
- package/isBrowser.d.ts +6 -0
- package/{ssr.js → isBrowser.js} +5 -2
- package/isDate.d.ts +7 -0
- package/isDate.js +10 -0
- package/isEmptyArray.d.ts +7 -0
- package/isEmptyArray.js +10 -0
- package/isEmptyObject.d.ts +9 -0
- package/isEmptyObject.js +10 -0
- package/isEmptyString.d.ts +7 -0
- package/isEmptyString.js +9 -0
- package/isError.d.ts +7 -0
- package/isError.js +10 -0
- package/isExternalUrl.d.ts +9 -0
- package/isExternalUrl.js +21 -0
- package/isFile.d.ts +7 -0
- package/isFile.js +10 -0
- package/isFloat.d.ts +7 -0
- package/isFloat.js +10 -0
- package/isFormData.d.ts +7 -0
- package/isFormData.js +10 -0
- package/isFullArray.d.ts +7 -0
- package/isFullArray.js +10 -0
- package/isFullObject.d.ts +8 -0
- package/isFullObject.js +10 -0
- package/isFullString.d.ts +7 -0
- package/isFullString.js +10 -0
- package/isFunction.d.ts +8 -0
- package/isFunction.js +9 -0
- package/{detect.d.ts → isIE.d.ts} +3 -4
- package/isIE.js +18 -0
- package/isInt.d.ts +7 -0
- package/isInt.js +10 -0
- package/isMap.d.ts +7 -0
- package/isMap.js +10 -0
- package/isMobile.d.ts +7 -0
- package/{detect.js → isMobile.js} +4 -15
- package/isNaNValue.d.ts +7 -0
- package/isNaNValue.js +10 -0
- package/isNegativeNumber.d.ts +7 -0
- package/isNegativeNumber.js +10 -0
- package/isNull.d.ts +7 -0
- package/isNull.js +10 -0
- package/isNullOrUndefined.d.ts +7 -0
- package/isNullOrUndefined.js +10 -0
- package/isNumber.d.ts +9 -0
- package/isNumber.js +12 -0
- package/isObject.d.ts +8 -0
- package/isObject.js +10 -0
- package/isObjectLike.d.ts +10 -0
- package/isObjectLike.js +12 -0
- package/isOneOf.d.ts +9 -0
- package/isOneOf.js +10 -0
- package/isPlainObject.d.ts +8 -0
- package/isPlainObject.js +13 -0
- package/isPositiveNumber.d.ts +7 -0
- package/isPositiveNumber.js +10 -0
- package/isPrimitive.d.ts +7 -0
- package/isPrimitive.js +20 -0
- package/isPromise.d.ts +7 -0
- package/isPromise.js +10 -0
- package/isRegExp.d.ts +7 -0
- package/isRegExp.js +10 -0
- package/isServer.d.ts +6 -0
- package/isServer.js +8 -0
- package/isSet.d.ts +7 -0
- package/isSet.js +10 -0
- package/isString.d.ts +7 -0
- package/isString.js +10 -0
- package/isSymbol.d.ts +7 -0
- package/isSymbol.js +10 -0
- package/isType.d.ts +11 -0
- package/isType.js +22 -0
- package/isUndefined.d.ts +7 -0
- package/isUndefined.js +10 -0
- package/isWeakMap.d.ts +7 -0
- package/isWeakMap.js +10 -0
- package/isWeakSet.d.ts +7 -0
- package/isWeakSet.js +10 -0
- package/location.d.ts +2 -145
- package/location.js +1 -299
- package/mapListBy.d.ts +7 -0
- package/mapListBy.js +14 -0
- package/{match-sorter.d.ts → matchSorter.d.ts} +1 -0
- package/{match-sorter.js → matchSorter.js} +2 -1
- package/mergeObjects.d.ts +7 -0
- package/{objects.js → mergeObjects.js} +2 -31
- package/mergeUrlQueryParams.d.ts +9 -0
- package/mergeUrlQueryParams.js +22 -0
- package/navigateToHashParams.d.ts +9 -0
- package/navigateToHashParams.js +22 -0
- package/navigateToMergedHashParams.d.ts +8 -0
- package/navigateToMergedHashParams.js +14 -0
- package/navigateToMergedParams.d.ts +9 -0
- package/navigateToMergedParams.js +14 -0
- package/navigateToParams.d.ts +10 -0
- package/navigateToParams.js +18 -0
- package/navigateWithoutUrlParam.d.ts +8 -0
- package/navigateWithoutUrlParam.js +19 -0
- package/node/{async.js → Defer.js} +3 -9
- package/node/{emitter.js → Emitter.js} +1 -0
- package/node/accentSets.js +41 -0
- package/node/addOrReplaceAtIdx.js +23 -0
- package/node/arrayToLookup.js +17 -0
- package/node/buildUrlQueryString.js +28 -0
- package/node/changeUrlPath.js +22 -0
- package/node/chunkByChunks.js +41 -0
- package/node/chunkBySize.js +16 -0
- package/node/clamp.js +14 -0
- package/node/clsx.js +53 -0
- package/node/convertRange.js +20 -0
- package/node/cookie.js +4 -227
- package/node/decode.js +14 -0
- package/node/encode.js +14 -0
- package/node/{cast.js → ensureInt.js} +4 -6
- package/node/findDuplicatedIndexes.js +18 -0
- package/node/getKeys.js +12 -0
- package/node/getNonce.js +13 -0
- package/node/getType.js +23 -0
- package/node/getUrlHashParams.js +20 -0
- package/node/getUrlHashPathname.js +17 -0
- package/node/getUrlPathnameParts.js +24 -0
- package/node/getUrlQueryParams.js +42 -0
- package/node/index.js +108 -16
- package/node/isAnyObject.js +14 -0
- package/node/isArray.js +14 -0
- package/node/isBlob.js +14 -0
- package/node/isBoolean.js +14 -0
- package/node/{ssr.js → isBrowser.js} +6 -3
- package/node/isDate.js +14 -0
- package/node/isEmptyArray.js +14 -0
- package/node/isEmptyObject.js +14 -0
- package/node/isEmptyString.js +13 -0
- package/node/isError.js +14 -0
- package/node/isExternalUrl.js +25 -0
- package/node/isFile.js +14 -0
- package/node/isFloat.js +14 -0
- package/node/isFormData.js +14 -0
- package/node/isFullArray.js +14 -0
- package/node/isFullObject.js +14 -0
- package/node/isFullString.js +14 -0
- package/node/isFunction.js +13 -0
- package/node/isIE.js +22 -0
- package/node/isInt.js +14 -0
- package/node/isMap.js +14 -0
- package/node/{detect.js → isMobile.js} +6 -18
- package/node/isNaNValue.js +14 -0
- package/node/isNegativeNumber.js +14 -0
- package/node/isNull.js +14 -0
- package/node/isNullOrUndefined.js +13 -0
- package/node/isNumber.js +16 -0
- package/node/isObject.js +14 -0
- package/node/isObjectLike.js +16 -0
- package/node/isOneOf.js +14 -0
- package/node/isPlainObject.js +17 -0
- package/node/isPositiveNumber.js +14 -0
- package/node/isPrimitive.js +24 -0
- package/node/isPromise.js +14 -0
- package/node/isRegExp.js +14 -0
- package/node/isServer.js +11 -0
- package/node/isSet.js +14 -0
- package/node/isString.js +14 -0
- package/node/isSymbol.js +14 -0
- package/node/isType.js +26 -0
- package/node/isUndefined.js +14 -0
- package/node/isWeakMap.js +14 -0
- package/node/isWeakSet.js +14 -0
- package/node/location.js +0 -321
- package/node/mapListBy.js +18 -0
- package/node/{match-sorter.js → matchSorter.js} +3 -2
- package/node/{objects.js → mergeObjects.js} +5 -36
- package/node/mergeUrlQueryParams.js +26 -0
- package/node/navigateToHashParams.js +26 -0
- package/node/navigateToMergedHashParams.js +18 -0
- package/node/navigateToMergedParams.js +18 -0
- package/node/navigateToParams.js +22 -0
- package/node/navigateWithoutUrlParam.js +23 -0
- package/node/normaliseUrl.js +18 -0
- package/node/normaliseUrlPathname.js +18 -0
- package/node/{analytics-google.js → pageview.js} +12 -10
- package/node/parseCookie.js +52 -0
- package/node/parseURL.js +24 -0
- package/node/randomInt.js +13 -0
- package/node/{misc.js → randomKey.js} +2 -0
- package/node/readCookie.js +38 -0
- package/node/redirectTo.js +19 -0
- package/node/removeAccents.js +19 -0
- package/node/removeCookie.js +19 -0
- package/node/removeDuplicatesByKey.js +25 -0
- package/node/removeDuplicatesComparing.js +14 -0
- package/node/removeIndexesFromArray.js +18 -0
- package/node/removeTrailingSlash.js +14 -0
- package/node/{math.js → roundTo.js} +3 -32
- package/node/serializeCookie.js +108 -0
- package/node/setCookie.js +61 -0
- package/node/shuffle.js +36 -0
- package/node/slugify.js +27 -0
- package/node/swapMap.js +18 -0
- package/node/{typography.js → titleCase.js} +3 -12
- package/node/toNumber.js +12 -0
- package/node/{colors.js → toRgba.js} +4 -0
- package/node/transformToUrlPathname.js +20 -0
- package/node/truncate.js +17 -0
- package/node/uid.js +4 -13
- package/node/updateLinkParams.js +18 -0
- package/node/updateUrlQueryParams.js +22 -0
- package/node/uuid.js +17 -0
- package/node/wait.js +13 -0
- package/node/whitelistObject.js +19 -0
- package/normaliseUrl.d.ts +10 -0
- package/normaliseUrl.js +14 -0
- package/normaliseUrlPathname.d.ts +10 -0
- package/normaliseUrlPathname.js +14 -0
- package/package.json +1 -1
- package/{analytics-google.d.ts → pageview.d.ts} +3 -0
- package/{analytics-google.js → pageview.js} +12 -10
- package/parseCookie.d.ts +11 -0
- package/parseCookie.js +48 -0
- package/parseURL.d.ts +17 -0
- package/parseURL.js +20 -0
- package/randomInt.d.ts +7 -0
- package/randomInt.js +9 -0
- package/{misc.d.ts → randomKey.d.ts} +2 -0
- package/{misc.js → randomKey.js} +2 -0
- package/readCookie.d.ts +3 -0
- package/readCookie.js +34 -0
- package/redirectTo.d.ts +9 -0
- package/redirectTo.js +15 -0
- package/removeAccents.d.ts +6 -0
- package/removeAccents.js +15 -0
- package/removeCookie.d.ts +10 -0
- package/removeCookie.js +15 -0
- package/removeDuplicatesByKey.d.ts +8 -0
- package/removeDuplicatesByKey.js +21 -0
- package/removeDuplicatesComparing.d.ts +5 -0
- package/removeDuplicatesComparing.js +10 -0
- package/removeIndexesFromArray.d.ts +5 -0
- package/removeIndexesFromArray.js +14 -0
- package/removeTrailingSlash.d.ts +7 -0
- package/removeTrailingSlash.js +10 -0
- package/roundTo.d.ts +8 -0
- package/{math.js → roundTo.js} +2 -28
- package/serializeCookie.d.ts +27 -0
- package/serializeCookie.js +104 -0
- package/setCookie.d.ts +10 -0
- package/setCookie.js +57 -0
- package/shuffle.d.ts +16 -0
- package/shuffle.js +32 -0
- package/{text.d.ts → slugify.d.ts} +2 -5
- package/slugify.js +23 -0
- package/swapMap.d.ts +7 -0
- package/swapMap.js +14 -0
- package/{typography.d.ts → titleCase.d.ts} +2 -4
- package/{typography.js → titleCase.js} +2 -10
- package/toNumber.d.ts +5 -0
- package/toNumber.js +8 -0
- package/{colors.d.ts → toRgba.d.ts} +4 -0
- package/{colors.js → toRgba.js} +4 -0
- package/transformToUrlPathname.d.ts +11 -0
- package/transformToUrlPathname.js +16 -0
- package/truncate.d.ts +7 -0
- package/truncate.js +13 -0
- package/uid.d.ts +3 -6
- package/uid.js +3 -11
- package/updateLinkParams.d.ts +10 -0
- package/updateLinkParams.js +14 -0
- package/updateUrlQueryParams.d.ts +9 -0
- package/updateUrlQueryParams.js +18 -0
- package/uuid.d.ts +8 -0
- package/uuid.js +13 -0
- package/wait.d.ts +7 -0
- package/wait.js +9 -0
- package/whitelistObject.d.ts +8 -0
- package/whitelistObject.js +15 -0
- package/arrays.d.ts +0 -41
- package/arrays.js +0 -153
- package/is.d.ts +0 -288
- package/is.js +0 -376
- package/math.d.ts +0 -28
- package/node/arrays.js +0 -166
- package/node/is.js +0 -417
- package/node/security.js +0 -29
- package/node/text.js +0 -70
- package/objects.d.ts +0 -22
- package/security.d.ts +0 -12
- package/security.js +0 -23
- package/ssr.d.ts +0 -2
- package/text.js +0 -65
package/cookie.js
CHANGED
|
@@ -1,228 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* @category cookie
|
|
5
5
|
*
|
|
6
|
+
* All cookie related code is inspired and adapted from:
|
|
6
7
|
* - [js-cookie](https://github.com/js-cookie/js-cookie)
|
|
7
|
-
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/js-cookie/index.d.ts
|
|
8
8
|
* - [cookie](https://github.com/jshttp/cookie)
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
import { __assign, __rest } from "tslib";
|
|
12
|
-
import { isNumber } from "./is";
|
|
13
|
-
/**
|
|
14
|
-
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
15
|
-
*
|
|
16
|
-
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
17
|
-
* field-vchar = VCHAR / obs-text
|
|
18
|
-
* obs-text = %x80-FF
|
|
19
|
-
*/
|
|
20
|
-
// eslint-disable-next-line no-control-regex
|
|
21
|
-
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
22
|
-
/**
|
|
23
|
-
* Parse a cookie header.
|
|
24
|
-
*
|
|
25
|
-
* Parse the given cookie header string into an object
|
|
26
|
-
* The object has the various cookies as keys(names) => values
|
|
27
|
-
*/
|
|
28
|
-
export function parseCookie(str, attributes) {
|
|
29
|
-
if (attributes === void 0) { attributes = {}; }
|
|
30
|
-
if (typeof str !== "string") {
|
|
31
|
-
throw new TypeError("argument str must be a string");
|
|
32
|
-
}
|
|
33
|
-
var obj = {};
|
|
34
|
-
var pairs = str.split(";");
|
|
35
|
-
var _a = attributes.decode, decode = _a === void 0 ? decodeURIComponent : _a;
|
|
36
|
-
for (var i = 0; i < pairs.length; i++) {
|
|
37
|
-
var pair = pairs[i];
|
|
38
|
-
var index = pair.indexOf("=");
|
|
39
|
-
// skip things that don't look like key=value
|
|
40
|
-
if (index < 0) {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
var key = pair.substring(0, index).trim();
|
|
44
|
-
// only assign once
|
|
45
|
-
if (undefined == obj[key]) {
|
|
46
|
-
var val = pair.substring(index + 1, pair.length).trim();
|
|
47
|
-
// quoted values
|
|
48
|
-
if (val[0] === '"') {
|
|
49
|
-
val = val.slice(1, -1);
|
|
50
|
-
}
|
|
51
|
-
obj[key] = tryDecode(val, decode);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return obj;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Serialize data into a cookie header.
|
|
58
|
-
*
|
|
59
|
-
* Serialize the a name value pair into a cookie string suitable for
|
|
60
|
-
* http headers. An optional attributes object specified cookie parameters.
|
|
61
|
-
*
|
|
62
|
-
* serialize('foo', 'bar', { httpOnly: true })
|
|
63
|
-
* => "foo=bar; httpOnly"
|
|
64
|
-
*/
|
|
65
|
-
export function serializeCookie(name, val, attributes) {
|
|
66
|
-
if (attributes === void 0) { attributes = {}; }
|
|
67
|
-
var _a = attributes.encode, encode = _a === void 0 ? encodeURIComponent : _a, domain = attributes.domain, path = attributes.path, httpOnly = attributes.httpOnly, secure = attributes.secure, sameSite = attributes.sameSite;
|
|
68
|
-
var maxAge = attributes.maxAge, expires = attributes.expires;
|
|
69
|
-
var value = encode(val);
|
|
70
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
71
|
-
if (!fieldContentRegExp.test(name)) {
|
|
72
|
-
throw new TypeError("argument name is invalid");
|
|
73
|
-
}
|
|
74
|
-
if (typeof attributes.encode !== "function") {
|
|
75
|
-
throw new TypeError("option encode is invalid");
|
|
76
|
-
}
|
|
77
|
-
if (value && !fieldContentRegExp.test(value)) {
|
|
78
|
-
throw new TypeError("argument val is invalid");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
var str = name + "=" + value;
|
|
82
|
-
if (null != maxAge) {
|
|
83
|
-
maxAge = maxAge - 0;
|
|
84
|
-
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
85
|
-
throw new TypeError("option maxAge is invalid");
|
|
86
|
-
}
|
|
87
|
-
str += "; Max-Age=" + Math.floor(maxAge);
|
|
88
|
-
}
|
|
89
|
-
if (domain) {
|
|
90
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
91
|
-
if (!fieldContentRegExp.test(domain)) {
|
|
92
|
-
throw new TypeError("option domain is invalid");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
str += "; Domain=" + domain;
|
|
96
|
-
}
|
|
97
|
-
if (path) {
|
|
98
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
99
|
-
if (!fieldContentRegExp.test(path)) {
|
|
100
|
-
throw new TypeError("option path is invalid");
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
str += "; Path=" + path;
|
|
104
|
-
}
|
|
105
|
-
if (expires) {
|
|
106
|
-
if (isNumber(expires)) {
|
|
107
|
-
expires = new Date(Date.now() + expires * 864e5);
|
|
108
|
-
}
|
|
109
|
-
str += "; Expires=" + expires.toUTCString();
|
|
110
|
-
}
|
|
111
|
-
if (httpOnly) {
|
|
112
|
-
str += "; HttpOnly";
|
|
113
|
-
}
|
|
114
|
-
if (secure) {
|
|
115
|
-
str += "; Secure";
|
|
116
|
-
}
|
|
117
|
-
if (sameSite) {
|
|
118
|
-
switch (sameSite.toLowerCase()) {
|
|
119
|
-
case "lax":
|
|
120
|
-
str += "; SameSite=Lax";
|
|
121
|
-
break;
|
|
122
|
-
case "strict":
|
|
123
|
-
str += "; SameSite=Strict";
|
|
124
|
-
break;
|
|
125
|
-
case "none":
|
|
126
|
-
str += "; SameSite=None";
|
|
127
|
-
break;
|
|
128
|
-
// default:
|
|
129
|
-
// throw new TypeError('option sameSite is invalid');
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return str;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Try decoding a string using a decoding function.
|
|
136
9
|
*/
|
|
137
|
-
|
|
138
|
-
try {
|
|
139
|
-
return decode(str);
|
|
140
|
-
}
|
|
141
|
-
catch (e) {
|
|
142
|
-
return str;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function converterRead(value) {
|
|
146
|
-
if (value[0] === '"') {
|
|
147
|
-
value = value.slice(1, -1);
|
|
148
|
-
}
|
|
149
|
-
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
|
|
150
|
-
}
|
|
151
|
-
function converterWrite(value) {
|
|
152
|
-
return encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent);
|
|
153
|
-
}
|
|
154
|
-
export function readCookie(name) {
|
|
155
|
-
if (typeof document === "undefined") {
|
|
156
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
157
|
-
console.warn("@koine/utils:cookie readCookie, document is undefined");
|
|
158
|
-
}
|
|
159
|
-
return name ? "" : {};
|
|
160
|
-
}
|
|
161
|
-
var cookies = document.cookie ? document.cookie.split("; ") : [];
|
|
162
|
-
var all = {};
|
|
163
|
-
for (var i = 0; i < cookies.length; i++) {
|
|
164
|
-
var parts = cookies[i].split("=");
|
|
165
|
-
var value = parts.slice(1).join("=");
|
|
166
|
-
try {
|
|
167
|
-
var found = decodeURIComponent(parts[0]);
|
|
168
|
-
all[found] = converterRead(value);
|
|
169
|
-
if (name === found) {
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
catch (e) {
|
|
174
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
175
|
-
console.warn("@koine/utils:cookie readCookie, failed to decode", value);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return name ? all[name] : all;
|
|
180
|
-
}
|
|
181
|
-
var defaultAttributesClient = { path: "/" };
|
|
182
|
-
export function setCookie(name, value, attributes) {
|
|
183
|
-
if (attributes === void 0) { attributes = {}; }
|
|
184
|
-
// eslint-disable-next-line prefer-const
|
|
185
|
-
var expires = attributes.expires, restAttrs = __rest(attributes, ["expires"]);
|
|
186
|
-
var cleanedAttrs = __assign(__assign({ expires: "" }, defaultAttributesClient), restAttrs);
|
|
187
|
-
if (typeof document === "undefined") {
|
|
188
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
189
|
-
console.warn("@koine/utils:cookie setCookie, document is undefined");
|
|
190
|
-
}
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
if (isNumber(expires)) {
|
|
194
|
-
expires = new Date(Date.now() + expires * 864e5);
|
|
195
|
-
}
|
|
196
|
-
if (expires) {
|
|
197
|
-
cleanedAttrs.expires = expires.toUTCString();
|
|
198
|
-
}
|
|
199
|
-
name = encodeURIComponent(name)
|
|
200
|
-
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
|
|
201
|
-
.replace(/[()]/g, escape);
|
|
202
|
-
var stringifiedAttributes = "";
|
|
203
|
-
for (var name_1 in attributes) {
|
|
204
|
-
var attrName = name_1;
|
|
205
|
-
if (!attributes[attrName]) {
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
stringifiedAttributes += "; " + attrName;
|
|
209
|
-
if (attributes[attrName] === true) {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
// Considers RFC 6265 section 5.2:
|
|
213
|
-
// ...
|
|
214
|
-
// 3. If the remaining unparsed-attributes contains a %x3B (";")
|
|
215
|
-
// character:
|
|
216
|
-
// Consume the characters of the unparsed-attributes up to,
|
|
217
|
-
// not including, the first %x3B (";") character.
|
|
218
|
-
// ...
|
|
219
|
-
stringifiedAttributes +=
|
|
220
|
-
"=" + String(attributes[attrName]).split(";")[0];
|
|
221
|
-
}
|
|
222
|
-
return (document.cookie =
|
|
223
|
-
name + "=" + converterWrite(value) + stringifiedAttributes);
|
|
224
|
-
}
|
|
225
|
-
export function removeCookie(name, attributes) {
|
|
226
|
-
if (attributes === void 0) { attributes = {}; }
|
|
227
|
-
setCookie(name, "", __assign(__assign(__assign({}, defaultAttributesClient), attributes), { expires: -1 }));
|
|
228
|
-
}
|
|
10
|
+
export var defaultAttributesClient = { path: "/" };
|
package/decode.d.ts
ADDED
package/decode.js
ADDED
package/encode.d.ts
ADDED
package/encode.js
ADDED
package/{cast.js → ensureInt.js}
RENAMED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { isNumber } from "./is";
|
|
2
|
-
export function toNumber(input, fallback) {
|
|
3
|
-
return isNumber(input) ? input : input ? parseFloat(input) : fallback || 0;
|
|
4
|
-
}
|
|
5
1
|
/**
|
|
6
2
|
* Ensure input to be an integer
|
|
3
|
+
*
|
|
4
|
+
* @category cast
|
|
7
5
|
*/
|
|
8
6
|
export var ensureInt = function (input) {
|
|
9
7
|
return typeof input === "string" ? parseInt(input, 10) : Math.round(input);
|
|
10
8
|
};
|
|
9
|
+
export default ensureInt;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @category array
|
|
3
|
+
*/
|
|
4
|
+
export function findDuplicatedIndexes(arr) {
|
|
5
|
+
var indexes = {};
|
|
6
|
+
for (var i = 0; i < arr.length; i++) {
|
|
7
|
+
var idxInArray = arr.indexOf(arr[i]);
|
|
8
|
+
if (idxInArray !== i && idxInArray !== -1) {
|
|
9
|
+
indexes[i] = true;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return indexes;
|
|
13
|
+
}
|
|
14
|
+
export default findDuplicatedIndexes;
|
package/getKeys.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type safe replacement for `Object.keys(myObject)` to iterate over a record
|
|
3
|
+
* without loosing the key's types in simple `string`s.
|
|
4
|
+
*
|
|
5
|
+
* @category objects
|
|
6
|
+
* @see https://stackoverflow.com/a/59459000/1938970
|
|
7
|
+
*/
|
|
8
|
+
export declare const getKeys: <T extends object>(obj: T) => (keyof T)[];
|
|
9
|
+
export default getKeys;
|
package/getKeys.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type safe replacement for `Object.keys(myObject)` to iterate over a record
|
|
3
|
+
* without loosing the key's types in simple `string`s.
|
|
4
|
+
*
|
|
5
|
+
* @category objects
|
|
6
|
+
* @see https://stackoverflow.com/a/59459000/1938970
|
|
7
|
+
*/
|
|
8
|
+
export var getKeys = Object.keys;
|
|
9
|
+
export default getKeys;
|
package/getNonce.d.ts
ADDED
package/getNonce.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isUndefined } from "./isUndefined";
|
|
2
|
+
/**
|
|
3
|
+
* @category security
|
|
4
|
+
* @see https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/nonce.ts
|
|
5
|
+
*/
|
|
6
|
+
export function getNonce() {
|
|
7
|
+
return isUndefined(__webpack_nonce__) ? null : __webpack_nonce__;
|
|
8
|
+
}
|
|
9
|
+
export default getNonce;
|
package/getType.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
*
|
|
4
|
+
* Same as [is-what](https://github.com/mesqueeb/is-what) plus:
|
|
5
|
+
*
|
|
6
|
+
* - `isFormData`
|
|
7
|
+
* - `isInt`
|
|
8
|
+
* - `isFloat`
|
|
9
|
+
*/
|
|
10
|
+
export declare type AnyFunction = (...args: any[]) => any;
|
|
11
|
+
export declare type AnyAsyncFunction = (...args: any[]) => Promise<any>;
|
|
12
|
+
export declare type AnyClass = new (...args: any[]) => any;
|
|
13
|
+
export declare type PlainObject = Record<string | number | symbol, any>;
|
|
14
|
+
export declare type TypeGuard<A, B extends A> = (payload: A) => payload is B;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the object type of the given payload
|
|
17
|
+
*
|
|
18
|
+
* @param {*} payload
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export declare function getType(payload: any): string;
|
|
22
|
+
export default getType;
|
package/getType.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
*
|
|
4
|
+
* Same as [is-what](https://github.com/mesqueeb/is-what) plus:
|
|
5
|
+
*
|
|
6
|
+
* - `isFormData`
|
|
7
|
+
* - `isInt`
|
|
8
|
+
* - `isFloat`
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Returns the object type of the given payload
|
|
12
|
+
*
|
|
13
|
+
* @param {*} payload
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function getType(payload) {
|
|
17
|
+
return Object.prototype.toString.call(payload).slice(8, -1);
|
|
18
|
+
}
|
|
19
|
+
export default getType;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type AnyQueryParams } from "./location";
|
|
2
|
+
/**
|
|
3
|
+
* It returns the "query params" as an object extracting it from the given `hash`
|
|
4
|
+
*string or, if not provided, failling back reading the `location.hash`
|
|
5
|
+
|
|
6
|
+
* @category location
|
|
7
|
+
*/
|
|
8
|
+
export declare function getUrlHashParams<T extends NonNullable<AnyQueryParams>>(hash?: string): T;
|
|
9
|
+
export default getUrlHashParams;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* It returns the "query params" as an object extracting it from the given `hash`
|
|
3
|
+
*string or, if not provided, failling back reading the `location.hash`
|
|
4
|
+
|
|
5
|
+
* @category location
|
|
6
|
+
*/
|
|
7
|
+
export function getUrlHashParams(hash) {
|
|
8
|
+
if (hash === void 0) { hash = ""; }
|
|
9
|
+
hash = hash || location.hash;
|
|
10
|
+
var hashParts = hash.split("?");
|
|
11
|
+
if (hashParts.length >= 1) {
|
|
12
|
+
return Object.fromEntries(new URLSearchParams(hashParts[1]));
|
|
13
|
+
}
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
export default getUrlHashParams;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* It returns the "pathname" cleaned up from the `#` and the initial slashes
|
|
3
|
+
* extracting it from the given `hash` string or, if not provided, failling
|
|
4
|
+
* back reading the `location.hash`
|
|
5
|
+
*
|
|
6
|
+
* @category location
|
|
7
|
+
*/
|
|
8
|
+
export declare function getUrlHashPathname(hash?: string): string;
|
|
9
|
+
export default getUrlHashPathname;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* It returns the "pathname" cleaned up from the `#` and the initial slashes
|
|
3
|
+
* extracting it from the given `hash` string or, if not provided, failling
|
|
4
|
+
* back reading the `location.hash`
|
|
5
|
+
*
|
|
6
|
+
* @category location
|
|
7
|
+
*/
|
|
8
|
+
export function getUrlHashPathname(hash) {
|
|
9
|
+
if (hash === void 0) { hash = ""; }
|
|
10
|
+
hash = hash || location.hash;
|
|
11
|
+
return hash.split("?")[0].replace(/^#\//, "");
|
|
12
|
+
}
|
|
13
|
+
export default getUrlHashPathname;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get pathname parts
|
|
3
|
+
*
|
|
4
|
+
* First clean the pathname from the first slash if any then split the pathname
|
|
5
|
+
* in parts,
|
|
6
|
+
* Given a pathname like: `"/en/{prefix}/{collection}/{slug}"` we obtain
|
|
7
|
+
* `[locale, prefix, collection, slug]`
|
|
8
|
+
*
|
|
9
|
+
* @category location
|
|
10
|
+
*/
|
|
11
|
+
export declare function getUrlPathnameParts(pathname?: string): string[];
|
|
12
|
+
export default getUrlPathnameParts;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import isBrowser from "./isBrowser";
|
|
2
|
+
/**
|
|
3
|
+
* Get pathname parts
|
|
4
|
+
*
|
|
5
|
+
* First clean the pathname from the first slash if any then split the pathname
|
|
6
|
+
* in parts,
|
|
7
|
+
* Given a pathname like: `"/en/{prefix}/{collection}/{slug}"` we obtain
|
|
8
|
+
* `[locale, prefix, collection, slug]`
|
|
9
|
+
*
|
|
10
|
+
* @category location
|
|
11
|
+
*/
|
|
12
|
+
export function getUrlPathnameParts(pathname) {
|
|
13
|
+
if (pathname === void 0) { pathname = ""; }
|
|
14
|
+
pathname = pathname || isBrowser ? location.pathname : "";
|
|
15
|
+
return pathname
|
|
16
|
+
.replace(/^\//, "")
|
|
17
|
+
.split("/")
|
|
18
|
+
.filter(function (part) { return part; });
|
|
19
|
+
}
|
|
20
|
+
export default getUrlPathnameParts;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AnyQueryParams } from "./location";
|
|
2
|
+
/**
|
|
3
|
+
* Get parsed query parameters as object dictionary (from URL or given query string)
|
|
4
|
+
*
|
|
5
|
+
* @category location
|
|
6
|
+
* @param url A URL which contains a `?`, e.g. `?myparam=x` or `https://a.com?myparams=x`.
|
|
7
|
+
* If not provided it defaults reading the current URL query string with
|
|
8
|
+
* `location.search`. Through this argument you can use this
|
|
9
|
+
* same function to parse, for instance, the query params of
|
|
10
|
+
* the `href` of a `<a href="...">` HTML tag.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export declare function getUrlQueryParams<T extends NonNullable<AnyQueryParams>>(url?: string): T;
|
|
14
|
+
export default getUrlQueryParams;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import isBrowser from "./isBrowser";
|
|
2
|
+
/**
|
|
3
|
+
* Get parsed query parameters as object dictionary (from URL or given query string)
|
|
4
|
+
*
|
|
5
|
+
* @category location
|
|
6
|
+
* @param url A URL which contains a `?`, e.g. `?myparam=x` or `https://a.com?myparams=x`.
|
|
7
|
+
* If not provided it defaults reading the current URL query string with
|
|
8
|
+
* `location.search`. Through this argument you can use this
|
|
9
|
+
* same function to parse, for instance, the query params of
|
|
10
|
+
* the `href` of a `<a href="...">` HTML tag.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export function getUrlQueryParams(url) {
|
|
14
|
+
var _a;
|
|
15
|
+
var params = {};
|
|
16
|
+
var search = url
|
|
17
|
+
? (_a = url.split("?")) === null || _a === void 0 ? void 0 : _a[1]
|
|
18
|
+
: isBrowser
|
|
19
|
+
? location.search.substring(1)
|
|
20
|
+
: "";
|
|
21
|
+
if (!search) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
// @see https://stackoverflow.com/a/8649003/1938970
|
|
26
|
+
var paramsAsObj = "{\"".concat(search
|
|
27
|
+
.replace(/&/g, '","')
|
|
28
|
+
.replace(/=/g, '":"'), "\"}");
|
|
29
|
+
params = JSON.parse(paramsAsObj, function (key, value) {
|
|
30
|
+
return key === "" ? value : decodeURIComponent(value);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
// do nothing or warn on process.env["NODE_ENV"] !== "production"
|
|
35
|
+
}
|
|
36
|
+
return params;
|
|
37
|
+
}
|
|
38
|
+
export default getUrlQueryParams;
|