utiller 1.0.346 → 1.0.348

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.
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports["default"] = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
10
9
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
10
  var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
12
11
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
@@ -352,25 +351,6 @@ var Utiller = /*#__PURE__*/function () {
352
351
  cloned.splice(targetIndex, 0, object);
353
352
  return cloned;
354
353
  });
355
- /**
356
- * 編碼成 Firestore-safe UID(Base64 URL 安全版,可還原)
357
- */
358
- (0, _defineProperty2["default"])(this, "encodeToUid", function (str) {
359
- var utf8Bytes = new TextEncoder().encode(str);
360
- var base64 = btoa(String.fromCharCode.apply(String, (0, _toConsumableArray2["default"])(utf8Bytes)));
361
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
362
- });
363
- /**
364
- * 解碼 UID 回原始組合
365
- */
366
- (0, _defineProperty2["default"])(this, "decodeFromUid", function (uid) {
367
- var base64 = uid.replace(/-/g, "+").replace(/_/g, "/");
368
- var binary = atob(base64);
369
- var bytes = Uint8Array.from(binary, function (c) {
370
- return c.charCodeAt(0);
371
- });
372
- return new TextDecoder().decode(bytes);
373
- });
374
354
  /**
375
355
  * const origin = [
376
356
  * { label: 'aa', value: 1203 },
@@ -506,10 +486,31 @@ var Utiller = /*#__PURE__*/function () {
506
486
  * { label: '黑|L號|長袖', value: 'ca-q5-s2' }
507
487
  * ] *
508
488
  */
