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

dynamic-product-filter

v1.0.4

Published

Dynamically generated product filters for online shopping sites of all types.

Downloads

7

Readme

Dynamic Product Filters

by: katreinhart
Dynamically generated product filters for online shopping sites of all types.

Description

  • Online shopping sites sometimes have problems with filtering. For example, on many online shopping sites, the same 3 or 4 filters are shown on every category page: e.g. Size, Color, Price, and Brand. However, for many categories of item, these categories are not relevant or not informative. For example, when I am shopping in the Home category, the Size filter usually only has "OS" (one size) which is not informative. What if these filters were populated dynamically, directly from the JSON file containing the products to be displayed on the page? This is a tool that will read in a JSON file of products, validate it against a JSONSchema (either provided by the user or the included boilerplate version), and dynamically append these filter categories to a page.

Screenshot

screenshot of the project

Installation

npm install dynamic-product-filter

or fork & clone the repo :)

Run the Example

npm run webpack-example and (in a new terminal tab) npm run live-server-example (requires live-server to be installed globally to work)

Dependencies

jsonschema

Additionally, use of this library requires a bundler - the example is running Webpack, but your favorite bundler should work just fine.

Setup

const DynamicFilter = require('dynamic-product-filter')
const df = new DynamicFilter(productSchema, data[, exclude, priceBuckets])

productSchema is a JSON-schema describing the structure of your data. A default option is included in the example, which has ID, name, and price as required fields and an array of optional fields.

If you want your own randomly-generated content like the data in the example, check out the sister package hipster-product-creator.

Arguments

-- data is a JSON object with a single key-value pair, where the key is "products" and the value is an array of product objects.

exclude is an array of properties to be excluded from the filters object. By default it is set to ['id', 'name', 'description', 'image']. If you want to not exclude these items, pass in an empty array ([]).

priceBuckets is an integer number of "buckets" you want your prices to be filterable by. By default it is set to 4. Allowable values are between 3 and 10 (inclusive).

API

Once the object is instantiated, the only necessary method to call is:

df.getFilters()

This will return the filter object (keys are the names of the categories and values are the arrays of values.)

The first time it is called, getFilters will call the validation & generation methods. Invalid/schema-nonconforming data or incorrect arguments will throw console errors.

See the example/src/index.js for more information on how to implement in your project.

Troubleshooting

This skeleton index.js file will test to make sure everything is working properly:

const DynamicFilter = require('dynamic-product-filter')
const schema = require('dynamic-product-filter/lib/product.schema.json')
const data = require('./products.json')

const df = new DynamicFilter(schema, data)
const filterObject = df.getFilters()
console.log(filterObject)

If this file runs and outputs a filter object, you are good to go and use it in your project.