@uniai-fe/util-functions 0.0.1
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/package.json +64 -0
- package/src/form/checkbox.ts +41 -0
- package/src/functions/api.server.ts +114 -0
- package/src/functions/api.ts +515 -0
- package/src/functions/chart.ts +304 -0
- package/src/functions/convert.ts +229 -0
- package/src/functions/crypto.ts +44 -0
- package/src/functions/date.ts +386 -0
- package/src/functions/file.ts +57 -0
- package/src/functions/format.ts +318 -0
- package/src/functions/log.ts +9 -0
- package/src/functions/mask.ts +175 -0
- package/src/functions/reg-exp.ts +26 -0
- package/src/functions/route.ts +46 -0
- package/src/functions/sort.ts +71 -0
- package/src/functions/validation.ts +155 -0
- package/src/index.tsx +20 -0
- package/src/react/convert.tsx +129 -0
- package/src/react/match.tsx +41 -0
- package/src/style/size.ts +128 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { isValidDateType, isValidTimeStringFormat } from "./validation";
|
|
2
|
+
import { dateFormat } from "./format";
|
|
3
|
+
import type {
|
|
4
|
+
CheckDateMomentType,
|
|
5
|
+
ScheduleBYDAY,
|
|
6
|
+
ScheduleFREQ,
|
|
7
|
+
ScheduleRepeatCycleDateType,
|
|
8
|
+
} from "@uniai/types";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 오늘 날짜를 yyyy-mm-dd 포맷 string으로 추출
|
|
12
|
+
* @util
|
|
13
|
+
* @param {string} [time] HH:MM:SS
|
|
14
|
+
* @return {string} yyyy-mm-dd
|
|
15
|
+
*/
|
|
16
|
+
export const getToday = (time?: string): string =>
|
|
17
|
+
`${dateFormat(new Date())}${time && isValidTimeStringFormat(time) ? `T${time}` : ""}`;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 과거 특정날짜 추출
|
|
21
|
+
* @util
|
|
22
|
+
* @param {string | Date} date
|
|
23
|
+
* @param {number} [weeks] 과거 몇 주 전
|
|
24
|
+
* @param {number} [days] 과거 며칠 전
|
|
25
|
+
* @return {Date | undefined}
|
|
26
|
+
*/
|
|
27
|
+
export function getPrevDay(
|
|
28
|
+
date: string | Date,
|
|
29
|
+
weeks?: number,
|
|
30
|
+
days?: number,
|
|
31
|
+
): Date | undefined {
|
|
32
|
+
if (!isValidDateType(date)) return undefined;
|
|
33
|
+
const standardDate = new Date(date);
|
|
34
|
+
|
|
35
|
+
const prevWeekDay: number = typeof weeks === "undefined" ? 0 : 7 * weeks;
|
|
36
|
+
const prevDays: number = typeof days === "undefined" ? 1 : days;
|
|
37
|
+
const prevDayAmount: number = prevWeekDay !== 0 ? prevWeekDay : prevDays;
|
|
38
|
+
|
|
39
|
+
standardDate.setDate(standardDate.getDate() - prevDayAmount);
|
|
40
|
+
|
|
41
|
+
return new Date(standardDate);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 미래 특정날짜 추출
|
|
46
|
+
* @util
|
|
47
|
+
* @param {string | Date} date
|
|
48
|
+
* @param {number} [weeks] 미래 몇 주 후
|
|
49
|
+
* @param {number} [days] 미래 며칠 후
|
|
50
|
+
* @return {Date | undefined}
|
|
51
|
+
*/
|
|
52
|
+
export function getNextDay(
|
|
53
|
+
date: string | Date,
|
|
54
|
+
weeks?: number,
|
|
55
|
+
days?: number,
|
|
56
|
+
): Date | undefined {
|
|
57
|
+
if (!isValidDateType(date)) return undefined;
|
|
58
|
+
const standardDate = new Date(date);
|
|
59
|
+
|
|
60
|
+
const nextWeekDay: number = typeof weeks === "undefined" ? 0 : 7 * weeks;
|
|
61
|
+
const nextDays: number = typeof days === "undefined" ? 1 : days;
|
|
62
|
+
const nextDayAmount: number = nextWeekDay !== 0 ? nextWeekDay : nextDays;
|
|
63
|
+
|
|
64
|
+
standardDate.setDate(standardDate.getDate() + nextDayAmount);
|
|
65
|
+
|
|
66
|
+
return new Date(standardDate);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 과거 특정시점의 주간 날짜 배열 추출
|
|
71
|
+
* @util
|
|
72
|
+
* @param {string | Date} date
|
|
73
|
+
* @param {number} weeks 과거 몇 주 전
|
|
74
|
+
* @return {Date[]}
|
|
75
|
+
*/
|
|
76
|
+
export function getLastWeeks(date: string | Date, weeks: number = 1): Date[] {
|
|
77
|
+
if (!isValidDateType(date)) return [];
|
|
78
|
+
const lastWeeks: Date[] = [];
|
|
79
|
+
for (let d = 1; d <= 7 * weeks; d++) {
|
|
80
|
+
const prevDate = new Date(date);
|
|
81
|
+
lastWeeks.unshift(new Date(prevDate.setDate(prevDate.getDate() - d)));
|
|
82
|
+
}
|
|
83
|
+
return lastWeeks;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 미래 특정시점의 주간 날짜 배열 추출
|
|
88
|
+
* @util
|
|
89
|
+
* @param {string | Date} date
|
|
90
|
+
* @param {number} weeks 미래 몇 주 전
|
|
91
|
+
* @return {Date[]}
|
|
92
|
+
*/
|
|
93
|
+
export function getNextWeeks(date: string | Date, weeks: number = 1): Date[] {
|
|
94
|
+
if (!isValidDateType(date)) return [];
|
|
95
|
+
const startDate = new Date(date);
|
|
96
|
+
return new Array(weeks * 7)
|
|
97
|
+
.fill(startDate)
|
|
98
|
+
.map((date: Date, index: number) => {
|
|
99
|
+
const nextDate = new Date(date);
|
|
100
|
+
return new Date(nextDate.setDate(nextDate.getDate() + index));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 지정된 범위 내의 모든 날짜 배열 추출
|
|
106
|
+
* @util
|
|
107
|
+
* @param {string | Date} startDate
|
|
108
|
+
* @param {string | Date} endDate
|
|
109
|
+
* @param {boolean} [isFormat] 날짜 배열을 yyyy-mm-dd string으로 생성할지 여부
|
|
110
|
+
* @return {(Date | string)[]}
|
|
111
|
+
*/
|
|
112
|
+
export function getDateArray(
|
|
113
|
+
startDate: string | Date,
|
|
114
|
+
endDate: string | Date,
|
|
115
|
+
isFormat?: boolean,
|
|
116
|
+
): (Date | string)[] {
|
|
117
|
+
if (!isValidDateType(startDate) || !isValidDateType(endDate)) return [];
|
|
118
|
+
|
|
119
|
+
const dayUnit = 1000 * 60 * 60 * 24;
|
|
120
|
+
const start = new Date(dateFormat(startDate));
|
|
121
|
+
const end = new Date(dateFormat(endDate));
|
|
122
|
+
const array: (Date | string)[] = [];
|
|
123
|
+
for (let day = start.getTime(); day <= end.getTime(); day += dayUnit) {
|
|
124
|
+
const d = isFormat ? dateFormat(new Date(day)) : new Date(day);
|
|
125
|
+
array.push(d);
|
|
126
|
+
}
|
|
127
|
+
return array;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 현재 시점을 기준으로 한 특정 날짜의 시점 추출
|
|
132
|
+
* @util
|
|
133
|
+
* @param {string | Date} date
|
|
134
|
+
* @return {CheckDateMomentType}
|
|
135
|
+
*/
|
|
136
|
+
export function checkDateMoment(date: string | Date): CheckDateMomentType {
|
|
137
|
+
const res: CheckDateMomentType = {
|
|
138
|
+
isToday: false,
|
|
139
|
+
isPast: false,
|
|
140
|
+
isFuture: false,
|
|
141
|
+
moment: "",
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
if (!isValidDateType(date)) return res;
|
|
145
|
+
|
|
146
|
+
const standardDate = dateFormat(date);
|
|
147
|
+
const standardTime = new Date(standardDate).getTime();
|
|
148
|
+
const today = getToday();
|
|
149
|
+
const todayTime = new Date(today).getTime();
|
|
150
|
+
|
|
151
|
+
if (standardDate === today) {
|
|
152
|
+
res.isToday = true;
|
|
153
|
+
res.moment = "today";
|
|
154
|
+
} else if (standardTime < todayTime) {
|
|
155
|
+
res.isPast = true;
|
|
156
|
+
res.moment = "past";
|
|
157
|
+
} else if (standardTime > todayTime) {
|
|
158
|
+
res.isFuture = true;
|
|
159
|
+
res.moment = "future";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return res;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 특정 날짜의 해당 월 주차 수
|
|
167
|
+
* @util
|
|
168
|
+
* @param {string|Date} date
|
|
169
|
+
* @return {number} 몇주차
|
|
170
|
+
*/
|
|
171
|
+
export function weekOrderIndex(date: string | Date): number {
|
|
172
|
+
if (date && new Date(date) instanceof Date) {
|
|
173
|
+
const standardDate = new Date(date).getDate();
|
|
174
|
+
const firstWeekday = new Date(new Date(date).setDate(1)).getDay();
|
|
175
|
+
return Math.ceil((standardDate + firstWeekday) / 7);
|
|
176
|
+
}
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 반복 주기의 명칭 한글표기로 변환
|
|
182
|
+
* @util
|
|
183
|
+
* @param {ScheduleFREQ} FREQ "DAILY" | "WEEKLY" | "MONTHLY" | "YEARLY"
|
|
184
|
+
* @return {string} "일" | "주" | "개월" | "년"
|
|
185
|
+
*/
|
|
186
|
+
export function getRepeatCycleUnit(FREQ: ScheduleFREQ): string {
|
|
187
|
+
switch (FREQ) {
|
|
188
|
+
case "DAILY":
|
|
189
|
+
return "일";
|
|
190
|
+
case "WEEKLY":
|
|
191
|
+
return "주";
|
|
192
|
+
case "MONTHLY":
|
|
193
|
+
return "개월";
|
|
194
|
+
case "YEARLY":
|
|
195
|
+
return "년";
|
|
196
|
+
default:
|
|
197
|
+
return "주기";
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* weekOrderIdx()의 index값을 "N번째"로 텍스트 변환
|
|
203
|
+
* @util
|
|
204
|
+
* @param {number|string} [weekIndex] 몇주차 인덱스
|
|
205
|
+
* @return {string}
|
|
206
|
+
*/
|
|
207
|
+
export function weekOrderKo(weekIndex?: string | number): string {
|
|
208
|
+
if (typeof weekIndex === "undefined") return "";
|
|
209
|
+
switch (String(weekIndex)) {
|
|
210
|
+
case "1":
|
|
211
|
+
return "첫번째";
|
|
212
|
+
case "2":
|
|
213
|
+
return "두번째";
|
|
214
|
+
case "3":
|
|
215
|
+
return "세번째";
|
|
216
|
+
case "4":
|
|
217
|
+
return "네번째";
|
|
218
|
+
case "5":
|
|
219
|
+
return "다섯번째";
|
|
220
|
+
case "6":
|
|
221
|
+
return "여섯번째";
|
|
222
|
+
default:
|
|
223
|
+
return "";
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 요일 인덱스에서 요일 코드 추출
|
|
229
|
+
* @util
|
|
230
|
+
* @param {number|string} weekdayIndex
|
|
231
|
+
* @return {ScheduleBYDAY} "MO"|"TU"|"WE"|"TH"|"FR"|"SA"|"SU"|""
|
|
232
|
+
*/
|
|
233
|
+
export function weekdayCodeByWeekdayIndex(
|
|
234
|
+
weekdayIndex?: number | string,
|
|
235
|
+
): ScheduleBYDAY {
|
|
236
|
+
if (typeof weekdayIndex === "undefined") return "";
|
|
237
|
+
|
|
238
|
+
switch (String(weekdayIndex)) {
|
|
239
|
+
case "0":
|
|
240
|
+
return "SU";
|
|
241
|
+
case "1":
|
|
242
|
+
return "MO";
|
|
243
|
+
case "2":
|
|
244
|
+
return "TU";
|
|
245
|
+
case "3":
|
|
246
|
+
return "WE";
|
|
247
|
+
case "4":
|
|
248
|
+
return "TH";
|
|
249
|
+
case "5":
|
|
250
|
+
return "FR";
|
|
251
|
+
case "6":
|
|
252
|
+
return "SA";
|
|
253
|
+
default:
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 요일 인덱스에서 "N요일" 텍스트 생성
|
|
260
|
+
* @util
|
|
261
|
+
* @param {string|number|ScheduleBYDAY} [dayCode] 요일 인덱스 또는 R-rule 요일 코드
|
|
262
|
+
* @param {boolean} [isFull] "요일"까지 붙은 전체 명칭생성 여부
|
|
263
|
+
* @return {string} "일(요일)","월(요일)", ..., "토(요일)"
|
|
264
|
+
*/
|
|
265
|
+
export function weekdayByDayCode(
|
|
266
|
+
dayCode?: string | number | ScheduleBYDAY,
|
|
267
|
+
isFull?: boolean,
|
|
268
|
+
): string {
|
|
269
|
+
if (typeof dayCode === "undefined") return "";
|
|
270
|
+
|
|
271
|
+
const fullName = isFull ? "요일" : "";
|
|
272
|
+
const code = String(dayCode);
|
|
273
|
+
|
|
274
|
+
if (code === "0" || code.includes("SU")) return `일${fullName}`;
|
|
275
|
+
else if (code === "1" || code.includes("MO")) return `월${fullName}`;
|
|
276
|
+
else if (code === "2" || code.includes("TU")) return `화${fullName}`;
|
|
277
|
+
else if (code === "3" || code.includes("WE")) return `수${fullName}`;
|
|
278
|
+
else if (code === "4" || code.includes("TH")) return `목${fullName}`;
|
|
279
|
+
else if (code === "5" || code.includes("FR")) return `금${fullName}`;
|
|
280
|
+
else if (code === "6" || code.includes("SA")) return `토${fullName}`;
|
|
281
|
+
|
|
282
|
+
return ``;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 월간/연간 반복에 대한 지정 날짜 코드 parsing
|
|
287
|
+
* @util
|
|
288
|
+
* @param {string} cycleDateString 전체 명칭생성 여부
|
|
289
|
+
* @return {ScheduleRepeatCycleDateType | undefined}
|
|
290
|
+
*/
|
|
291
|
+
export function generateRepeatCycleDate(
|
|
292
|
+
cycleDateString: string,
|
|
293
|
+
): ScheduleRepeatCycleDateType | undefined {
|
|
294
|
+
const isRepeatDate = cycleDateString?.startsWith("date,") || false;
|
|
295
|
+
const isRepeatWeek = cycleDateString?.startsWith("weekday,") || false;
|
|
296
|
+
|
|
297
|
+
if (cycleDateString === "" || !(isRepeatDate || isRepeatWeek))
|
|
298
|
+
return undefined;
|
|
299
|
+
|
|
300
|
+
const selected = cycleDateString.split(",");
|
|
301
|
+
|
|
302
|
+
const repeatCycleDate: ScheduleRepeatCycleDateType = {
|
|
303
|
+
cycleDateUnit: selected[0] as "date" | "weekday",
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
if (selected[0] === "date") {
|
|
307
|
+
const repeatDay = selected[1]?.split("_")?.[1] || "";
|
|
308
|
+
Object.assign(repeatCycleDate, { repeatDay });
|
|
309
|
+
|
|
310
|
+
if (selected[2] !== "" && selected[2]?.startsWith("month_")) {
|
|
311
|
+
const repeatMonth = selected[2].split("_")?.[1] || "";
|
|
312
|
+
Object.assign(repeatCycleDate, { repeatMonth });
|
|
313
|
+
}
|
|
314
|
+
} else if (selected[0] === "weekday") {
|
|
315
|
+
const repeatWeekOrder = selected[1]?.split("_")?.[1] || "";
|
|
316
|
+
const repeatWeekDayIndex = selected[2]?.split("_")?.[1] || "";
|
|
317
|
+
Object.assign(repeatCycleDate, { repeatWeekOrder, repeatWeekDayIndex });
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return repeatCycleDate;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* 반복 지정 주기/날짜/요일에 대해, 코드에서 한글로 변환
|
|
325
|
+
* @util
|
|
326
|
+
* @param {string} repeatCycleDate 반복 주기/날짜/요일 코드
|
|
327
|
+
* @return {string}
|
|
328
|
+
*/
|
|
329
|
+
export function repeatCycleDateKo(repeatCycleDate?: string): string {
|
|
330
|
+
if (typeof repeatCycleDate === "undefined" || repeatCycleDate === "")
|
|
331
|
+
return "";
|
|
332
|
+
|
|
333
|
+
const option = generateRepeatCycleDate(repeatCycleDate as string);
|
|
334
|
+
if (option === undefined) return "";
|
|
335
|
+
|
|
336
|
+
if (option.cycleDateUnit === "date") {
|
|
337
|
+
const repeatMonthKo = option.repeatMonth ? `${option.repeatMonth}월 ` : "";
|
|
338
|
+
const repeatDayKo = option.repeatDay ? `${option.repeatDay}일` : "";
|
|
339
|
+
return `${repeatMonthKo}${repeatDayKo}`;
|
|
340
|
+
} else if (option.cycleDateUnit === "weekday") {
|
|
341
|
+
const repeatWeekOrderKo = weekOrderKo(option.repeatWeekOrder);
|
|
342
|
+
const repeatWeekDayKo = weekdayByDayCode(option.repeatWeekDayIndex, true);
|
|
343
|
+
return `${repeatWeekOrderKo} ${repeatWeekDayKo}`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return "";
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 반복 지정 정보 전체를 한글표현으로 생성
|
|
351
|
+
* @util
|
|
352
|
+
* @param {ScheduleFREQ | undefined} repeatUnit
|
|
353
|
+
* @param {string | number | undefined} repeatCycle
|
|
354
|
+
* @param {string | undefined} repeatCycleDate
|
|
355
|
+
* @param {string | undefined} repeatWeekly
|
|
356
|
+
* @return {string}
|
|
357
|
+
*/
|
|
358
|
+
export function convertSelectedRepeatInfo(
|
|
359
|
+
repeatUnit: ScheduleFREQ | undefined,
|
|
360
|
+
repeatCycle: string | number | undefined,
|
|
361
|
+
repeatCycleDate: string | undefined,
|
|
362
|
+
repeatWeekly: string | undefined,
|
|
363
|
+
): string {
|
|
364
|
+
const weekdayKorean: string =
|
|
365
|
+
repeatUnit === "WEEKLY" &&
|
|
366
|
+
typeof repeatWeekly !== "undefined" &&
|
|
367
|
+
repeatWeekly !== ""
|
|
368
|
+
? String(repeatWeekly)
|
|
369
|
+
.split(",")
|
|
370
|
+
.map((dayIndex: string) => weekdayByDayCode(dayIndex as string, true))
|
|
371
|
+
.join(",")
|
|
372
|
+
: "";
|
|
373
|
+
|
|
374
|
+
switch (repeatUnit) {
|
|
375
|
+
case "DAILY":
|
|
376
|
+
return `${repeatCycle}일마다`;
|
|
377
|
+
case "WEEKLY":
|
|
378
|
+
return `매주 ${weekdayKorean}`;
|
|
379
|
+
case "MONTHLY":
|
|
380
|
+
return `${repeatCycle}개월마다 ${repeatCycleDateKo(repeatCycleDate)}`;
|
|
381
|
+
case "YEARLY":
|
|
382
|
+
return `${repeatCycle}년마다 ${repeatCycleDateKo(repeatCycleDate)}`;
|
|
383
|
+
default:
|
|
384
|
+
return "--";
|
|
385
|
+
}
|
|
386
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 파일 유틸리티; 다운로드
|
|
5
|
+
* @util
|
|
6
|
+
* @param {Object} options - 다운로드 옵션
|
|
7
|
+
* @param {string} options.url - 다운로드할 파일의 URL
|
|
8
|
+
* @param {string} options.name - 다운로드할 파일의 이름
|
|
9
|
+
*/
|
|
10
|
+
export const fileDownload = ({ url, name }: { url: string; name: string }) =>
|
|
11
|
+
fetch(url)
|
|
12
|
+
.then(res => {
|
|
13
|
+
const contentType = res.headers.get("content-type") || "";
|
|
14
|
+
if (!res.ok || contentType.includes("text/html")) {
|
|
15
|
+
throw new Error("파일을 찾을 수 없거나 다운로드할 수 없습니다.");
|
|
16
|
+
}
|
|
17
|
+
return res.blob();
|
|
18
|
+
})
|
|
19
|
+
.then(blob => {
|
|
20
|
+
const url = URL.createObjectURL(blob);
|
|
21
|
+
const a = document.createElement("a");
|
|
22
|
+
a.href = url;
|
|
23
|
+
a.download = String(name);
|
|
24
|
+
document.body.appendChild(a);
|
|
25
|
+
a.click();
|
|
26
|
+
document.body.removeChild(a);
|
|
27
|
+
URL.revokeObjectURL(url);
|
|
28
|
+
})
|
|
29
|
+
.catch(err => {
|
|
30
|
+
console.error("Download error:", err);
|
|
31
|
+
alert("다운로드 중 오류가 발생했습니다.");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 파일 유틸리티; 파일 크기 포맷팅
|
|
36
|
+
* @util
|
|
37
|
+
* @param {unknown} size - 파일 크기 (바이트 단위)
|
|
38
|
+
* @param {boolean} [unit=true] - 단위 표시 여부 (기본값: true)
|
|
39
|
+
* @returns {string} - 포맷된 파일 크기 문자열
|
|
40
|
+
*/
|
|
41
|
+
export const fileSize = (size: unknown, unit: boolean = true): string => {
|
|
42
|
+
if (typeof size !== "number" || (size && isNaN(Number(size)))) return "";
|
|
43
|
+
|
|
44
|
+
const AMOUNT = 1024;
|
|
45
|
+
const K_AMOUNT = AMOUNT * AMOUNT;
|
|
46
|
+
const M_AMOUNT = AMOUNT * AMOUNT * AMOUNT;
|
|
47
|
+
|
|
48
|
+
const byte = new Decimal(size);
|
|
49
|
+
const kb = byte.div(AMOUNT);
|
|
50
|
+
const mb = kb.div(AMOUNT);
|
|
51
|
+
const gb = mb.div(AMOUNT);
|
|
52
|
+
|
|
53
|
+
if (byte.toNumber() < AMOUNT) return `${byte.toFixed(0)}B`;
|
|
54
|
+
if (byte.toNumber() < K_AMOUNT) return `${kb.toFixed(0)}KB`;
|
|
55
|
+
if (byte.toNumber() < M_AMOUNT) return `${mb.toFixed(0)}MB`;
|
|
56
|
+
return unit ? `${gb.toFixed(0)}GB` : `${gb.toFixed(2)} GB`;
|
|
57
|
+
};
|