string-packer

1.0.3 • Public • Published

string-packer npm npm

First-fit bin packing to turn a list into delimiter-separated strings

npm install string-packer

Built this to optimize the batching of data within an http get request. i.e., if you have a list of items that need to fit into a url, squeeze them in there.

You tell it the list of items, the maximum size of the generated string, optionally supply a delimiter for separating the elements, and you'll get back a list of packed strings.

  • The algorithm first sorts a copy of the list to pack the shortest strings first.

Usage

Returns a list of packed strings.

const packer = require('string-packer');
 
const results = packer({
  list: ['foo', 'bar', 'car'],
  binSize: 7,
  delimiter: ',' // optional (default is an empty string)
});
 
console.log(results); // ['foo,bar', 'car']
  • Throws if the binSize is too small to fit any items.
Sorted example (packing the smallest items first):
const packer = require('string-packer');
 
const results = packer({
  list: ['food', 'bar', 'do'],
  binSize: 6
});
 
console.log(results); // ['bardo', 'food']

License:

MIT

Package Sidebar

Install

npm i string-packer

Weekly Downloads

10

Version

1.0.3

License

MIT

Last publish

Collaborators

  • mrjoelkemp