helpers.js
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Handlebars helpers
* bikai kai.bi@yoho.cn
* 2016-05-10
*/
'use strict';
const querystring = require('querystring');
const _ = require('lodash');
const moment = require('moment');
const config = require('../config/common');
/**
* 七牛图片路径处理
* @param {[string]} url
* @param {[string]} width
* @param {[string]} height
* @param {[string]} mode
* @return {[string]}
*/
exports.image = (url, width, height, mode) => {
mode = _.isNumber(mode) ? mode : 2;
url = url || '';
return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
};
/**
* 条件判断
* @param {[string]} v1
* @param {[string]} v2
* @param {[object]} options 上下文环境,一般不手动传
* @return {[boolen]}
*/
exports.ifEqualTo = (v1, v2, _options) => {
if (v1 === v2) {
return _options.fn(this); // eslint-disable-line
}
return _options.inverse(this); // eslint-disable-line
};
/**
* 站内地址格式化
* @param {[string]} uri 路径
* @param {[object]} qs 查询字符串
* @param {[string]} module 模块
* @return {[string]}
*/
exports.urlFormat = (uri, qs, module) => {
const subDomain = '.yohobuy.com';
const subName = {
default: config.siteUrl,
guang: `//guang${subDomain}`,
list: `//list${subDomain}`,
search: `//search${subDomain}`,
huodong: `//huodong${subDomain}`,
activity: '//activity.yohobuy.com',
index: config.siteUrl
};
let url;
module = module || 'default';
if (subName[module]) {
url = subName[module];
} else {
url = `//${module}${subDomain}`; // 规则没匹配到就把模块当作子域名
}
url += uri;
if (qs) {
url += `?${querystring.stringify(qs)}`;
}
return url;
};
/**
* 大写转小写处理
* @param {[string]} str 转换字符
*/
exports.lowerCase = (str) => {
str = str || '';
return str.toLowerCase();
};
/**
* 小写转大写处理
* @param {[string]} str 转换字符
*/
exports.upperCase = (str) => {
str = str || '';
return str.toUpperCase();
};
/**
* 时间格式化
* @param format 格式化token @see{http://momentjs.cn/docs/#/displaying/format/}
* @param date 日期或者数字
* @return string
*
*/
exports.dateFormat = (format, date) => {
if (typeof format !== 'string' || typeof date === 'undefined') {
return '';
} else {
if (date instanceof Date) {
return moment(date).format(format);
} else {
const d = moment.unix(date);
return moment(d).utc().format(format);
}
}
};
/**
* 时间差格式化
* @param {[string]} format 格式化字符串
* @param {[number]} diff 相差值
* @param {[string]} type diff时间类型 默认ms
*
* Key Shorthand
* years y
* quarters Q
* months M
* weeks w
* days d
* hours h
* minutes m
* seconds s
* milliseconds ms
*
* @example
* let diff = 60 * 60 * 24 * (1.3) + 2;
*
* let s = helpers.dateDiffFormat('{d}天{h}小时', diff, 's');
* >>> 1天7小时
*/
exports.dateDiffFormat = (format, diff, type) => {
if (typeof format !== 'string' || typeof diff === 'undefined') {
return '';
} else {
type = type || 'ms';
const m = moment.duration(diff, type);
format.match(/(\{.*?\})/g).forEach((s) => {
format = format.replace(s, m.get(s.substring(1, s.length - 1)));
});
return format;
}
};
/**
* 强行获取原图片
* @param imageUrl
*/
exports.getForceSourceUrl = (imageUrl) => {
return _.head(imageUrl.split('?'));
};