utiller 1.0.310 → 1.0.312

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,25 +3395,26 @@ 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
3420
  value: function generateCombinations() {
@@ -3422,8 +3423,10 @@ var Utiller = /*#__PURE__*/function () {
3422
3423
  }
3423
3424
  var keys = attributes.map(function (attr) {
3424
3425
  return attr.key;
3425
- });
3426
- var labelMap = _lodash["default"].keyBy(attributes, "key");
3426
+ }); // 屬性順序
3427
+ var labelMap = _lodash["default"].keyBy(attributes, "key"); // 用於 content 查 label
3428
+
3429
+ // 把每個屬性的 options 提取成格式化陣列
3427
3430
  var optionArrays = attributes.map(function (attr) {
3428
3431
  return attr.options.map(function (option) {
3429
3432
  return {
@@ -3433,6 +3436,8 @@ var Utiller = /*#__PURE__*/function () {
3433
3436
  };
3434
3437
  });
3435
3438
  });
3439
+
3440
+ // 計算笛卡兒積
3436
3441
  var cartesianProduct = _lodash["default"].reduce(optionArrays, function (acc, curr) {
3437
3442
  return _lodash["default"].flatMap(acc, function (a) {
3438
3443
  return curr.map(function (b) {
@@ -3440,8 +3445,10 @@ var Utiller = /*#__PURE__*/function () {
3440
3445
  });
3441
3446
  });
3442
3447
  }, [[]]);
3448
+
3449
+ // 格式化每一筆組合
3443
3450
  var results = cartesianProduct.map(function (combination) {
3444
- var item = {};
3451
+ var trait = {};
3445
3452
  var idParts = [];
3446
3453
  var contentParts = [];
3447
3454
  var _iterator19 = _createForOfIteratorHelper(combination),
@@ -3452,7 +3459,7 @@ var Utiller = /*#__PURE__*/function () {
3452
3459
  _key40 = _step19$value.key,
3453
3460
  value = _step19$value.value,
3454
3461
  label = _step19$value.label;
3455
- item[_key40] = value;
3462
+ trait[_key40] = value;
3456
3463
  idParts.push("".concat(_key40, "_").concat(value));
3457
3464
  contentParts.push("".concat(labelMap[_key40].label, "\uFF1A").concat(label));
3458
3465
  }
@@ -3461,15 +3468,17 @@ var Utiller = /*#__PURE__*/function () {
3461
3468
  } finally {
3462
3469
  _iterator19.f();
3463
3470
  }
3464
- item.id = idParts.join("_");
3465
- item.content = contentParts.join(" ");
3466
- return item;
3471
+ return {
3472
+ trait: trait,
3473
+ id: idParts.join("_"),
3474
+ content: contentParts.join(" ")
3475
+ };
3467
3476
  });
3468
3477
 
3469
- // 🔽 排序邏輯:從最後一個 key -> 第一個 key 做排序(變化最慢的排最前)
3478
+ // 排序:依照屬性順序的 value 遞增(右邊 key 變化最快)
3470
3479
  return _lodash["default"].sortBy(results, function (item) {
3471
3480
  return keys.map(function (key) {
3472
- return item[key];
3481
+ return item.trait[key];
3473
3482
  });
3474
3483
  });
3475
3484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.310",
3
+ "version": "1.0.312",
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.311",
15
15
  "linepayer": "^1.0.4",
16
16
  "databazer": "^1.0.12",
17
17
  "lodash": "^4.17.20",