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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@yamatomo/typed-json-mapper

v1.1.0

Published

Type-safe json mapper

Downloads

38

Readme

typed-json-mapper

test

Type-safe json mapper

install

$ npm install --save @yamatomo/typed-json-mapper

or

$ yarn add @yamatomo/typed-json-mapper

usage

basic

import { TypedJsonMapper, map } from '@yamatomo/typed-json-mapper';

class Mapper extends TypedJsonMapper {
  str = '';
  num = Number.MIN_SAFE_INTEGER;
  bool = false;
  nil = null;
  @map(String) arrayOfStrings: string[] = [];
}

// ------------------
const [mapper, err] = Mapper.map({
  str: 'str', num: 123, bool: true, nil: null, array_of_strings: ['a', 'b']
});

/*
mapper => Mapper {
  str: 'str',
  num: 123,
  bool: true,
  nil: null,
  arrayOfStrings: ['a', 'b']
}
*/

array type mapping

Use @map decorator. (must)

class Mapper extends TypedJsonMapper {
  @map(String) arrayOfString: string[] = [];
  @map(Number) arrayOfNumber: number[] = [];
}

complex type mapping

  • custom type
    • Implement a mapper class that inherit from TypeJsonMapper, and pass it to @map decorator.
  • union type
    • Implement a mapper functions, and pass it to @map decorator.
import { TypedJsonMapper, map, Errors } from '@yamatomo/typed-json-mapper';

// you should implement mapper class
class CustomType extends TypedJsonMapper {
  id = -1;
  str = '';
}

// you should implement mapper function
const toNullableString = (data: unknown): [string | null, Errors] => {
  const errors: Errors = typeof data === 'number' ? ['data type is number!'] : undefined;
  return [data != null ? String(data) : null, errors];
}

class Mapper extends TypedJsonMapper {
  // custom type
  @map(CustomType) custom: CustomType = new CustomType();
  // custom type + array
  @map(CustomType) arrayOfCustom: CustomType[] = [];

  // union type
  @map(toNullableString) nullableStr: string | null = null;
  // union type + array
  @map(toNullableString) nullableStrings: (string | null)[] = [];
}

error handling

const toNullableString = (data: unknown): [string | null, Errors] => {
  const errors: Errors = typeof data === 'number' ? ['data type is number!'] : undefined;
  return [data != null ? String(data) : null, errors];
}

class Mapper extends TypedJsonMapper {
  str = '';
  num = -1
  @map(toNullableString) nullableStr: string | null = 'default';
}

const [mapper, err] = Mapper.map({ num: 'string', nullable_str: 111 });
if (err) {
  // something error handling
  /*
  err => [
    '`Mapper.str` not exists mapping value.',
    '`Mapper.num` type mismatch. expected-type: `number` actual: `"string"`',
    '`Mapper.nullableStr` -> data type is number!'
  ]
  */
}

/*
mapper => Mapper {
  str: '',
  num: 0,
  nullableStr: '111',
}
*/

ignore errors only for specific key

Use @ignoreError decorator.

class Mapper extends TypedJsonMapper {
  @ignoreError str = '';
}

const [mapper, err] = Mapper.map({ str: 1111 });
/*
mapper => Mapper {
  str: '1111',
}
*/

/*
err => undefined
*/

options

  • disableTransformKeys (default: true)
    • Disable snake-case to camel-case transform of keys when mapping.

limitation

  • Can't use Non-null assertion operator.
  • Can't use undefined type for property, use null instead

linter

The typescript compiler can't detect mismatches between property types and mapper return types.

for example

class WrongTypeClass extends TypedJsonMapper {
  @map(String) list: number[] = []; // <--- mapper return type = string, property type = number.
                                    // A mismatch occurs, but no compile error occurs.
}

Check for such mismatches with this tool.

$ npx mapper-code-lint ./src/**/*.ts

or

$ yarn mapper-code-lint ./src/**/*.ts

license

MIT