Authored by 毕凯

增加类库

/**
* 对象键名驼峰化
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const _ = require('lodash');
let camelCase,
camelCaseObject,
camelCaseArray;
camelCaseObject = (obj) => {
_.forEach(Object.keys(obj), (k) => {
obj[k] = camelCase(obj[k]);
obj[_.camelCase(k)] = obj[k];
});
return obj;
};
camelCaseArray = (list) => {
_.forEach(list, (k) => {
k = camelCase(k);
});
return list;
};
camelCase = (data) => {
if (_.isObject(data)) {
data = camelCaseObject(data);
}
if (_.isArray(data)) {
data = camelCaseArray(data);
}
return data;
};
module.exports = camelCase;
... ...
/*
* @Author: Targaryen
* @Date: 2016-05-10 10:11:34
* @Last Modified by: Targaryen
* @Last Modified time: 2016-05-12 11:25:53
/**
* Handlebars helpers
* bikai kai.bi@yoho.cn
* 2016-05-10
*/
/* -----------------------------------------
* 实现 PHP 的 Helpers 函数
* -----------------------------------------
*/
'use strict';
const SUB_DOMAIN = '.yohobuy.com';
const SITE_MAIN = 'http://www.yohobuy.com';
const querystring = require('querystring');
const _ = require('lodash');
const config = require('../config/common');
class HELPERS {
/**
* 构建网站的URL
* @param uri String
* @param param Array
* @param module String
*/
url(uri, param, module) {
let url = '';
if (module === null) {
module = 'index';
}
switch (module) {
case 'default':
url = 'http://m.yohobuy.com';
break;
case 'guang':
url = 'http://guang' + SUB_DOMAIN;
break;
case 'list':
url = 'http://list' + SUB_DOMAIN;
break;
case 'search':
url = 'http://search' + SUB_DOMAIN;
break;
case 'index':
url = SITE_MAIN;
break;
case '':
break;
case undefined:
break;
default:
url = 'http://' + module + SUB_DOMAIN;
break;
}
/**
* 七牛图片路径处理
* @param {[tring]} url
* @param {[tring]} width
* @param {[tring]} height
* @param {[tring]} mode
* @return {[tring]}
*/
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);
};
url += uri;
if (param !== null && param !== undefined) {
url += '?';
_.forEach(param, function(value, key) {
url += (key + '=' + value + '&');
});
}
if (url.substr(-1, 1) === '&') {
return url.substr(0, url.length - 1);
} else {
return url;
}
/**
* 站内地址格式化
* @param {[string]} uri 路径
* @param {[object]} qs 查询字符串
* @param {[string]} module 模块
* @return {[string]}
*/
exports.url = (uri, qs, module) => {
const subDomain = '.m.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
* @param uri String
* @param param Array
* @param module String
*/
getImageUrl(url, width, height, mode) {
if (mode === null) {
mode = 2;
}
return url.replace(/{width}/g, width)
.replace(/{height}/g, height)
.replace(/{mode}/g, mode);
url += uri;
if (qs) {
url += '?' + querystring.stringify(qs);
}
}
module.exports = HELPERS;
return url;
};
/**
* 大写转小写处理
* @param {[string]} str 转换字符
*/
exports.lowerCase = (str) => {
str = str || '';
return str.toLowerCase();
};
/**
* 小写转大写处理
* @param {[string]} str 转换字符
*/
exports.upperCase = (str) => {
str = str || '';
return str.toUpperCase();
};
... ...