@basiln/utils 0.1.4 → 0.1.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,134 +0,0 @@
1
- /**
2
- * @description 쿼리 스트링을 생성합니다.
3
- * @example createSearchParamString({ a: 1, b: 2, c: 3 }) // 'a=1&b=2&c=3'
4
- */
5
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
- function createSearchParamString(params: Record<string, any>) {
7
- return (
8
- new URLSearchParams(
9
- Object.entries(params)
10
- .filter(([, value]) => value != null)
11
- .map(([key, value]) => {
12
- if (Array.isArray(value)) {
13
- return value.map((x) => [key, x]);
14
- }
15
- return [[key, value]];
16
- })
17
- .flat()
18
- )
19
- .toString()
20
- // Convert space characters to '%20' according to RFC3986 spec, from RFC1738.
21
- .replace(/\+/g, '%20')
22
- );
23
- }
24
-
25
- /**
26
- * @description 물음표를 포함한 쿼리 스트링을 생성합니다.
27
- * @example createQueryString({ a: 1, b: 2, c: 3 }) // '?a=1&b=2&c=3'
28
- */
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
- function createQueryString(params: Record<string, any>) {
31
- const queryString = createSearchParamString(params);
32
-
33
- if (queryString === '') {
34
- return '';
35
- }
36
-
37
- return `?${queryString}`;
38
- }
39
-
40
- /**
41
- * @description 이터러블에서 키와 값을 가진 객체로 변환합니다.
42
- * @example fromEntries([['a', 1], ['b', 2], ['c', 3]]) // { a: 1, b: 2, c: 3 }
43
- */
44
- function fromEntries<T extends readonly [string | number, unknown]>(
45
- iterable: Iterable<T>
46
- ) {
47
- const result: Record<string | number | symbol, T[1]> = {};
48
-
49
- for (const [key, value] of Array.from(iterable)) {
50
- if (result[key]) {
51
- if (Array.isArray(result[key])) {
52
- (result[key] as Array<string | number>).push(value as string | number);
53
- } else {
54
- result[key] = [result[key], value];
55
- }
56
- } else {
57
- result[key] = value;
58
- }
59
- }
60
-
61
- return result;
62
- }
63
-
64
- /**
65
- * @description 쿼리 스트링을 객체로 변환합니다.
66
- * @example parseQueryString('?a=1&b=2&c=3') // { a: '1', b: '2', c: '3' }
67
- */
68
- function parseQueryString<Result = Record<string, string>>(
69
- queryString: string = typeof location !== 'undefined' ? location.search : ''
70
- ): Result {
71
- const query = queryString.trim().replace(/^[?#&]/, '');
72
-
73
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- return fromEntries(new URLSearchParams(query)) as any;
75
- }
76
-
77
- /**
78
- * @description 쿼리 스트링에서 특정 키의 값을 가져옵니다.
79
- *
80
- * @param name 가져올 쿼리 스트링의 키
81
- * @param parser 가져온 값을 파싱할 함수
82
- * @returns 파싱된 값
83
- *
84
- * @example
85
- * url: 'http://example.com/?a=1'
86
- * getQueryString('a') // '1'
87
- * getQueryString('a', parseInt) // 1
88
- * getQueryString('b') // undefined
89
- * getQueryString('b', parseInt) // undefined
90
- */
91
- function getQueryString(name: string): string | undefined;
92
- function getQueryString<T>(
93
- name: string,
94
- parser: (val: string) => T
95
- ): T | undefined;
96
- function getQueryString<T = string>(name: string, parser?: (val: string) => T) {
97
- const value = queryString.parse<{ [name: string]: string | undefined }>()[
98
- name
99
- ];
100
-
101
- if (parser == null || value == null) {
102
- return value;
103
- } else {
104
- return parser(value);
105
- }
106
- }
107
-
108
- /**
109
- * @description 기존 쿼리 스트링에 새로운 쿼리 스트링을 추가합니다.
110
- * @example setQueryString({ qs: '?a=1', key: 'b', value: '2' }) // '?a=1&b=2'
111
- */
112
- function setQueryString({
113
- qs,
114
- key,
115
- value,
116
- }: {
117
- qs: string;
118
- key: string;
119
- value: string;
120
- }) {
121
- const parsed = parseQueryString(qs);
122
-
123
- return createQueryString({
124
- ...parsed,
125
- [key]: value,
126
- });
127
- }
128
-
129
- export const queryString = {
130
- create: createQueryString,
131
- parse: parseQueryString,
132
- get: getQueryString,
133
- set: setQueryString,
134
- };
@@ -1,24 +0,0 @@
1
- import { coerceCssPixelValue, type CSSPixelValue } from './coerceCssPixelValue';
2
-
3
- export type SafeAreaCssValueProps = {
4
- top?: CSSPixelValue;
5
- bottom?: CSSPixelValue;
6
- left?: CSSPixelValue;
7
- right?: CSSPixelValue;
8
- };
9
-
10
- const useSafeArea = ({
11
- top: T = 0,
12
- bottom: B = 0,
13
- left: L = 0,
14
- right: R = 0,
15
- }: SafeAreaCssValueProps = {}) => {
16
- const top = `max(${coerceCssPixelValue(T)}, env(safe-area-inset-top))`;
17
- const bottom = `max(${coerceCssPixelValue(B)}, env(safe-area-inset-bottom))`;
18
- const left = `max(${coerceCssPixelValue(L)}, env(safe-area-inset-left))`;
19
- const right = `max(${coerceCssPixelValue(R)}, env(safe-area-inset-right))`;
20
-
21
- return { top, bottom, left, right };
22
- };
23
-
24
- export { useSafeArea };
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "jsx": "react-jsx",
5
- "resolveJsonModule": true,
6
- "isolatedModules": true,
7
- "esModuleInterop": true,
8
- "declaration": true,
9
- "outDir": "./dist"
10
- },
11
- "include": ["src", "./tsup.config.ts"],
12
- "exclude": ["node_modules"]
13
- }
package/tsup.config.ts DELETED
@@ -1,13 +0,0 @@
1
- import { defineConfig } from 'tsup';
2
- import pkgJson from './package.json';
3
-
4
- const external = [...Object.keys((pkgJson as any).peerDependencies || {})];
5
-
6
- export default defineConfig({
7
- entry: ['src/**/*.{ts,tsx}'],
8
- format: ['esm', 'cjs'],
9
- sourcemap: true,
10
- clean: true,
11
- dts: 'src/index.ts',
12
- external,
13
- });