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

@abw/badger-codecs

v1.0.3

Published

Wrapper functions for encoding/decoding data

Downloads

18

Readme

Badger Codecs

This implements a consistent interface for encoding and decoding data to and from various different formats.

Installation

Install badger-codecs using your favourite package manager.

npm

npm add @abw/badger-codecs

pnpm

pnpm add @abw/badger-codecs

yarn

yarn add @abw/badger-codecs

Why?

If you want to encode/decode some data as JSON, you can do this:

const encoded = JSON.stringify(data);
const decoded = JSON.parse(encoded);

Or if you want to encode/decode some data as YAML, you can do this:

import yaml from 'js-yaml';
const encoded = yaml.dump(data);
const decoded = yaml.load(encoded);

This module provides a codec() function for selecting a codec. The codec returned has encode() and decode() functions which are wrappers around the appropriate code for the format.

For JSON:

import { codec } from '@abw/badger-codecs'
const myCodec = codec('json');
const encoded = myCodec.encode(data);
const decoded = myCodec.decode(encoded);

Or for YAML:

import { codec } from '@abw/badger-codecs'
const myCodec = codec('yaml');
const encoded = myCodec.encode(data);
const decoded = myCodec.decode(encoded);

The only thing that needs to change is the line that selects the codec.

A typical example of use is where you have a function that expects to be passed a data file. You can look at the file extension so see if it's .json or .yaml and select the correct codec accordingly. You don't need an if/else statement to run the right piece of code. You just select a codec using the file extension and then call its encode() and decode() functions.

Codecs

The json codec is for encoding/decoding JSON.

The yaml codec is for encoding/decoding YAML.

The base64 codec is for encoding/decoding UTF8 text to and from Base64.

The yml codec is provided as an alias for yaml, and jsn as an alias for json. It's quite common to see YAML files with a .yml extension so this simplifies the process of selecting a codec using the file extension. I've never see a JSON file with a .jsn extension in the wild, but it's provided for the sake of consistency.

Chained Codecs

You can chain multiple codecs together with +. For example, a codec to JSON encode some data and then Base64 encode it would be json+base64.

import { codec } from '@abw/badger-codecs'
const myCodec = codec('json+base64');
const encoded = myCodec.encode(data);
const decoded = myCodec.decode(encoded);

The encode() function will apply the encode() function for each of the chained codecs in sequence. The decode() function will apply the decode() function for each of them, but in reverse order.

Codec Configuration

The codec() function takes an optional second argument as an object of configuration options.

For the json codec the only option is space to set the number of spaces for the indent level. The default is 2.

import { codec } from '@abw/badger-codecs'

const json = codec('json', { space: 4 });   // set space to 4

For the yaml code the configuration options are used when encoding YAML. These include indent for the indent level (default is 2) and any of the other options supported by the dump() method of js-yaml.

import { codec } from '@abw/badger-codecs'

const yaml = codec('yaml', { indent: 4 });

Global Configuration

The codecConfig() function can be used to set the global configuration defaults for a codec. Note that any changes made will only affect codecs created after that point. You can still override the defaults by providing a configuration when you create the codec.

import { codec, codecConfig } from '@abw/badger-codecs'

const json2 = codec('json')                 // has default space of 2

codecConfig('json', { space: 4 })           // set default space to 4

const json4 = codec('json')                 // has default space of 4
const json3 = codec('json', { space: 3 })   // explicit space of 3

Adding Codecs

The module exports an addCodec() function which can be used to register a new codec. It expects three or four arguments: the codec name, an encode() function, a decode() function and an optional config() function to set the configuration defaults.

This trivial example shows how a codec can be registered for "encoding" text as upper case, and then "decoding" it back to lower case.

import { addCodec } from '@abw/badger-codecs'

addCodec(
  'upper',
  lower => lower.toUpperCase(),
  upper => upper.toLowerCase(),
)

You can now use the codec like any other.

import { codec } from '@abw/badger-codecs'
const myCodec = codec('upper');
const encoded = myCodec.encode("hello");    // => HELLO
const decoded = myCodec.decode("HELLO");    // => hello

NOTE: For consistency, all codecs are stored with lower case names. That means that codec("JSON") and codec("json") both return the same codec. You can register your codec with an upper or mixed case name if you really want to, but be warned that internally it is converted to lower case. The outcome is that you can't register two different codecs with names that only differ in case.

Author

Andy Wardley https://github.com/abw