From 6f79616654a8b2c64e88bd1602b416ab3ef57b9f Mon Sep 17 00:00:00 2001 From: Lei OT Date: Thu, 25 Dec 2025 15:03:48 +0800 Subject: [PATCH] =?UTF-8?q?merge:=20=E4=BB=8E=20heytrip,=20train-mobile;?= =?UTF-8?q?=20commons.js;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/commons.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/utils/commons.js b/src/utils/commons.js index 2642646..1293a4b 100644 --- a/src/utils/commons.js +++ b/src/utils/commons.js @@ -101,6 +101,22 @@ export function formatDate(date) { return formatted; } +/** + * @param {Date} date + */ +export function formatDateToStr(date) { + if(date === ''){ + return null; + } + const year = date.getFullYear(); + const month = date.getMonth(); + const day = date.getDate(); + + const enMonthArr = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spt","Oct","Nov","Dec"]; + const formatted = enMonthArr[month] +'. '+ day +', '+ year; + return formatted; +} + /** * @param {Date} date */ @@ -517,6 +533,15 @@ export const fixTo4Decimals = curriedFix(4); export const fixTo1Decimals = curriedFix(1); export const fixToInt = curriedFix(0); +/** + * 创建一个按大小分组的元素数组 + */ +export const chunk = (input = [], size = 0) => { + return input.reduce((arr, item, idx) => { + return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]]; + }, []); +}; + /** * 映射 * @example @@ -528,7 +553,7 @@ export const fixToInt = curriedFix(0); // result = {a1: 1, a2: 2, b1: 3} * */ -export function objectMapper(input, keyMap) { +export function objectMapper(input, keyMap, keep = true) { // Loop through array mapping if (Array.isArray(input)) { return input.map((obj) => objectMapper(obj, keyMap)); @@ -539,7 +564,7 @@ export function objectMapper(input, keyMap) { Object.keys(input).forEach((key) => { // Keep original keys not in keyMap - if (!keyMap[key]) { + if (!keyMap[key] && keep) { mappedObj[key] = input[key]; } // Handle array of maps @@ -557,7 +582,7 @@ export function objectMapper(input, keyMap) { let value = input[key]; if (map.transform) value = map.transform(value); if (typeof map === 'string') mappedObj[map] = value; - mappedObj[map.key || key] = value; + mappedObj[map.key || map] = value; // mappedObj[map.key || map] = value; } }