react-util-tools 1.0.24 → 1.0.25

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/README.md CHANGED
@@ -60,6 +60,16 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
60
60
 
61
61
  [查看文档](./src/address/README.md)
62
62
 
63
+ ### 🍪 Cookie - Cookie 操作
64
+ - `setCookie()` - 设置 Cookie
65
+ - `getCookie()` - 获取 Cookie
66
+ - `removeCookie()` - 删除 Cookie
67
+ - `hasCookie()` - 检查 Cookie 是否存在
68
+ - `getAllCookies()` - 获取所有 Cookie
69
+ - `clearAllCookies()` - 清除所有 Cookie
70
+
71
+ [查看文档](./src/cookie/README.md)
72
+
63
73
  ### 📱 Device - 设备检测
64
74
  - `getOS()` - 获取操作系统
65
75
  - `getBrowser()` - 获取浏览器类型
@@ -76,6 +86,8 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
76
86
  - `formatNumber()` - 数字格式化
77
87
  - `formatMoneyToChinese()` - 金额转中文大写
78
88
  - `formatPercent()` - 百分比格式化
89
+ - `maskEmail()` - 邮箱脱敏
90
+ - `unmaskEmail()` - 邮箱解脱敏
79
91
 
80
92
  [查看文档](./src/format/README.md)
81
93
 
@@ -108,6 +120,31 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
108
120
 
109
121
  [查看文档](./src/decimal/README.md)
110
122
 
123
+ #### Decimal Utils - 高精度计算工具函数
124
+ - `add()` - 加法运算
125
+ - `subtract()` - 减法运算
126
+ - `multiply()` - 乘法运算
127
+ - `divide()` - 除法运算
128
+ - `equals()` - 判断相等
129
+ - `greaterThan()` / `lessThan()` - 大小比较
130
+ - `round()` / `ceil()` / `floor()` - 取整
131
+ - `abs()` / `negate()` - 绝对值/取反
132
+
133
+ [查看文档](./src/decimal/utils/README.md)
134
+
135
+ ### 📊 Excel - Excel 文件处理
136
+ 基于 SheetJS (xlsx) 的 Excel 文件处理工具:
137
+ - `XLSX` - 完整的 SheetJS 对象
138
+ - `readExcelFile()` - 读取 Excel 文件
139
+ - `exportJSONToExcel()` - 导出 JSON 为 Excel
140
+ - `workbookToJSON()` - WorkBook 转 JSON
141
+ - `jsonToWorkbook()` - JSON 转 WorkBook
142
+ - `readExcelToJSON()` - 直接读取为 JSON
143
+ - 支持多种格式(.xlsx, .xls, .csv 等)
144
+ - 完整保留 SheetJS 所有功能
145
+
146
+ [查看文档](./src/excel/README.md)
147
+
111
148
  ## 使用示例
112
149
 
113
150
  ### 节流防抖
@@ -136,6 +173,26 @@ const id = getQueryParam('id') // "123"
136
173
  const params = getAllQueryParams() // { id: "123", name: "test" }
137
174
  ```
138
175
 
176
+ ### Cookie 操作
177
+
178
+ ```typescript
179
+ import { setCookie, getCookie, removeCookie } from '@dj49846917/react-tools'
180
+
181
+ // 设置 Cookie
182
+ setCookie('token', 'abc123', {
183
+ expires: 7 * 24 * 60 * 60, // 7天(秒)
184
+ path: '/',
185
+ secure: true,
186
+ sameSite: 'Strict'
187
+ })
188
+
189
+ // 获取 Cookie
190
+ const token = getCookie('token') // "abc123"
191
+
192
+ // 删除 Cookie
193
+ removeCookie('token')
194
+ ```
195
+
139
196
  ### 设备检测
140
197
 
141
198
  ```typescript