489
+ /**
490
+ * 產生排列組合(容忍空陣列,將非空單一陣列視為結果)
491
+ * @param {Array<Array<{label: string, value: string}>>} arrays
492
+ * @param {string} labelSeparator
493
+ * @param {string} valueSeparator
494
+ * @returns {Array<{label: string, value: string}>}
495
+ */
509
496
  (0, _defineProperty2["default"])(this, "generateVariants", function (arrays) {
510
497
  var labelSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "|";
511
498
  var valueSeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "-";
512
- var combinations = _lodash["default"].reduce(arrays, function (acc, curr) {
499
+ // 過濾掉空陣列
500
+ var nonEmptyArrays = arrays.filter(function (arr) {
501
+ return arr.length > 0;
502
+ });
503
+ if (nonEmptyArrays.length === 0) return [];
504
+ if (nonEmptyArrays.length === 1) {
505
+ // 若只有一個非空陣列,回傳它(每項轉為 {label, value} 格式)
506
+ return nonEmptyArrays[0].map(function (item) {
507
+ return {
508
+ label: item.label,
509
+ value: item.value
510
+ };
511
+ });
512
+ }
513
+ var combinations = _lodash["default"].reduce(nonEmptyArrays, function (acc, curr) {
513
514
  return _lodash["default"].flatMap(acc, function (a) {
514
515
  return curr.map(function (b) {
515
516
  return [].concat((0, _toConsumableArray2["default"])(a), [b]);
@@ -562,6 +563,37 @@ var Utiller = /*#__PURE__*/function () {
562
563
  });
563
564
  });
564
565
  });
566
+ /**
567
+ * 將 array2 的對應項目合併到 array1 中(支援巢狀 idKey 路徑)
568
+ * @param {Array<Object>} array1
569
+ * @param {Array<Object>} array2
570
+ * @param {string} idKey - 用來比對的 key(可為巢狀路徑,例如 'meta.id')
571
+ * @returns {Array<Object>} - 合併後的 array1
572
+ *
573
+ * const array1 = [
574
+ * { meta: { id: 'a1' }, name: 'Red' },
575
+ * { meta: { id: 'b2' }, name: 'Black' }
576
+ * ];
577
+ *
578
+ * const array2 = [
579
+ * { meta: { id: 'a1' }, price: 200 },
580
+ * { meta: { id: 'b2' }, name: 'Black Special' }
581
+ * ];
582
+ *
583
+ * const result = mergeById(array1, array2, 'meta.id');
584
+ * console.log(result);
585
+ */
586
+ (0, _defineProperty2["default"])(this, "getArrayOfMergeBySpecificId", function (array1, array2) {
587
+ var idKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
588
+ var map2 = _lodash["default"].keyBy(array2, function (item) {
589
+ return _lodash["default"].get(item, idKey);
590
+ });
591
+ return array1.map(function (item) {
592
+ var id = _lodash["default"].get(item, idKey);
593
+ var match = map2[id];
594
+ return match ? _lodash["default"].merge({}, item, match) : item;
595
+ });
596
+ });
565
597
  this.init();
566
598
  this.env = "dev";
567
599
  }
@@ -3978,107 +4010,9 @@ var Utiller = /*#__PURE__*/function () {
3978
4010
  }).keys().compact() // 過濾掉 null, undefined, '' 等 falsy 值
3979
4011
  .value();
3980
4012
  }
3981
- }, {
3982
- key: "generateUidCombinations",
3983
- value:
3984
- /** ------------------------------------------------------------------------------------- 起始UID-COMBINATION
3985
- * 建立組合清單及 UID 映射清單
3986
- *
3987
- * 原始資料組合
3988
- * const array1 = ['黑色', '綠色']
3989
- * const array2 = ['S', 'M']
3990
- * const array3 = ['短袖', '長袖']
3991
- *
3992
- * 產生 UID 清單
3993
- * const uidList = generateUidCombinations([array1, array2, array3])
3994
- *
3995
- * console.log('📦 所有組合的 UID 對照:')
3996
- * console.log(uidList)
3997
- *
3998
- * 建立查詢表(可選)
3999
- * const lookupTable = buildLookupTable(uidList)
4000
- *
4001
- * 任選一個 UID 測試還原
4002
- * const sampleUid = Object.keys(uidList[2])[0]
4003
- * console.log('\n🔍 測試 UID:', sampleUid)
4004
- * console.log('✅ 還原結果:', getOriginalFromUid(sampleUid, lookupTable))
4005
- *
4006
- */
4007
- /**
4008
- * 產生所有排列組合並轉成 Firestore-safe UID
4009
- * @param {string[][]} arrays - 字串陣列的陣列,例如 [['黑色', '綠色'], ['S', 'M']]
4010
- * @param {string} separator - 分隔符號,預設為 ' | '
4011
- * @returns {Array<{[uid: string]: string}>} - 每個 UID 對應一組原始組合的對照表陣列
4012
- */
4013
- function generateUidCombinations(arrays) {
4014
- var _this4 = this;
4015
- var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : " | ";
4016
- if (!arrays.length) return [];
4017
-
4018
- // 過濾每個子陣列中的 null、undefined、空字串
4019
- var sanitizedArrays = arrays.map(function (group) {
4020
- return group.filter(function (item) {
4021
- return item !== null && item !== undefined && item !== "";
4022
- });
4023
- }).filter(function (group) {
4024
- return group.length > 0;
4025
- }); // 移除空子陣列
4026
-
4027
- // 若最終沒東西,回傳空
4028
- if (sanitizedArrays.length === 0) return [];
4029
-
4030
- // 只有一組有效子陣列,直接映射
4031
- if (sanitizedArrays.length === 1) {
4032
- return sanitizedArrays[0].map(function (str) {
4033
- var uid = _this4.encodeToUid(str);
4034
- return (0, _defineProperty2["default"])({}, uid, str);
4035
- });
4036
- }
4037
-
4038
- // 多組時進行組合
4039
- var _sanitizedArrays = (0, _toArray2["default"])(sanitizedArrays),
4040
- first = _sanitizedArrays[0],
4041
- rest = _sanitizedArrays.slice(1);
4042
- var combinations = rest.reduce(function (acc, curr) {
4043
- return _lodash["default"].flatMap(acc, function (a) {
4044
- return curr.map(function (b) {
4045
- return (Array.isArray(a) ? a.join(separator) : a) + separator + b;
4046
- });
4047
- });
4048
- }, first);
4049
- return combinations.map(function (str) {
4050
- var uid = _this4.encodeToUid(str);
4051
- return (0, _defineProperty2["default"])({}, uid, str);
4052
- });
4053
- }
4054
-
4055
- /**
4056
- * 快速建立 UID -> 組合 對照表(Object)
4057
- */
4058
- }, {
4059
- key: "buildLookupTable",
4060
- value: function buildLookupTable(uidList) {
4061
- return uidList.reduce(function (acc, item) {
4062
- var _Object$entries$ = (0, _slicedToArray2["default"])(Object.entries(item)[0], 2),
4063
- uid = _Object$entries$[0],
4064
- value = _Object$entries$[1];
4065
- acc[uid] = value;
4066
- return acc;
4067
- }, {});
4068
- }
4069
-
4070
- /**
4071
- * 根據 UID 還原原始組合(透過對照表或解碼備援)
4072
- */
4073
- }, {
4074
- key: "getOriginalFromUid",
4075
- value: function getOriginalFromUid(uid, lookupTable) {
4076
- return (lookupTable === null || lookupTable === void 0 ? void 0 : lookupTable[uid]) || this.decodeFromUid(uid) || null;
4077
- }
4078
4013
  }, {
4079
4014
  key: "getArrayOfFillMissingValues",
4080
- value: /** ------------------------------------------------------------------------------------- 結束UID-COMBINATION */
4081
-
4015
+ value:
4082
4016
  /**
4083
4017
  * const input = [
4084
4018
  * { value: 'xx0132', label: 'A款' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.346",
3
+ "version": "1.0.348",
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.345",
14
+ "utiller": "^1.0.347",
15
15
  "linepayer": "^1.0.4",
16
16
  "databazer": "^1.0.12",
17
17
  "lodash": "^4.17.20",