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

@backpackjs/transforms

v1.2.4

Published

An optional @backpackjs/sync extension that enables json/data re-shaping and enriching during the syncing lifecycle of products and collections.

Downloads

156

Readme

@backpackjs/transform

An optional @backpackjs/sync extension that enables json/data re-shaping and enriching during the syncing lifecycle of products and collections.

Transforms are performed after fetching and before saving product and collections json files.

How to

Transforms live inside ./transform/products and or ./transform/collections

Some examples..

  • products: Pre-format the product and variant prices, so that we don't have have to constantly do replace('.00', '') formatting hacks in the front-end
  • products: Convert variant options to a more front-end friendly such as optionsMap ..

transforms | [] | [...fns] | A set of product transformation functions that allows you to completely reshape (add, modify and delete any fields) of the source products data fetched from Shopify.This is an alternative method to loading transforms via the /transforms/products folder. For more information on product transforms please see @backpackjs/transforms and our open source @backpackjs/transforms-catalog.For a basic transform example please see "Example code: Product transform"

Adding swatch images to products

The transform code:

{
  ...,
  transforms: [
    // Adds a custom field "swatchImages" to each product. This field is an object that includes each color (optionName) as key and a url to an image for this color (in this case images hosted in shopify)
    {
      fields: ['swatchImages'],
      resolver: (product) => {
        const colorOption = product && product.options && product.options.find(({ name }) => name === 'Color')
        const colorValues = colorOption && colorOption.values // array of colors

        const colorValuesToImages = colorValues
          ? colorValues.reduce((colorSwatches, color) => {
              colorSwatches[color] = `//cdn.shopify.com/s/files/1/122/4944/files/${color}.png`
              return colorSwatches
            }, {})
          : {}

        return colorValuesToImages
      }
    }
  ]
}

The resulting product

{
 ...otherProductProperties,
 swatchImages: {
    'blue-102': '//cdn.shopify.com/s/files/1/122/4944/files/blue-102.png',
    'white-201': '//cdn.shopify.com/s/files/1/122/4944/files/white-201.png',
    'red-100': '//cdn.shopify.com/s/files/1/122/4944/files/red-100.png',
    ...
 }
}

Example transform (imagesByAlt)

Add simple transform that performs a potentially costly front-end operation of grouping images by their alt attribute

transforms/products/imagesByAlt.js

const imagesAltMapFromImagesArray = ({ product }) => new Promise((resolve) => {
  let imagesByAlt = { untagged: [] }
  if (!product || !product.images || !product.images.length) resolve(imagesByAlt)

  // sort images with slug atlText as keys and rest as untagged
  for (let i = 0; i < product.images.length; i++) {
    let image = product.images[i]
    const altText = image && image.altText
    const altTextDashed = altText && altText.split('-')
    if (altText && altTextDashed.length) {
      if (Array.isArray(imagesByAlt[altText])) {
        imagesByAlt[altText] = [...imagesByAlt[altText], image]
      } else {
        imagesByAlt[altText] = [image]
      }
    } else {
      imagesByAlt.untagged = [...imagesByAlt.untagged, image]
    }

    if (i === product.images.length - 1) {
      resolve(imagesByAlt)
    }
  }
})

module.exports = {
  fields: ['imagesByAlt'],
  resolver: imagesAltMapFromImagesArray
}

Example transform (in/output)

fetched product (i.e transform input)

  {
    "handle": "product-a",
    "images" : [
      {
        "src": "...image-1.jpg",
        "alt": "blue",
      },
      {
        "src": "...image-2.jpg",
        "alt": "red",
      }
      {
        "src": "...image-3.jpg",
        "alt": "red",
      }
    ]
    ...
  }

transformed product (i.e transform output)

  {
    "handle": "product-a",
    "images" : [
      {
        "src": "...image-1.jpg",
        "alt": "blue",
      },
      {
        "src": "...image-2.jpg",
        "alt": "red",
      }
      {
        "src": "...image-3.jpg",
        "alt": "red",
      }
    ],
    // added by transform
    "imagesByAlt": {
      "blue": [
        {
          "src": "...image-1.jpg",
          "alt": "blue",
        },
      ],
      "red": [
        {
          "src": "...image-2.jpg",
          "alt": "red",
        }
        {
          "src": "...image-3.jpg",
          "alt": "red",
        }
      ]
    }
    ...
  }

Transforms Signature

module.exports = {
  fields: ['imagesByAlt'],
  resolver: imagesAltMapFromImagesArray
}
  • fields: and array of field keys that this transform will act upon. It can be an existing field that you want to overwrite, a field you want to delete or simply a field that does not yet exist.

  • resolver: a function (sync or async) which receives every product and/or collection and returns a value (string, integer, array or object) that will be applied to field(s) defined in this transform

Resolver function

Explain all the available inputs for each endpoint, importance of order of execution

Cache invalidation on transform(s) change

Ask JP while this is fleshed out

Best practices & caveats

Migrate computing heavy front-end code to transforms after its been tested in the frontend. This will make your front-end code, leaner faster and much easier to reason.

Ask JP while this is fleshed out.