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

value-transformer

v0.11.0-next.7

Published

This library allows you to serialize/deserialize complex data using transformers. There is no need to interact with the raw representation: the signature of JSON literals and ArrayBuffers is encapsulated.

Downloads

11

Readme

value-transformer

Warning! This library is at an early stage of development. The API may change without backwards compatibility.

This library allows you to serialize/deserialize complex data using transformers. There is no need to interact with the raw representation: the signature of JSON literals and ArrayBuffers is encapsulated.

CI

Example

export class VectorPictureDto {
  @asString()
  public readonly url: string;

  public constructor(url: VectorPictureDto['url']) {
    this.url = url;
  }
}

export class BitmapPictureDto {
  @asMap(asString(), asString())
  public readonly urls: ReadonlyMap<string, string>;

  public constructor(urls: BitmapPictureDto['urls']) {
    this.urls = urls;
  }
}

export class PictureDto {
  @asDate()
  public readonly createAt: ReadonlyDate;

  @asFloat64()
  public readonly rating: number;

  @asSet(asString())
  public readonly tags: ReadonlySet<string>;

  @asUnion([asClass(BitmapPictureDto), asClass(VectorPictureDto)])
  public readonly type: BitmapPictureDto | VectorPictureDto;

  public constructor(
    createAt: PictureDto['createAt'],
    rating: PictureDto['rating'],
    tags: PictureDto['tags'],
    type: PictureDto['type'],
  ) {
    this.createAt = createAt;
    this.rating = rating;
    this.tags = tags;
    this.type = type;
  }
}

const transformer = asClass(PictureDto);

API

All transformers implement the methods (listed below) in the abstract class ValueTransformer.

compatibleWith(data)

Check compatibility with the type.

decoder()

TODO doc

encode(data)

TODO doc

fromLiteral(literal)

Strictly check the literal for validity and deserialize it into data.

toLiteral(data, compact)

Serialize the passed data into a JSON-like literal.

Transformers

Simple transformers

| Name | Type | Usage example | | ----------- | -------------------- | ------------- | | asBoolean | boolean | asBoolean() | | asFloat64 | number | asFloat64() | | asInt8 | number | asInt8() | | asInt16 | number | asInt16() | | asInt32 | number | asInt32() | | asUint8 | number | asUint8() | | asUint16 | number | asUint16() | | asUint32 | number | asUint32() | | asBigInt | bigint | asBigInt() | | asString | string | asString() | | asDate | Date | asDate() | | asRegExp | RegExp | asRegExp() |

Collection transformers

| Name | Type | Usage example | | --------- | ---------------- | ------------------------------- | | asArray | Array | asArray(asString()) | | asSet | Set | asSet(asString()) | | asMap | Map | asMap(asString(), asString()) |

asNever

Blocks any transformations.

The value can only be null:

const transformer = asNullable(asNever());

Ensuring that the collection is empty:

const transformer = asArray(asNever());
const transformer = asSet(asNever());
const transformer = asMap(asNever(), asNever());

As a stub when updating variants in asUnion:

// version 1
const transformer = asUnion([
  asClass(MediaDto), // actual in version 1
  asClass(BinaryFileDto), // index is 1
]);
// version 2
const transformer = asUnion([
  asNever(), // unactual in version 2
  asClass(BinaryFileDto), // index still 1
  asClass(VideoDto),
  asClass(AudioDto),
]);

asUnion

Combines several transformers. The first transformer passed compatibleWith is used for serialization.

String or number:

const transformer = asUnion([asString(), asFloat64()]);

String or array of strings:

const transformer = asUnion([asString(), asArray(asString())]);

Classes:

const transformer = asUnion([
  asClass(LandscapeDto),
  asClass(PortraitDto),
  asClass(UnderWaterDto),
]);

asNullable

The value may be null.

Only null:

const transformer = asNullable(asNever());

String or null:

const transformer = asNullable(asString());

asClass

Transforms the fields of the passed class that have decorators.

Empty class

class Foo {}

const transformer = asClass(Foo);

asEnumFloat64

Transformation of numeric enum.

enum Direction {
  UP = 0,
  DOWN = 1,
  LEFT = 2,
  RIGHT = 3,
}

const transformer = asEnumFloat64(UserDto);

asEnumString

Transformation of string enum.

enum Direction {
  UP = 'up',
  DOWN = 'down',
  LEFT = 'left',
  RIGHT = 'right',
}

const transformer = asEnumString(UserDto);

asUuidString

Transformation of UUID string.

type UserId = UuidString & {readonly __userId: unique symbol};

const transformer = asUuidString<UserId>();