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

@taqtile/ts-data-mapper

v0.7.1

Published

TypeScript data mapper

Downloads

743

Readme

Typescript Data Mapper

This module contains all default mapping operation and funcions commonly used by several projects.

Contents

Generic Mapper

Generic mapper is a type-safe function that receives the source object and an array of mappting operations and apply these operations to create a mapped object. Differently from lodash's get, pick, omit functions, generic mapper checks in compilation time if the accessed properties are type safe (internally we still use some of lodash's functions).

There are several built-in operations you can use and also a customizable option. Here is a full example of it's capabilities:

const source = { a: 1, b: false, c: undefined, d: '2' };

let target = map(source, ['a']); // { a: 1 }

const copy_b_to_be_op = {
  from: 'b', to: 'be', invokeMethod: 'toString',
};
map(source, [copy_b_to_be_op], target); // { a: 1, be: 'false' }
 
const use_default_val_op = {
  from: 'c', to: 'c', defaultValue: 'default',
};
map(source, [use_default_val_op]); // { c: 'default' }
 
const transform_op = {
  from: 'd', to: 'd', transform: (value) => parseInt(value),
};
map(source, [transform_op]); // { d: 2 }

Copying property values

General case

The general and repeated common case is the mapping operation of renaming properties. For these kind of operations all you have to do is specify the source's property name and the target's property name.

const source: SourceType = { a: 1 };

const mapOps: MapOpArray<SourceType, MappedType> = [{ from: 'a', to: 'b' }];

const mapped: MappedType = map(source, mapOps); // { b: 1 }

Keeping the same property name

Another common case is to copy a property value into a property of the same name. You can use the classical mapping operation { from: 'a', to: 'a' }, but you can simply pass the property name:

const source: SourceType = { a: 1, b: 2, c: 3 };

const mapOps: MapOpArray<SourceType, MappedType> = ['a', 'b'];

const mapped: MappedType = map(source, mapOps); // { a: 1, b: 2 }

Default values

Sometimes you want to set a default value when there is none:

const source: SourceType = { a: 1, b: undefined };

const mapOps: MapOpArray<SourceType, MappedType> = ['a', { from: 'b', to: 'b', defaultValue: 2 }];

const mapped: MappedType = map(source, mapOps); // { a: 1, b: 2 }

!!! Warning !!!

  • Default value will be used only if source's value is undefined.

Transforming mapped values

It's also possible to change the value retrieved from source by transforming it.

Invoking a value's method

If the transformation you want if a value's method, it's possible to safely use it by defining the invoke method name:

const source: SourceType = { a: 1, b: [2, 3], c: undefined };

const mapOps: MapOpArray<SourceType, MappedType> = [
  { from: 'a', to: 'a', invokeMethod: 'toString' },
  { from: 'b', to: 'b', invokeMethod: 'join', invokeArgs: ['-'] },
  { from: 'c', to: 'c', invokeMethod: 'toString' },
];

const mapped: MappedType = map(source, mapOps); // { a: '1', b: '2-3', c: undefined }

Custom transformation

It's also possible to use custom functions to alter a source's value:

const source: SourceType = { a: 1 };

const mapOps: MapOpArray<SourceType, MappedType> = [
  { from: 'a', to: 'a', transform: (value) => value + 1 },
];

const mapped: MappedType = map(source, mapOps); // { a: 2 }

!!! Warning !!!

  • Transform invocation is not value safe, meaning that if value is undefined, transform will be called with undefined as argument

Existing target object

If you already have an object, you can pass it to be used as target:

const sourceA: SourceAType = { a: 1 };

const target: MappedType = map(sourceA, ['a']); // { a: 1 }

const sourceB: SourceBType = { b: 2 };

map(sourceB, ['b'], target); // mapped = { a: 1, b: 2 }

!!! Warning !!!

  • map mutates the passed target object
  • map overrides existing properties values if there is an operation targeting the same property

Full MapOp cycle

The full map operation cycle is as follow:

  • get value from source's property or get defaultValue
  • assign to value result from invoke method
  • assign to value rsult from tranform function
  • assign value to target's property