Skip to content

Instantly share code, notes, and snippets.

@iahu
Last active June 23, 2020 10:56
Show Gist options
  • Select an option

  • Save iahu/ccb10cbdd7bf322f836f63c7ec305da3 to your computer and use it in GitHub Desktop.

Select an option

Save iahu/ccb10cbdd7bf322f836f63c7ec305da3 to your computer and use it in GitHub Desktop.
use es6 destructuring assignment to reduce logic control (the if else statement)
export const NORMAL_RULE = 1
export const WEEKDAY_RULE = 2
export const WEEKEND_RULE = 3
export const getRuleTypeByDate = ms => Math.ceil(new Date(ms).getDay() / 5) + 1
export const typeMap = {
[NORMAL_RULE]: '不区分常规休息日',
[WEEKDAY_RULE]: '常规',
[WEEKEND_RULE]: '休息日'
}
export const getRuleTypeList = (rules = {}) => {
// const { distanceFee = [] } = rules
// 兼容 distanceFee 为 null 的情况
return (rules.distanceFee || [])
.filter(d => d.type !== 1)
.map(d => ({
label: typeMap[d.type],
value: d.type
}))
}
/**
* 把旧数据格式转成新格式
* 兼容2个/3个字段的格式
*/
const mapStartPrice = data => {
const {
secondKeyWord: _secondKeyWord,
thirdKeyWord = _secondKeyWord
} = data
const [firstKeyWord = '', secondKeyWord = _secondKeyWord] = (
data.firstKeyWord || ''
)
.split(' 且 ')
.map(parseFloat)
return {
distance: firstKeyWord,
weight: secondKeyWord,
configs: [{ amount: thirdKeyWord, period: '' }]
}
}
const mapOverMilesPrice = data => [
{
period: '',
configs: data.map(d => {
const { firstKeyWord, secondKeyWord, thridKeyWord } = d
return {
distance: firstKeyWord,
step: secondKeyWord,
fee: thridKeyWord
}
})
}
]
/**
* 1. 把新旧数据格式统一为一种格式
* 2. 根据 ruleType 过滤新数据
*/
export const getComputedRules = (rules, ruleType) => {
if (!rules) {
return rules
}
if (!ruleType) {
throw new Error('argument error [ruleType]:', ruleType)
}
const {
type = 1,
startPrice: _startPrice,
beyondDistanceFee,
distanceFee = [], // 新数据字段
...others
} = rules
const {
startPrice = mapStartPrice(_startPrice),
overMilesPrice = mapOverMilesPrice(beyondDistanceFee)
} = distanceFee.find(d => d.type === ruleType) || {}
return {
...others,
type,
startPrice,
overMilesPrice
}
}
export const hm2Num = hm => +hm.split(/\D/).join('')
export const onPeriod = (period, hm) => {
const [a, b] = period.split('-').map(hm2Num)
return hm >= a && hm < b
}
const _padStart = a => `0${a}`.slice(-2)
const getHM = ms => {
const d = new Date(ms)
return +[d.getHours(), d.getMinutes(), d.getMinutes()]
.map(_padStart)
.join('')
}
/**
* 根据当前时间及规则类型获取对应的分时区段
* @param {Array} configs 要计算的分时数据
* @param {Number} currentTime 当前时间[ms]
* @param {Number} ruleType 当前规则类型
* @return {String} 对应的分时区段
*/
const getPeriod = (configs, currentTime, ruleType) => {
if (configs) {
const hm = getHM(currentTime)
const matchRule =
// 普通数据,没有区分周末
ruleType === NORMAL_RULE ||
getRuleTypeByDate(currentTime) === ruleType
const { period } =
(matchRule && configs.find(c => onPeriod(c.period, hm))) ||
configs[0]
return period
}
return ''
}
export const getDefaultPeriods = (rules, ruleType) => {
const { startPrice, overMilesPrice, currentTime } = rules
return {
startPricePeriod: getPeriod(startPrice.configs, currentTime, ruleType),
overMilesPeriod: getPeriod(overMilesPrice, currentTime, ruleType)
}
}
/**
* 计算初始化属性
* @param {Object} data 未适配的接口数据
* @return {Object} 计算好的属性
*/
export const getInitState = data => {
if (!data) {
return {}
}
const ruleTypeList = getRuleTypeList(data)
let [{ value: ruleType } = {}] = ruleTypeList
if (!ruleType) {
ruleType = NORMAL_RULE
} else {
ruleType = getRuleTypeByDate(data.currentTime)
}
return {
ruleTypeList,
ruleType
}
}
export const currentFormat = n => (n / 100).toFixed(2)
/**
* 存在有效分时数据
*/
const judge = o => !!o.period && o.period !== '普通时段'
export const isPeriodData = data =>
data.some(d => judge(d) || d.configs.some(judge))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment