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

fulminate-serializer

v1.2.3

Published

An easy way of serializing objects.

Downloads

5

Readme

Fulminate Serializer

DEPRECATED!

Go here for up-to-date version of fulminate. This repository will be removed on January 1, 2018.

FS simplifies the process of serialization for objects of any kind. The original idea of this package was to create an easy way of serializing objects that arrive from TypeORM.

Installation

npm install --save fulminate-serializer

Basic Usage

TypeScript

import { Serializer } from "fulminate-serializer";

let obj = {
    "id": 1,
    "title": "Foo",
    "description": "Bar",
}

let serializer : Serializer = new Serializer();

console.dir(serializer.serialize(obj, ["title", "description"]));

// Output:
// Object { title: "Foo", description: "Bar" }

JavaScript

var serializer = require("fulminate-serializer").Serializer;

var obj = {
    "id": 1,
    "title": "Foo",
    "description": "Bar"
}

console.dir(serializer.serialize(obj, ["title", "description"]));

// Output:
// Object { title: "Foo", description: "Bar" }

Advanced Usage

  • input: object/array of objects to serialize
  • serializationFields (optional, defaults to []): array of strings that represents object properties to be used
  • isWhiteList (optional, defaults to true): boolean that defines if properties from the first argument should show (true) or hide (false) given properties
  • toJson (optional, defaults to false): boolean that defines if JSON.stringify() should be applied on the output.
  • serializeChilren (optional, defaults to true): boolean that defines if child objects of given input should also be serialized ( to serialize child objects, provide .-separated field names to serializationFields array). Bear in mind that currently serializer can only go one level deep (e.g. ["user.credentials.socialNetworks"] will only apply serialization to user and credentials, but NOT socialNetworks).

Example of using children serialization

import {Serializer} from "fulminate-serializer";
let object = {
  db: {
    name: "database",
    port: 3306,
    host: "localhost",
    user: "root",
    pass: "",
  },
  name: "Ostap",
  surname: "Bender",
};

let objectArray = [object, object, object];
console.dir(new Serializer().serialize(object, ["db.port", "name"], false));
console.dir(new Serializer().serialize(objectArray, ["db.port", "db.user", "name"], false));

// Output: { db: { name: 'database', host: 'localhost', user: 'root', pass: '' },
//             surname: 'Bender' }
//         [ { db: { name: 'database', host: 'localhost', pass: '' },
//             surname: 'Bender' },
//           { db: { name: 'database', host: 'localhost', pass: '' },
//             surname: 'Bender' },
//           { db: { name: 'database', host: 'localhost', pass: '' },
//             surname: 'Bender' } ]

TypeScript (with Express, TypeORM and RoutingControllers)

Read more about:

UserController.ts

import { Serializer } from "fulminate-serializer";
import { JsonController, Get } from "routing-controllers";
import { getConnectionManager, Repository } from "typeorm";
import { User } from "/* PATH_TO_USER_MODEL */";

@JsonController()
export class UserController {
    private userRepository : Repository<User>;
    private serializer : Serializer;

    constructor() {
        this.userRepository = getConnectionManager().get().getRepository(User);
        this.serializer = new Serializer();
    }

    @Get("/users")
    async getAll() {
        return this.serializer.serialize(await this.userRepository.find(), User.SHORT_RESPONSE);
    }
}

User.ts

import { ColumnTypes } from "typeorm/metadata/types/ColumnTypes";
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity("account_users", {
    engine: "InnoDB",
})
export class User {
    public static readonly SHORT_RESPONSE : Array<string> = [
        "id",
        "username",
    ];

    @PrimaryGeneratedColumn()
    id : number;

    @Column(ColumnTypes.STRING, {
        unique: true,
        length: 30,
    })
    username : string;

    @Column(ColumnTypes.STRING, {
        length: 50,
    })
    password : string;
}

On the page, you will see:

[
    {
        "__comment": {
            "title": "This will NOT appear in your JSON output, it is just a comment to deliver a message to you personally",
            "message": "Assuming you have users in your DB you will see results for each of them serialized"
        }
    },
    {
        "id": "ID_OF_SERIALIZED_USER",
        "username": "USERNAME_OF_SERIALIZED_USER"
    }
]

ToDo

  • Unit testing
  • Recursive object serialization