Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

crialize

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crialize

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

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

crialize

A simple library to transform objects.

Installation

$ npm i crialize # or yarn add crialize

What is it good for?

Crialize 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('crialize');

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:

ParameterTypeDescriptionDefault Value
descriptorObjectDefines 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).-
mapAllValuesBooleanDefines if keys missing in the descriptor will be mapped.false
defaultTransformFunction (a, b) => ObjectHow 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('crialize');

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.

FAQs

Package last updated on 09 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts