merge: 从 heytrip, train-mobile; commons.js;

main
Lei OT 4 months ago
parent 0fc4faae9c
commit 6f79616654

@ -101,6 +101,22 @@ export function formatDate(date) {
return formatted; 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 * @param {Date} date
*/ */
@ -517,6 +533,15 @@ export const fixTo4Decimals = curriedFix(4);
export const fixTo1Decimals = curriedFix(1); export const fixTo1Decimals = curriedFix(1);
export const fixToInt = curriedFix(0); 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 * @example
@ -528,7 +553,7 @@ export const fixToInt = curriedFix(0);
// result = {a1: 1, a2: 2, b1: 3} // result = {a1: 1, a2: 2, b1: 3}
* *
*/ */
export function objectMapper(input, keyMap) { export function objectMapper(input, keyMap, keep = true) {
// Loop through array mapping // Loop through array mapping
if (Array.isArray(input)) { if (Array.isArray(input)) {
return input.map((obj) => objectMapper(obj, keyMap)); return input.map((obj) => objectMapper(obj, keyMap));
@ -539,7 +564,7 @@ export function objectMapper(input, keyMap) {
Object.keys(input).forEach((key) => { Object.keys(input).forEach((key) => {
// Keep original keys not in keyMap // Keep original keys not in keyMap
if (!keyMap[key]) { if (!keyMap[key] && keep) {
mappedObj[key] = input[key]; mappedObj[key] = input[key];
} }
// Handle array of maps // Handle array of maps
@ -557,7 +582,7 @@ export function objectMapper(input, keyMap) {
let value = input[key]; let value = input[key];
if (map.transform) value = map.transform(value); if (map.transform) value = map.transform(value);
if (typeof map === 'string') mappedObj[map] = value; if (typeof map === 'string') mappedObj[map] = value;
mappedObj[map.key || key] = value; mappedObj[map.key || map] = value;
// mappedObj[map.key || map] = value; // mappedObj[map.key || map] = value;
} }
} }

Loading…
Cancel
Save