You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.7 KiB
TypeScript

import dayjs from 'dayjs';
import _Vue from 'vue';
export const PATTERN_DATETIME = 'YYYY-MM-DD HH:mm:ss';
export const PATTERN_DATE = 'YYYY-MM-DD';
export const PATTERN_TIME = 'HH:mm:ss';
/**
*
* list
* @param list
* @param valueKey valueKey
*/
export const removeDuplicates = (list: any, valueKey: string) => {
const temp = {};
let arr = [];
arr = list.reduce(function (item, next) {
temp[next[valueKey]] ? '' : (temp[next[valueKey]] = true && item.push(next));
return item;
}, []);
return arr;
};
/**
* NULL
* @param object
* @param path objectpath a.b.c
* @param defaultValue
*/
export const optional = (object: any, path: any, defaultValue: any) => {
try {
if (defaultValue === undefined) {
defaultValue = path;
} else if (path) {
// this means path='a.b.c', object = object.a.b.c
object = path.split('.').reduce((o: any, i: any) => o[i], object);
}
} catch (e) {
console.warn(e.message);
}
return object || defaultValue;
};
/**
* 123456 => 12**56
* @param data
* @return {string}
*/
export const masking = (data: string) => {
if (!data) return '****';
let size = data.length / 2 - 1;
if (size < 2) size = 2;
const masking = '********************'.substring(0, size);
const prefix = data.substring(0, (data.length - masking.length) / 2);
const suffix = data.substring(prefix.length + masking.length, data.length);
return prefix + masking + suffix;
};
/**
*
* @param object
* @param path objectpath a.b.c
* @return {string}
*/
export const date = (object: any, path: any) => {
return format(object, path, PATTERN_DATE);
};
/**
*
* @param object
* @param path objectpath a.b.c
* @return {string}
*/
export const time = (object: any, path: any) => {
return format(object, path, PATTERN_TIME);
};
/**
*
* @param object
* @param path objectpath a.b.c
* @return {string}
*/
export const datetime = (object: any, path: any) => {
return format(object, path, PATTERN_DATETIME);
};
/**
*
* @param object
* @param path objectpath a.b.c
* @param pattern
* @return {string}
*/
export const format = (object: any, path: any, pattern: any) => {
const date = optional(object, path, '');
return date && dayjs(date).format(pattern);
};
/**
* N
* @param object
* @param path objectpath a.b.c
* @param fractionDigits N
*/
export const fixed = (object: any, path: any, fractionDigits: any) => {
if (fractionDigits === undefined) {
fractionDigits = path;
path = undefined;
}
return Number(optional(object, path, 0)).toFixed(fractionDigits);
};
/**
* 0.00
* @param object
* @param path objectpath a.b.c
*/
export const money = (object: any, path: any) => {
return fixed(object, path, 2);
};
/**
*
* @param object
* @param path objectpath a.b.c
*/
export const distance = (object: any, path: any) => {
return fixed(object, path, 2) + 'km';
};
/**
*
* @param staticPath
* @return {function(*=, *=): *}
*/
export const assets = (staticPath: any) => {
/**
*
* @param return.object
* @param path objectpath a.b.c
* @return {*}
*/
return (object: any, path: any) => {
const suffixPath = optional(object, path, '');
if (!suffixPath || suffixPath.startsWith('http')) {
return suffixPath;
}
return staticPath + suffixPath;
};
};
export default {
install(Vue: typeof _Vue): void {
Vue.filter('optional', optional);
Vue.filter('masking', masking);
Vue.filter('date', date);
Vue.filter('time', time);
Vue.filter('datetime', datetime);
Vue.filter('format', format);
Vue.filter('fixed', fixed);
Vue.filter('money', money);
Vue.filter('distance', distance);
Vue.filter('assets', assets(process.env.VUE_APP_STATIC_PATH));
},
};