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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sku-manager

v1.0.2

Published

电商 sku选择组件

Readme

sku-manage

NPM

img img img img

电商 sku选择组件,相关文章 两万字长文-电商sku组合查询状态细究与实现

img

Live Demo

移动端查看效果更佳 Demo

Install

npm i sku-manager -S

Usage

导入

import SkuManage, { joinKVStr, joinAttrStr } from 'sku-manager'

SkuManage是主类,通过 new SkuManage得到实例,joinKVStrjoinAttrStr是辅助分隔符

实例

const skuManage = new SkuManage(skuParamVoList, skuRankList)

skuParamVoList的数据结构:

interface ISkuParamVoList Array<{
  paramId: string
  paramValue: string
  valueList: Array<{
    valueId: string
    valueValue: string
  }>
}>

就是 sku的组合结构,例如:

[
  "paramId": "6977",
  "paramValue": "成色",
  "valueList": [{
      "valueId": "1081969",
      "valueValue": "全新"
  }, {
      "valueId": "1080699",
      "valueValue": "仅拆封"
  }]
]

skuRankList的数据结构:

interface ISkuRankList Array<{
  spuDId: string
  paramIdJoin: string
  priceRange: Array<string>
  count: number
}>

就是一条完整的 sku

"skuRankList": [
    {
      "spuDId": '111111',
      "paramIdJoin": '6977_1081969__6975_730004',
      "priceRange": [6888, 7001],
      "count": 19
    }
]

计算

当用户切换 sku属性时,例如选中了 颜色_银色这个属性,那么需要都调用 实例 skuManageexcuteBySeleted进行计算,得到当前用户选择状态下的 库存信息、置灰sku信息、价格范围等数据:

// 例如,当前选中了 颜色_银色,即 6975_730004,实际场景中,此对象是根据用户选中的 sku属性得到
const activeSkuTagMap = {
  6975: 730004
}
let currentSelectSkuRst = skuManage.excuteBySeleted(activeSkuTagMap)
// 输出选中后计算出来的信息(库存、价格范围,需要置灰的 sku)
console.log(currentSelectSkuRst)

currentSelectSkuRst 的数据结构:

interface ICurrentSelectSkuRst {
  currentRst: {
    priceArr: number[]
    spuDIdArr: string[] | number[]
    totalCount: number
  }
  nextEmptyKV: string[]
}

currentRst 表示当前选中状态下的信息,priceArr表示价格范围,例如 [6899, 9100, 9812]spuDIdArr表示 spuDiD集合(每一条完整的 sku组合就是一个 spu),例如 [1932394, 19123234, 19832345]totalCount 表示当前选中状态下的总库存,例如 99

nextEmptyKV表示当前选中状态下需要置灰的 sku属性集合,例如 ["6977_1080699"]

Example

import SkuManage, { joinKVStr, joinAttrStr } from 'sku-manager'

// mock 数据
const mockData = {
  "skuParamVoList": [{
      "paramId": "6977",
      "paramValue": "成色",
      "valueList": [{
          "valueId": "1081969",
          "valueValue": "全新"
      }, {
          "valueId": "1080699",
          "valueValue": "仅拆封"
      }]
  }, {
      "paramId": "6975",
      "paramValue": "颜色",
      "valueList": [{
          "valueId": "730004",
          "valueValue": "银色"
      }, {
          "valueId": "730005",
          "valueValue": "金色"
      }]
  }],
  "skuRankList": [
    {
      "spuDId": '111111',
      "paramIdJoin": '6977_1081969__6975_730004',
      "priceRange": [6888, 7001],
      "count": 19
    },
    {
      "spuDId": '222222',
      "paramIdJoin": '6977_1081969__6975_730005',
      "priceRange": [6888, 7001],
      "count": 12
    },
    {
      "spuDId": '333333',
      "paramIdJoin": '6977_1080699__6975_730004',
      "priceRange": [6888, 7001],
      "count": 0
    },
    {
      "spuDId": '444444',
      "paramIdJoin": '6977_1080699__6975_730005',
      "priceRange": [6888, 7001],
      "count": 7
    }
  ]
}
// 实例
const skuManage = new SkuManage(mockData.skuParamVoList, mockData.skuRankList)
// 例如,当前选中了 颜色_银色,即 6975_730004,实际场景中,此对象是根据用户选中的 sku属性得到
const activeSkuTagMap = {
  6975: 730004
}
let currentSelectSkuRst = skuManage.excuteBySeleted(activeSkuTagMap)
// 输出选中后计算出来的信息(库存、价格范围,需要置灰的 sku)
console.log(currentSelectSkuRst)

更加实际具体的 例子参见