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

bigcommerce-custom-fields-hierarchy

v0.2.2

Published

Utility module for formatting hierarchically constructed custom fields data.

Downloads

10

Readme

BigCommerce Custom Fields Hierarchy

Utility module for transforming hierarchically constructed products custom fields to Javascript objects.

Info

  • URL: https://www.npmjs.com/package/bigcommerce-custom-fields-hierarchy
  • Platform: BigCommerce

Setup

Requirements: NodeJS, npm

1. Run npm ci to install dependencies.

2. Inject products in your cornerstone template.

You can use any types of product list, for example products that are coming with category or related products.

{{inject "products" category.products}}

Or

{{inject "relatedProducts" product.related_products}}

Or any other product list which has custom fields property

3. Import the library main function in your theme JS.

For example, for Cornerstone it could be any PageManager file.

import transformPathToHierarchy from 'bigcommerce-custom-fields-hierarchy';

4. Transform custom fields to JS object

const customFieldsObj = transformPathToHierarchy(this.context.category.products, 'Chart');

Guidelines

Constructing custom fields

Let's say we have a parent child relationship with custom fields, for example: "Dimensions" custom field can have two sub custom fields "Dimension A" and "Dimension B".

We can build this relationship by constructing custom field names as paths, so with "Dimensions" custom field hierarchy we get:

Custom Field #1 Name: Dimensions \ A
Custom Field #2 Name: Dimensions \ B

So the delimiter is a slash.

Now let's say we want to separate these kinds of custom fields from the other custom fields, so they can be used separately. For example if we want to generate a chart from this custom fields paths, we can add a name space in the beginning of the custom field name:

Custom Field #1 Name: Chart \ Dimensions \ A
Custom Field #2 Name: Chart \ Dimensions \ B

Transforming custom fields to an object

Now as we have custom fields paths built, we can transform them into object:

import transformPathToHierarchy from 'bigcommerce-custom-fields-hierarchy';

const customFieldsObj = transformPathToHierarchy(this.context.products, 'Chart');
console.log(customFieldsObj);

As a result we get an object:

[
  {
    "name": " Dimensions ",
    "children": [
      {
        "name": " A",
        "path": "Dimensions \ A"
      },
      {
        "name": " B",
        "path": "Dimensions \ B"
      }
    ]
  }
]

Please note that the depth can be unlimited. For example Chart \ Thread Sizes \ Caps \ Baseball will get us:

[{
  "name": " Thread Sizes ",
  "path": " Thread Sizes ",
  "children": [
    {
      "name": " Caps",
      "path": " Thread Sizes \ Caps",
      "children": [
        {
          "name": " Baseball",
          "path": " Thread Sizes \ Caps \ Baseball",
          "children": []
        }
      ]
    }
  ]
}]

Example of constructing custom fields

BigCommerce Custom fields

Delimiter

Delimiter format

The format for delimiter is [whitespace]\[whitespace], so backslashes which are not surrounded with whitespace won't be considered as separator and will be included as a custom field title.

Importing Delimiter

Delimiter can be imported into your project:

import { delimiter } from 'bigcommerce-custom-fields-hierarchy';

API

transformPathToHierarchy(products, namespace) ⇒ Array

Transforms products custom fields paths into Javascript object

| Param | Type | Description | | --- | --- | --- | | products | Array | products that are coming from BigCommerce context | | namespace | string | only transform custom fields which have this namespace |

Notes

  • Since BigCommerce doesn't transpile external package code (for oldies like IE11), we provide transpiled files inside dist/ folder. You can access these files adding an alias on your webpack.conf.js file like:
'bigcommerce-custom-fields-hierarchy': path.resolve(__dirname, 'node_modules/bigcommerce-custom-fields-hierarchy/dist/custom-fields-hierarchy.min.js')

Authors

  • Shota Karkashadze

License

This project is licensed under the MIT License - see the LICENSE file for details

alt text