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

srlz

v1.0.0

Published

A small serialization library that can be adapted to your own needs.

Downloads

2

Readme

srlz Build Status

A small serialization library that can be adapted to your own needs.

API

GenericSerializer

A generic serializer which returns the input value and is the base for every serializer implementation.

ObjectSerializer

A serializer which serializes / deserializes values based on the configures fields and corresponding serializers.

In most cases the class has to be extends by a more specific serializer class which takes care about a specific object type.

fields

Instance member

Type: Object

Each key of the objects maps to a field name and each value to a field specification.

Field specification
  • serializer (optional) Type: Class extends GenericSerializer Defines the serializer class which should be used for the field.
  • required (optional) Type: boolean Throws an error if the field is missing during deserialization.

Usage

Custom serializer

Creates serializer which convers the given array to a string by using a configured seperator.

import { GenericSerializer } from 'srlz';
// OR
const { GenericSerializer } = require('srlz');

class JoinedArraySerializer extends GenericSerializer {
    separator = '||';

    serialize(value) {
        return value.join(this.separator);
    }

    deserialize(value) {
        return value.splut(this.separator);
    }
}

const serializer = new JoinedArraySerializer();
serializer.serialize([ 1, 2, 3, 4, 5 ]);
// Output: '1||2||3||4||5'

Custom object serializer

Converts a person object into an object in which the skills are represented as string.

import { ObjectSerializer } from 'srlz';
// OR
const { ObjectSerializer } = require('srlz');

class PersonSerializer extends ObjectSerializer {
    fields = {
        name: {},
        skills: { serializer: JoinedArraySerializer }
    }
}

const serializer = new PersonSerializer();
serializer.serialize({
    name: 'John Doe',
    skills: [ 'Javascript', 'Typescript' ]
});
// Output: {
//   name: 'John Doe',
//   skills: 'Javascript||Typescript'
// }

Complex example

This example shows how to create custom serializers and how a serialization for the non scalar type Date could look like.

const { GenericSerializer, ObjectSerializer } = require('srlz');

const post = {
    title: 'My Blog Title',
    publishedAt: new Date('2020-08-01T09:30:00.000Z')
};

class ISODateSerializer extends GenericSerializer {
    serialize(value) {
        return value.toISOString();
    }

    deserialize(value) {
        return new Date(value);
    }
}

class PostSerializer extends ObjectSerializer {
    fields = {
        title: {},
        publishedAt: { serializer: ISODateSerializer }
    }
}

const postSerializer = new PostSerializer();
postSerializer.serialize(post);
// Output: {
//   title: 'My Blog Title',
//   publishedAt: '2020-08-01T09:30:00.000Z'
// }