如何将字符串解析为比较运算符?

时间:2018-06-09 15:56:01

标签: javascript

我实现了一种在指定选项上过滤商品的方法。该方法将包含用于搜索的参数的选项对象作为参数,例如:{name:“item 2”,price:“< = 1000”,count:“> 2”},每个选项都是可选的。该方法必须返回带有货物的过滤数组。 filterProductBy(options)。 我设法按名称过滤。告诉我如何正确过滤数字,以便过滤器调用如下所示:

console.log(shop.filterProductBy({
name: "product 1",
count: ">1",
price: "<=1000"}));

“count”和“price”需要与比较运算符一起解析,它设置了过滤器的范围,这里有一个可以做到这一点的函数,如何在我的方法中实现这个func,即一切会工作吗?

function parseCompOperator(oString, lhs) {
    if (oString.match("(?:<|>)=?\\d+") === null) {
        return "Invalid input";
    }

    let rator = oString.match("(?:<|>)=?")[0];
    let rhs = oString.match("\\d+")[0] * 1;

    if (rator === '>') {
        return (lhs > rhs);
    }
    else if (rator === '<') {
        return (lhs < rhs);
    }
    else if (rator === '>=') {
        return (lhs >= rhs);
    }
    else if (rator === '<=') {
        return (lhs <= rhs);
    }
}

所有代码:

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }
    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    //method for filtering products by specified parameters
    filterProductBy(options) {
        const optionName = options.name,
            optionCount = options.count,
            optionPrice = options.price;

        const filters = {
            byName: function (actualName, optionName) {
                return (actualName === undefined) || (actualName === optionName);
            },

            byCount: function (actualCount, optionCount) {
                return (actualCount === undefined) || (actualCount === optionCount);
            },

            byPrice: function (actualPrice, optionPrice) {
                return (actualPrice === undefined) || (actualPrice === optionPrice);
            }
    };
        return this.products.filter(
            (product) => filters.byName(product.name, optionName)
            || filters.byCount(product.count, optionCount)
            || filters.byPrice(product.price, optionPrice));
        }
}
const shop = new Shop();
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("item 2", 2, 100));
shop.addProduct(new Product("some 3", 3, 500));
shop.addProduct(new Product("anything 4", 4, 1000));
console.log(shop.filterProductBy({
    name: "product 1",
    count: ">1",
    price: ">=500"

}));

1 个答案:

答案 0 :(得分:3)

您可以将运算符作为键,并将函数作为值,如

var operators = {
        '===': function (a, b) { return a === b; },
        '==': function (a, b) { return a == b; },
        '<=': function (a, b) { return a <= b; },
        '>=': function (a, b) { return a >= b; },
        '<': function (a, b) { return a < b; },
        '>': function (a, b) { return a > b; }
    };

使用时,请将分离的运算符与两个值进行比较。

您可以省略对运算符的检查并将函数作为值,如

return operators[operator](leftValue, rightValue);