index.js 1.14 KB
/**
 * Handlebars helpers
 * dongjinhu jinhu.dong@yoho.cn
 * 2016-07-14
 */

'use strict';

/**
 * 乘法
 * @param  {[Number]} num1
 * @param  {[Number]} num2
 * @return {[boolen]}
 */
exports.multiple = (num1, num2) => {
    num1 = typeof num1 === 'number' ? num1 : parseFloat(num1, 10);
    num2 = typeof num2 === 'number' ? num2 : parseFloat(num2, 10);

    if (num1 && num2) {
        return num1 * num2;
    }
};

/**
 * 小于等于
 * @param  {[Number]} num1
 * @param  {[Number]} num2
 * @param  {[object]} options 上下文环境,一般不手动传
 * @return {[boolen]}
 */
exports.lte = (num1, num2, options) => {
    num1 = typeof num1 === 'number' ? num1 : parseFloat(num1, 10);
    num2 = typeof num2 === 'number' ? num2 : parseFloat(num2, 10);

    if (num1 <= num2) {
        return options.fn(this);
    }
    return options.inverse(this);
};

/**
 * 显示剩余库存
 * @param  {[Number]} leftNumber
 * @return {[boolen]}
 */
exports.showStorage = (leftNumber) => {
    leftNumber = +leftNumber;

    if (leftNumber <= 3 && leftNumber >= 0) {
        return '余量有限';
    } else if (leftNumber < 0) {
        return '库存不足';
    }
};