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

@modulify/morph

v0.0.2

Published

Mapping util

Downloads

2

Readme

@modulify/morph

codecov Tests Status npm version

This component helps transform objects into each other by reusable maps. One of the main use cases – converting JSON API responses into appropriate form.

Here's an example of how to use it:

import {
  MorphEach,
  MorphOne,
} from '@modulify/morph'

const morph = new MorphEach(
  new MorphOne()
    .move('_studio', 'studio')
    .process('studio', new MorphOne()
      .move('_name', 'name'))
    .move('_films', 'films')
    .process('films', new MorphEach(
      new MorphOne()
        .move('_id', 'id')
        .move('_name', 'name')
    ))
)

const collection = morph.convert({
  _studio: { _name: 'Lucasfilm Ltd. LLC' },
  _films: [{
    _id: 1,
    _name: 'Star Wars. Episode IV: A New Hope',
  }, {
    _id: 6,
    _name: 'Star Wars. Episode III. Revenge of the Sith',
  }],
})

result is

{
  "studio": { "name": "Lucasfilm Ltd. LLC" },
  "films": [{
    "id": 1,
    "name": "Star Wars. Episode IV: A New Hope"
  }, {
    "id": 6,
    "name": "Star Wars. Episode III. Revenge of the Sith"
  }]
}

Installation

yarn add @modulify/morph

or

npm i @modulify/morph

API

Morph

Interface with simple signature:

export interface Morph<Source = unknown, Target = unknown> {
  convert (source: Source): Target;
}

There is no basic class for this interface. It is used to define signature for Morph* classes.

MorphOne

Designed to transform source objects into target objects using a set of user-defined transformations, such as moving, extracting, and injecting values.

Constructor

constructor (target: Returns<Target> = () => ({} as Target))

The constructor takes one optional argument: target, which is a factory function to create the target object. If no argument is provided, it defaults to a function returning an empty object {}.

Methods

  • move (srcPath, dstKey, fallback = undefined) – this method defines value extraction from a source at the path srcPath to the dstKey property of the target object; the dstKey can also be a name of a setter of the target object;
  • extract (key, by) – this method defines value extraction from a source to the key property of the target object by a callback function by;
  • inject (path, by) – this method defines value injection from a source at the specified path by a callback function by;
  • process (key, by) – this method processes a value that is being injected into the key property;
  • exclude (key) – this method excludes the extraction of the key property of the target object;
  • override (target = undefined) – this method creates a new MorphOne instance with the same extraction/injection settings as the instance being overridden. A new target's constructor can be supplied with this method if necessary.

MorphEach

Designed to transform arrays of source objects (Source[]) into arrays of target objects (Target[]). Conversion is performed using a provided instance of a Morph class (MorphOne or else) at the time of MorphEach instance creation.

Constructor

constructor (morph: Morph<Source, Target>)

The constructor takes one argument: morph. This is an instance of a class that implements the Morph interface. These classes are used to transform a source object into a target object.

Methods

  • convert (source: Source[]): Target[]

The convert method takes an array of source objects and returns an array of target type objects. Each object's transformation is carried out using the Morph instance provided when creating the MorphEach instance.