es6代码在es5中被破坏了

时间:2016-08-24 05:52:27

标签: javascript html ecmascript-6

我一直在努力将我的代码从es6转换为es5,因为我的工作中有一些框架限制......虽然我一直在努力找到问题所在。由于某种原因,代码不能完全相同,并且没有错误......

有人可以告诉我,如果我已正确翻译了吗?

这是ES6代码:

function filterFunction(items, filters, stringFields = ['Title', 'Description'], angular = false) {
    // Filter by the keys of the filters parameter
    const filterKeys = Object.keys(filters);

    // Set up a mutable filtered object with items
    let filtered;

    // Angular doesn't like deep clones... *sigh*
    if (angular) {
        filtered = items;
    } else {
        filtered = _.cloneDeep(items);
    }

    // For each key in the supplied filters
    for (let key of filterKeys) {
        if (key !== 'TextInput') {
            filtered = filtered.filter(item => {

                // Make sure we have something to filter by...
                if (filters[key].length !== 0) {
                    return _.intersection(filters[key], item[key]).length >= 1;
                }

                return true;
            });
        }

        // If we're at TextInput, handle things differently
        else if (key === 'TextInput') {
            filtered = filtered.filter(item => {
                let searchString = "";

                // For each field specified in the strings array, build a string to search through
                for (let field of stringFields) {
                    // Handle arrays differently
                    if (!Array.isArray(item[field])) {
                        searchString += `${item[field]} `.toLowerCase();
                    } else {
                        searchString += item[field].join(' ').toLowerCase();
                    }
                }

                // Return the item if the string matches our input
                return searchString.indexOf(filters[key].toLowerCase()) !== -1;
            });
        }
    }
    return filtered;
}

这是我翻译的代码,部分99%工作..

function filterFunction(items, filters, stringFields, angular) {
    // Filter by the keys of the filters parameter
    var filterKeys = Object.keys(filters);

    // Set up a mutable filtered object with items
    var filtered;

    // Angular doesn't like deep clones... *sigh*
    if (angular) {
        filtered = items;
    } else {
        filtered = _.cloneDeep(items);
    }

    // For each key in the supplied filters
    for (var key = 0 ;  key < filterKeys.length ; key ++) {

        if (filterKeys[key] !== 'TextInput') {
            filtered = filtered.filter( function(item) {

                // Make sure we have something to filter by...
                if (filters[filterKeys[key]].length !== 0) {
                    return _.intersection(filters[filterKeys[key]], item[filterKeys[key]]).length >= 1;
                }

                return true;
            });
        }

        // If we're at TextInput, handle things differently
        else if (filterKeys[key] === 'TextInput') {
            filtered = filtered.filter(function(item) {
                var searchString = "";

                // For each field specified in the strings array, build a string to search through
                for (var field = 0; field < stringFields.length; field ++) {
                    // Handle arrays differently
                    console.log(field);
                    if (!Array.isArray(item[stringFields[field]])) {
                        searchString += item[stringFields[field]] + ' '.toLowerCase();
                    } else {
                        searchString += item[stringFields[field]].join(' ').toLowerCase();
                    }
                }

                // Return the item if the string matches our input
                return searchString.indexOf(filters[filterKeys[key]].toLowerCase()) !== -1;
            });
        }
    }
    return filtered;
}

2 个答案:

答案 0 :(得分:5)

这两行

searchString += `${item[field]} `.toLowerCase();
searchString += item[stringFields[field]] + ' '.toLowerCase();

确实不等同。要对字符串的所有部分应用toLowerCase方法,您需要将ES5连接包装在括号中:

searchString += (item[stringFields[field]] + ' ').toLowerCase();

或者,由于空白无论如何都不能小写,只需使用

searchString += item[stringFields[field]].toLowerCase() + ' ';

答案 1 :(得分:0)

以上是babeljs本身的翻译代码,如上所述。

'use strict';

function filterFunction(items, filters) {
    var stringFields = arguments.length <= 2 || arguments[2] === undefined ? ['Title', 'Description'] : arguments[2];
    var angular = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];

    // Filter by the keys of the filters parameter
    var filterKeys = Object.keys(filters);

    // Set up a mutable filtered object with items
    var filtered = void 0;

    // Angular doesn't like deep clones... *sigh*
    if (angular) {
        filtered = items;
    } else {
        filtered = _.cloneDeep(items);
    }

    // For each key in the supplied filters
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        var _loop = function _loop() {
            var key = _step.value;

            if (key !== 'TextInput') {
                filtered = filtered.filter(function (item) {

                    // Make sure we have something to filter by...
                    if (filters[key].length !== 0) {
                        return _.intersection(filters[key], item[key]).length >= 1;
                    }

                    return true;
                });
            }

            // If we're at TextInput, handle things differently
            else if (key === 'TextInput') {
                    filtered = filtered.filter(function (item) {
                        var searchString = "";

                        // For each field specified in the strings array, build a string to search through
                        var _iteratorNormalCompletion2 = true;
                        var _didIteratorError2 = false;
                        var _iteratorError2 = undefined;

                        try {
                            for (var _iterator2 = stringFields[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                                var field = _step2.value;

                                // Handle arrays differently
                                if (!Array.isArray(item[field])) {
                                    searchString += (item[field] + ' ').toLowerCase();
                                } else {
                                    searchString += item[field].join(' ').toLowerCase();
                                }
                            }

                            // Return the item if the string matches our input
                        } catch (err) {
                            _didIteratorError2 = true;
                            _iteratorError2 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion2 && _iterator2.return) {
                                    _iterator2.return();
                                }
                            } finally {
                                if (_didIteratorError2) {
                                    throw _iteratorError2;
                                }
                            }
                        }

                        return searchString.indexOf(filters[key].toLowerCase()) !== -1;
                    });
                }
        };

        for (var _iterator = filterKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            _loop();
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

    return filtered;
}

p.s。 或者有一种更好的方法可以直接使用babeljs而无需手动转换它。