react-util-tools 1.0.25 → 1.0.26

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/index.js CHANGED
@@ -921,6 +921,237 @@ function aoaToSheet(data, options) {
921
921
  function sheetToAOA(worksheet, options) {
922
922
  return XLSX.utils.sheet_to_json(worksheet, { ...options, header: 1 });
923
923
  }
924
+
925
+ // src/string/index.ts
926
+ function capitalize(str) {
927
+ if (!str) return "";
928
+ return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
929
+ }
930
+ function camelCase(str) {
931
+ if (!str) return "";
932
+ return str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^[A-Z]/, (char) => char.toLowerCase());
933
+ }
934
+ function pascalCase(str) {
935
+ if (!str) return "";
936
+ const camel = camelCase(str);
937
+ return camel.charAt(0).toUpperCase() + camel.slice(1);
938
+ }
939
+ function snakeCase(str) {
940
+ if (!str) return "";
941
+ return str.replace(/([A-Z])/g, "_$1").replace(/[-\s]+/g, "_").replace(/^_/, "").toLowerCase();
942
+ }
943
+ function kebabCase(str) {
944
+ if (!str) return "";
945
+ return str.replace(/([A-Z])/g, "-$1").replace(/[_\s]+/g, "-").replace(/^-/, "").toLowerCase();
946
+ }
947
+ function truncate(str, length2, suffix = "...") {
948
+ if (!str || str.length <= length2) return str;
949
+ return str.slice(0, length2) + suffix;
950
+ }
951
+ function trim(str) {
952
+ return str ? str.trim() : "";
953
+ }
954
+ function trimStart(str) {
955
+ return str ? str.trimStart() : "";
956
+ }
957
+ function trimEnd(str) {
958
+ return str ? str.trimEnd() : "";
959
+ }
960
+ function reverse(str) {
961
+ if (!str) return "";
962
+ return str.split("").reverse().join("");
963
+ }
964
+ function repeat(str, count) {
965
+ if (!str || count <= 0) return "";
966
+ return str.repeat(count);
967
+ }
968
+ function padStart(str, length2, padStr = " ") {
969
+ if (!str) return padStr.repeat(length2);
970
+ return str.padStart(length2, padStr);
971
+ }
972
+ function padEnd(str, length2, padStr = " ") {
973
+ if (!str) return padStr.repeat(length2);
974
+ return str.padEnd(length2, padStr);
975
+ }
976
+ function startsWith(str, searchString) {
977
+ if (!str) return false;
978
+ return str.startsWith(searchString);
979
+ }
980
+ function endsWith(str, searchString) {
981
+ if (!str) return false;
982
+ return str.endsWith(searchString);
983
+ }
984
+ function includes(str, searchString) {
985
+ if (!str) return false;
986
+ return str.includes(searchString);
987
+ }
988
+ function replaceAll(str, search, replacement) {
989
+ if (!str) return "";
990
+ if (typeof search === "string") {
991
+ return str.split(search).join(replacement);
992
+ }
993
+ return str.replace(search, replacement);
994
+ }
995
+ function stripHtml(str) {
996
+ if (!str) return "";
997
+ return str.replace(/<[^>]*>/g, "");
998
+ }
999
+ function escapeHtml(str) {
1000
+ if (!str) return "";
1001
+ const map = {
1002
+ "&": "&amp;",
1003
+ "<": "&lt;",
1004
+ ">": "&gt;",
1005
+ '"': "&quot;",
1006
+ "'": "&#39;"
1007
+ };
1008
+ return str.replace(/[&<>"']/g, (char) => map[char]);
1009
+ }
1010
+ function unescapeHtml(str) {
1011
+ if (!str) return "";
1012
+ const map = {
1013
+ "&amp;": "&",
1014
+ "&lt;": "<",
1015
+ "&gt;": ">",
1016
+ "&quot;": '"',
1017
+ "&#39;": "'"
1018
+ };
1019
+ return str.replace(/&(amp|lt|gt|quot|#39);/g, (entity) => map[entity]);
1020
+ }
1021
+ function toLowerCase(str) {
1022
+ return str ? str.toLowerCase() : "";
1023
+ }
1024
+ function toUpperCase(str) {
1025
+ return str ? str.toUpperCase() : "";
1026
+ }
1027
+ function titleCase(str) {
1028
+ if (!str) return "";
1029
+ return str.toLowerCase().split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
1030
+ }
1031
+ function isEmpty(str) {
1032
+ return !str || str.trim().length === 0;
1033
+ }
1034
+ function isNotEmpty(str) {
1035
+ return !isEmpty(str);
1036
+ }
1037
+ function length(str) {
1038
+ if (!str) return 0;
1039
+ return Array.from(str).length;
1040
+ }
1041
+ function split(str, separator) {
1042
+ if (!str) return [];
1043
+ return str.split(separator);
1044
+ }
1045
+ function extractNumbers(str) {
1046
+ if (!str) return [];
1047
+ const matches = str.match(/\d+(\.\d+)?/g);
1048
+ return matches ? matches.map(Number) : [];
1049
+ }
1050
+ function removeSpaces(str) {
1051
+ if (!str) return "";
1052
+ return str.replace(/\s+/g, "");
1053
+ }
1054
+ function normalizeSpaces(str) {
1055
+ if (!str) return "";
1056
+ return str.replace(/\s+/g, " ").trim();
1057
+ }
1058
+ function countOccurrences(str, searchString) {
1059
+ if (!str || !searchString) return 0;
1060
+ return str.split(searchString).length - 1;
1061
+ }
1062
+ function randomString(length2, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
1063
+ let result = "";
1064
+ for (let i = 0; i < length2; i++) {
1065
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
1066
+ }
1067
+ return result;
1068
+ }
1069
+ function uuid() {
1070
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
1071
+ const r = Math.random() * 16 | 0;
1072
+ const v = c === "x" ? r : r & 3 | 8;
1073
+ return v.toString(16);
1074
+ });
1075
+ }
1076
+ function maskPhone(phone) {
1077
+ if (!phone || phone.length < 11) return phone;
1078
+ return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
1079
+ }
1080
+ function maskIdCard(idCard) {
1081
+ if (!idCard || idCard.length < 18) return idCard;
1082
+ return idCard.replace(/(\d{6})\d{8}(\d{4})/, "$1********$2");
1083
+ }
1084
+ function maskBankCard(cardNumber) {
1085
+ if (!cardNumber || cardNumber.length < 16) return cardNumber;
1086
+ return cardNumber.replace(/(\d{4})\d+(\d{4})/, "$1 **** **** $2");
1087
+ }
1088
+ function maskName(name) {
1089
+ if (!name) return "";
1090
+ if (name.length === 2) {
1091
+ return name.charAt(0) + "*";
1092
+ }
1093
+ return name.charAt(0) + "*".repeat(name.length - 2) + name.charAt(name.length - 1);
1094
+ }
1095
+ function isValidPhone(phone) {
1096
+ if (!phone) return false;
1097
+ return /^1[3-9]\d{9}$/.test(phone);
1098
+ }
1099
+ function isValidEmail(email) {
1100
+ if (!email) return false;
1101
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
1102
+ }
1103
+ function isValidUrl(url) {
1104
+ if (!url) return false;
1105
+ try {
1106
+ new URL(url);
1107
+ return true;
1108
+ } catch {
1109
+ return false;
1110
+ }
1111
+ }
1112
+ function isValidIdCard(idCard) {
1113
+ if (!idCard) return false;
1114
+ return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idCard);
1115
+ }
1116
+ function toBase64(str) {
1117
+ if (!str) return "";
1118
+ if (typeof window !== "undefined" && window.btoa) {
1119
+ try {
1120
+ return window.btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_, p1) => {
1121
+ return String.fromCharCode(parseInt(p1, 16));
1122
+ }));
1123
+ } catch {
1124
+ return "";
1125
+ }
1126
+ }
1127
+ try {
1128
+ const BufferClass = globalThis.Buffer;
1129
+ return BufferClass ? BufferClass.from(str, "utf-8").toString("base64") : "";
1130
+ } catch {
1131
+ return "";
1132
+ }
1133
+ }
1134
+ function fromBase64(base64) {
1135
+ if (!base64) return "";
1136
+ if (typeof window !== "undefined" && window.atob) {
1137
+ try {
1138
+ const binary = window.atob(base64);
1139
+ const bytes = new Uint8Array(binary.length);
1140
+ for (let i = 0; i < binary.length; i++) {
1141
+ bytes[i] = binary.charCodeAt(i);
1142
+ }
1143
+ return new TextDecoder().decode(bytes);
1144
+ } catch {
1145
+ return "";
1146
+ }
1147
+ }
1148
+ try {
1149
+ const BufferClass = globalThis.Buffer;
1150
+ return BufferClass ? BufferClass.from(base64, "base64").toString("utf-8") : "";
1151
+ } catch {
1152
+ return "";
1153
+ }
1154
+ }
924
1155
  export {
925
1156
  default2 as Decimal,
926
1157
  XLSX,
@@ -931,13 +1162,19 @@ export {
931
1162
  addMonthsToDate,
932
1163
  addMonthsUTC,
933
1164
  aoaToSheet,
1165
+ camelCase,
1166
+ capitalize,
934
1167
  ceil,
935
1168
  clearAllCookies,
1169
+ countOccurrences,
936
1170
  debounceFn as debounce,
937
1171
  divide,
1172
+ endsWith,
938
1173
  equals,
1174
+ escapeHtml,
939
1175
  exportExcelFile,
940
1176
  exportJSONToExcel,
1177
+ extractNumbers,
941
1178
  floor,
942
1179
  formatDate,
943
1180
  formatDateOnly,
@@ -950,6 +1187,7 @@ export {
950
1187
  formatUTC,
951
1188
  formatUTCDateOnly,
952
1189
  formatUTCTimeOnly,
1190
+ fromBase64,
953
1191
  fromUTC,
954
1192
  getAllCookies,
955
1193
  getAllQueryParams,
@@ -1004,35 +1242,61 @@ export {
1004
1242
  greaterThan,
1005
1243
  greaterThanOrEqual,
1006
1244
  hasCookie,
1245
+ includes,
1007
1246
  isAfterDate,
1008
1247
  isAndroid,
1009
1248
  isBeforeDate,
1010
1249
  isDesktop,
1250
+ isEmpty,
1011
1251
  isIOS,
1012
1252
  isMobile,
1253
+ isNotEmpty,
1013
1254
  isSameDayDate,
1014
1255
  isTablet,
1015
1256
  isTouchDevice,
1016
1257
  isValidDate,
1258
+ isValidEmail,
1259
+ isValidIdCard,
1260
+ isValidPhone,
1261
+ isValidUrl,
1017
1262
  isWeChat,
1018
1263
  jsonToWorkbook,
1264
+ kebabCase,
1265
+ length,
1019
1266
  lessThan,
1020
1267
  lessThanOrEqual,
1268
+ maskBankCard,
1021
1269
  maskEmail,
1270
+ maskIdCard,
1271
+ maskName,
1272
+ maskPhone,
1022
1273
  multiply,
1023
1274
  negate,
1275
+ normalizeSpaces,
1276
+ padEnd,
1277
+ padStart,
1024
1278
  parseDate,
1025
1279
  parseMoney,
1280
+ pascalCase,
1281
+ randomString,
1026
1282
  read2 as read,
1027
1283
  readExcelFile,
1028
1284
  readExcelToJSON,
1029
1285
  readFile,
1030
1286
  removeCookie,
1287
+ removeSpaces,
1288
+ repeat,
1289
+ replaceAll,
1290
+ reverse,
1031
1291
  round,
1032
1292
  setCookie,
1033
1293
  sheetToAOA,
1034
1294
  sheetToCSV,
1035
1295
  sheetToHTML,
1296
+ snakeCase,
1297
+ split,
1298
+ startsWith,
1299
+ stripHtml,
1036
1300
  subDaysFromDate,
1037
1301
  subDaysUTC,
1038
1302
  subMonthsFromDate,
@@ -1040,10 +1304,20 @@ export {
1040
1304
  subtract,
1041
1305
  tableToSheet,
1042
1306
  throttleFn as throttle,
1307
+ titleCase,
1308
+ toBase64,
1043
1309
  toISOString,
1310
+ toLowerCase,
1044
1311
  toUTC,
1312
+ toUpperCase,
1313
+ trim,
1314
+ trimEnd,
1315
+ trimStart,
1316
+ truncate,
1317
+ unescapeHtml,
1045
1318
  unmaskEmail,
1046
1319
  utils2 as utils,
1320
+ uuid,
1047
1321
  workbookToJSON,
1048
1322
  write,
1049
1323
  writeFile2 as writeFile,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-util-tools",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
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",
package/src/index.ts CHANGED
@@ -149,3 +149,48 @@ export type {
149
149
  Sheet2JSONOpts,
150
150
  JSON2SheetOpts
151
151
  } from './excel/index'
152
+ export {
153
+ capitalize,
154
+ camelCase,
155
+ pascalCase,
156
+ snakeCase,
157
+ kebabCase,
158
+ truncate,
159
+ trim,
160
+ trimStart,
161
+ trimEnd,
162
+ reverse,
163
+ repeat,
164
+ padStart,
165
+ padEnd,
166
+ startsWith,
167
+ endsWith,
168
+ includes,
169
+ replaceAll,
170
+ stripHtml,
171
+ escapeHtml,
172
+ unescapeHtml,
173
+ toLowerCase,
174
+ toUpperCase,
175
+ titleCase,
176
+ isEmpty,
177
+ isNotEmpty,
178
+ length,
179
+ split,
180
+ extractNumbers,
181
+ removeSpaces,
182
+ normalizeSpaces,
183
+ countOccurrences,
184
+ randomString,
185
+ uuid,
186
+ maskPhone,
187
+ maskIdCard,
188
+ maskBankCard,
189
+ maskName,
190
+ isValidPhone,
191
+ isValidEmail,
192
+ isValidUrl,
193
+ isValidIdCard,
194
+ toBase64,
195
+ fromBase64
196
+ } from './string/index'