Skip to content

Commit

Permalink
Added decimaljs.light for item caculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-bennett committed Mar 16, 2021
1 parent cd97cf1 commit 999aead
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion itemsize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BASE_LOGICAL_SIZE_OF_NESTED_TYPES = 1
const LOGICAL_SIZE_OF_EMPTY_DOCUMENT = 3
const { encode } = require('utf8')
const atob = require('atob')
const Decimal = require('decimal.js-light')

// Inspired by https://zaccharles.github.io/dynamodb-calculator/ and adapted for node

Expand Down Expand Up @@ -31,7 +32,8 @@ const calculateAttributeSizeInBytes = (attribute) => {

const calculateNumberSizeInBytes = (number) => {
if (number === 0) return 1
const fixed = Number.parseFloat(number).toFixed()
const decimal = new Decimal(number)
const fixed = decimal.toFixed()
let size = calculateFixedSizeInBytes(fixed.replace('-', '')) + 1
if (fixed.startsWith('-')) size++
if (size > 21) size = 21
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@aws-sdk/util-dynamodb": "^3.7.0",
"abstract-leveldown": "^6.3.0",
"atob": "^2.1.2",
"decimal.js-light": "^2.5.1",
"inherits": "^2.0.1",
"through2": "^4.0.2",
"utf8": "^3.0.0"
Expand Down

0 comments on commit 999aead

Please sign in to comment.