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

bidirectional-adapter

v1.2.1

Published

bi-directional adapter factory, used to decouple systems across shared data structures

Downloads

1,743

Readme

Bi-Directional Adapter

npm version npm downloads

Factory to create bi-directional adapters that can convert between two distinct data structures. Using adapters helps to decouple systems which share common data structures and may need to alter them independently without introducing conflicts in each other.

Why?

You have multiple services which each operate on a single shared resource but want to represent the data differently in each service or want to isolate each service from data structure changes required by the other.

With bidirectional-adapter you can define a simple entity which can be used to transform data between two formats.

const adapter = createAdapter<string, number>(
  (stringValue) => parseInt(stringValue, 10),
  (numericValue) => String(numericValue)
);

Example

import axios from 'axios';
import createAdapter from 'bidirectional-adapter';

const accountAdapter = createAdapter(
  (dbAccount) => ({
    id: dbAccount.user_id,
    name: `${dbAccount.user.firstName} ${dbAccount.user.lastName}`,
    email: dbAccount.user.email,
  }),
  (appAccount) => ({
    user_id: appAcount.id,
    user: {
      firstName: appAccount.name.split(' ')[0],
      lastName: appAccount.name.split(' ')[1],
    },
    email: appAccount.email,
  })
);

const fetchAccount = (userID) => {
  // state is in the shape of propertyTwo
  // do reducer stuff here
  return axios.get(`/account/${userID}`).then((res) => accountAdapter.fromDB(res.data));
};

const updateAccount = (account) => {
  // state is in the shape of propertyTwo
  // do reducer stuff here
  return axios.post(`/account/${userID}`, accountAdapter.toDB(account));
};

Smart Adapter Example

import { createSmartMultiAdapter } from 'bidirectional-adapter';

interface DBModel {
  x: number;
  a: number;
  b: string;
  c1: boolean;
}

interface Model {
  x: number;
  ab: string;
  c2: boolean;
}

type KeyMap = [['a' | 'b', 'ab'], ['c1', 'c2']];

const adapter = createSmartMultiAdapter<DBModel, Model, [], [], KeyMap>(
  () => ({} as any),
  () => ({} as any)
);

adapter.fromDB({ a: 1, b: 'a', c1: false, x: 1 }); // Model
adapter.fromDB({ a: 1, b: 'a', c1: false }); // Pick<Model, 'ab' | 'c2'>
adapter.fromDB({ b: 'a', c1: false }); // Pick<Model, 'c2'>
adapter.fromDB({ x: 1 }); // Pick<Model, 'x1'>
adapter.fromDB({}); // EmptyObject

adapter.mapFromDB([{ a: 1, b: 'a', c1: false, x: 1 }]); // Model[]
adapter.mapFromDB([{ a: 1, b: 'a', c1: false }]); // Pick<Model, 'ab' | 'c2'>[]
adapter.mapFromDB([{ b: 'a', c1: false }]); // Pick<Model, 'c2'>[]
adapter.mapFromDB([{ x: 1 }]); // Pick<Model, 'x1'>[]
adapter.mapFromDB([{}]); // EmptyObject[]

adapter.toDB({ ab: '1', c2: false, x: 1 }); // DBModel
adapter.toDB({ ab: '1', x: 1 }); // Pick<DBModel, 'a' | 'b' | 'x'>
adapter.toDB({ c2: false }); // Pick<Model, 'c2'>
adapter.toDB({ x: 1 }); // Pick<Model, 'x1'>
adapter.toDB({}); // EmptyObject

adapter.mapToDB([{ ab: '1', c2: false, x: 1 }]); // DBModel
adapter.mapToDB([{ ab: '1', x: 1 }]); // Pick<DBModel, 'a' | 'b' | 'x'>
adapter.mapToDB([{ c2: false }]); // Pick<Model, 'c2'>
adapter.mapToDB([{ x: 1 }]); // Pick<Model, 'x1'>
adapter.mapToDB([{}]); // EmptyObject

Installation

To use bidirectional-adapter, install it as a dependency:

# If you use npm:
npm install bidirectional-adapter

# Or if you use Yarn:
yarn add bidirectional-adapter

This assumes that you’re using a package manager such as npm.

License

ISC