utiller 1.0.310 → 1.0.311
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utiller/index.js +41 -35
- package/package.json +1 -1
- package/template/sample.package.json +1 -1
package/lib/utiller/index.js
CHANGED
|
@@ -3395,35 +3395,35 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
3395
3395
|
}
|
|
3396
3396
|
|
|
3397
3397
|
/**
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3398
|
+
* ({key: 'color', label: '顏色', options: [ { value: 0, label: '紅' }, { value: 1, label: '白' }, { value: 2, label: '黑'}]},
|
|
3399
|
+
* {key: 'size', label: '尺寸', options: [ { value: 0, label: 'S' }, { value: 1, label: 'M' }, { value: 2, label: 'L' }]})
|
|
3400
|
+
*
|
|
3401
|
+
* [
|
|
3402
|
+
* { trait: {color: 0, size: 0}, id: 'color_0_size_0', content: '顏色:紅 尺寸:S' },
|
|
3403
|
+
* { trait: {color: 0, size: 1}, id: 'color_0_size_1', content: '顏色:紅 尺寸:M' },
|
|
3404
|
+
* { trait: {color: 0, size: 2}, id: 'color_0_size_2', content: '顏色:紅 尺寸:L' },
|
|
3405
|
+
* { trait: {color: 1, size: 0}, id: 'color_1_size_0', content: '顏色:白 尺寸:S' },
|
|
3406
|
+
* { trait: {color: 1, size: 1}, id: 'color_1_size_1', content: '顏色:白 尺寸:M' },
|
|
3407
|
+
* { trait: {color: 1, size: 2}, id: 'color_1_size_2', content: '顏色:白 尺寸:L' },
|
|
3408
|
+
* { trait: {color: 2, size: 0}, id: 'color_2_size_0', content: '顏色:黑 尺寸:S' },
|
|
3409
|
+
* { trait: {color: 2, size: 1}, id: 'color_2_size_1', content: '顏色:黑 尺寸:M' },
|
|
3410
|
+
* { trait: {color: 2, size: 2}, id: 'color_2_size_2', content: '顏色:黑 尺寸:L' }
|
|
3411
|
+
* ]
|
|
3412
|
+
*
|
|
3413
|
+
/**
|
|
3414
|
+
* 生成所有組合並依照 value 遞增排序,並回傳指定格式
|
|
3415
|
+
* @param {Array} attributes - 屬性陣列
|
|
3416
|
+
* @returns {Array} - 格式化組合
|
|
3417
|
+
*/
|
|
3417
3418
|
}, {
|
|
3418
3419
|
key: "generateCombinations",
|
|
3419
|
-
value: function generateCombinations() {
|
|
3420
|
-
for (var _len29 = arguments.length, attributes = new Array(_len29), _key39 = 0; _key39 < _len29; _key39++) {
|
|
3421
|
-
attributes[_key39] = arguments[_key39];
|
|
3422
|
-
}
|
|
3420
|
+
value: function generateCombinations(attributes) {
|
|
3423
3421
|
var keys = attributes.map(function (attr) {
|
|
3424
3422
|
return attr.key;
|
|
3425
|
-
});
|
|
3426
|
-
var labelMap = _lodash["default"].keyBy(attributes, "key");
|
|
3423
|
+
}); // 屬性順序
|
|
3424
|
+
var labelMap = _lodash["default"].keyBy(attributes, "key"); // 用於 content 查 label
|
|
3425
|
+
|
|
3426
|
+
// 把每個屬性的 options 提取成格式化陣列
|
|
3427
3427
|
var optionArrays = attributes.map(function (attr) {
|
|
3428
3428
|
return attr.options.map(function (option) {
|
|
3429
3429
|
return {
|
|
@@ -3433,6 +3433,8 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
3433
3433
|
};
|
|
3434
3434
|
});
|
|
3435
3435
|
});
|
|
3436
|
+
|
|
3437
|
+
// 計算笛卡兒積
|
|
3436
3438
|
var cartesianProduct = _lodash["default"].reduce(optionArrays, function (acc, curr) {
|
|
3437
3439
|
return _lodash["default"].flatMap(acc, function (a) {
|
|
3438
3440
|
return curr.map(function (b) {
|
|
@@ -3440,8 +3442,10 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
3440
3442
|
});
|
|
3441
3443
|
});
|
|
3442
3444
|
}, [[]]);
|
|
3445
|
+
|
|
3446
|
+
// 格式化每一筆組合
|
|
3443
3447
|
var results = cartesianProduct.map(function (combination) {
|
|
3444
|
-
var
|
|
3448
|
+
var trait = {};
|
|
3445
3449
|
var idParts = [];
|
|
3446
3450
|
var contentParts = [];
|
|
3447
3451
|
var _iterator19 = _createForOfIteratorHelper(combination),
|
|
@@ -3449,27 +3453,29 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
3449
3453
|
try {
|
|
3450
3454
|
for (_iterator19.s(); !(_step19 = _iterator19.n()).done;) {
|
|
3451
3455
|
var _step19$value = _step19.value,
|
|
3452
|
-
|
|
3456
|
+
_key39 = _step19$value.key,
|
|
3453
3457
|
value = _step19$value.value,
|
|
3454
3458
|
label = _step19$value.label;
|
|
3455
|
-
|
|
3456
|
-
idParts.push("".concat(
|
|
3457
|
-
contentParts.push("".concat(labelMap[
|
|
3459
|
+
trait[_key39] = value;
|
|
3460
|
+
idParts.push("".concat(_key39, "_").concat(value));
|
|
3461
|
+
contentParts.push("".concat(labelMap[_key39].label, "\uFF1A").concat(label));
|
|
3458
3462
|
}
|
|
3459
3463
|
} catch (err) {
|
|
3460
3464
|
_iterator19.e(err);
|
|
3461
3465
|
} finally {
|
|
3462
3466
|
_iterator19.f();
|
|
3463
3467
|
}
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3468
|
+
return {
|
|
3469
|
+
trait: trait,
|
|
3470
|
+
id: idParts.join("_"),
|
|
3471
|
+
content: contentParts.join(" ")
|
|
3472
|
+
};
|
|
3467
3473
|
});
|
|
3468
3474
|
|
|
3469
|
-
//
|
|
3475
|
+
// 排序:依照屬性順序的 value 遞增(右邊 key 變化最快)
|
|
3470
3476
|
return _lodash["default"].sortBy(results, function (item) {
|
|
3471
3477
|
return keys.map(function (key) {
|
|
3472
|
-
return item[key];
|
|
3478
|
+
return item.trait[key];
|
|
3473
3479
|
});
|
|
3474
3480
|
});
|
|
3475
3481
|
}
|
package/package.json
CHANGED