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

negotiator-currency

v0.3.0

Published

HTTP content negotiation (plus currency)

Downloads

4

Readme

Negotiator

An HTTP content negotiator for node.js written in javascript.

Accept Negotiation

Negotiator = require('negotiator')

availableMediaTypes = ['text/html', 'text/plain', 'application/json']

// The negotiator constructor receives a request object
negotiator = new Negotiator(request)

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'

negotiator.preferredMediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']

negotiator.preferredMediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']

negotiator.preferredMediaType(availableMediaTypes)
// -> 'text/html'

You can check a working example at examples/accept.js.

Methods

preferredMediaTypes(availableMediaTypes):

Returns an array of preferred media types ordered by priority from a list of available media types.

preferredMediaType(availableMediaType):

Returns the top preferred media type from a list of available media types.

Accept-Language Negotiation

Negotiator = require('negotiator')

negotiator = new Negotiator(request)

availableLanguages = 'en', 'es', 'fr'

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.preferredLanguages()
// -> ['es', 'pt', 'en']

negotiator.preferredLanguages(availableLanguages)
// -> ['es', 'en']

language = negotiator.preferredLanguage(availableLanguages)
// -> 'es'

You can check a working example at examples/language.js.

Accept-Language Negotiation

(WARNING: This is not an already standard header, so browsers don't actually send it, nor is planned to be a standard header)

Negotiator = require('negotiator')

negotiator = new Negotiator(request)

availableCurrencies = 'usd', 'eur', 'aud'

// Let's say Accept-Currency header is 'usd;q=0.8, eur, chf'

negotiator.preferredCurrencies()
// -> ['usd', 'eur', 'chf']

negotiator.preferredCurrencies(availableCurrencies)
// -> ['eur', 'usd']

currency = negotiator.preferredCurrency(availableCurrencies)
// -> 'eur'

You can check a working example at examples/currency.js.

Methods

preferredLanguages(availableLanguages):

Returns an array of preferred languages ordered by priority from a list of available languages.

preferredLanguage(availableLanguages):

Returns the top preferred language from a list of available languages.

Accept-Charset Negotiation

Negotiator = require('negotiator')

availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']

negotiator = new Negotiator(request)

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.preferredCharsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']

negotiator.preferredCharsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']

negotiator.preferredCharset(availableCharsets)
// -> 'utf-8'

You can check a working example at examples/charset.js.

Methods

preferredCharsets(availableCharsets):

Returns an array of preferred charsets ordered by priority from a list of available charsets.

preferredCharset(availableCharsets):

Returns the top preferred charset from a list of available charsets.

Accept-Encoding Negotiation

Negotiator = require('negotiator').Negotiator

availableEncodings = ['identity', 'gzip']

negotiator = new Negotiator(request)

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.preferredEncodings()
// -> ['gzip', 'identity', 'compress']

negotiator.preferredEncodings(availableEncodings)
// -> ['gzip', 'identity']

negotiator.preferredEncoding(availableEncodings)
// -> 'gzip'

You can check a working example at examples/encoding.js.

Methods

preferredEncodings(availableEncodings):

Returns an array of preferred encodings ordered by priority from a list of available encodings.

preferredEncoding(availableEncodings):

Returns the top preferred encoding from a list of available encodings.

License

MIT