From 4531de80f911d6f33775edab2a138bcebecdcca0 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 12 Jun 2019 04:09:27 +0100 Subject: [PATCH] feat: add common-utils (#2374) --- lib/routes/atfd/index.js | 12 ++---------- lib/routes/metacritic/release.js | 21 +++++++++++++++------ lib/utils/common-utils.js | 11 +++++++++++ test/utils/common-utils.js | 7 +++++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 lib/utils/common-utils.js create mode 100644 test/utils/common-utils.js diff --git a/lib/routes/atfd/index.js b/lib/routes/atfd/index.js index 21fac0c149..00a7ae8213 100644 --- a/lib/routes/atfd/index.js +++ b/lib/routes/atfd/index.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { toTitleCase } = require('@/utils/common-utils'); const dayjs = require('dayjs'); module.exports = async (ctx) => { @@ -13,7 +14,7 @@ module.exports = async (ctx) => { locations.split(',').forEach(function(pair) { const country = pair.split('+')[0]; - const city = titleCase(pair.split('+')[1]); + const city = toTitleCase(pair.split('+')[1]); url += `{"country":"${country}","city":"${city}"},`; host += `/${country}/${city.replace(' ', '_')}`; @@ -43,12 +44,3 @@ module.exports = async (ctx) => { })), }; }; - -const titleCase = (str) => - str - .toLowerCase() - .split(' ') - .map(function(word) { - return word.replace(word[0], word[0].toUpperCase()); - }) - .join(' '); diff --git a/lib/routes/metacritic/release.js b/lib/routes/metacritic/release.js index f9aeba8661..54f46f18ad 100644 --- a/lib/routes/metacritic/release.js +++ b/lib/routes/metacritic/release.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { toTitleCase } = require('@/utils/common-utils'); const cheerio = require('cheerio'); module.exports = async (ctx) => { @@ -34,19 +35,27 @@ module.exports = async (ctx) => { const result = list.map((item) => { const $ = cheerio.load(item); return { - title: $('.product_title').text(), + title: $('.product_title') + .text() + .trim(), url: 'https://www.metacritic.com' + $('.product_title > a').attr('href'), - metascore: $('.brief_metascore').text(), - userscore: $('.textscore').text(), - date: $('.release_date > .data').text(), + metascore: $('.brief_metascore') + .text() + .trim(), + userscore: $('.textscore') + .text() + .trim(), + date: $('.release_date > .data') + .text() + .trim(), }; }); ctx.state.data = { - title: `Metacritic ${ctx.params.platform} ${title}`, + title: toTitleCase(`Metacritic ${ctx.params.platform} games ${title}`), link: url, item: result.map((item) => ({ - title: `${item.metascore === 'tbd' ? '' : [item.metascore]} ${item.title}`, + title: `${item.metascore === 'tbd' ? '' : '[' + item.metascore + ']'} ${item.title}`, description: `Release Date: ${item.date}
Metacritic Score: ${item.metascore}
User Score: ${item.userscore}
`, link: item.url, })), diff --git a/lib/utils/common-utils.js b/lib/utils/common-utils.js new file mode 100644 index 0000000000..5f009802d4 --- /dev/null +++ b/lib/utils/common-utils.js @@ -0,0 +1,11 @@ +// convert a string into title case +const toTitleCase = (str) => + str + .toLowerCase() + .split(' ') + .map((word) => word.replace(word[0], word[0].toUpperCase())) + .join(' '); + +module.exports = { + toTitleCase, +}; diff --git a/test/utils/common-utils.js b/test/utils/common-utils.js new file mode 100644 index 0000000000..66b51e7722 --- /dev/null +++ b/test/utils/common-utils.js @@ -0,0 +1,7 @@ +const utils = require('../../lib/utils/common-utils'); + +describe('common-utils', () => { + it('toTitleCase', async () => { + expect(utils.toTitleCase('RSSHub IS AS aweSOme aS henry')).toBe('Rsshub Is As Awesome As Henry'); + }); +});