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.
@@ -3395,35 +3395,35 @@ var Utiller = /*#__PURE__*/function () {
3395
3395
  }
3396
3396
 
3397
3397
  /**
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
- * { color: 0, size: 0, id: 'color_0_size_0', content: '顏色:紅 尺寸:S' },
3403
- * { color: 0, size: 1, id: 'color_0_size_1', content: '顏色:紅 尺寸:M' },
3404
- * { color: 0, size: 2, id: 'color_0_size_2', content: '顏色:紅 尺寸:L' },
3405
- * { color: 1, size: 0, id: 'color_1_size_0', content: '顏色:白 尺寸:S' },
3406
- * { color: 1, size: 1, id: 'color_1_size_1', content: '顏色:白 尺寸:M' },
3407
- * { color: 1, size: 2, id: 'color_1_size_2', content: '顏色:白 尺寸:L' },
3408
- * { color: 2, size: 0, id: 'color_2_size_0', content: '顏色:黑 尺寸:S' },
3409
- * { color: 2, size: 1, id: 'color_2_size_1', content: '顏色:黑 尺寸:M' },
3410
- * { color: 2, size: 2, id: 'color_2_size_2', content: '顏色:黑 尺寸:L' }
3411
- * ]
3412
- *
3413
- * 生成所有組合並依照 value 做數字遞增排序
3414
- * @param {Array} attributes - 屬性陣列
3415
- * @returns {Array} - 所有組合
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 item = {};
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
- _key40 = _step19$value.key,
3456
+ _key39 = _step19$value.key,
3453
3457
  value = _step19$value.value,
3454
3458
  label = _step19$value.label;
3455
- item[_key40] = value;
3456
- idParts.push("".concat(_key40, "_").concat(value));
3457
- contentParts.push("".concat(labelMap[_key40].label, "\uFF1A").concat(label));
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
- item.id = idParts.join("_");
3465
- item.content = contentParts.join(" ");
3466
- return item;
3468
+ return {
3469
+ trait: trait,
3470
+ id: idParts.join("_"),
3471
+ content: contentParts.join(" ")
3472
+ };
3467
3473
  });
3468
3474
 
3469
- // 🔽 排序邏輯:從最後一個 key -> 第一個 key 做排序(變化最慢的排最前)
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.310",
3
+ "version": "1.0.311",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
13
  "configerer": "^1.0.11",
14
- "utiller": "^1.0.309",
14
+ "utiller": "^1.0.310",
15
15
  "linepayer": "^1.0.4",
16
16
  "databazer": "^1.0.12",
17
17
  "lodash": "^4.17.20",