# pad-ratio

> Pad or trim an array to sum up to a maximum value

Latest version **2.1.0** (published 2026-03-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install pad-ratio
pnpm add pad-ratio
yarn add pad-ratio
bun add pad-ratio
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2026-03-25 |
| First published | 2019-03-10 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=1 |
| Dependencies | 0 |
| Unpacked size | 17.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Miraculous Owonubi |
| Maintainers | miraclx |
| Keywords | pad, parse, trim, slice, array, maximum, total, ratio, values, numbers |

## Links

- npm: https://www.npmjs.com/package/pad-ratio
- Repository: https://github.com/miraclx/pad-ratio
- Homepage: https://github.com/miraclx/pad-ratio#readme
- Issues: https://github.com/miraclx/pad-ratio/issues
- npm.io page: https://npm.io/package/pad-ratio

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2026-03-25
- 2.0.0 — 2020-04-21
- 1.1.1 — 2019-07-15
- 1.1.0 — 2019-05-07
- 1.0.3 — 2019-03-11
- 1.0.2 — 2019-03-11
- 1.0.1 — 2019-03-10
- 1.0.0 — 2019-03-10

## README

# pad-ratio

> Pad or trim an array to sum up to a maximum value

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]

[![NPM][npm-image-url]][npm-url]

## Installing

Via [NPM][npm]:

``` bash
npm install pad-ratio
```

## Usage

``` javascript
// Node CommonJS
const padRatio = require('pad-ratio');
// Or Node ES6
import padRatio from 'pad-ratio';
```

``` html
<!-- Or in the Browser -->
<script src="pad-ratio/dist/index.js"></script>
```

## Examples

``` javascript
padRatio([20, 30]); // [ 20, 30, 50 ]
padRatio([20, 30], 100); // [ 20, 30, 50 ]
padRatio([45.642, 15.027], 120); // [ 45.642, 15.027, 59.330999999999996 ]
padRatio([45.642, 15.027], 120, false); // [ 45.642, 74.35799999999999 ]
padRatio('5:2:8', 20); // [ 5, 2, 8, 5 ]
padRatio('5:2:8', 20, false); // [ 5, 2, 13 ]
```

## API

### padRatio(array[, maximum[, append])

* `array`: &lt;[HybridInput](#hybridinput)&gt;
* `maximum`: &lt;[number][]&gt; **Default**: `100`
* `append`: &lt;[boolean][]&gt; **Default**: `true`

Parse and pad `array` appending or trimming it till it's contents sum up to `maximum`.
The `append` argument determined whether to push the remnant to the array or to increment the final value with it.

### <a id='hybridinput'></a> HybridInput: [string][]|[number][]|[number][][]

If `HybridInput` is a string, the value separators can be any of `,`, `:`, `;`, `|`.

``` javascript
padRatio([20, 30, 40], 100)
// [ 20, 30, 40, 10 ]

padRatio([20, 30, 40], 100, false)
// [ 20, 30, 50 ]
```

## Development

### Building

Feel free to clone, use in adherance to the [license](#license) and perhaps send pull requests

``` bash
git clone https://github.com/miraclx/pad-ratio.git
cd pad-ratio
npm install
# hack on code
npm test
npm run build
```

### Testing

Tests are executed with [Jest][jest]. To use it, simple run `npm install`, it will install
Jest and its dependencies in your project's `node_modules` directory followed by `npm run build` and finally `npm test`.

To run the tests:

```bash
npm install
npm run build
npm test
```

## License

[Apache 2.0][license] © **Miraculous Owonubi** ([@miraclx][author-url]) &lt;omiraculous@gmail.com&gt;

[npm]:  https://github.com/npm/cli 'The Node Package Manager'
[jest]:  https://github.com/facebook/jest 'Delightful JavaScript Testing'
[license]:  LICENSE 'Apache 2.0 License'
[author-url]: https://github.com/miraclx

[npm-url]: https://npmjs.org/package/pad-ratio
[npm-image]: https://badgen.net/npm/node/pad-ratio
[npm-image-url]: https://nodei.co/npm/pad-ratio.png?stars&downloads
[downloads-url]: https://npmjs.org/package/pad-ratio
[downloads-image]: https://badgen.net/npm/dm/pad-ratio

[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type

---
_Source: https://npm.io/package/pad-ratio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
