unstring
Configurable strings cache swaps between strings and their ID.
This package:
- knows strings to replace instead of sending the whole string
- can be pre-configured with strings
- can learn strings on-the-fly
- can limit the number of strings in its cache
- can limit the bytes of strings stored in its cache
- can limit by the size of a string, both minimum and maximum length
See packages:
Install
npm install unstring --save
Usage
// get the buildervar buildUnstring = // build one and configure itvar unstring = // get `id` for this string in this cache.// we know from above it's the first string,// returns an object with `id` and `known`.// so, its `id` is `0` and `known` is `true`// because it was known before the call.var id = unstring // an unknown string will be learned and its ID returned.// this one will be learned.// so, its `id` is `2` (the third string),// and `known` is `false` because it wasn't// known *before* the call.id = unstring // if it isn't allowed due to a limit, such as min length:// this returns `false` because it can't be learned.id = unstring // a string beyond the `max` will also be denied with `false`.id = unstring // if we'd already added 200 strings// then this would be denied with a `false`.id = unstring // if the total bytes used by all the strings learned// exceeded (10 * 1024) bytes (configured above)// then this would be denied with `false`.id = unstring // for all known strings it's possible to get the// string back using its ID.// if `id` was `1` then we'd get `'some value'`.var string = unstring // can teach it strings at any time,// ignoring all restrictions,// via `add1()` and `add()`.unstringunstringunstringunstring // test if a string is known:unstring // returns `true`unstring // returns `true`unstring // returns `true` (above...)unstring // returns false // ignore restrictions and learn a string anyway:unstring// the length is optional.// when you don't specify it then unstring// will do `Buffer.byteLength(string)`.// Note, this can override a current known string at that ID.