@daeryun/dr_components 0.1.9 → 0.1.11

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/lib/utils.ts CHANGED
@@ -12,68 +12,65 @@
12
12
  *
13
13
  * Copyright (C) by DeaRyunLaw All right Preserved.
14
14
  */
15
+ import { ParsedUrlQueryInput } from 'node:querystring';
16
+ import { NextRouter } from 'next/router';
15
17
 
16
18
  export function htmlTagRemove(str: string) {
17
- if (str === "") return "";
19
+ if (str === '') return '';
18
20
 
19
21
  const removeMd = require('remove-markdown');
20
22
 
21
23
  str = removeMd(str);
22
24
 
23
25
  return str
24
- .replace(/<br\/>/gi, "\n")
25
- .replace(/<(\/)?([a-zA-Z]*)(\s[a-zA-Z]*=[^>]*)?(\s)*(\/)?>/gi, "")
26
- .replace(/&amp;/g, "&")
27
- .replace(/&lt;/g, "<")
28
- .replace(/&gt;/g, ">")
26
+ .replace(/<br\/>/gi, '\n')
27
+ .replace(/<(\/)?([a-zA-Z]*)(\s[a-zA-Z]*=[^>]*)?(\s)*(\/)?>/gi, '')
28
+ .replace(/&amp;/g, '&')
29
+ .replace(/&lt;/g, '<')
30
+ .replace(/&gt;/g, '>')
29
31
  .replace(/&quot;/g, '"')
30
32
  .replace(/&#039;/g, "'")
31
33
  .replace(/&#39;/g, "'")
32
- .replace(/&nbsp;/g, " ");
34
+ .replace(/&nbsp;/g, ' ');
33
35
  }
34
36
 
35
37
  export function htmlUrlDecode(str: string) {
36
- if (str === "") return "";
38
+ if (str === '') return '';
37
39
 
38
40
  return str
39
- .replace(/&amp;/g, "&")
40
- .replace(/&lt;/g, "<")
41
- .replace(/&gt;/g, ">")
41
+ .replace(/&amp;/g, '&')
42
+ .replace(/&lt;/g, '<')
43
+ .replace(/&gt;/g, '>')
42
44
  .replace(/&quot;/g, '"')
43
45
  .replace(/&#039;/g, "'")
44
46
  .replace(/&#39;/g, "'")
45
- .replace(/&nbsp;/g, " ");
47
+ .replace(/&nbsp;/g, ' ');
46
48
  }
47
49
 
48
50
  export function htmlUrlEncode(str: string) {
49
- if (str === "") return "";
51
+ if (str === '') return '';
50
52
 
51
53
  return str
52
- .replace(new RegExp("&", 'g'), "&amp;")
53
- .replace(new RegExp("<", 'g'), "&lt;")
54
- .replace(new RegExp(">", 'g'), "&gt;")
55
- .replace(new RegExp('"', 'g'), "&quot;")
56
- .replace(new RegExp("'", 'g'), "&#039;")
57
- .replace(new RegExp("'", 'g'), "&#39;")
58
- .replace(new RegExp("\r\n", 'g'), " ")
59
- .replace(new RegExp("\n", 'g'), " ");
54
+ .replace(new RegExp('&', 'g'), '&amp;')
55
+ .replace(new RegExp('<', 'g'), '&lt;')
56
+ .replace(new RegExp('>', 'g'), '&gt;')
57
+ .replace(new RegExp('"', 'g'), '&quot;')
58
+ .replace(new RegExp("'", 'g'), '&#039;')
59
+ .replace(new RegExp("'", 'g'), '&#39;')
60
+ .replace(new RegExp('\r\n', 'g'), ' ')
61
+ .replace(new RegExp('\n', 'g'), ' ');
60
62
  }
61
63
 
62
64
  export function numComma(x: number) {
63
65
  if (x === undefined || x === null) {
64
- return "";
66
+ return '';
65
67
  }
66
- return ("" + x).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
68
+ return ('' + x).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
67
69
  }
68
70
 
69
- export function bodyFetch(
70
- url: string,
71
- sendData: any,
72
- processFn: any,
73
- method = "POST"
74
- ) {
75
- let myHeaders = new Headers();
76
- myHeaders.append("Content-Type", "application/json");
71
+ export async function bodyFetch(url: string, sendData: any, processFn: any, method = 'POST') {
72
+ const myHeaders = new Headers();
73
+ myHeaders.append('Content-Type', 'application/json');
77
74
 
78
75
  let parsedJson;
79
76
  try {
@@ -83,52 +80,61 @@ export function bodyFetch(
83
80
  parsedJson = null;
84
81
  }
85
82
 
86
- let requestOptions = {
83
+ const requestOptions = {
87
84
  method: method,
88
85
  headers: myHeaders,
89
- body: parsedJson,
86
+ body: parsedJson
90
87
  };
91
88
 
92
- fetch(url, requestOptions)
93
- .then((response) => response.json())
94
- .then(processFn)
95
- .catch((error) => {
96
- console.error(error);
97
- });
89
+ try {
90
+ const response = await fetch(url, requestOptions);
91
+ if (!response.ok) {
92
+ throw new Error(`HTTP error! status: ${response.status}`);
93
+ }
94
+ const data = await response.json();
95
+ processFn(data);
96
+ } catch (error) {
97
+ console.error('Error:', error);
98
+ throw error;
99
+ }
98
100
  }
99
101
 
100
- export function paramFetch(
101
- url: string,
102
- sendData: any,
103
- processFn: any,
104
- method = "POST"
105
- ) {
106
- let myHeaders = new Headers();
102
+ export async function paramFetch(url: string, sendData: any, processFn: any, method = 'POST') {
103
+ const myHeaders = new Headers();
107
104
 
108
- let sendParam = "";
105
+ let sendParam = '';
109
106
  for (const key in sendData) {
110
- if (sendParam !== "") sendParam += "&";
111
- sendParam += key + "=" + sendData[key];
107
+ if (sendParam !== '') sendParam += '&';
108
+ sendParam += key + '=' + sendData[key];
112
109
  }
113
110
 
114
- let requestOptions = {
111
+ const requestOptions = {
115
112
  method: method,
116
- headers: myHeaders,
113
+ headers: myHeaders
117
114
  };
118
115
 
119
- url += "?" + sendParam;
120
- fetch(url, requestOptions)
121
- .then((response) => response.json())
122
- .then(processFn)
123
- .catch((error) => {
124
- console.error(error);
125
- });
116
+ url += '?' + sendParam;
117
+
118
+ try {
119
+ const response = await fetch(url, requestOptions);
120
+
121
+ if (!response.ok) {
122
+ throw new Error(`HTTP error! status: ${response.status}`);
123
+ }
124
+ const data = await response.json();
125
+
126
+ if (processFn != null) {
127
+ processFn(data);
128
+ }
129
+ } catch (error) {
130
+ console.error('Error:', error);
131
+ throw error;
132
+ }
126
133
  }
127
134
 
128
135
  //이메일 정규식
129
136
  export function isEmail(asValue: string) {
130
- const regExp =
131
- /^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i;
137
+ const regExp = /^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i;
132
138
  return regExp.test(asValue); // 형식에 맞는 경우 true 리턴
133
139
  }
134
140
 
@@ -156,70 +162,60 @@ export function isCelluar(asValue: string) {
156
162
  }
157
163
  // 생년월일 체크 정규식
158
164
  export function isBirthDay(asValue: string) {
159
- const regExp =
160
- /^(19[0-9][0-9]|20\d{2})(0[0-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/;
165
+ const regExp = /^(19[0-9][0-9]|20\d{2})(0[0-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/;
161
166
  return regExp.test(asValue); // 형식에 맞는 경우 true 리턴
162
167
  }
163
168
  // 핸드폰 번호 체크 정규식
164
169
  export function setCelluar(asValue: string) {
165
- if (asValue === undefined || asValue === "" || asValue === null) {
166
- return "";
170
+ if (asValue === undefined || asValue === '' || asValue === null) {
171
+ return '';
167
172
  } else {
168
- return asValue.replace(
169
- /(^02.{0}|^01.{1}|[0-9]{3})([0-9]+)([0-9]{4})/,
170
- "$1-$2-$3"
171
- );
173
+ return asValue.replace(/(^02.{0}|^01.{1}|[0-9]{3})([0-9]+)([0-9]{4})/, '$1-$2-$3');
172
174
  }
173
175
  }
174
176
 
175
177
  export function dateFomat(date: string, formatStr: string) {
176
- const moment = require("moment");
178
+ const dayjs = require('dayjs');
177
179
 
178
- const returnDate = moment(date).format(formatStr);
180
+ const returnDate = dayjs(date).format(formatStr);
179
181
  return returnDate;
180
182
  }
181
183
 
182
184
  export function nowDate(formatStr: string) {
183
- const moment = require("moment");
185
+ const dayjs = require('dayjs');
184
186
 
185
- const returnDate = moment().format(formatStr);
187
+ const returnDate = dayjs().format(formatStr);
186
188
  return returnDate;
187
189
  }
188
190
 
189
191
  export function dateAddDay(formatStr: string, addDay: number) {
190
- const moment = require("moment");
192
+ const dayjs = require('dayjs');
191
193
 
192
- const returnDate = moment().add(addDay, "day").format(formatStr);
194
+ const returnDate = dayjs().add(addDay, 'day').format(formatStr);
193
195
  return returnDate;
194
196
  }
195
197
  export function dateAddMonth(formatStr: string, addMonth: number) {
196
- const moment = require("moment");
198
+ const dayjs = require('dayjs');
197
199
 
198
- const returnDate = moment().add(addMonth, "month").format(formatStr);
200
+ const returnDate = dayjs().add(addMonth, 'month').format(formatStr);
199
201
  return returnDate;
200
202
  }
201
203
 
202
204
  //전화번호 마스킹 20220816 임의로 수정이후 테스트안함. 사용시 테스트해보고하세요~~
203
205
  export function maskTell(tell: string) {
204
- if (tell === undefined || tell === null) return "";
205
- let maskingData = "";
206
- let phoneMatchValue: RegExpMatchArray | null = tell.match(
207
- /\d{2,3}-\d{3,4}-\d{4}/gi
208
- );
206
+ if (tell === undefined || tell === null) return '';
207
+ let maskingData = '';
208
+ let phoneMatchValue: RegExpMatchArray | null = tell.match(/\d{2,3}-\d{3,4}-\d{4}/gi);
209
209
  if (phoneMatchValue === null) {
210
- return "";
210
+ return '';
211
211
  }
212
212
  let str = phoneMatchValue.toString();
213
213
  if (/-[0-9]{3}-/.test(str)) {
214
214
  // 000-000-0000 형태인 경우
215
- maskingData = tell
216
- .toString()
217
- .replace(str, str.replace(/-[0-9]{3}-/g, "-***-"));
215
+ maskingData = tell.toString().replace(str, str.replace(/-[0-9]{3}-/g, '-***-'));
218
216
  } else if (/-[0-9]{4}-/.test(str)) {
219
217
  // 000-0000-0000 형태인 경우
220
- maskingData = tell
221
- .toString()
222
- .replace(str, str.replace(/-[0-9]{4}-/g, "-****-"));
218
+ maskingData = tell.toString().replace(str, str.replace(/-[0-9]{4}-/g, '-****-'));
223
219
  } else {
224
220
  // - 없을 경우
225
221
  phoneMatchValue =
@@ -229,29 +225,21 @@ export function maskTell(tell: string) {
229
225
  : tell.match(/\d{10}/gi)
230
226
  : tell.match(/\d{11}/gi);
231
227
  if (phoneMatchValue === null) {
232
- return "";
228
+ return '';
233
229
  }
234
230
  str = phoneMatchValue.toString();
235
231
  if (tell.length < 11) {
236
232
  if (tell.length < 10) {
237
- maskingData = tell
238
- .toString()
239
- .replace(str, str.replace(/(\d{2})(\d{3})(\d{4})/gi, "$1-***-$3"));
233
+ maskingData = tell.toString().replace(str, str.replace(/(\d{2})(\d{3})(\d{4})/gi, '$1-***-$3'));
240
234
  } else {
241
- if (tell.indexOf("02") < 0) {
242
- maskingData = tell
243
- .toString()
244
- .replace(str, str.replace(/(\d{3})(\d{3})(\d{4})/gi, "$1-***-$3"));
235
+ if (tell.indexOf('02') < 0) {
236
+ maskingData = tell.toString().replace(str, str.replace(/(\d{3})(\d{3})(\d{4})/gi, '$1-***-$3'));
245
237
  } else {
246
- maskingData = tell
247
- .toString()
248
- .replace(str, str.replace(/(\d{2})(\d{4})(\d{4})/gi, "$1-****-$3"));
238
+ maskingData = tell.toString().replace(str, str.replace(/(\d{2})(\d{4})(\d{4})/gi, '$1-****-$3'));
249
239
  }
250
240
  }
251
241
  } else {
252
- maskingData = tell
253
- .toString()
254
- .replace(str, str.replace(/(\d{3})(\d{4})(\d{4})/gi, "$1-****-$3"));
242
+ maskingData = tell.toString().replace(str, str.replace(/(\d{3})(\d{4})(\d{4})/gi, '$1-****-$3'));
255
243
  }
256
244
  }
257
245
  return maskingData;
@@ -259,23 +247,23 @@ export function maskTell(tell: string) {
259
247
 
260
248
  //이름 마스킹
261
249
  export function maskName(name: string) {
262
- if (name === null || name === undefined) return "";
263
- return setCharAt(name, 1, "*");
250
+ if (name === null || name === undefined) return '';
251
+ return setCharAt(name, 1, '*');
264
252
  }
265
253
 
266
254
  //이메일 마스킹
267
255
  export function maskEmail(email: string) {
268
- if (email === null || email === undefined) return "";
269
- var temp = email.split("@");
270
- var maskEmail;
256
+ if (email === null || email === undefined) return '';
257
+ const temp = email.split('@');
258
+ let maskEmail;
271
259
  //아이디 뒤 세자리
272
260
  if (temp[0].length > 3) {
273
- maskEmail = temp[0].substring(0, temp[0].length - 3) + "***";
261
+ maskEmail = temp[0].substring(0, temp[0].length - 3) + '***';
274
262
  } else {
275
- maskEmail = "***";
263
+ maskEmail = '***';
276
264
  }
277
265
  if (temp[1] !== undefined) {
278
- return maskEmail + "@" + temp[1];
266
+ return maskEmail + '@' + temp[1];
279
267
  } else {
280
268
  return maskEmail;
281
269
  }
@@ -287,15 +275,93 @@ function setCharAt(str: string, index: number, chr: string) {
287
275
  return str.substr(0, index) + chr + str.substr(index + 1);
288
276
  }
289
277
 
290
- export function cutString(str:string,length:number){
291
- if(str.length>length-3){
292
- let tempStr = htmlTagRemove(str)
293
- return tempStr.substring(0,length-3)+"..."
294
- }else{
295
- return htmlTagRemove(str)
278
+ export function cutString(str: string, length: number) {
279
+ if (str.length > length) {
280
+ return htmlTagRemove(str).substring(0, length);
281
+ } else {
282
+ return htmlTagRemove(str);
296
283
  }
284
+ }
285
+
286
+ export function isCurrentTimeWithinPopupRange(
287
+ days: number[],
288
+ startHour: number | null,
289
+ startMin: number,
290
+ endHour: number,
291
+ endMin: number
292
+ ) {
293
+ if (startHour == null || startMin == null || endHour == null || endMin == null) {
294
+ return true;
295
+ }
296
+ const today = new Date();
297
+ const currentDayIndex = today.getDay();
297
298
 
299
+ const popupStartTime = new Date();
300
+ popupStartTime.setHours(startHour, startMin, 0);
301
+
302
+ const popupEndTime = new Date();
303
+ popupEndTime.setHours(endHour, endMin, 59);
304
+
305
+ return days.includes(currentDayIndex) && today >= popupStartTime && today <= popupEndTime;
298
306
  }
299
- export const getLangClassName = (type: string | undefined, kor: string, en: string, jp: string, cn:string) => {
300
- return (type == "ko" ? ` ${kor} ` : type == "en" ? ` ${en} ` : type === 'jp' ? ` ${jp} ` : type === 'cn' ? ` ${cn} ` : '')
301
- }
307
+
308
+ export const formatPhoneNumber = (str: string) => {
309
+ if (typeof str !== 'string') return '';
310
+ str = str.replace(/[^0-9]/g, '');
311
+ if (str.indexOf('82') == 0) {
312
+ return str.replace(/(^82)(2|\d{2})(\d+)?(\d{4})$/, '+$1-$2-$3-$4'); // +82
313
+ } else if (str.indexOf('1') == 0) {
314
+ return str.replace(/(^1\d{3})(\d{4})$/, '$1-$2'); // 1588, 1566, 1677, ...
315
+ }
316
+ return str.replace(/(^02|^0504|^0505|^0\d{2})(\d+)?(\d{4})$/, '$1-$2-$3'); // 02/0504/0505/010/011/031
317
+ };
318
+
319
+ export function telValidator(tel: string) {
320
+ return /^[0-9]{2,3}-[0-9]{3,4}-[0-9]{4}/.test(tel);
321
+ }
322
+
323
+ //첫번째 글자가 자음 포함하는지 체크하는 정규식, 쌍자음 포함
324
+ //str > ㄱ,ㄴ,ㄷ,ㄹ
325
+ //testStr > 체크할 단어
326
+ export const consonantRegExp = (str: string, testStr: string) => {
327
+ switch (str) {
328
+ case 'ㄱ':
329
+ return testStr.match(/^[가-깋|까-낗]/);
330
+ case 'ㄴ':
331
+ return testStr.match(/^[나-닣]/);
332
+ case 'ㄷ':
333
+ return testStr.match(/^[다-딯|따-띻]/);
334
+ case 'ㄹ':
335
+ return testStr.match(/^[라-맇]/);
336
+ case 'ㅁ':
337
+ return testStr.match(/^[마-밓]/);
338
+ case 'ㅂ':
339
+ return testStr.match(/^[바-빟|빠-삫]/);
340
+ case 'ㅅ':
341
+ return testStr.match(/^[사-싷|싸-앃]/);
342
+ case 'ㅇ':
343
+ return testStr.match(/^[아-잏]/);
344
+ case 'ㅈ':
345
+ return testStr.match(/^[자-짛|짜-찧]/);
346
+ case 'ㅊ':
347
+ return testStr.match(/^[차-칳]/);
348
+ case 'ㅋ':
349
+ return testStr.match(/^[카-킿]/);
350
+ case 'ㅌ':
351
+ return testStr.match(/^[타-팋]/);
352
+ case 'ㅍ':
353
+ return testStr.match(/^[파-핗]/);
354
+ case 'ㅎ':
355
+ return testStr.match(/^[하-힣]/);
356
+ default:
357
+ return true;
358
+ }
359
+ };
360
+
361
+ export const getDomainHandler = (centerCode: string, subDomain: string) => {
362
+ if (centerCode === 'CT0000') {
363
+ return `https://www.daeryunlaw.com`;
364
+ } else {
365
+ return `https://www.daeryunlaw-${subDomain}.com`;
366
+ }
367
+ };