helpers.js 1.79 KB
const url = require('url');
const config = require('../config/common');
const assetUrl = config.assetUrl;

module.exports = {
    imgSrc: function(imgSrc) {
        return url.resolve(assetUrl, imgSrc);
    },
    image2: function(url, opts) {
        if (url) {
            let params = opts.hash;
            let urls = url.split('?');
            let query = urls[1] || '';

            if (query) {
                query = query.replace(/{width}/g, params.w).replace(/{height}/g, params.h).replace(/{mode}/g, (params.mode || 2));

                if (query.indexOf('imageView2') === 0) {
                    if (params.q && query.indexOf('/q/') > 0) {
                        query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
                    } else {
                        query += '/q/' + params.q;
                    }
                } else if (query.indexOf('imageMogr2') === 0) {
                    if (params.q && query.indexOf('/quality/') > 0) {
                        query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
                    } else {
                        query += '/quality/' + params.q;
                    }
                }
            } else {
                query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
            }
            return urls[0] + '?' + query;
        } else {
            return url;
        }
    },
    ifor: function() {
        var args = Array.prototype.slice.call(arguments);
        var opt = args[args.length - 1];
        var isTrue = false;

        for (var i = 0; i < args.length - 1; i ++ ) {
            if (args[i]) {
                isTrue = true;
                break;
            }
        }

        if (isTrue) {
            return opt.fn(this);
        } else {
            return opt.inverse(this);
        }
    }
};