npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unstring

v0.3.0

Published

Configurable strings cache swaps between strings and their ID.

Downloads

5

Readme

unstring

Build Status Dependency Status npm version Coverage Status

Configurable strings cache swaps between strings and their ID.

This package:

  1. knows strings to replace instead of sending the whole string
  2. can be pre-configured with strings
  3. can learn strings on-the-fly
  4. can limit the number of strings in its cache
  5. can limit the bytes of strings stored in its cache
  6. can limit by the size of a string, both minimum and maximum length

See packages:

  1. endeo
  2. enbyte
  3. debyte

Install

npm install unstring --save

Usage

// get the builder
var buildUnstring = require('unstring')

// build one and configure it
var unstring = buildUnstring({
  // by default, there are no limits.
  limit: 200,      // limit by *number* of strings
  bytes: 10 * 1024,// limit by bytes of all stored strings
  min: 4,          // min chars in string to learn it
  max: 100,        // avoid "learning" very long strings
  // which strings should it know already.
  // by default, it knows none.
  strings: [
    'key1', 'some value'
  ]
})

// 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.string('key1')

// 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.string('unknown')

// if it isn't allowed due to a limit, such as min length:
// this returns `false` because it can't be learned.
id = unstring.string('a')

// a string beyond the `max` will also be denied with `false`.
id = unstring.string('blah blah blah......imagine 101+ chars')

// if we'd already added 200 strings
// then this would be denied with a `false`.
id = unstring.string('the 201+ string() attempt')

// 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.string('too many bytes learned')

// 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.restring(id)


// can teach it strings at any time,
// ignoring all restrictions,
// via `add1()` and `add()`.
unstring.add1('single string *only*')
unstring.add('one string')
unstring.add('or', 'multiple', 'strings')
unstring.add([ 'or', 'array', 'of', 'strings' ])

// test if a string is known:
unstring.has('key1') // returns `true`
unstring.has('some value') // returns `true`
unstring.has('unknown') // returns `true` (above...)
unstring.has('not this one') // returns false


// ignore restrictions and learn a string anyway:
unstring.learn(123, 'string', stringByteLength)
// 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.

MIT License