descartes-sku.js
SKU
Stock Keeping Unit,是库存进出计量的基本单元。 即库存进出计量的基本单元,可以是以件,盒,托盘等为单位.
笛卡尔乘积算法
笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员。
功能
基于笛卡尔乘积算法基础上编写的一个快捷插件,只需传入需要生成 SKU 属性数组的原始数据即可快捷生成对应的 SKU。
download
git clone https://github.com/vxhly/descartes-sku.js.git
npm
npm install descartes-sku.js --save
bower
bower install descartes-sku.js --save
USE
提供两套算法,推荐使用算法二
const arr1 = 'a' 'b' 'c'const descartes1 = arr1const result1 = descartes1 // 算法一// descartes1.descartes() 算法二 console// => [ ['a'], ['b'], ['c'] ] const arr2 = 'a' 'b' 'c' 1 2 3 4 5const descartes2 = arr2const result2 = descartes2// descartes2.descartes() 算法二 console// => [ // [ 'a', 1 ], [ 'a', 2 ], [ 'a', 3 ], [ 'a', 4 ], [ 'a', 5 ],// [ 'b', 1 ], [ 'b', 2 ], [ 'b', 3 ], [ 'b', 4 ], [ 'b', 5 ],// [ 'c', 1 ], [ 'c', 2 ], [ 'c', 3 ], [ 'c', 4 ], [ 'c', 5 ] // ] const arr3 = '黄色' '绿色' '黑色' 'XL' 'X' 'XXL' 'L' 'aa' 'bb'const descartes3 = arr3const result3 = descartes3// descartes2.descartes() 算法二 console// => [ // [ '黄色', 'XL', 'aa' ], [ '黄色', 'XL', 'bb' ], [ '黄色', 'X', 'aa' ],// [ '黄色', 'X', 'bb' ], [ '黄色', 'XXL', 'aa' ], [ '黄色', 'XXL', 'bb' ],// [ '黄色', 'L', 'aa' ], [ '黄色', 'L', 'bb' ], [ '绿色', 'XL', 'aa' ],// [ '绿色', 'XL', 'bb' ], [ '绿色', 'X', 'aa' ], [ '绿色', 'X', 'bb' ],// [ '绿色', 'XXL', 'aa' ], [ '绿色', 'XXL', 'bb' ], [ '绿色', 'L', 'aa' ],// [ '绿色', 'L', 'bb' ], [ '黑色', 'XL', 'aa' ], [ '黑色', 'XL', 'bb' ],// [ '黑色', 'X', 'aa' ], [ '黑色', 'X', 'bb' ], [ '黑色', 'XXL', 'aa' ],// [ '黑色', 'XXL', 'bb' ], [ '黑色', 'L', 'aa' ], [ '黑色', 'L', 'bb' ] // ]
如果需要输出结果带有 key,则传入的必须为对象
const obj = size: 'XL' 'XXL' type: '羊毛' const descartes = objconst result = descartes console// => [ { size: 'XL', type: '羊毛' }, { size: 'XXL', type: '羊毛' } ]
如果需要将 SKU 转换成笛卡儿积形式,则需要使用 SKU2Descartes()
const obj1 = size: 'XL' type: '羊毛' size: 'XXL' type: '羊毛' const descartes1 = obj1const result1 = descartes1 console// { size: ['XL', 'XXL'], type: ['羊毛'] } const obj2 = size: 'XL' size: 'XXL' const descartes2 = obj2const result2 = descartes2 console// { size: ['XL', 'XXL'] } const obj3 = const descartes3 = obj3const result3 = descartes3 console// {} const obj4 = color: '黄色' size: 'XL' const descartes4 = obj4const result4 = descartes4 console// { color: ['黄色'], size: ['XL'] }
提供搜索功能,暂时只支持传入对象的形式
const arr2 = color: '黄色' size: 'XL' type: '羊毛' stock: 99 price: 12 oteh: '测试' test: 'test1' color: '黄色' size: 'XXL' type: '棉' stock: 99 price: 12 oteh: '测试' test: 'test2' color: '黄绿色' size: 'XL' type: '羊毛' stock: 99 price: 12 oteh: '测试' test: 'test1' color: '黄绿色' size: 'XXL' type: '棉' stock: 99 price: 12 oteh: '测试' test: 'test2' const search_sku4 = color: '黄绿色' size: 'XL' const descartes4 = arr2 search_sku4const result4 = descartes4 console// => { color: '绿色', size: 'XL', type: '羊毛', stock: 99, price: 12, oteh: '测试', test: 'test1' }
如果以上例子还不过瘾的话可以看 src/test1.js ~ src/test5.js 文件哦
test
npm run test1 => 算法一(逐个匹配)npm run test2 => 算法二(递归)npm run test3 => 算法三(对象传入对象输出)npm run test4 => SKU 转 笛卡尔乘积数组(传入数组,数组套数组)npm run test5 => SKU 转 笛卡尔乘积数组(传入数组,数组套对象)npm run test6 => 搜索功能(传入对象)