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

json-forgefy

v1.1.0

Published

Creates a new JSON Object, based on a blueprint object with params.

Downloads

6

Readme

JSON-FORGEFY

Ideia:

The idea of this package is to provide a way to transform an upcoming JSON string into a new Object, following a blueprint configuration file.

This came with the necessity of consuming 3rd services API where you can setup a JSON Schema Like configuration file to obtain the desired JSON structure as response, before sending it forward to the next downstream service.

The purpose of this package is to make it heavily agnostic of the system. You don't have to bother parsing each prop from A to B, just provide the blueprint and let the package do the rest.

This is heavily inspired on the MongoDB aggregation pipeline, where you can transform the data as you wish. I'll try to add as much operators as needed, following their official documentation.

Example:

export const incomingPayload = {
  transactionId: "1234567890",
  transactionType: "Money Transfer",
  transactionDate: "2024-01-01T00:00:00Z",
  transactionStatus: "Pending",
  sender: {
    accountId: "A123",
    accountType: "Savings",
    bank: {
      bankId: "B123",
      bankName: "Bank A",
      branchName: "Branch A",
      ifscCode: "IFSC123",
      swiftCode: "SWIFT123",
      address: {
        street: "Street A",
        city: "City A",
        state: "State A",
        country: "Country A",
        zipCode: "ZIP123",
      },
    },
  },
  receiver: {
    accountId: "B456",
    accountType: "Checking",
    bank: {
      bankId: "B456",
      bankName: "Bank B",
      branchName: "Branch B",
      ifscCode: "IFSC456",
      swiftCode: "SWIFT456",
      address: {
        street: "Street B",
        city: "City B",
        state: "State B",
        country: "Country B",
        zipCode: "ZIP456",
      },
    },
  },
  transactionDetails: {
    amount: "1000.0",
    currency: "USD",
    exchangeRate: "1.0",
    transactionFee: "10.0",
    netAmount: "990.0",
    description: "Money Transfer to Account B456",
  },
};

To transform our incoming JSON string into a new Object, we can create a blueprint configuration file like this one below:

export const yourBlueprint = {
  accountId: "$sender.accountId",
  accountType: "$sender.accountType",
  transactionId: "$transactionId",
  transactionStatus: { $toUpper: "$transactionStatus" },
  transactionDetails: {
    amount: {
      $multiply: [{ $toNumber: "$transactionDetails.amount" }, 100],
    },
    currency: "$transactionDetails.currency",
    fees: {
      transactionFee: "$transactionDetails.transactionFee",
      netAmount: "$transactionDetails.netAmount",
    },
  },
  receiverType: {
    $cond: {
      if: { $eq: ["$receiver.accountType", "Preparing"] },
      then: "It is checking...",
      else: "Finished",
    },
  },
  branch: {
    $switch: {
      branches: [
        {
          case: { $eq: ["$sender.bank.branchName", "Branch A"] },
          then: "Open Banking Name A",
        },
        {
          case: { $eq: ["$sender.bank.branchName", "Branch B"] },
          then: "Open Banking Name B",
        },
      ],
      default: "Unknown Branch",
    },
  },
};

The end result will be:

export const expectedResult = {
  accountId: "A123",
  accountType: "Savings",
  transactionId: "1234567890",
  transactionStatus: "PENDING",
  transactionDetails: {
    amount: 100000,
    currency: "USD",
    fees: { transactionFee: "10.0", netAmount: "990.0" },
  },
  receiverType: "Finished",
  branch: "Open Banking Name A",
};

How to:

Import the forgefy function from the package and pass two inputs, the incoming JSON string and the blueprint configuration file.

import { forgefy } from "json-forgefy";
const result = forgefy(incomingPayload, yourBlueprint);