Authored by 毕凯

增加类库

  1 +/**
  2 + * 对象键名驼峰化
  3 + * @author: Bi Kai<kai.bi@yoho.cn>
  4 + * @date: 2016/05/09
  5 + */
  6 +'use strict';
  7 +const _ = require('lodash');
  8 +
  9 +let camelCase,
  10 + camelCaseObject,
  11 + camelCaseArray;
  12 +
  13 +camelCaseObject = (obj) => {
  14 + _.forEach(Object.keys(obj), (k) => {
  15 + obj[k] = camelCase(obj[k]);
  16 + obj[_.camelCase(k)] = obj[k];
  17 + });
  18 + return obj;
  19 +};
  20 +
  21 +camelCaseArray = (list) => {
  22 + _.forEach(list, (k) => {
  23 + k = camelCase(k);
  24 + });
  25 + return list;
  26 +};
  27 +
  28 +camelCase = (data) => {
  29 + if (_.isObject(data)) {
  30 + data = camelCaseObject(data);
  31 + }
  32 +
  33 + if (_.isArray(data)) {
  34 + data = camelCaseArray(data);
  35 + }
  36 +
  37 + return data;
  38 +};
  39 +
  40 +module.exports = camelCase;
1 -/*  
2 - * @Author: Targaryen  
3 - * @Date: 2016-05-10 10:11:34  
4 - * @Last Modified by: Targaryen  
5 - * @Last Modified time: 2016-05-12 11:25:53 1 +/**
  2 + * Handlebars helpers
  3 + * bikai kai.bi@yoho.cn
  4 + * 2016-05-10
6 */ 5 */
7 -/* -----------------------------------------  
8 - * 实现 PHP 的 Helpers 函数  
9 - * -----------------------------------------  
10 - */  
11 -  
12 'use strict'; 6 'use strict';
13 -  
14 -const SUB_DOMAIN = '.yohobuy.com';  
15 -const SITE_MAIN = 'http://www.yohobuy.com'; 7 +const querystring = require('querystring');
16 const _ = require('lodash'); 8 const _ = require('lodash');
  9 +const config = require('../config/common');
17 10
18 -class HELPERS { 11 +/**
  12 + * 七牛图片路径处理
  13 + * @param {[tring]} url
  14 + * @param {[tring]} width
  15 + * @param {[tring]} height
  16 + * @param {[tring]} mode
  17 + * @return {[tring]}
  18 + */
  19 +exports.image = (url, width, height, mode) => {
  20 + mode = _.isNumber(mode) ? mode : 2;
  21 + url = url || '';
  22 + return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
  23 +};
19 24
20 - /**  
21 - * 构建网站的URL  
22 - * @param uri String  
23 - * @param param Array  
24 - * @param module String 25 +/**
  26 + * 站内地址格式化
  27 + * @param {[string]} uri 路径
  28 + * @param {[object]} qs 查询字符串
  29 + * @param {[string]} module 模块
  30 + * @return {[string]}
25 */ 31 */
26 - url(uri, param, module) {  
27 - let url = ''; 32 +exports.url = (uri, qs, module) => {
  33 + const subDomain = '.m.yohobuy.com';
  34 + const subName = {
  35 + default: config.siteUrl,
  36 + guang: '//guang' + subDomain,
  37 + list: '//list' + subDomain,
  38 + search: '//search' + subDomain,
  39 + huodong: '//huodong' + subDomain,
  40 + activity: '//activity.yohobuy.com',
  41 + index: config.siteUrl
  42 + };
  43 + let url;
28 44
29 - if (module === null) {  
30 - module = 'index';  
31 - }  
32 - switch (module) {  
33 - case 'default':  
34 - url = 'http://m.yohobuy.com';  
35 - break;  
36 - case 'guang':  
37 - url = 'http://guang' + SUB_DOMAIN;  
38 - break;  
39 - case 'list':  
40 - url = 'http://list' + SUB_DOMAIN;  
41 - break;  
42 - case 'search':  
43 - url = 'http://search' + SUB_DOMAIN;  
44 - break;  
45 - case 'index':  
46 - url = SITE_MAIN;  
47 - break;  
48 - case '':  
49 - break;  
50 - case undefined:  
51 - break;  
52 - default:  
53 - url = 'http://' + module + SUB_DOMAIN;  
54 - break; 45 + module = module || 'default';
  46 + if (subName[module]) {
  47 + url = subName[module];
  48 + } else {
  49 + url = '//' + module + subDomain; // 规则没匹配到就把模块当作子域名
55 } 50 }
56 51
57 url += uri; 52 url += uri;
58 - if (param !== null && param !== undefined) {  
59 - url += '?';  
60 - _.forEach(param, function(value, key) {  
61 - url += (key + '=' + value + '&');  
62 - }); 53 + if (qs) {
  54 + url += '?' + querystring.stringify(qs);
63 } 55 }
64 - if (url.substr(-1, 1) === '&') {  
65 - return url.substr(0, url.length - 1);  
66 - } else { 56 +
67 return url; 57 return url;
68 - } 58 +};
69 59
70 - }  
71 60
72 - /**  
73 - * 根据尺寸获得图片url  
74 - * @param uri String  
75 - * @param param Array  
76 - * @param module String 61 +/**
  62 + * 大写转小写处理
  63 + * @param {[string]} str 转换字符
77 */ 64 */
78 - getImageUrl(url, width, height, mode) {  
79 - if (mode === null) {  
80 - mode = 2;  
81 - }  
82 - return url.replace(/{width}/g, width)  
83 - .replace(/{height}/g, height)  
84 - .replace(/{mode}/g, mode);  
85 - }  
86 -} 65 +exports.lowerCase = (str) => {
  66 + str = str || '';
  67 + return str.toLowerCase();
  68 +};
87 69
88 -module.exports = HELPERS; 70 +/**
  71 + * 小写转大写处理
  72 + * @param {[string]} str 转换字符
  73 + */
  74 +exports.upperCase = (str) => {
  75 + str = str || '';
  76 + return str.toUpperCase();
  77 +};