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

binary-serializer-next

v1.0.0

Published

--- Next gen Class hydration library, super slim ~1Kb. Written mostly for Typescript. Works in both node and browser. While it is possible to use with plain JS it will require some pre-processor like babel with decorators support.

Downloads

3

Readme

Hydrator Next


Next gen Class hydration library, super slim ~1Kb. Written mostly for Typescript. Works in both node and browser. While it is possible to use with plain JS it will require some pre-processor like babel with decorators support.

Only depends on reflect-metadata polyfil.

For Typescript, you need to have these two options turned on in tsconfig.json:

{
	"compilerOptions": {
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true
	}
}

npm

WARNING: API is not yet final

Requirements

Technically it should have no requirements.


Docs

Read the docs here


Installation

For yarn:

yarn add hydrator-next

For npm:

npm i hydrator-next

For browser just include the script or import it via ES6 import statement.

Some examples


Imports:

// ES6 JS/Typescript style
import { Hydrator } from 'hydrator-next';

// require

const { Hydrator } = require('hydrator-next');

Basic usage:

class Test {
	some = 1;
	property = 'a';
}

// hydrate from raw data
const obj = Hydrator.hydrate(Test, {some: 2, property: 'b'});

FieldType options (all of them are optional):

| Property | Type | Description | |----------|------|-------------| | type | string | Property type, has to be registered with Converter.registerConverter | | nested | constructor | If property is another nested class, explicitly state the class, ex. Date | | array | boolean | Is property an array, need to also provide type otherwise it will have no effect | | converter | (value: any, direction: DataDirection, type: string) => any; | custom converter function, example of usage below | | translate | string | Should this property be translated into some other field when hydrating/dehydrating, useful for mapping plain object under_score_case to camelCaseProperty | | translateDatabase | string | Same as translate however will only have effect when hydrating/dehydrating to database | | ignoreDatabase | boolean | value will be ignored when coming or going to database, which basically means field does not exist in database | | ignorePlain | boolean | value will be ignored when coming or going to plain object, which basically means field should not be hydrated while operating on raw data | | ignore | boolean | value will be ignored, won't ever be touched by Hydrator |

Advanced usage:

Converter.registerConverter('string', convertString);
Converter.registerConverter('number', convertNumber);
Converter.registerConverter('bigint', convertBigInt);

function customConverter(value: any, direction: DataDirection, type: string) {
	switch (direction) {
		case DataDirection.FromDatabase:
		case DataDirection.FromPlain:
			return '_' + value;
		case DataDirection.ToDatabase:
		case DataDirection.ToPlain:
			return value.substr(1);
	}
}

class TestClassCustom {
	@FieldType({ converter: customConverter })
	cust: any;
	
	// Forced to string upon hydration
	@FieldType({ type: 'string' })
	str: string;
	
	// Forced to number upon hydration
	@FieldType({ type: 'number' })
	num: string;

	// Forced to BigInt upon hydration
	@FieldType({ type: 'bigint' })
	bi: string;
	
	// Any data will come here
	fieldOfAnyValue: any = null;

	// Will not be changed at all upon migration
	@FieldType({ ignore: true })
	ignorethis: string = '9';
}

Hydrator.hydrate(TestClassCustom, {
	cust: 'a',
	str: 123,
	num: '999',
	bi: 123123,
	fieldOfAnyValue: new Date(),
	// field is not decorated nor present as property in class, thus will be ignored.
	willBeIgnored: 'aaa',
	ignorethis: 'nope'
})