utiller 1.0.380 → 1.0.382
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 +116 -9
- package/package.json +1 -1
- package/template/sample.package.json +1 -1
package/lib/utiller/index.js
CHANGED
|
@@ -306,6 +306,64 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
306
306
|
minimumFractionDigits: 0 // 確保不顯示小數
|
|
307
307
|
}).format(number);
|
|
308
308
|
});
|
|
309
|
+
/**
|
|
310
|
+
* 使用 ES10+ 和 Lodash,將短句子轉換為潛在的搜尋關鍵字陣列。
|
|
311
|
+
* 策略:1. 清理 2. 窮舉所有可能的 2 字或 3 字詞組 3. 去重和過濾
|
|
312
|
+
* @param {string} sentence - 原始字串。
|
|
313
|
+
* @param {number} [maxLength=50] - 允許的最大字串長度,預設為 30。
|
|
314
|
+
* @returns {string[]} - 潛在關鍵字陣列(至少 2 個中文字)。
|
|
315
|
+
*/
|
|
316
|
+
(0, _defineProperty2["default"])(this, "generateUniversalKeywords", function (sentence) {
|
|
317
|
+
var maxLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 50;
|
|
318
|
+
// 引入 Lodash,確保在執行環境中可用
|
|
319
|
+
// 由於在瀏覽器環境中執行,我們假設 Lodash 已經全域引入或在 Node.js 環境中運行。
|
|
320
|
+
// 在大多數現代前端框架中,這行可能需要根據實際模組系統調整(例如 import _ from 'lodash')。
|
|
321
|
+
if (!sentence || typeof sentence !== "string") {
|
|
322
|
+
return [];
|
|
323
|
+
}
|
|
324
|
+
var textToProcess = sentence;
|
|
325
|
+
|
|
326
|
+
// 警告/截斷邏輯
|
|
327
|
+
if (textToProcess.length > maxLength) {
|
|
328
|
+
console.warn("\u8B66\u544A\uFF1A\u8F38\u5165\u5B57\u4E32\u9577\u5EA6\u70BA ".concat(textToProcess.length, "\uFF0C\u5DF2\u6839\u64DA maxLength: ").concat(maxLength, " \u622A\u65B7\u3002"));
|
|
329
|
+
textToProcess = textToProcess.substring(0, maxLength);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 步驟 1: 清理和標準化
|
|
333
|
+
var cleanText = textToProcess.toLowerCase();
|
|
334
|
+
|
|
335
|
+
// 移除規格、數量和常見符號,然後移除通用詞
|
|
336
|
+
cleanText = cleanText.replace(/[a-z0-9]+(w|led|uv|顆|組|片|入|ml|g|oz|號|色|\/|\-|\~|k)/g, " ") // 移除規格和數量 (新增了 k, 修正了 w)
|
|
337
|
+
.replace(/[!@#$%^&*()_+={}\[\]:;"'<>,.?\/\\|`~]/g, " ") // 移除特殊符號
|
|
338
|
+
.replace(/系列|一組|單色|多款|套組|全套|專用|迷你|頂級|高品質|超閃|奢華|最新|款式|新款|超亮|的|與|和|閃|美甲/g, " ") // 移除通用詞 (新增了 閃, 美甲)
|
|
339
|
+
.replace(/\s+/g, "") // **移除所有空格**,確保中文句子連貫提取
|
|
340
|
+
.trim();
|
|
341
|
+
if (cleanText.length < 2) {
|
|
342
|
+
return []; // 如果清理後長度不足,直接返回空陣列
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// 步驟 2: 窮舉所有 >= 2 個字的 N-gram 詞組
|
|
346
|
+
var keywords = [];
|
|
347
|
+
var minLength = 2;
|
|
348
|
+
var maxNgramLength = 3; // N-gram 最大長度
|
|
349
|
+
|
|
350
|
+
for (var len = minLength; len <= maxNgramLength; len++) {
|
|
351
|
+
for (var i = 0; i <= cleanText.length - len; i++) {
|
|
352
|
+
var keyword = cleanText.substring(i, i + len);
|
|
353
|
+
keywords.push(keyword);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// 步驟 3: 標準化、去重和過濾
|
|
358
|
+
var finalKeywords = _lodash["default"].chain(keywords)
|
|
359
|
+
// 確保只包含長度 >= 2 的詞
|
|
360
|
+
.filter(function (k) {
|
|
361
|
+
return k.length >= 2;
|
|
362
|
+
})
|
|
363
|
+
// 去除重複
|
|
364
|
+
.uniq().value();
|
|
365
|
+
return finalKeywords;
|
|
366
|
+
});
|
|
309
367
|
/**
|
|
310
368
|
* 直接修改原本 array,將 object 移動到指定 index
|
|
311
369
|
* @param {Array} array - 要修改的陣列
|
|
@@ -930,8 +988,8 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
930
988
|
}, {
|
|
931
989
|
key: "accumulate",
|
|
932
990
|
value:
|
|
933
|
-
/**
|
|
934
|
-
*
|
|
991
|
+
/**
|
|
992
|
+
* 把敘述句和條件子句累加在一起,讓其變成一個合理的functional呼叫
|
|
935
993
|
* condition2(condition1(target)) => 為了應付 collection(path).where('age','>',11).orderBy('age');
|
|
936
994
|
* */
|
|
937
995
|
function accumulate(target, conditions) {
|
|
@@ -4059,14 +4117,14 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
4059
4117
|
}).keys().compact() // 移除 null、undefined、''、0、false、NaN
|
|
4060
4118
|
.value();
|
|
4061
4119
|
}
|
|
4062
|
-
|
|
4120
|
+
}, {
|
|
4121
|
+
key: "getObjectOfSpecifyKey",
|
|
4122
|
+
value:
|
|
4063
4123
|
/**
|
|
4064
4124
|
* 減少不必要的{}
|
|
4065
4125
|
* 例如 array.map(each => {return {key,value}})
|
|
4066
4126
|
**/
|
|
4067
|
-
|
|
4068
|
-
key: "getObjectOfSpecifyKey",
|
|
4069
|
-
value: function getObjectOfSpecifyKey(value, key) {
|
|
4127
|
+
function getObjectOfSpecifyKey(value, key) {
|
|
4070
4128
|
var object = {};
|
|
4071
4129
|
object[key] = value;
|
|
4072
4130
|
return object;
|
|
@@ -4291,8 +4349,55 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
4291
4349
|
}, {});
|
|
4292
4350
|
}
|
|
4293
4351
|
}, {
|
|
4294
|
-
key: "
|
|
4295
|
-
value:
|
|
4352
|
+
key: "mergeArrayByKey",
|
|
4353
|
+
value:
|
|
4354
|
+
/**
|
|
4355
|
+
* 📦 mergeArrayByKey(array)
|
|
4356
|
+
* ---------------------------------------
|
|
4357
|
+
* 將陣列中相同 key 的物件進行「巢狀合併」,並直接 mutate 原陣列內容。
|
|
4358
|
+
*
|
|
4359
|
+
* ✅ 特點:
|
|
4360
|
+
* - 自動合併相同 key 的物件內容(深層合併)
|
|
4361
|
+
* - 不建立新陣列,會直接修改傳入的 array
|
|
4362
|
+
* - 適用於需要整併設定、狀態、表單資料等情境
|
|
4363
|
+
*
|
|
4364
|
+
* 🧩 範例:
|
|
4365
|
+
* ```js
|
|
4366
|
+
* const array = [
|
|
4367
|
+
* { a: { c: 1 } },
|
|
4368
|
+
* { b: { d: 2 } },
|
|
4369
|
+
* { a: { f: 3 } }
|
|
4370
|
+
* ];
|
|
4371
|
+
*
|
|
4372
|
+
* ArrayHelper.mergeArrayByKey(array);
|
|
4373
|
+
*
|
|
4374
|
+
* console.log(array);
|
|
4375
|
+
* // 👉 [ { a: { c: 1, f: 3 } }, { b: { d: 2 } } ]
|
|
4376
|
+
* ```
|
|
4377
|
+
*
|
|
4378
|
+
* @param {Array<Object>} array - 需被合併的物件陣列
|
|
4379
|
+
* @returns {Array<Object>} - 回傳同一個(已被 mutate 的)陣列
|
|
4380
|
+
*/
|
|
4381
|
+
function mergeArrayByKey(array) {
|
|
4382
|
+
if (!Array.isArray(array)) return array;
|
|
4383
|
+
|
|
4384
|
+
// 1️⃣ 利用 lodash 深層合併所有物件(例如 {a:{x}} + {a:{y}} => {a:{x,y}})
|
|
4385
|
+
var merged = _lodash["default"].merge.apply(_lodash["default"], [{}].concat((0, _toConsumableArray2["default"])(array)));
|
|
4386
|
+
|
|
4387
|
+
// 2️⃣ 清空原陣列(mutate)
|
|
4388
|
+
array.length = 0;
|
|
4389
|
+
|
|
4390
|
+
// 3️⃣ 將合併結果重新拆成 [{a:{...}}, {b:{...}}] 結構
|
|
4391
|
+
Object.entries(merged).forEach(function (_ref12) {
|
|
4392
|
+
var _ref13 = (0, _slicedToArray2["default"])(_ref12, 2),
|
|
4393
|
+
key = _ref13[0],
|
|
4394
|
+
value = _ref13[1];
|
|
4395
|
+
array.push((0, _defineProperty2["default"])({}, key, value));
|
|
4396
|
+
});
|
|
4397
|
+
return array;
|
|
4398
|
+
}
|
|
4399
|
+
|
|
4400
|
+
/** ============== 排課系統公式 開始 ============== */
|
|
4296
4401
|
|
|
4297
4402
|
/**
|
|
4298
4403
|
* /const arrayWithDup = [
|
|
@@ -4309,7 +4414,9 @@ var Utiller = /*#__PURE__*/function () {
|
|
|
4309
4414
|
* 2. idOfBooze和idOfVariant為PK, 重複的只保留一組
|
|
4310
4415
|
* 3. 回傳filter array,(反查出哪些課程重複會用到其他資訊)
|
|
4311
4416
|
* */
|
|
4312
|
-
|
|
4417
|
+
}, {
|
|
4418
|
+
key: "getFilteredHeraPeriods",
|
|
4419
|
+
value: function getFilteredHeraPeriods(arr, idOfCurrentBooze) {
|
|
4313
4420
|
return _lodash["default"].chain(arr)
|
|
4314
4421
|
// 1️⃣ 刪掉 idOfBooze 等於目標值的項目
|
|
4315
4422
|
.filter(function (item) {
|
package/package.json
CHANGED