MCPcopy Create free account
hub / github.com/Effect-TS/effect / decodeNumeric

Function decodeNumeric

packages/sql/pg/src/PgTypes.ts:721–768  ·  view source on GitHub ↗
(bytes: Uint8Array, offset: number, size: number)

Source from the content-addressed store, hash-verified

719}
720
721const decodeNumeric = (bytes: Uint8Array, offset: number, size: number): string => {
722 if (size < 8) return fail("Truncated numeric value")
723 const count = readInt16(bytes, offset)
724 const weight = readInt16(bytes, offset + 2)
725 const sign = readUint16(bytes, offset + 4)
726 const scale = readInt16(bytes, offset + 6)
727 if (sign === NUMERIC_NAN) return "NaN"
728 if (sign === NUMERIC_PINF) return "Infinity"
729 if (sign === NUMERIC_NINF) return "-Infinity"
730 if (sign !== NUMERIC_POS && sign !== NUMERIC_NEG) {
731 return fail(`Invalid numeric sign: ${sign}`)
732 }
733 requireSize(size, 8 + count * 2, "numeric")
734
735 const digits = offset + 8
736 // Groups outside 0-9999 have no four-digit text, and reading past the ones
737 // the value carries is how the fractional part is padded out.
738 const digitAt = (index: number): number => {
739 if (index < 0 || index >= count) return 0
740 const digit = readInt16(bytes, digits + index * 2)
741 return digit >= 0 && digit <= 9999 ? digit : fail(`Invalid numeric digit: ${digit}`)
742 }
743
744 let result = sign === NUMERIC_NEG ? "-" : ""
745 if (weight < 0) {
746 result += "0"
747 } else {
748 for (let i = 0; i <= weight; i++) {
749 const digit = digitAt(i)
750 if (i === 0) {
751 result += String(digit)
752 } else {
753 result += twoDigits[(digit / 100) | 0]
754 result += twoDigits[digit % 100]
755 }
756 }
757 }
758 if (scale > 0) {
759 let fraction = ""
760 for (let i = weight + 1; fraction.length < scale; i++) {
761 const digit = digitAt(i)
762 fraction += twoDigits[(digit / 100) | 0]
763 fraction += twoDigits[digit % 100]
764 }
765 result += `.${fraction.slice(0, scale)}`
766 }
767 return result
768}
769
770// -----------------------------------------------------------------------------
771// network addresses

Callers

nothing calls this directly

Calls 7

readInt16Function · 0.85
readUint16Function · 0.85
requireSizeFunction · 0.85
digitAtFunction · 0.85
sliceMethod · 0.80
failFunction · 0.70
StringFunction · 0.50

Tested by

no test coverage detected