@@ -152,7 +209,7 @@ if (isMobile()) {
152
209
  ### 金额格式化
153
210
 
154
211
  ```typescript
155
- import { formatMoney, parseMoney, formatMoneyToChinese } from 'react-tools'
212
+ import { formatMoney, parseMoney, formatMoneyToChinese, maskEmail } from 'react-tools'
156
213
 
157
214
  formatMoney(1234.56) // "¥1,234.56"
158
215
  formatMoney(1234.56, { symbol: '$' }) // "$1,234.56"
@@ -160,6 +217,9 @@ formatMoney(1234.56, { symbol: '$' }) // "$1,234.56"
160
217
  parseMoney('¥1,234.56') // 1234.56
161
218
 
162
219
  formatMoneyToChinese(1234.56) // "壹仟贰佰叁拾肆元伍角陆分"
220
+
221
+ // 邮箱脱敏
222
+ maskEmail('dj49846917@proton.me') // "dj4***@proton.me"
163
223
  ```
164
224
 
165
225
  ### 日期处理
@@ -203,9 +263,9 @@ getUTCWeekStart(2024, 10) // Date 对象
203
263
  ### 高精度计算
204
264
 
205
265
  ```typescript
206
- import { Decimal } from 'react-tools'
266
+ import { Decimal, add, multiply, divide } from 'react-tools'
207
267
 
208
- // 解决浮点数精度问题
268
+ // 使用 Decimal 类
209
269
  const result = new Decimal('0.1').plus('0.2')
210
270
  console.log(result.toString()) // "0.3"
211
271
 
@@ -214,6 +274,32 @@ const price = new Decimal('19.99')
214
274
  const quantity = new Decimal('3')
215
275
  const total = price.times(quantity)
216
276
  console.log(total.toFixed(2)) // "59.97"
277
+
278
+ // 使用工具函数(返回 number 类型)
279
+ add(0.1, 0.2) // 0.3
280
+ multiply(19.99, 3) // 59.97
281
+ divide(100, 3) // 33.333...
282
+ ```
283
+
284
+ ### Excel 文件处理
285
+
286
+ ```typescript
287
+ import { readExcelToJSON, exportJSONToExcel } from 'react-tools'
288
+
289
+ // 读取 Excel 文件
290
+ async function handleFileUpload(file: File) {
291
+ const data = await readExcelToJSON(file)
292
+ console.log(data) // [{ name: '张三', age: 25 }, ...]
293
+ }
294
+
295
+ // 导出 JSON 为 Excel
296
+ function exportData() {
297
+ const data = [
298
+ { name: '张三', age: 25, city: '北京' },
299
+ { name: '李四', age: 30, city: '上海' }
300
+ ]
301
+ exportJSONToExcel(data, 'users.xlsx', 'UserList')
302
+ }
217
303
  ```
218
304
 
219
305
  ## TypeScript 支持
@@ -244,6 +330,7 @@ import { formatMoney, Decimal } from 'react-tools'
244
330
  - `date-fns` - 日期处理
245
331
  - `date-fns-tz` - 时区处理
246
332
  - `decimal.js` - 高精度计算
333
+ - `xlsx` - Excel 文件处理
247
334
 
248
335
  ## 许可证
249
336
 
package/dist/index.cjs CHANGED
@@ -31,17 +31,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  Decimal: () => import_decimal.default,
34
+ XLSX: () => XLSX,
34
35
  abs: () => abs,
35
36
  add: () => add,
36
37
  addDaysToDate: () => addDaysToDate,
37
38
  addDaysUTC: () => addDaysUTC,
38
39
  addMonthsToDate: () => addMonthsToDate,
39
40
  addMonthsUTC: () => addMonthsUTC,
41
+ aoaToSheet: () => aoaToSheet,
40
42
  ceil: () => ceil,
41
43
  clearAllCookies: () => clearAllCookies,
42
44
  debounce: () => debounceFn,
43
45
  divide: () => divide,
44
46
  equals: () => equals,
47
+ exportExcelFile: () => exportExcelFile,
48
+ exportJSONToExcel: () => exportJSONToExcel,
45
49
  floor: () => floor,
46
50
  formatDate: () => formatDate,
47
51
  formatDateOnly: () => formatDateOnly,
@@ -75,6 +79,8 @@ __export(index_exports, {
75
79
  getQueryParam: () => getQueryParam,
76
80
  getQueryParamAll: () => getQueryParamAll,
77
81
  getScreenResolution: () => getScreenResolution,
82
+ getSheet: () => getSheet,
83
+ getSheetNames: () => getSheetNames,
78
84
  getStartOfDay: () => getStartOfDay,
79
85
  getStartOfMonth: () => getStartOfMonth,
80
86
  getStartOfWeek: () => getStartOfWeek,
@@ -117,6 +123,7 @@ __export(index_exports, {
117
123
  isTouchDevice: () => isTouchDevice,
118
124
  isValidDate: () => isValidDate,
119
125
  isWeChat: () => isWeChat,
126
+ jsonToWorkbook: () => jsonToWorkbook,
120
127
  lessThan: () => lessThan,
121
128
  lessThanOrEqual: () => lessThanOrEqual,
122
129
  maskEmail: () => maskEmail,
@@ -124,18 +131,31 @@ __export(index_exports, {
124
131
  negate: () => negate,
125
132
  parseDate: () => parseDate,
126
133
  parseMoney: () => parseMoney,
134
+ read: () => read2,
135
+ readExcelFile: () => readExcelFile,
136
+ readExcelToJSON: () => readExcelToJSON,
137
+ readFile: () => readFile,
127
138
  removeCookie: () => removeCookie,
128
139
  round: () => round,
129
140
  setCookie: () => setCookie,
141
+ sheetToAOA: () => sheetToAOA,
142
+ sheetToCSV: () => sheetToCSV,
143
+ sheetToHTML: () => sheetToHTML,
130
144
  subDaysFromDate: () => subDaysFromDate,
131
145
  subDaysUTC: () => subDaysUTC,
132
146
  subMonthsFromDate: () => subMonthsFromDate,
133
147
  subMonthsUTC: () => subMonthsUTC,
134
148
  subtract: () => subtract,
149
+ tableToSheet: () => tableToSheet,
135
150
  throttle: () => throttleFn,
136
151
  toISOString: () => toISOString,
137
152
  toUTC: () => toUTC,
138
- unmaskEmail: () => unmaskEmail
153
+ unmaskEmail: () => unmaskEmail,
154
+ utils: () => utils2,
155
+ workbookToJSON: () => workbookToJSON,
156
+ write: () => write,
157
+ writeFile: () => writeFile2,
158
+ writeFileXLSX: () => writeFileXLSX
139
159
  });
140
160
  module.exports = __toCommonJS(index_exports);
141
161
 
@@ -941,20 +961,102 @@ function negate(value) {
941
961
  return 0;
942
962
  }
943
963
  }
964
+
965
+ // src/excel/index.ts
966
+ var XLSX = __toESM(require("xlsx"), 1);
967
+ var {
968
+ read: read2,
969
+ readFile,
970
+ write,
971
+ writeFile: writeFile2,
972
+ writeFileXLSX,
973
+ utils: utils2
974
+ } = XLSX;
975
+ function readExcelFile(file, options) {
976
+ return new Promise((resolve, reject) => {
977
+ const reader = new FileReader();
978
+ reader.onload = (e) => {
979
+ try {
980
+ const data = e.target?.result;
981
+ const workbook = XLSX.read(data, options);
982
+ resolve(workbook);
983
+ } catch (error) {
984
+ reject(error);
985
+ }
986
+ };
987
+ reader.onerror = () => {
988
+ reject(new Error("Failed to read file"));
989
+ };
990
+ reader.readAsArrayBuffer(file);
991
+ });
992
+ }
993
+ function workbookToJSON(workbook, sheetName, options) {
994
+ const sheet = sheetName ? workbook.Sheets[sheetName] : workbook.Sheets[workbook.SheetNames[0]];
995
+ if (!sheet) {
996
+ throw new Error(`Sheet "${sheetName}" not found`);
997
+ }
998
+ return XLSX.utils.sheet_to_json(sheet, options);
999
+ }
1000
+ function jsonToWorkbook(data, sheetName = "Sheet1", options) {
1001
+ const worksheet = XLSX.utils.json_to_sheet(data, options);
1002
+ const workbook = XLSX.utils.book_new();
1003
+ XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
1004
+ return workbook;
1005
+ }
1006
+ function exportExcelFile(workbook, filename = "export.xlsx", options) {
1007
+ XLSX.writeFile(workbook, filename, options);
1008
+ }
1009
+ function exportJSONToExcel(data, filename = "export.xlsx", sheetName = "Sheet1", options) {
1010
+ const workbook = jsonToWorkbook(data, sheetName);
1011
+ exportExcelFile(workbook, filename, options);
1012
+ }
1013
+ async function readExcelToJSON(file, sheetName, parseOptions, jsonOptions) {
1014
+ const workbook = await readExcelFile(file, parseOptions);
1015
+ return workbookToJSON(workbook, sheetName, jsonOptions);
1016
+ }
1017
+ function getSheetNames(workbook) {
1018
+ return workbook.SheetNames;
1019
+ }
1020
+ function getSheet(workbook, sheetName) {
1021
+ const sheet = workbook.Sheets[sheetName];
1022
+ if (!sheet) {
1023
+ throw new Error(`Sheet "${sheetName}" not found`);
1024
+ }
1025
+ return sheet;
1026
+ }
1027
+ function sheetToCSV(worksheet, options) {
1028
+ return XLSX.utils.sheet_to_csv(worksheet, options);
1029
+ }
1030
+ function sheetToHTML(worksheet, options) {
1031
+ return XLSX.utils.sheet_to_html(worksheet, options);
1032
+ }
1033
+ function tableToSheet(table, options) {
1034
+ return XLSX.utils.table_to_sheet(table, options);
1035
+ }
1036
+ function aoaToSheet(data, options) {
1037
+ return XLSX.utils.aoa_to_sheet(data, options);
1038
+ }
1039
+ function sheetToAOA(worksheet, options) {
1040
+ return XLSX.utils.sheet_to_json(worksheet, { ...options, header: 1 });
1041
+ }
944
1042
  // Annotate the CommonJS export names for ESM import in node:
945
1043
  0 && (module.exports = {
946
1044
  Decimal,
1045
+ XLSX,
947
1046
  abs,
948
1047
  add,
949
1048
  addDaysToDate,
950
1049
  addDaysUTC,
951
1050
  addMonthsToDate,
952
1051
  addMonthsUTC,
1052
+ aoaToSheet,
953
1053
  ceil,
954
1054
  clearAllCookies,
955
1055
  debounce,
956
1056
  divide,
957
1057
  equals,
1058
+ exportExcelFile,
1059
+ exportJSONToExcel,
958
1060
  floor,
959
1061
  formatDate,
960
1062
  formatDateOnly,
@@ -988,6 +1090,8 @@ function negate(value) {
988
1090
  getQueryParam,
989
1091
  getQueryParamAll,
990
1092
  getScreenResolution,
1093
+ getSheet,
1094
+ getSheetNames,
991
1095
  getStartOfDay,
992
1096
  getStartOfMonth,
993
1097
  getStartOfWeek,
@@ -1030,6 +1134,7 @@ function negate(value) {
1030
1134
  isTouchDevice,
1031
1135
  isValidDate,
1032
1136
  isWeChat,
1137
+ jsonToWorkbook,
1033
1138
  lessThan,
1034
1139
  lessThanOrEqual,
1035
1140
  maskEmail,
@@ -1037,16 +1142,29 @@ function negate(value) {
1037
1142
  negate,
1038
1143
  parseDate,
1039
1144
  parseMoney,
1145
+ read,
1146
+ readExcelFile,
1147
+ readExcelToJSON,
1148
+ readFile,
1040
1149
  removeCookie,
1041
1150
  round,
1042
1151
  setCookie,
1152
+ sheetToAOA,
1153
+ sheetToCSV,
1154
+ sheetToHTML,
1043
1155
  subDaysFromDate,
1044
1156
  subDaysUTC,
1045
1157
  subMonthsFromDate,
1046
1158
  subMonthsUTC,
1047
1159
  subtract,
1160
+ tableToSheet,
1048
1161
  throttle,
1049
1162
  toISOString,
1050
1163
  toUTC,
1051
- unmaskEmail
1164
+ unmaskEmail,
1165
+ utils,
1166
+ workbookToJSON,
1167
+ write,
1168
+ writeFile,
1169
+ writeFileXLSX
1052
1170
  });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,8 @@
1
1
  import Decimal from 'decimal.js';
2
2
  export { default as Decimal, Decimal as DecimalType } from 'decimal.js';
3
+ import * as XLSX from 'xlsx';
4
+ export { XLSX };
5
+ export { BookType, CellObject, JSON2SheetOpts, ParsingOptions, Range, Sheet2JSONOpts, WorkBook, WorkSheet, WritingOptions } from 'xlsx';
3
6
 
4
7
  declare function throttleFn<T extends (...args: any[]) => any>(fn: T, wait?: number, options?: {
5
8
  leading?: boolean;
@@ -167,4 +170,24 @@ declare function floor(value: number | string | Decimal, decimalPlaces?: number)
167
170
  declare function abs(value: number | string | Decimal): number;
168
171
  declare function negate(value: number | string | Decimal): number;
169
172
 
170
- export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, clearAllCookies, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, removeCookie, round, setCookie, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC, unmaskEmail };
173
+ declare const read: typeof XLSX.read;
174
+ declare const readFile: typeof XLSX.readFile;
175
+ declare const write: typeof XLSX.write;
176
+ declare const writeFile: typeof XLSX.writeFile;
177
+ declare const writeFileXLSX: typeof XLSX.writeFileXLSX;
178
+ declare const utils: XLSX.XLSX$Utils;
179
+ declare function readExcelFile(file: File, options?: XLSX.ParsingOptions): Promise<XLSX.WorkBook>;
180
+ declare function workbookToJSON<T = any>(workbook: XLSX.WorkBook, sheetName?: string, options?: XLSX.Sheet2JSONOpts): T[];
181
+ declare function jsonToWorkbook<T = any>(data: T[], sheetName?: string, options?: XLSX.JSON2SheetOpts): XLSX.WorkBook;
182
+ declare function exportExcelFile(workbook: XLSX.WorkBook, filename?: string, options?: XLSX.WritingOptions): void;
183
+ declare function exportJSONToExcel<T = any>(data: T[], filename?: string, sheetName?: string, options?: XLSX.WritingOptions): void;
184
+ declare function readExcelToJSON<T = any>(file: File, sheetName?: string, parseOptions?: XLSX.ParsingOptions, jsonOptions?: XLSX.Sheet2JSONOpts): Promise<T[]>;
185
+ declare function getSheetNames(workbook: XLSX.WorkBook): string[];
186
+ declare function getSheet(workbook: XLSX.WorkBook, sheetName: string): XLSX.WorkSheet;
187
+ declare function sheetToCSV(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2CSVOpts): string;
188
+ declare function sheetToHTML(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2HTMLOpts): string;
189
+ declare function tableToSheet(table: HTMLElement | string, options?: XLSX.Table2SheetOpts): XLSX.WorkSheet;
190
+ declare function aoaToSheet(data: any[][], options?: XLSX.AOA2SheetOpts): XLSX.WorkSheet;
191
+ declare function sheetToAOA(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2JSONOpts): any[][];
192
+
193
+ export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, ceil, clearAllCookies, debounceFn as debounce, divide, equals, exportExcelFile, exportJSONToExcel, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, jsonToWorkbook, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, read, readExcelFile, readExcelToJSON, readFile, removeCookie, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, toISOString, toUTC, unmaskEmail, utils, workbookToJSON, write, writeFile, writeFileXLSX };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import Decimal from 'decimal.js';
2
2
  export { default as Decimal, Decimal as DecimalType } from 'decimal.js';
3
+ import * as XLSX from 'xlsx';
4
+ export { XLSX };
5
+ export { BookType, CellObject, JSON2SheetOpts, ParsingOptions, Range, Sheet2JSONOpts, WorkBook, WorkSheet, WritingOptions } from 'xlsx';
3
6
 
4
7
  declare function throttleFn<T extends (...args: any[]) => any>(fn: T, wait?: number, options?: {
5
8
  leading?: boolean;
@@ -167,4 +170,24 @@ declare function floor(value: number | string | Decimal, decimalPlaces?: number)
167
170
  declare function abs(value: number | string | Decimal): number;
168
171
  declare function negate(value: number | string | Decimal): number;
169
172
 
170
- export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, clearAllCookies, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, removeCookie, round, setCookie, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC, unmaskEmail };
173
+ declare const read: typeof XLSX.read;
174
+ declare const readFile: typeof XLSX.readFile;
175
+ declare const write: typeof XLSX.write;
176
+ declare const writeFile: typeof XLSX.writeFile;
177
+ declare const writeFileXLSX: typeof XLSX.writeFileXLSX;
178
+ declare const utils: XLSX.XLSX$Utils;
179
+ declare function readExcelFile(file: File, options?: XLSX.ParsingOptions): Promise<XLSX.WorkBook>;
180
+ declare function workbookToJSON<T = any>(workbook: XLSX.WorkBook, sheetName?: string, options?: XLSX.Sheet2JSONOpts): T[];
181
+ declare function jsonToWorkbook<T = any>(data: T[], sheetName?: string, options?: XLSX.JSON2SheetOpts): XLSX.WorkBook;
182
+ declare function exportExcelFile(workbook: XLSX.WorkBook, filename?: string, options?: XLSX.WritingOptions): void;
183
+ declare function exportJSONToExcel<T = any>(data: T[], filename?: string, sheetName?: string, options?: XLSX.WritingOptions): void;
184
+ declare function readExcelToJSON<T = any>(file: File, sheetName?: string, parseOptions?: XLSX.ParsingOptions, jsonOptions?: XLSX.Sheet2JSONOpts): Promise<T[]>;
185
+ declare function getSheetNames(workbook: XLSX.WorkBook): string[];
186
+ declare function getSheet(workbook: XLSX.WorkBook, sheetName: string): XLSX.WorkSheet;
187
+ declare function sheetToCSV(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2CSVOpts): string;
188
+ declare function sheetToHTML(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2HTMLOpts): string;
189
+ declare function tableToSheet(table: HTMLElement | string, options?: XLSX.Table2SheetOpts): XLSX.WorkSheet;
190
+ declare function aoaToSheet(data: any[][], options?: XLSX.AOA2SheetOpts): XLSX.WorkSheet;
191
+ declare function sheetToAOA(worksheet: XLSX.WorkSheet, options?: XLSX.Sheet2JSONOpts): any[][];
192
+
193
+ export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, aoaToSheet, ceil, clearAllCookies, debounceFn as debounce, divide, equals, exportExcelFile, exportJSONToExcel, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getSheet, getSheetNames, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, jsonToWorkbook, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, read, readExcelFile, readExcelToJSON, readFile, removeCookie, round, setCookie, sheetToAOA, sheetToCSV, sheetToHTML, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, tableToSheet, throttleFn as throttle, toISOString, toUTC, unmaskEmail, utils, workbookToJSON, write, writeFile, writeFileXLSX };
package/dist/index.js CHANGED
@@ -843,19 +843,101 @@ function negate(value) {
843
843
  return 0;
844
844
  }
845
845
  }
846
+
847
+ // src/excel/index.ts
848
+ import * as XLSX from "xlsx";
849
+ var {
850
+ read: read2,
851
+ readFile,
852
+ write,
853
+ writeFile: writeFile2,
854
+ writeFileXLSX,
855
+ utils: utils2
856
+ } = XLSX;
857
+ function readExcelFile(file, options) {
858
+ return new Promise((resolve, reject) => {
859
+ const reader = new FileReader();
860
+ reader.onload = (e) => {
861
+ try {
862
+ const data = e.target?.result;
863
+ const workbook = XLSX.read(data, options);
864
+ resolve(workbook);
865
+ } catch (error) {
866
+ reject(error);
867
+ }
868
+ };
869
+ reader.onerror = () => {
870
+ reject(new Error("Failed to read file"));
871
+ };
872
+ reader.readAsArrayBuffer(file);
873
+ });
874
+ }
875
+ function workbookToJSON(workbook, sheetName, options) {
876
+ const sheet = sheetName ? workbook.Sheets[sheetName] : workbook.Sheets[workbook.SheetNames[0]];
877
+ if (!sheet) {
878
+ throw new Error(`Sheet "${sheetName}" not found`);
879
+ }
880
+ return XLSX.utils.sheet_to_json(sheet, options);
881
+ }
882
+ function jsonToWorkbook(data, sheetName = "Sheet1", options) {
883
+ const worksheet = XLSX.utils.json_to_sheet(data, options);
884
+ const workbook = XLSX.utils.book_new();
885
+ XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
886
+ return workbook;
887
+ }
888
+ function exportExcelFile(workbook, filename = "export.xlsx", options) {
889
+ XLSX.writeFile(workbook, filename, options);
890
+ }
891
+ function exportJSONToExcel(data, filename = "export.xlsx", sheetName = "Sheet1", options) {
892
+ const workbook = jsonToWorkbook(data, sheetName);
893
+ exportExcelFile(workbook, filename, options);
894
+ }
895
+ async function readExcelToJSON(file, sheetName, parseOptions, jsonOptions) {
896
+ const workbook = await readExcelFile(file, parseOptions);
897
+ return workbookToJSON(workbook, sheetName, jsonOptions);
898
+ }
899
+ function getSheetNames(workbook) {
900
+ return workbook.SheetNames;
901
+ }
902
+ function getSheet(workbook, sheetName) {
903
+ const sheet = workbook.Sheets[sheetName];
904
+ if (!sheet) {
905
+ throw new Error(`Sheet "${sheetName}" not found`);
906
+ }
907
+ return sheet;
908
+ }
909
+ function sheetToCSV(worksheet, options) {
910
+ return XLSX.utils.sheet_to_csv(worksheet, options);
911
+ }
912
+ function sheetToHTML(worksheet, options) {
913
+ return XLSX.utils.sheet_to_html(worksheet, options);
914
+ }
915
+ function tableToSheet(table, options) {
916
+ return XLSX.utils.table_to_sheet(table, options);
917
+ }
918
+ function aoaToSheet(data, options) {
919
+ return XLSX.utils.aoa_to_sheet(data, options);
920
+ }
921
+ function sheetToAOA(worksheet, options) {
922
+ return XLSX.utils.sheet_to_json(worksheet, { ...options, header: 1 });
923
+ }
846
924
  export {
847
925
  default2 as Decimal,
926
+ XLSX,
848
927
  abs,
849
928
  add,
850
929
  addDaysToDate,
851
930
  addDaysUTC,
852
931
  addMonthsToDate,
853
932
  addMonthsUTC,
933
+ aoaToSheet,
854
934
  ceil,
855
935
  clearAllCookies,
856
936
  debounceFn as debounce,
857
937
  divide,
858
938
  equals,
939
+ exportExcelFile,
940
+ exportJSONToExcel,
859
941
  floor,
860
942
  formatDate,
861
943
  formatDateOnly,
@@ -889,6 +971,8 @@ export {
889
971
  getQueryParam,
890
972
  getQueryParamAll,
891
973
  getScreenResolution,
974
+ getSheet,
975
+ getSheetNames,
892
976
  getStartOfDay,
893
977
  getStartOfMonth,
894
978
  getStartOfWeek,
@@ -931,6 +1015,7 @@ export {
931
1015
  isTouchDevice,
932
1016
  isValidDate,
933
1017
  isWeChat,
1018
+ jsonToWorkbook,
934
1019
  lessThan,
935
1020
  lessThanOrEqual,
936
1021
  maskEmail,
@@ -938,16 +1023,29 @@ export {
938
1023
  negate,
939
1024
  parseDate,
940
1025
  parseMoney,
1026
+ read2 as read,
1027
+ readExcelFile,
1028
+ readExcelToJSON,
1029
+ readFile,
941
1030
  removeCookie,
942
1031
  round,
943
1032
  setCookie,
1033
+ sheetToAOA,
1034
+ sheetToCSV,
1035
+ sheetToHTML,
944
1036
  subDaysFromDate,
945
1037
  subDaysUTC,
946
1038
  subMonthsFromDate,
947
1039
  subMonthsUTC,
948
1040
  subtract,
1041
+ tableToSheet,
949
1042
  throttleFn as throttle,
950
1043
  toISOString,
951
1044
  toUTC,
952
- unmaskEmail
1045
+ unmaskEmail,
1046
+ utils2 as utils,
1047
+ workbookToJSON,
1048
+ write,
1049
+ writeFile2 as writeFile,
1050
+ writeFileXLSX
953
1051
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-util-tools",
3
- "version": "1.0.24",
4
- "description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations and more",
3
+ "version": "1.0.25",
4
+ "description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations, Excel processing and more",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -36,6 +36,10 @@
36
36
  "browser-detection",
37
37
  "money-format",
38
38
  "url-params",
39
+ "excel",
40
+ "xlsx",
41
+ "sheetjs",
42
+ "cookie",
39
43
  "typescript"
40
44
  ],
41
45
  "author": "alice & bobby",
@@ -62,6 +66,7 @@
62
66
  "dependencies": {
63
67
  "date-fns": "^4.1.0",
64
68
  "date-fns-tz": "^3.2.0",
65
- "decimal.js": "^10.6.0"
69
+ "decimal.js": "^10.6.0",
70
+ "xlsx": "^0.18.5"
66
71
  }
67
72
  }