Skip to content

Struggling to understand the allocate function #423

Answered by sarahdayan
JayyWalker asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @JayyWalker!

When using allocate, you're splitting the value into "buckets". If you do CSS, you can see it as CSS Grids, when you distribute grid-template-columns (1fr 1fr 2fr ...).

If you need to split your object into twelve equal parts, your code should look like this:

const d1 = dinero({ amount: 51998, currency: GBP });

const allocation = allocate(d1, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); // an array of 12 ratios of value `1` (even distribution)
const values = allocation.map((allocated) => toUnit(allocated));

console.log(values); // [ 52, 52, 52, 52, 52, 52, 52, 52, 51.99, 51.99 ]

As you can see, allocate split the value into twelve parts, trying as much as possible to make them equa…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@JayyWalker
Comment options

@sarahdayan
Comment options

Answer selected by sarahdayan
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants