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

cerealizr

v1.0.1-alpha

Published

A simple framework to easily transform your data to a useful format

Downloads

1,321

Readme

cerealizr

A simple library to transform objects.

Installation

$ npm i cerealizr # or yarn add cerealizr

What is it good for?

Cerealizr provides a Serializer class which allows you to transform objects into whatever you want. It's specially useful for transforming API requests or responses into nicely formatted and useful data to use in your app.

Basic Usage

const Serializer = require('cerealizr');

const someObject = {
  id: 1,
  first_name: 'John',
  country_id: 200,
  nicknames: ['johny', 'dude'],
  unmapped_key: 'no mapping'
};

const serializer = new Serializer({
  descriptor: {
    id: 'id',
    first_name: 'firstName',
    nicknames: 'nicknames',
    country_id: (key, value) => ({ [`${key}_number`]: value + 100 })
  },
  mapAllValues: false
});

serializer.serialize(someObject);
/*
Result:
{
  id: 1,
  firstName: 'John',
  country_id_number: 300,
  nicknames: ['johny', 'dude']
}
*/

Classes and functions

Serializer

Serializer accepts an object with the following parameters:

| Parameter | Type | Description | Default Value | | ---------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------ | | descriptor | Object | Defines how map your objects. Keys should match with the ones the original object has, and values can be strings (to map keys) or functions (to map keys and/or values). | - | | mapAllValues | Boolean | Defines if keys missing in the descriptor will be mapped. | false | | defaultTransform | Function (a, b) => Object | How to transform those keys that weren't defined in the descriptor. The object that returns MUST have only one key. Only works if mapAllValues is set to true. | (key, value) => ({ [key]: value }) |

CamelcaseSerializer && SnakecaseSerializer

This are special serializers. This serializers map the all the keys as camelcase/snakecase. This can be overridden with the descriptor. The reason this classes are provided, is because it's a usual use case to map an object from and to JSON.

For the time being, they only receive a descriptor parameter and it always maps all values. Also, the function used as values in the descriptor can only map values, as the key tranform is already defined:

const { CamelcaseSerializer } = require('cerealizr');

const someObject = {
  id: 1,
  first_name: 'John',
  country_id: 200,
  nicknames: ['johny', 'dude'],
  unmapped_key: 'no mapping'
};

const serializer = new CamelcaseSerializer({
  descriptor: {
    country_id: (value) => value + 100
  }
});

serializer.serialize(someObject);
/*
Result:
{
  id: 1,
  firstName: 'John',
  countryId: 300,
  nicknames: ['johny', 'dude'],
  unmappedKey: 'no mapping'
}
*/

Useful functions

setValue

Allows to pass a hardcoded value to a descriptor:

const descriptor = { id: 'ID', some_key: setValue(1000) }
setCamelcaseKey && setSnakecaseKey

Maps the key to camelcase/snakecase and receives a function (which defaults to value => value) to map the value:

const descriptor = { id: 'ID', some_key: setCamelcaseKey(value => value + 100) }

Custom Serializers

You can define your own serializers! Both CamelcaseSerializer and SnakecaseSerializer extend KeySerializer, which in turn extends Serializer defining a function to map the key. You can extend KeySerializer to define how to map keys, just as those serializers do.

If you want even more custom behaviour, you can even extend Serializer and override its methods.