@sqlrooms/utils 0.27.0-rc.0 → 0.27.0-rc.2

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.
@@ -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;IACrD,wCAAwC;IACxC,gFAAgF;IAChF,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;QACvB,MAAM,OAAO,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC;QAC9B,KA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AI,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/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,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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AAC,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/utils",
3
- "version": "0.27.0-rc.0",
3
+ "version": "0.27.0-rc.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -43,5 +43,5 @@
43
43
  "react": ">=18",
44
44
  "react-dom": ">=18"
45
45
  },
46
- "gitHead": "ceafff23c197b8188040f8c93baf4e7d3dd4b081"
46
+ "gitHead": "eec06537352a9e760de21d7bcc77d9aa2db2a5ec"
47
47
  }