<commit_before>const express = require("express");
const HttpError = require("../../libs/errors").HttpError;
const lodash = require("lodash");
const CompanyModel = require("../../models/company_model");
const getCompanyName = require("../company_helper").getCompanyName;

const router = express.Router();

function _generateGetCompanyViewModel(companies) {
    const result = companies.map(company => ({
        id: company.id,
        name: getCompanyName(company.name),
        capital: company.capital,
    }));
    return result;
}

/**
 * @api {get} /companies/search Search Company
 * @apiName SearchCompany
 * @apiGroup Company
 * @apiParam {String} key
 * @apiParam {Number} [page=0]
 * @apiSuccess {Object[]} . Companies
 */
router.get("/search", (req, res, next) => {
    const search = req.query.key || "";
    const page = req.query.page || 0;

    if (search === "") {
        next(new HttpError("key is required", 422));
        return;
    }

    const query = {
        $or: [
            {
                name: new RegExp(
                    `^${lodash.escapeRegExp(search.toUpperCase())}`
                ),
            },
            { id: search },
        ],
    };

    const sort_by = {
        capital: -1,
        type: -1,
        name: 1,
        id: 1,
    };

    const company_model = new CompanyModel(req.db);
    const skip = 25 * page;
    const limit = 25;

    company_model
        .searchCompany(query, sort_by, skip, limit)
        .then(results => {
            res.send(_generateGetCompanyViewModel(results));
        })
        .catch(err => {
            next(new HttpError("Internal Server Error", 500));
        });
});

module.exports = router;<commit_msg>用 ramda 改寫, 並改成 async/await<commit_after>const express = require("express");
const lodash = require("lodash");
const R = require("ramda");
const { HttpError } = require("../../libs/errors");
const CompanyModel = require("../../models/company_model");
const { getCompanyName } = require("../company_helper");
const wrap = require("../../libs/wrap");
const { combineSelector } = require("../../view_models/helper");

const router = express.Router();

/**
 * @param company
 */
const companyView = combineSelector([
    R.pick(["id", "capital"]),
    company => ({ name: getCompanyName(company.name) }),
]);

/**
 * @param companies
 */
const searchView = R.map(companyView);

/**
 * @api {get} /companies/search Search Company
 * @apiName SearchCompany
 * @apiGroup Company
 * @apiParam {String} key
 * @apiParam {Number} [page=0]
 * @apiSuccess {Object[]} . Companies
 */
router.get(
    "/search",
    wrap(async (req, res) => {
        const search = req.query.key || "";
        const page = req.query.page || 0;

        if (search === "") {
            throw new HttpError("key is required", 422);
            return;
        }

        const query = {
            $or: [
                {
                    name: new RegExp(
                        `^${lodash.escapeRegExp(search.toUpperCase())}`
                    ),
                },
                { id: search },
            ],
        };

        const sort_by = {
            capital: -1,
            type: -1,
            name: 1,
            id: 1,
        };

        const company_model = new CompanyModel(req.db);
        const skip = 25 * page;
        const limit = 25;

        const companies = await company_model.searchCompany(
            query,
            sort_by,
            skip,
            limit
        );

        res.send(searchView(companies));
    })
);

module.exports = router;
{'fraction_non_alphanumeric': 0.15505177721802407, 'fraction_numerical': 0.00811642877134061, 'mean_word_length': 1.9932998324958124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4006941', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1017, 'n_words': 238}