jordy 0.20.4 → 0.20.6

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.
@@ -1,5 +1,10 @@
1
1
  import { __read } from "tslib";
2
2
  import { isNullable, isObject } from './typeCheck';
3
+ function encodeFieldValue(key, value) {
4
+ return (key +
5
+ '=' +
6
+ encodeURIComponent(isNullable(value) || Number.isNaN(value) ? '' : value));
7
+ }
3
8
  function _serialize(params, parentKey) {
4
9
  if (parentKey === void 0) { parentKey = ''; }
5
10
  return Object.entries(params).reduce(function (acc, _a) {
@@ -10,34 +15,57 @@ function _serialize(params, parentKey) {
10
15
  ret += _serialize(value, key);
11
16
  }
12
17
  else {
13
- ret =
14
- ret +
15
- key +
16
- '=' +
17
- encodeURIComponent(isNullable(value) || Number.isNaN(value) ? '' : value);
18
+ ret += encodeFieldValue(key, value);
18
19
  }
19
20
  return ret;
20
21
  }, '');
21
22
  }
23
+ function _serializeWithBrackets(params, parentKey) {
24
+ if (parentKey === void 0) { parentKey = ''; }
25
+ return Object.entries(params)
26
+ .reduce(function (outerAcc, _a) {
27
+ var _b = __read(_a, 2), oriKey = _b[0], value = _b[1];
28
+ var key = parentKey ? "".concat(parentKey, "%5B").concat(oriKey, "%5D") : oriKey;
29
+ if (Array.isArray(value)) {
30
+ value.reduce(function (acc, val) {
31
+ var subKey = "".concat(key, "%5B%5D");
32
+ if (isObject(val)) {
33
+ acc.push(_serializeWithBrackets(val, subKey));
34
+ }
35
+ else {
36
+ acc.push(encodeFieldValue(subKey, val));
37
+ }
38
+ return acc;
39
+ }, outerAcc);
40
+ }
41
+ else if (isObject(value)) {
42
+ outerAcc.push(_serializeWithBrackets(value, key));
43
+ }
44
+ else {
45
+ outerAcc.push(encodeFieldValue(key, value));
46
+ }
47
+ return outerAcc;
48
+ }, [])
49
+ .join('&');
50
+ }
22
51
  var cache = new Map();
23
52
  export var qs = {
24
53
  parse: function (url) {
25
- var _a;
26
54
  if (cache.has(url)) {
27
55
  return cache.get(url);
28
56
  }
29
- cache.clear();
57
+ if (cache.size > 100) {
58
+ cache.clear();
59
+ }
30
60
  var result = {};
31
61
  try {
32
62
  var queryString = url.split('?')[1];
33
63
  var splittedQueries = queryString.split('&');
34
- var len = splittedQueries.length;
35
- var key = '';
36
- var value = '';
37
- for (var i = 0; i < len; i++) {
38
- _a = __read(splittedQueries[i].split('='), 2), key = _a[0], value = _a[1];
39
- result[key] = decodeURIComponent(value);
40
- }
64
+ result = Object.fromEntries(splittedQueries.reduce(function (tmpMap, item) {
65
+ var _a = __read(item.split('='), 2), key = _a[0], value = _a[1];
66
+ tmpMap.set(key, decodeURIComponent(value));
67
+ return tmpMap;
68
+ }, new Map()));
41
69
  }
42
70
  catch (error) {
43
71
  }
@@ -51,6 +79,13 @@ export var qs = {
51
79
  }
52
80
  return (withQuestionMark === true ? '?' : '') + _serialize(params);
53
81
  },
82
+ serializeWithBrackets: function (params, withQuestionMark) {
83
+ if (withQuestionMark === void 0) { withQuestionMark = false; }
84
+ if (!isObject(params)) {
85
+ throw new Error("serializeToQueryString: params is not object.\n".concat(params ? JSON.stringify(params) : params));
86
+ }
87
+ return ((withQuestionMark === true ? '?' : '') + _serializeWithBrackets(params));
88
+ },
54
89
  append: function (search, data) {
55
90
  if (!search) {
56
91
  return qs.serialize(data, true);
@@ -11,11 +11,24 @@ interface QueryString {
11
11
  parse(url: string): Record<string, string>;
12
12
  /**
13
13
  * 전달되는 객체의 key 와 value 를 이용하여 쿼리 파라미터 문자열로 바꿔준다.
14
+ *
15
+ * 배열 데이터는 쉼표(,) 로 각 요소를 구분하도록 표현된다.
16
+ *
17
+ * @param params
18
+ * @param withQuestionMark
19
+ * @returns
20
+ */
21
+ serialize<T = Record<string, any>>(params: T, withQuestionMark?: boolean): string;
22
+ /**
23
+ * 전달되는 객체의 key 와 value 를 이용하여 쿼리 파라미터 문자열로 바꿔준다.
24
+ *
25
+ * 배열 데이터는 bracket([]) 형식으로 바꿔준다.
26
+ *
14
27
  * @param params
15
28
  * @param withQuestionMark
16
29
  * @returns
17
30
  */
18
- serialize<T = Record<string, unknown>>(params: T, withQuestionMark?: boolean): string;
31
+ serializeWithBrackets<T = Record<string, any>>(params: T, withQuestionMark?: boolean): string;
19
32
  /**
20
33
  * 검색 문자열 (search string) 에 지정된 자료로 쿼리 파라미터를 덧붙인다.
21
34
  * @param search "?"로 시작되는 search string
@@ -41,4 +41,4 @@ export declare function isFunction(val: unknown): val is CallableFunction;
41
41
  * @param val
42
42
  * @returns
43
43
  */
44
- export declare function isObject(val: unknown): val is Object;
44
+ export declare function isObject(val: unknown): val is Record<string, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jordy",
3
- "version": "0.20.4",
3
+ "version": "0.20.6",
4
4
  "description": "typescript based frontend toolkit",
5
5
  "repository": {
6
6
  "type": "git",