@sqlrooms/utils 0.26.1-rc.7 → 0.27.0-rc.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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright 2025 Ilya Boyandin
3
+ Copyright 2025 SQLRooms Contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Determine if a decimal value is negative by checking the sign bit.
3
+ * Follows the two's complement representation used in Arrow decimals.
4
+ * @param value The Uint32Array representing the decimal value
5
+ * @returns true if the value is negative, false otherwise
6
+ * @ignore
7
+ */
8
+ export declare function isNegativeDecimal(value: Uint32Array): boolean;
9
+ /**
10
+ * Negate a decimal value in-place using two's complement arithmetic.
11
+ * @param value The Uint32Array to negate
12
+ * @returns The negated value (modified in-place for efficiency)
13
+ * @ignore
14
+ */
15
+ export declare function negateDecimal(value: Uint32Array): Uint32Array;
16
+ /**
17
+ * Convert a decimal value to a formatted string representation.
18
+ * Handles both Decimal128 (128-bit) and Decimal256 (256-bit) values.
19
+ *
20
+ * @param value The Uint32Array representing the decimal value
21
+ * @param scale The number of decimal places (digits after the decimal point)
22
+ * @returns A string representation of the decimal value
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { toDecimalString } from 'apache-arrow';
27
+ *
28
+ * const value = new Uint32Array([1, 0, 0, 0]);
29
+ * const result = toDecimalString(value, 2);
30
+ * // Returns: "0.01"
31
+ * ```
32
+ * @ignore
33
+ */
34
+ export declare function toDecimalString(value: Uint32Array, scale: number): string;
35
+ /**
36
+ * Convert a decimal value to a number.
37
+ * Note: This may lose precision for very large decimal values
38
+ * that exceed JavaScript's 53-bit integer precision.
39
+ *
40
+ * @param value The Uint32Array representing the decimal value
41
+ * @param scale The number of decimal places
42
+ * @returns A number representation of the decimal value
43
+ * @ignore
44
+ */
45
+ export declare function toDecimalNumber(value: Uint32Array, scale: number): number;
46
+ /**
47
+ * Create a Decimal128 value from a string representation.
48
+ * @param str String representation (e.g., "123.45")
49
+ * @param scale The scale (number of decimal places) to use
50
+ * @returns Uint32Array representing the decimal value
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { fromDecimalString } from 'apache-arrow';
55
+ *
56
+ * const value = fromDecimalString("123.45", 2);
57
+ * // Returns Uint32Array representing 12345 with scale 2
58
+ * ```
59
+ * @ignore
60
+ */
61
+ export declare function fromDecimalString(str: string, scale: number): Uint32Array;
62
+ //# sourceMappingURL=decimal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../src/decimal.ts"],"names":[],"mappings":"AAoBA;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAM7D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,CAY7D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAuDzE;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CA+BzE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CA0CzE"}
@@ -0,0 +1,206 @@
1
+ // Licensed to the Apache Software Foundation (ASF) under one
2
+ // or more contributor license agreements. See the NOTICE file
3
+ // distributed with this work for additional information
4
+ // regarding copyright ownership. The ASF licenses this file
5
+ // to you under the Apache License, Version 2.0 (the
6
+ // "License"); you may not use this file except in compliance
7
+ // with the License. You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing,
12
+ // software distributed under the License is distributed on an
13
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ // KIND, either express or implied. See the License for the
15
+ // specific language governing permissions and limitations
16
+ // under the License.
17
+ // TODO: Remove this file once Apache Arrow merges the PR below
18
+ // https://github.com/apache/arrow-js/pull/341/files#diff-41952a54a60919187ac886be5edde88ec0c80027e2815342d29ec2a77b14d0a6
19
+ /**
20
+ * Determine if a decimal value is negative by checking the sign bit.
21
+ * Follows the two's complement representation used in Arrow decimals.
22
+ * @param value The Uint32Array representing the decimal value
23
+ * @returns true if the value is negative, false otherwise
24
+ * @ignore
25
+ */
26
+ export function isNegativeDecimal(value) {
27
+ // Check the sign bit of the most significant 32-bit word
28
+ // This follows the Arrow C++ implementation:
29
+ // https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/basic_decimal.h
30
+ const MAX_INT32 = 2 ** 31 - 1;
31
+ return value.at(-1) > MAX_INT32;
32
+ }
33
+ /**
34
+ * Negate a decimal value in-place using two's complement arithmetic.
35
+ * @param value The Uint32Array to negate
36
+ * @returns The negated value (modified in-place for efficiency)
37
+ * @ignore
38
+ */
39
+ export function negateDecimal(value) {
40
+ // Two's complement negation: flip all bits and add 1
41
+ // Follows the Arrow C++ implementation:
42
+ // https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/basic_decimal.cc
43
+ let carry = 1;
44
+ for (let i = 0; i < value.length; i++) {
45
+ const elem = value[i];
46
+ const updated = ~elem + carry;
47
+ value[i] = updated >>> 0; // Ensure 32-bit unsigned
48
+ carry &= elem === 0 ? 1 : 0;
49
+ }
50
+ return value;
51
+ }
52
+ /**
53
+ * Convert a decimal value to a formatted string representation.
54
+ * Handles both Decimal128 (128-bit) and Decimal256 (256-bit) values.
55
+ *
56
+ * @param value The Uint32Array representing the decimal value
57
+ * @param scale The number of decimal places (digits after the decimal point)
58
+ * @returns A string representation of the decimal value
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * import { toDecimalString } from 'apache-arrow';
63
+ *
64
+ * const value = new Uint32Array([1, 0, 0, 0]);
65
+ * const result = toDecimalString(value, 2);
66
+ * // Returns: "0.01"
67
+ * ```
68
+ * @ignore
69
+ */
70
+ export function toDecimalString(value, scale) {
71
+ // Build BigInt from little-endian Uint32 words (supports 128-bit and 256-bit)
72
+ const toBigIntLE = (words) => {
73
+ let acc = BigInt(0);
74
+ for (let i = 0; i < words.length; i++) {
75
+ acc |= BigInt(words[i]) << BigInt(32 * i);
76
+ }
77
+ return acc;
78
+ };
79
+ // Detect sign via MSB of most-significant word
80
+ const isNegative = (value[value.length - 1] & 0x80000000) !== 0;
81
+ // Dynamic width mask (replaces fixed `mask128`)
82
+ const mask = (BigInt(1) << BigInt(value.length * 32)) - BigInt(1);
83
+ const n = toBigIntLE(value);
84
+ // If negative, convert two's complement to magnitude:
85
+ // magnitude = (~n + 1) & mask128
86
+ const magnitude = isNegative ? (~n + BigInt(1)) & mask : n;
87
+ // Magnitude as decimal string
88
+ const digits = magnitude.toString(10);
89
+ // Special-case: zero
90
+ if (magnitude === BigInt(0)) {
91
+ if (scale === 0) {
92
+ return '0';
93
+ }
94
+ // Tests expect "0.0" for zero with any positive scale
95
+ return '0.0';
96
+ }
97
+ if (scale === 0) {
98
+ const res = digits;
99
+ return isNegative ? '-' + res : res;
100
+ }
101
+ // Ensure we have at least scale digits for fractional part
102
+ let integerPart;
103
+ let fracPart;
104
+ if (digits.length <= scale) {
105
+ integerPart = '0';
106
+ fracPart = digits.padStart(scale, '0');
107
+ }
108
+ else {
109
+ const split = digits.length - scale;
110
+ integerPart = digits.slice(0, split);
111
+ fracPart = digits.slice(split);
112
+ }
113
+ // Trim trailing zeros in fractional part
114
+ fracPart = fracPart.replace(/0+$/, '');
115
+ const result = fracPart === '' ? integerPart : integerPart + '.' + fracPart;
116
+ return isNegative ? '-' + result : result;
117
+ }
118
+ /**
119
+ * Convert a decimal value to a number.
120
+ * Note: This may lose precision for very large decimal values
121
+ * that exceed JavaScript's 53-bit integer precision.
122
+ *
123
+ * @param value The Uint32Array representing the decimal value
124
+ * @param scale The number of decimal places
125
+ * @returns A number representation of the decimal value
126
+ * @ignore
127
+ */
128
+ export function toDecimalNumber(value, scale) {
129
+ const negative = isNegativeDecimal(value);
130
+ // Create a copy to avoid modifying the original
131
+ const valueCopy = new Uint32Array(value);
132
+ if (negative) {
133
+ negateDecimal(valueCopy);
134
+ }
135
+ // Convert to BigInt for calculation
136
+ let num = BigInt(0);
137
+ for (let i = valueCopy.length - 1; i >= 0; i--) {
138
+ num = (num << BigInt(32)) | BigInt(valueCopy[i]);
139
+ }
140
+ if (negative) {
141
+ num = -num;
142
+ }
143
+ // Apply scale
144
+ if (scale === 0) {
145
+ return Number(num);
146
+ }
147
+ // Calculate divisor as 10^scale
148
+ // Using a loop instead of BigInt exponentiation (**) for ES2015 compatibility
149
+ let divisor = BigInt(1);
150
+ for (let i = 0; i < scale; i++) {
151
+ divisor *= BigInt(10);
152
+ }
153
+ return Number(num) / Number(divisor);
154
+ }
155
+ /**
156
+ * Create a Decimal128 value from a string representation.
157
+ * @param str String representation (e.g., "123.45")
158
+ * @param scale The scale (number of decimal places) to use
159
+ * @returns Uint32Array representing the decimal value
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { fromDecimalString } from 'apache-arrow';
164
+ *
165
+ * const value = fromDecimalString("123.45", 2);
166
+ * // Returns Uint32Array representing 12345 with scale 2
167
+ * ```
168
+ * @ignore
169
+ */
170
+ export function fromDecimalString(str, scale) {
171
+ // Remove leading/trailing whitespace
172
+ str = str.trim();
173
+ // Detect negative
174
+ const negative = str.startsWith('-');
175
+ if (negative) {
176
+ str = str.slice(1);
177
+ }
178
+ // Split on decimal point
179
+ const [wholePart = '0', fracPart = ''] = str.split('.');
180
+ // Pad or truncate fractional part to match scale
181
+ const adjustedFrac = (fracPart + '0'.repeat(scale)).slice(0, scale);
182
+ const intStr = wholePart + adjustedFrac;
183
+ // Convert string to BigInt
184
+ let num = BigInt(intStr);
185
+ // Apply negative if needed
186
+ if (negative) {
187
+ num = -num;
188
+ }
189
+ // Convert BigInt to Uint32Array (Decimal128 = 4 x Uint32)
190
+ const result = new Uint32Array(4);
191
+ if (negative && num !== BigInt(0)) {
192
+ // Use two's complement for negative numbers
193
+ num = -(num + BigInt(1));
194
+ for (let i = 0; i < 4; i++) {
195
+ result[i] = Number((num >> BigInt(i * 32)) & BigInt(0xffffffff));
196
+ result[i] = ~result[i];
197
+ }
198
+ }
199
+ else {
200
+ for (let i = 0; i < 4; i++) {
201
+ result[i] = Number((num >> BigInt(i * 32)) & BigInt(0xffffffff));
202
+ }
203
+ }
204
+ return result;
205
+ }
206
+ //# sourceMappingURL=decimal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal.js","sourceRoot":"","sources":["../src/decimal.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,+DAA+D;AAC/D,wDAAwD;AACxD,6DAA6D;AAC7D,oDAAoD;AACpD,6DAA6D;AAC7D,6DAA6D;AAC7D,EAAE;AACF,+CAA+C;AAC/C,EAAE;AACF,6DAA6D;AAC7D,8DAA8D;AAC9D,yDAAyD;AACzD,4DAA4D;AAC5D,0DAA0D;AAC1D,qBAAqB;AAErB,+DAA+D;AAC/D,0HAA0H;AAE1H;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAkB;IAClD,yDAAyD;IACzD,6CAA6C;IAC7C,+EAA+E;IAC/E,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9B,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,GAAG,SAAS,CAAC;AACnC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,KAAkB;IAC9C,qDAAqD;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AAE,CAAC;YAC3B,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;YACjE,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAE,CAAC;QAC1B,CAAC;IACH,CAAC;SAAM,CAAC;QACN,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["// Licensed to the Apache Software Foundation (ASF) under one\n// or more contributor license agreements. See the NOTICE file\n// distributed with this work for additional information\n// regarding copyright ownership. The ASF licenses this file\n// to you under the Apache License, Version 2.0 (the\n// \"License\"); you may not use this file except in compliance\n// with the License. You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing,\n// software distributed under the License is distributed on an\n// \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n// KIND, either express or implied. See the License for the\n// specific language governing permissions and limitations\n// under the License.\n\n// TODO: Remove this file once Apache Arrow merges the PR below\n// https://github.com/apache/arrow-js/pull/341/files#diff-41952a54a60919187ac886be5edde88ec0c80027e2815342d29ec2a77b14d0a6\n\n/**\n * Determine if a decimal value is negative by checking the sign bit.\n * Follows the two's complement representation used in Arrow decimals.\n * @param value The Uint32Array representing the decimal value\n * @returns true if the value is negative, false otherwise\n * @ignore\n */\nexport function isNegativeDecimal(value: Uint32Array): boolean {\n // Check the sign bit of the most significant 32-bit word\n // This follows the Arrow C++ implementation:\n // https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/basic_decimal.h\n const MAX_INT32 = 2 ** 31 - 1;\n return value.at(-1)! > MAX_INT32;\n}\n\n/**\n * Negate a decimal value in-place using two's complement arithmetic.\n * @param value The Uint32Array to negate\n * @returns The negated value (modified in-place for efficiency)\n * @ignore\n */\nexport function negateDecimal(value: Uint32Array): Uint32Array {\n // Two's complement negation: flip all bits and add 1\n // Follows the Arrow C++ implementation:\n // https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/basic_decimal.cc\n let carry = 1;\n for (let i = 0; i < value.length; i++) {\n const elem = value[i]!;\n const updated = ~elem + carry;\n value[i] = updated >>> 0; // Ensure 32-bit unsigned\n carry &= elem === 0 ? 1 : 0;\n }\n return value;\n}\n\n/**\n * Convert a decimal value to a formatted string representation.\n * Handles both Decimal128 (128-bit) and Decimal256 (256-bit) values.\n *\n * @param value The Uint32Array representing the decimal value\n * @param scale The number of decimal places (digits after the decimal point)\n * @returns A string representation of the decimal value\n *\n * @example\n * ```ts\n * import { toDecimalString } from 'apache-arrow';\n *\n * const value = new Uint32Array([1, 0, 0, 0]);\n * const result = toDecimalString(value, 2);\n * // Returns: \"0.01\"\n * ```\n * @ignore\n */\nexport function toDecimalString(value: Uint32Array, scale: number): string {\n // Build BigInt from little-endian Uint32 words (supports 128-bit and 256-bit)\n const toBigIntLE = (words: Uint32Array) => {\n let acc = BigInt(0);\n for (let i = 0; i < words.length; i++) {\n acc |= BigInt(words[i]!) << BigInt(32 * i);\n }\n return acc;\n };\n\n // Detect sign via MSB of most-significant word\n const isNegative = (value[value.length - 1]! & 0x80000000) !== 0;\n // Dynamic width mask (replaces fixed `mask128`)\n const mask = (BigInt(1) << BigInt(value.length * 32)) - BigInt(1);\n\n const n = toBigIntLE(value);\n\n // If negative, convert two's complement to magnitude:\n // magnitude = (~n + 1) & mask128\n const magnitude: bigint = isNegative ? (~n + BigInt(1)) & mask : n;\n\n // Magnitude as decimal string\n const digits = magnitude.toString(10);\n\n // Special-case: zero\n if (magnitude === BigInt(0)) {\n if (scale === 0) {\n return '0';\n }\n // Tests expect \"0.0\" for zero with any positive scale\n return '0.0';\n }\n\n if (scale === 0) {\n const res = digits;\n return isNegative ? '-' + res : res;\n }\n\n // Ensure we have at least scale digits for fractional part\n let integerPart: string;\n let fracPart: string;\n if (digits.length <= scale) {\n integerPart = '0';\n fracPart = digits.padStart(scale, '0');\n } else {\n const split = digits.length - scale;\n integerPart = digits.slice(0, split);\n fracPart = digits.slice(split);\n }\n\n // Trim trailing zeros in fractional part\n fracPart = fracPart.replace(/0+$/, '');\n\n const result = fracPart === '' ? integerPart : integerPart + '.' + fracPart;\n return isNegative ? '-' + result : result;\n}\n\n/**\n * Convert a decimal value to a number.\n * Note: This may lose precision for very large decimal values\n * that exceed JavaScript's 53-bit integer precision.\n *\n * @param value The Uint32Array representing the decimal value\n * @param scale The number of decimal places\n * @returns A number representation of the decimal value\n * @ignore\n */\nexport function toDecimalNumber(value: Uint32Array, scale: number): number {\n const negative = isNegativeDecimal(value);\n\n // Create a copy to avoid modifying the original\n const valueCopy = new Uint32Array(value);\n if (negative) {\n negateDecimal(valueCopy);\n }\n\n // Convert to BigInt for calculation\n let num = BigInt(0);\n for (let i = valueCopy.length - 1; i >= 0; i--) {\n num = (num << BigInt(32)) | BigInt(valueCopy[i]!);\n }\n\n if (negative) {\n num = -num;\n }\n\n // Apply scale\n if (scale === 0) {\n return Number(num);\n }\n\n // Calculate divisor as 10^scale\n // Using a loop instead of BigInt exponentiation (**) for ES2015 compatibility\n let divisor = BigInt(1);\n for (let i = 0; i < scale; i++) {\n divisor *= BigInt(10);\n }\n return Number(num) / Number(divisor);\n}\n\n/**\n * Create a Decimal128 value from a string representation.\n * @param str String representation (e.g., \"123.45\")\n * @param scale The scale (number of decimal places) to use\n * @returns Uint32Array representing the decimal value\n *\n * @example\n * ```ts\n * import { fromDecimalString } from 'apache-arrow';\n *\n * const value = fromDecimalString(\"123.45\", 2);\n * // Returns Uint32Array representing 12345 with scale 2\n * ```\n * @ignore\n */\nexport function fromDecimalString(str: string, scale: number): Uint32Array {\n // Remove leading/trailing whitespace\n str = str.trim();\n\n // Detect negative\n const negative = str.startsWith('-');\n if (negative) {\n str = str.slice(1);\n }\n\n // Split on decimal point\n const [wholePart = '0', fracPart = ''] = str.split('.');\n\n // Pad or truncate fractional part to match scale\n const adjustedFrac = (fracPart + '0'.repeat(scale)).slice(0, scale);\n const intStr = wholePart + adjustedFrac;\n\n // Convert string to BigInt\n let num = BigInt(intStr);\n\n // Apply negative if needed\n if (negative) {\n num = -num;\n }\n\n // Convert BigInt to Uint32Array (Decimal128 = 4 x Uint32)\n const result = new Uint32Array(4);\n\n if (negative && num !== BigInt(0)) {\n // Use two's complement for negative numbers\n num = -(num + BigInt(1));\n for (let i = 0; i < 4; i++) {\n result[i] = Number((num >> BigInt(i * 32)) & BigInt(0xffffffff));\n result[i] = ~result[i]!;\n }\n } else {\n for (let i = 0; i < 4; i++) {\n result[i] = Number((num >> BigInt(i * 32)) & BigInt(0xffffffff));\n }\n }\n\n return result;\n}\n"]}
package/dist/filepaths.js CHANGED
@@ -147,7 +147,7 @@ export function generateUniquePath(filePath, existingPaths) {
147
147
  export function convertToUniqueS3ObjectName(str, existingObjects) {
148
148
  let rv = str
149
149
  .trim() // Remove leading and trailing white spaces
150
- .replace(/[^\w\s-\.]/g, '_') // Replace special characters with underscores
150
+ .replace(/[^\w\s-.]/g, '_') // Replace special characters with underscores
151
151
  .replace(/\s+/g, '_') // Replace consecutive spaces with a single underscore
152
152
  // .replace(/_+/g, '_') // Remove consecutive underscores
153
153
  // .replace(/^_/, '') // Remove leading underscores
@@ -1 +1 @@
1
- {"version":3,"file":"filepaths.js","sourceRoot":"","sources":["../src/filepaths.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB;IAM5C,oGAAoG;IACpG,MAAM,SAAS,GAAG,QAAQ,CAAC,QAA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,CAAC,CAAC,CAAC,mDAAmD;IAErE,IAAI,eAAe,EAAE,MAAM,EAAE,CAAC;QAC5B,EAAE,GAAG,kBAAkB,CAAC,EAAE,EAAE,eAAe,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,2BAA2B,CACzC,GAAW,EACX,eAA0B;IAE1B,IAAI,IAAI,GAAG,2BAA2B,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;IAC7D,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,IAAI,IAAI,GAAG,CAAC,CAAC,oCAAoC;IAC1E,OAAO,IAAI,CAAC;IACZ,WAAW;IACX,QAAQ;IACR,0DAA0D;IAC1D,+EAA+E;IAC/E,kFAAkF;IAClF,mFAAmF;IACnF,oDAAoD;IACpD,mCAAmC;IACnC,KAAK;AACP,CAAC","sourcesContent":["/**\n * Splits a file path into its directory, name, and extension components.\n * Preserves the original path separator style (Windows backslashes or Unix forward slashes).\n * @param filePath - The full file path to split\n * @returns An object containing the directory path, file name (without extension), extension, and full filename\n * @example\n * splitFilePath(\"path/to/file.txt\") // returns { dir: \"path/to\", name: \"file\", ext: \"txt\", filename: \"file.txt\" }\n * splitFilePath(\"C:\\\\Users\\\\file.txt\") // returns { dir: \"C:\\\\Users\", name: \"file\", ext: \"txt\", filename: \"file.txt\" }\n */\nexport function splitFilePath(filePath: string): {\n dir: string;\n name: string;\n ext: string;\n filename: string;\n} {\n // If backslash appears, assume Windows filesystem (backslash is not a valid path separator on Unix)\n const separator = filePath.includes('\\\\') ? '\\\\' : '/';\n\n // Handle both Windows backslashes and Unix forward slashes\n const pathParts = filePath.split(/[/\\\\]/);\n const file = pathParts.pop() || '';\n\n const dotIndex = file.lastIndexOf('.');\n if (dotIndex === -1 || dotIndex === 0)\n return {\n dir: pathParts.join(separator),\n name: file,\n ext: '',\n filename: file,\n };\n\n const name = file.substring(0, dotIndex);\n const ext = file.substring(dotIndex + 1);\n\n return {\n dir: pathParts.join(separator),\n name,\n ext,\n filename: file,\n };\n}\n\n/**\n * Converts a filename into a valid column or table name for database use.\n * - Removes file extension\n * - Replaces invalid characters with underscores\n * - Ensures the name starts with a letter or underscore\n * - Truncates to max length of 63 characters\n *\n * @param filename - The original filename to convert\n * @returns A valid table/column name\n * @example\n * convertToValidColumnOrTableName(\"my-file.csv\") // returns \"my_file\"\n * convertToValidColumnOrTableName(\"123data.csv\") // returns \"_123data\"\n */\nexport function convertToValidColumnOrTableName(filename: string): string {\n // Remove file extension\n const base = filename.replace(/\\.[^/.]+$/, '');\n\n // Replace any invalid character with underscore, and convert to lowercase\n let tableName = base.replace(/[^a-z0-9_]/gi, '_');\n\n // If the first character is a number, prepend an underscore\n if (/^\\d/.test(tableName)) {\n tableName = '_' + tableName;\n }\n\n // Truncate to the max length 63\n if (tableName.length > 63) {\n tableName = tableName.substring(0, 63);\n }\n\n return tableName;\n}\n\n/**\n * Converts a filename into a valid and unique column or table name for database use.\n * - Removes file extension\n * - Replaces invalid characters with underscores\n * - Ensures the name starts with a letter or underscore\n * - Truncates to max length of 63 characters\n * - Ensures uniqueness among existing names\n *\n * @param filename - The original filename to convert\n * @param existingTables - Optional array of existing table names to ensure uniqueness\n * @returns A valid and unique table/column name\n * @example\n * convertToUniqueColumnOrTableName(\"my-file.csv\") // returns \"my_file\"\n * convertToUniqueColumnOrTableName(\"123data.csv\") // returns \"_123data\"\n */\nexport function convertToUniqueColumnOrTableName(\n filename: string,\n existingTables?: string[],\n): string {\n const tableName = convertToValidColumnOrTableName(filename);\n return generateUniqueName(tableName, existingTables);\n}\n\n/**\n * Generates a unique name by appending a numeric suffix if the name already exists.\n * @param name - The base name to make unique\n * @param usedNames - Optional array of existing names to check against\n * @returns A unique name, potentially with a numeric suffix\n * @example\n * generateUniqueName(\"table\", [\"table\"]) // returns \"table_1\"\n * generateUniqueName(\"table_1\", [\"table_1\"]) // returns \"table_2\"\n */\nexport function generateUniqueName(name: string, usedNames?: string[]) {\n const usedNamesLower = usedNames?.map((n) => n.toLowerCase());\n\n // If tableName exists in the list\n if (usedNamesLower?.includes(name.toLowerCase())) {\n let baseName: string | undefined = name;\n let i = 0;\n\n // If tableName ends with `_${i}` pattern, update the baseTableName and i\n const matched = name.match(/^(.+)_(\\d+)$/);\n if (matched) {\n baseName = matched[1];\n i = Number(matched[2]);\n }\n\n do {\n i++;\n name = `${baseName}_${i}`;\n } while (usedNamesLower.includes(name.toLowerCase()));\n }\n\n return name;\n}\n\n/**\n * Generates a unique file path by appending a numeric suffix if the path already exists.\n * @param filePath - The original file path\n * @param existingPaths - Array of existing file paths to check against\n * @returns A unique file path\n * @example\n * generateUniquePath(\"file.txt\", [\"file.txt\"]) // returns \"file_1.txt\"\n */\nexport function generateUniquePath(\n filePath: string,\n existingPaths: string[],\n): string {\n let nextPath = filePath;\n if (existingPaths?.includes(filePath)) {\n const {dir, name, ext} = splitFilePath(filePath);\n\n let i = 0;\n let baseName: string | undefined = name;\n const matched = name.match(/^(.+)_(\\d+)$/);\n if (matched) {\n baseName = matched[1];\n i = Number(matched[2]);\n }\n\n do {\n i++;\n const fname = `${baseName}_${i}${ext ? `.${ext}` : ''}`;\n nextPath = `${dir}${dir ? '/' : ''}${fname}`;\n } while (existingPaths.includes(nextPath));\n }\n\n return nextPath;\n}\n\n/**\n * Converts a string into a valid and unique S3 object name.\n * - Replaces special characters with underscores\n * - Ensures name is within S3's length limits\n * - Ensures uniqueness among existing objects\n *\n * @param str - The string to convert into an S3 object name\n * @param existingObjects - Optional array of existing S3 object names to ensure uniqueness\n * @returns A valid and unique S3 object name\n * @example\n * convertToUniqueS3ObjectName(\"my file.txt\") // returns \"my_file.txt\"\n */\nexport function convertToUniqueS3ObjectName(\n str: string,\n existingObjects?: string[],\n): string {\n let rv = str\n .trim() // Remove leading and trailing white spaces\n .replace(/[^\\w\\s-\\.]/g, '_') // Replace special characters with underscores\n .replace(/\\s+/g, '_') // Replace consecutive spaces with a single underscore\n // .replace(/_+/g, '_') // Remove consecutive underscores\n // .replace(/^_/, '') // Remove leading underscores\n // .replace(/_$/, '') // Remove trailing underscores\n .slice(0, 255); // Truncate the string if it exceeds 255 characters\n\n if (existingObjects?.length) {\n rv = generateUniquePath(rv, existingObjects);\n }\n\n return rv;\n}\n\n/**\n * Converts a string into a valid and unique S3 folder path.\n * - Ensures the path ends with a forward slash\n * - Replaces special characters with underscores\n * - Ensures uniqueness among existing paths\n *\n * @param str - The string to convert into an S3 folder path\n * @param existingObjects - Optional array of existing S3 paths to ensure uniqueness\n * @returns A valid and unique S3 folder path ending with a forward slash\n * @example\n * convertToUniqueS3FolderPath(\"my folder\") // returns \"my_folder/\"\n */\nexport function convertToUniqueS3FolderPath(\n str: string,\n existingObjects?: string[],\n): string {\n let next = convertToUniqueS3ObjectName(str, existingObjects);\n if (!next.endsWith('/')) next += '/'; // Add trailing slash if not present\n return next;\n // return (\n // str\n // .trim() // Remove leading and trailing white spaces\n // .replace(/\\/+/g, '/') // Replace consecutive slashes with a single slash\n // .replace(/[^\\w\\s-\\/]/g, '_') // Replace special characters with underscores\n // .replace(/\\s+/g, '_') // Replace consecutive spaces with a single underscore\n // .replace(/^\\//, '') + // Remove leading slash\n // (str.endsWith('/') ? '' : '/')\n // );\n}\n"]}
1
+ {"version":3,"file":"filepaths.js","sourceRoot":"","sources":["../src/filepaths.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB;IAM5C,oGAAoG;IACpG,MAAM,SAAS,GAAG,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;IAEvD,2DAA2D;IAC3D,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,IAAI,GAAG,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;IAEnC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,GAAW,EACX,eAA0B;IAE1B,IAAI,IAAI,GAAG,2BAA2B,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;IAC7D,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,IAAI,IAAI,GAAG,CAAC,CAAC,oCAAoC;IAC1E,OAAO,IAAI,CAAC;IACZ,WAAW;IACX,QAAQ;IACR,0DAA0D;IAC1D,+EAA+E;IAC/E,kFAAkF;IAClF,mFAAmF;IACnF,oDAAoD;IACpD,mCAAmC;IACnC,KAAK;AACP,CAAC","sourcesContent":["/**\n * Splits a file path into its directory, name, and extension components.\n * Preserves the original path separator style (Windows backslashes or Unix forward slashes).\n * @param filePath - The full file path to split\n * @returns An object containing the directory path, file name (without extension), extension, and full filename\n * @example\n * splitFilePath(\"path/to/file.txt\") // returns { dir: \"path/to\", name: \"file\", ext: \"txt\", filename: \"file.txt\" }\n * splitFilePath(\"C:\\\\Users\\\\file.txt\") // returns { dir: \"C:\\\\Users\", name: \"file\", ext: \"txt\", filename: \"file.txt\" }\n */\nexport function splitFilePath(filePath: string): {\n dir: string;\n name: string;\n ext: string;\n filename: string;\n} {\n // If backslash appears, assume Windows filesystem (backslash is not a valid path separator on Unix)\n const separator = filePath.includes('\\\\') ? '\\\\' : '/';\n\n // Handle both Windows backslashes and Unix forward slashes\n const pathParts = filePath.split(/[/\\\\]/);\n const file = pathParts.pop() || '';\n\n const dotIndex = file.lastIndexOf('.');\n if (dotIndex === -1 || dotIndex === 0)\n return {\n dir: pathParts.join(separator),\n name: file,\n ext: '',\n filename: file,\n };\n\n const name = file.substring(0, dotIndex);\n const ext = file.substring(dotIndex + 1);\n\n return {\n dir: pathParts.join(separator),\n name,\n ext,\n filename: file,\n };\n}\n\n/**\n * Converts a filename into a valid column or table name for database use.\n * - Removes file extension\n * - Replaces invalid characters with underscores\n * - Ensures the name starts with a letter or underscore\n * - Truncates to max length of 63 characters\n *\n * @param filename - The original filename to convert\n * @returns A valid table/column name\n * @example\n * convertToValidColumnOrTableName(\"my-file.csv\") // returns \"my_file\"\n * convertToValidColumnOrTableName(\"123data.csv\") // returns \"_123data\"\n */\nexport function convertToValidColumnOrTableName(filename: string): string {\n // Remove file extension\n const base = filename.replace(/\\.[^/.]+$/, '');\n\n // Replace any invalid character with underscore, and convert to lowercase\n let tableName = base.replace(/[^a-z0-9_]/gi, '_');\n\n // If the first character is a number, prepend an underscore\n if (/^\\d/.test(tableName)) {\n tableName = '_' + tableName;\n }\n\n // Truncate to the max length 63\n if (tableName.length > 63) {\n tableName = tableName.substring(0, 63);\n }\n\n return tableName;\n}\n\n/**\n * Converts a filename into a valid and unique column or table name for database use.\n * - Removes file extension\n * - Replaces invalid characters with underscores\n * - Ensures the name starts with a letter or underscore\n * - Truncates to max length of 63 characters\n * - Ensures uniqueness among existing names\n *\n * @param filename - The original filename to convert\n * @param existingTables - Optional array of existing table names to ensure uniqueness\n * @returns A valid and unique table/column name\n * @example\n * convertToUniqueColumnOrTableName(\"my-file.csv\") // returns \"my_file\"\n * convertToUniqueColumnOrTableName(\"123data.csv\") // returns \"_123data\"\n */\nexport function convertToUniqueColumnOrTableName(\n filename: string,\n existingTables?: string[],\n): string {\n const tableName = convertToValidColumnOrTableName(filename);\n return generateUniqueName(tableName, existingTables);\n}\n\n/**\n * Generates a unique name by appending a numeric suffix if the name already exists.\n * @param name - The base name to make unique\n * @param usedNames - Optional array of existing names to check against\n * @returns A unique name, potentially with a numeric suffix\n * @example\n * generateUniqueName(\"table\", [\"table\"]) // returns \"table_1\"\n * generateUniqueName(\"table_1\", [\"table_1\"]) // returns \"table_2\"\n */\nexport function generateUniqueName(name: string, usedNames?: string[]) {\n const usedNamesLower = usedNames?.map((n) => n.toLowerCase());\n\n // If tableName exists in the list\n if (usedNamesLower?.includes(name.toLowerCase())) {\n let baseName: string | undefined = name;\n let i = 0;\n\n // If tableName ends with `_${i}` pattern, update the baseTableName and i\n const matched = name.match(/^(.+)_(\\d+)$/);\n if (matched) {\n baseName = matched[1];\n i = Number(matched[2]);\n }\n\n do {\n i++;\n name = `${baseName}_${i}`;\n } while (usedNamesLower.includes(name.toLowerCase()));\n }\n\n return name;\n}\n\n/**\n * Generates a unique file path by appending a numeric suffix if the path already exists.\n * @param filePath - The original file path\n * @param existingPaths - Array of existing file paths to check against\n * @returns A unique file path\n * @example\n * generateUniquePath(\"file.txt\", [\"file.txt\"]) // returns \"file_1.txt\"\n */\nexport function generateUniquePath(\n filePath: string,\n existingPaths: string[],\n): string {\n let nextPath = filePath;\n if (existingPaths?.includes(filePath)) {\n const {dir, name, ext} = splitFilePath(filePath);\n\n let i = 0;\n let baseName: string | undefined = name;\n const matched = name.match(/^(.+)_(\\d+)$/);\n if (matched) {\n baseName = matched[1];\n i = Number(matched[2]);\n }\n\n do {\n i++;\n const fname = `${baseName}_${i}${ext ? `.${ext}` : ''}`;\n nextPath = `${dir}${dir ? '/' : ''}${fname}`;\n } while (existingPaths.includes(nextPath));\n }\n\n return nextPath;\n}\n\n/**\n * Converts a string into a valid and unique S3 object name.\n * - Replaces special characters with underscores\n * - Ensures name is within S3's length limits\n * - Ensures uniqueness among existing objects\n *\n * @param str - The string to convert into an S3 object name\n * @param existingObjects - Optional array of existing S3 object names to ensure uniqueness\n * @returns A valid and unique S3 object name\n * @example\n * convertToUniqueS3ObjectName(\"my file.txt\") // returns \"my_file.txt\"\n */\nexport function convertToUniqueS3ObjectName(\n str: string,\n existingObjects?: string[],\n): string {\n let rv = str\n .trim() // Remove leading and trailing white spaces\n .replace(/[^\\w\\s-.]/g, '_') // Replace special characters with underscores\n .replace(/\\s+/g, '_') // Replace consecutive spaces with a single underscore\n // .replace(/_+/g, '_') // Remove consecutive underscores\n // .replace(/^_/, '') // Remove leading underscores\n // .replace(/_$/, '') // Remove trailing underscores\n .slice(0, 255); // Truncate the string if it exceeds 255 characters\n\n if (existingObjects?.length) {\n rv = generateUniquePath(rv, existingObjects);\n }\n\n return rv;\n}\n\n/**\n * Converts a string into a valid and unique S3 folder path.\n * - Ensures the path ends with a forward slash\n * - Replaces special characters with underscores\n * - Ensures uniqueness among existing paths\n *\n * @param str - The string to convert into an S3 folder path\n * @param existingObjects - Optional array of existing S3 paths to ensure uniqueness\n * @returns A valid and unique S3 folder path ending with a forward slash\n * @example\n * convertToUniqueS3FolderPath(\"my folder\") // returns \"my_folder/\"\n */\nexport function convertToUniqueS3FolderPath(\n str: string,\n existingObjects?: string[],\n): string {\n let next = convertToUniqueS3ObjectName(str, existingObjects);\n if (!next.endsWith('/')) next += '/'; // Add trailing slash if not present\n return next;\n // return (\n // str\n // .trim() // Remove leading and trailing white spaces\n // .replace(/\\/+/g, '/') // Replace consecutive slashes with a single slash\n // .replace(/[^\\w\\s-\\/]/g, '_') // Replace special characters with underscores\n // .replace(/\\s+/g, '_') // Replace consecutive spaces with a single underscore\n // .replace(/^\\//, '') + // Remove leading slash\n // (str.endsWith('/') ? '' : '/')\n // );\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -12,4 +12,5 @@ export { postData, downloadFile, uploadFile, type ProgressInfo } from './xhr';
12
12
  export { splitFilePath, convertToValidColumnOrTableName, convertToUniqueColumnOrTableName, generateUniqueName, generateUniquePath, convertToUniqueS3ObjectName, convertToUniqueS3FolderPath, } from './filepaths';
13
13
  export { safeJsonParse } from './json';
14
14
  export { memoizeOnce } from './memoization';
15
+ export { isNegativeDecimal, negateDecimal, toDecimalString, toDecimalNumber, fromDecimalString, } from './decimal';
15
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAC,WAAW,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AAE9E,OAAO,EACL,aAAa,EACb,YAAY,EACZ,cAAc,EACd,UAAU,EACV,eAAe,EACf,kBAA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ACb,+BAA+B,EAC/B,gCAAgC,EAChC,kBAAkB,EAClB,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAC1C,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,eAAe,EACf,iBAAiB,GAClB,MAAM,WAAW,CAAC"}
package/dist/index.js CHANGED
@@ -12,4 +12,5 @@ export { postData, downloadFile, uploadFile } from './xhr';
12
12
  export { splitFilePath, convertToValidColumnOrTableName, convertToUniqueColumnOrTableName, generateUniqueName, generateUniquePath, convertToUniqueS3ObjectName, convertToUniqueS3FolderPath, } from './filepaths';
13
13
  export { safeJsonParse } from './json';
14
14
  export { memoizeOnce } from './memoization';
15
+ export { isNegativeDecimal, negateDecimal, toDecimalString, toDecimalNumber, fromDecimalString, } from './decimal';
15
16
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAC,WAAW,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AAE9E,OAAO,EACL,aAAa,EACb,YAAY,EACZ,cAAc,EACd,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAElC,OAAO,EAAC,YAAY,EAAC,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAC,WAAW,EAAE,gBAAgB,EAAE,UAAU,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAE1E,OAAO,EAAC,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAoB,MAAM,OAAO,CAAC;AAE5E,OAAO,EACL,aAAa,EACb,+BAA+B,EAC/B,gCAAgC,EAChC,kBAAkB,EAClB,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport {opacifyHex} from './color';\n\nexport {formatCount, formatCount4, formatCountShort, shorten} from './format';\n\nexport {\n NUMBER_FORMAT,\n formatNumber,\n formatDateTime,\n formatDate,\n formatTimeOfDay,\n formatTimeRelative,\n formatTimestampForFilename,\n getErrorMessageForDisplay,\n} from './helpers';\n\nexport {isMacOS} from './browser';\n\nexport {genRandomStr} from './random';\n\nexport {formatBytes, camelCaseToTitle, capitalize, truncate} from './str';\n\nexport {postData, downloadFile, uploadFile, type ProgressInfo} from './xhr';\n\nexport {\n splitFilePath,\n convertToValidColumnOrTableName,\n convertToUniqueColumnOrTableName,\n generateUniqueName,\n generateUniquePath,\n convertToUniqueS3ObjectName,\n convertToUniqueS3FolderPath,\n} from './filepaths';\n\nexport {safeJsonParse} from './json';\nexport {memoizeOnce} from './memoization';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAC,WAAW,EAAE,YAAY,EAAE,gBAAgB,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AAE9E,OAAO,EACL,aAAa,EACb,YAAY,EACZ,cAAc,EACd,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAElC,OAAO,EAAC,YAAY,EAAC,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAC,WAAW,EAAE,gBAAgB,EAAE,UAAU,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAE1E,OAAO,EAAC,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAoB,MAAM,OAAO,CAAC;AAE5E,OAAO,EACL,aAAa,EACb,+BAA+B,EAC/B,gCAAgC,EAChC,kBAAkB,EAClB,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAC1C,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,eAAe,EACf,iBAAiB,GAClB,MAAM,WAAW,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport {opacifyHex} from './color';\n\nexport {formatCount, formatCount4, formatCountShort, shorten} from './format';\n\nexport {\n NUMBER_FORMAT,\n formatNumber,\n formatDateTime,\n formatDate,\n formatTimeOfDay,\n formatTimeRelative,\n formatTimestampForFilename,\n getErrorMessageForDisplay,\n} from './helpers';\n\nexport {isMacOS} from './browser';\n\nexport {genRandomStr} from './random';\n\nexport {formatBytes, camelCaseToTitle, capitalize, truncate} from './str';\n\nexport {postData, downloadFile, uploadFile, type ProgressInfo} from './xhr';\n\nexport {\n splitFilePath,\n convertToValidColumnOrTableName,\n convertToUniqueColumnOrTableName,\n generateUniqueName,\n generateUniquePath,\n convertToUniqueS3ObjectName,\n convertToUniqueS3FolderPath,\n} from './filepaths';\n\nexport {safeJsonParse} from './json';\nexport {memoizeOnce} from './memoization';\nexport {\n isNegativeDecimal,\n negateDecimal,\n toDecimalString,\n toDecimalNumber,\n fromDecimalString,\n} from './decimal';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"memoization.d.ts","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,WAAW,CAAC,KAAK,SAAS,SAAS,OAAO,EAAE,EAAE,OAAO,EACnE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,GAC9B,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAkB7B"}
1
+ {"version":3,"file":"memoization.d.ts","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,WAAW,CAAC,KAAK,SAAS,SAAS,OAAO,EAAE,EAAE,OAAO,EACnE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,GAC9B,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAsB7B"}
@@ -36,7 +36,9 @@ export function memoizeOnce(fn) {
36
36
  return lastResult;
37
37
  }
38
38
  // Call the function with the correct context and cache the result
39
- lastResult = fn.apply(this, args);
39
+ lastResult = fn.apply(this,
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ args);
40
42
  lastArgs = args;
41
43
  hasResult = true;
42
44
  return lastResult;
@@ -1 +1 @@
1
- {"version":3,"file":"memoization.js","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,WAAW,CACzB,EAA+B;IAE/B,IAAI,QAA2B,CAAC;IAChC,IAAI,UAAmB,CAAC;IACxB,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,OAAO,UAAqB,GAAG,IAAW;QACxC,iEAAiE;QACjE,IAAI,SAAS,IAAI,QAAQ,IAAI,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC;YACvD,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,kEAAkE;QAClE,UAAU,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAwB,CAAC,CAAC;QACtD,QAAQ,GAAG,IAAI,CAAC;QAChB,SAAS,GAAG,IAAI,CAAC;QAEjB,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,SAAS,CAA+B,CAAI,EAAE,CAAI;IACzD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["/**\n * Creates a memoized version of a function that caches only the last result.\n * The cache is invalidated when any of the arguments change.\n *\n * This is useful for expensive operations that are likely to be called\n * multiple times with the same arguments, like database queries or API calls.\n *\n * @param fn - The function to memoize\n * @returns A memoized version of the function\n *\n * @example\n * ```ts\n * const expensiveQuery = async (userId: string, limit: number) => {\n * return await database.query(`SELECT * FROM users WHERE id = ? LIMIT ?`, [userId, limit]);\n * };\n *\n * const memoizedQuery = memoizeOnce(expensiveQuery);\n *\n * // First call executes the function\n * const result1 = await memoizedQuery(\"123\", 10);\n *\n * // Second call with same arguments returns cached result\n * const result2 = await memoizedQuery(\"123\", 10); // Uses cache\n *\n * // Call with different arguments invalidates cache and executes function\n * const result3 = await memoizedQuery(\"456\", 10); // Executes function\n * ```\n */\nexport function memoizeOnce<TArgs extends readonly unknown[], TReturn>(\n fn: (...args: TArgs) => TReturn,\n): (...args: TArgs) => TReturn {\n let lastArgs: TArgs | undefined;\n let lastResult: TReturn;\n let hasResult = false;\n\n return function (this: any, ...args: TArgs): TReturn {\n // Check if we have a cached result and arguments haven't changed\n if (hasResult && lastArgs && argsEqual(lastArgs, args)) {\n return lastResult;\n }\n\n // Call the function with the correct context and cache the result\n lastResult = fn.apply(this, args as unknown as any[]);\n lastArgs = args;\n hasResult = true;\n\n return lastResult;\n };\n}\n\n/**\n * Shallow comparison of two arrays to check if all elements are equal\n */\nfunction argsEqual<T extends readonly unknown[]>(a: T, b: T): boolean {\n if (a.length !== b.length) return false;\n return a.every((val, index) => val === b[index]);\n}\n"]}
1
+ {"version":3,"file":"memoization.js","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,WAAW,CACzB,EAA+B;IAE/B,IAAI,QAA2B,CAAC;IAChC,IAAI,UAAmB,CAAC;IACxB,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,OAAO,UAAyB,GAAG,IAAW;QAC5C,iEAAiE;QACjE,IAAI,SAAS,IAAI,QAAQ,IAAI,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC;YACvD,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,kEAAkE;QAClE,UAAU,GAAG,EAAE,CAAC,KAAK,CACnB,IAAI;QACJ,8DAA8D;QAC9D,IAAwB,CACzB,CAAC;QACF,QAAQ,GAAG,IAAI,CAAC;QAChB,SAAS,GAAG,IAAI,CAAC;QAEjB,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,SAAS,CAA+B,CAAI,EAAE,CAAI;IACzD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["/**\n * Creates a memoized version of a function that caches only the last result.\n * The cache is invalidated when any of the arguments change.\n *\n * This is useful for expensive operations that are likely to be called\n * multiple times with the same arguments, like database queries or API calls.\n *\n * @param fn - The function to memoize\n * @returns A memoized version of the function\n *\n * @example\n * ```ts\n * const expensiveQuery = async (userId: string, limit: number) => {\n * return await database.query(`SELECT * FROM users WHERE id = ? LIMIT ?`, [userId, limit]);\n * };\n *\n * const memoizedQuery = memoizeOnce(expensiveQuery);\n *\n * // First call executes the function\n * const result1 = await memoizedQuery(\"123\", 10);\n *\n * // Second call with same arguments returns cached result\n * const result2 = await memoizedQuery(\"123\", 10); // Uses cache\n *\n * // Call with different arguments invalidates cache and executes function\n * const result3 = await memoizedQuery(\"456\", 10); // Executes function\n * ```\n */\nexport function memoizeOnce<TArgs extends readonly unknown[], TReturn>(\n fn: (...args: TArgs) => TReturn,\n): (...args: TArgs) => TReturn {\n let lastArgs: TArgs | undefined;\n let lastResult: TReturn;\n let hasResult = false;\n\n return function (this: unknown, ...args: TArgs): TReturn {\n // Check if we have a cached result and arguments haven't changed\n if (hasResult && lastArgs && argsEqual(lastArgs, args)) {\n return lastResult;\n }\n\n // Call the function with the correct context and cache the result\n lastResult = fn.apply(\n this,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n args as unknown as any[],\n );\n lastArgs = args;\n hasResult = true;\n\n return lastResult;\n };\n}\n\n/**\n * Shallow comparison of two arrays to check if all elements are equal\n */\nfunction argsEqual<T extends readonly unknown[]>(a: T, b: T): boolean {\n if (a.length !== b.length) return false;\n return a.every((val, index) => val === b[index]);\n}\n"]}
package/dist/random.d.ts CHANGED
@@ -1,12 +1,11 @@
1
1
  /**
2
- * Generates a random string of specified length with optional seed
2
+ * Generates a random string of specified length
3
3
  * @param length - The length of the random string to generate
4
- * @param seed - Seed will be ignored.
5
4
  * @returns Random string containing uppercase letters, lowercase letters, and numbers
6
5
  * @example
7
6
  * ```ts
8
7
  * const random = genRandomStr(10); // e.g., "aB3kF9mN2x"
9
8
  * ```
10
9
  */
11
- export declare function genRandomStr(length: number, seed?: string): string;
10
+ export declare function genRandomStr(length: number): string;
12
11
  //# sourceMappingURL=random.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,UAezD"}
1
+ {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,UAe1C"}
package/dist/random.js CHANGED
@@ -1,14 +1,13 @@
1
1
  /**
2
- * Generates a random string of specified length with optional seed
2
+ * Generates a random string of specified length
3
3
  * @param length - The length of the random string to generate
4
- * @param seed - Seed will be ignored.
5
4
  * @returns Random string containing uppercase letters, lowercase letters, and numbers
6
5
  * @example
7
6
  * ```ts
8
7
  * const random = genRandomStr(10); // e.g., "aB3kF9mN2x"
9
8
  * ```
10
9
  */
11
- export function genRandomStr(length, seed) {
10
+ export function genRandomStr(length) {
12
11
  return Array.from((function* () {
13
12
  for (let i = 0; i < length; i++) {
14
13
  const v = Math.floor(Math.random() * (26 * 2 + 10));
@@ -1 +1 @@
1
- {"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,IAAa;IACxD,OAAO,KAAK,CAAC,IAAI,CACf,CAAC,QAAQ,CAAC;QACR,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAChC,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACpD,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBACX,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;iBAAM,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBAClB,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;iBAAM,CAAC;gBACN,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;QACH,CAAC;IACH,CAAC,CAAC,EAAE,CACL,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACb,CAAC","sourcesContent":["/**\n * Generates a random string of specified length with optional seed\n * @param length - The length of the random string to generate\n * @param seed - Seed will be ignored.\n * @returns Random string containing uppercase letters, lowercase letters, and numbers\n * @example\n * ```ts\n * const random = genRandomStr(10); // e.g., \"aB3kF9mN2x\"\n * ```\n */\nexport function genRandomStr(length: number, seed?: string) {\n return Array.from(\n (function* () {\n for (let i = 0; i < length; i++) {\n const v = Math.floor(Math.random() * (26 * 2 + 10));\n if (v < 26) {\n yield String.fromCharCode(v + 65); // 'A' - 'Z'\n } else if (v < 52) {\n yield String.fromCharCode(v + 71); // 'a' - 'z'\n } else {\n yield String.fromCharCode(v + 48); // '0' - '9'\n }\n }\n })(),\n ).join('');\n}\n"]}
1
+ {"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,OAAO,KAAK,CAAC,IAAI,CACf,CAAC,QAAQ,CAAC;QACR,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAChC,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACpD,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBACX,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;iBAAM,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBAClB,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;iBAAM,CAAC;gBACN,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY;YACjD,CAAC;QACH,CAAC;IACH,CAAC,CAAC,EAAE,CACL,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACb,CAAC","sourcesContent":["/**\n * Generates a random string of specified length\n * @param length - The length of the random string to generate\n * @returns Random string containing uppercase letters, lowercase letters, and numbers\n * @example\n * ```ts\n * const random = genRandomStr(10); // e.g., \"aB3kF9mN2x\"\n * ```\n */\nexport function genRandomStr(length: number) {\n return Array.from(\n (function* () {\n for (let i = 0; i < length; i++) {\n const v = Math.floor(Math.random() * (26 * 2 + 10));\n if (v < 26) {\n yield String.fromCharCode(v + 65); // 'A' - 'Z'\n } else if (v < 52) {\n yield String.fromCharCode(v + 71); // 'a' - 'z'\n } else {\n yield String.fromCharCode(v + 48); // '0' - '9'\n }\n }\n })(),\n ).join('');\n}\n"]}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@sqlrooms/utils",
3
- "version": "0.26.1-rc.7",
3
+ "version": "0.27.0-rc.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
7
7
  "type": "module",
8
8
  "sideEffects": false,
9
- "author": "Ilya Boyandin <ilya@boyandin.me>",
9
+ "author": "SQLRooms Contributors",
10
10
  "license": "MIT",
11
11
  "repository": {
12
12
  "type": "git",
@@ -37,11 +37,11 @@
37
37
  "@types/d3-color": "^3.1.3",
38
38
  "@types/d3-format": "^3.0.4",
39
39
  "@types/d3-time-format": "^4.0.3",
40
- "usehooks-ts": "^3.1.1"
40
+ "ts-jest": "^29.4.4"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "react": ">=18",
44
44
  "react-dom": ">=18"
45
45
  },
46
- "gitHead": "ca84c57cb88c90be981083ee366f83bc805ba899"
46
+ "gitHead": "392da9702a049dc3d57fa467bbbf52bf2db7ffd1"
47
47
  }