@nu-art/ts-common 0.204.103 → 0.204.105

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/ts-common",
3
- "version": "0.204.103",
3
+ "version": "0.204.105",
4
4
  "description": "js and ts infra",
5
5
  "keywords": [
6
6
  "TacB0sS",
@@ -1,6 +1,15 @@
1
1
  export declare function padNumber(num: number | string, length: number): string;
2
2
  export declare function stringToHashCode(stringToHash: string): number;
3
3
  export declare function escape_RegExp(string: string): string;
4
+ export declare function maxSubstring(string: string, maxBytes: number, direction?: 'start' | 'end'): string;
5
+ /**
6
+ * Calculate the size of a string in the specified unit.
7
+ * @param str - The input string whose size is to be calculated.
8
+ * @param unit - The unit of size ('KB', 'MB', or 'GB').
9
+ * @returns The size of the string in the specified unit.
10
+ * @throws Error if the unit is invalid.
11
+ */
12
+ export declare function getStringSize(str: string, unit?: 'KB' | 'MB' | 'GB'): number;
4
13
  export declare function stringFormat(input: string, params?: string[]): string;
5
14
  export declare function replaceStringAt(origin: string, index: number, replacement: string): string;
6
15
  export declare function capitalizeAllFirstLetters(value: string): string;
@@ -17,7 +17,7 @@
17
17
  * limitations under the License.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.convertUpperCamelCase = exports.normalizeString = exports.levenshteinDistance = exports.createLevenshteinDistanceMatrix = exports.capitalizeFirstLetter = exports.capitalizeAllFirstLetters = exports.replaceStringAt = exports.stringFormat = exports.escape_RegExp = exports.stringToHashCode = exports.padNumber = void 0;
20
+ exports.convertUpperCamelCase = exports.normalizeString = exports.levenshteinDistance = exports.createLevenshteinDistanceMatrix = exports.capitalizeFirstLetter = exports.capitalizeAllFirstLetters = exports.replaceStringAt = exports.stringFormat = exports.getStringSize = exports.maxSubstring = exports.escape_RegExp = exports.stringToHashCode = exports.padNumber = void 0;
21
21
  function padNumber(num, length) {
22
22
  const _num = num.toString();
23
23
  return _num.length < length ? padNumber('0' + _num, length) : _num;
@@ -38,6 +38,46 @@ function escape_RegExp(string) {
38
38
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
39
39
  }
40
40
  exports.escape_RegExp = escape_RegExp;
41
+ function maxSubstring(string, maxBytes, direction = 'start') {
42
+ const encoder = new TextEncoder();
43
+ const decoder = new TextDecoder();
44
+ const bytes = encoder.encode(string);
45
+ if (bytes.length <= maxBytes) {
46
+ return string;
47
+ }
48
+ let slicedBytes;
49
+ if (direction === 'start') {
50
+ slicedBytes = bytes.slice(0, maxBytes);
51
+ }
52
+ else {
53
+ slicedBytes = bytes.slice(-maxBytes);
54
+ }
55
+ return decoder.decode(slicedBytes);
56
+ }
57
+ exports.maxSubstring = maxSubstring;
58
+ /**
59
+ * Calculate the size of a string in the specified unit.
60
+ * @param str - The input string whose size is to be calculated.
61
+ * @param unit - The unit of size ('KB', 'MB', or 'GB').
62
+ * @returns The size of the string in the specified unit.
63
+ * @throws Error if the unit is invalid.
64
+ */
65
+ function getStringSize(str, unit = 'KB') {
66
+ const encoder = new TextEncoder();
67
+ const bytes = encoder.encode(str);
68
+ const byteLength = bytes.length;
69
+ switch (unit) {
70
+ case 'KB':
71
+ return byteLength / 1024;
72
+ case 'MB':
73
+ return byteLength / (1024 * 1024);
74
+ case 'GB':
75
+ return byteLength / (1024 * 1024 * 1024);
76
+ default:
77
+ throw new Error('Invalid unit. Please specify "KB", "MB", or "GB".');
78
+ }
79
+ }
80
+ exports.getStringSize = getStringSize;
41
81
  function stringFormat(input, params = []) {
42
82
  return (params === null || params === void 0 ? void 0 : params.reduce((toRet, param, index) => {
43
83
  return toRet.replace(new RegExp(`\\{${index}\\}`, 'g'), param);