diff --git a/scripts/core/idCreator.ts b/scripts/core/idCreator.ts new file mode 100644 index 00000000..e547f83e --- /dev/null +++ b/scripts/core/idCreator.ts @@ -0,0 +1,17 @@ +export class IDCreator { + create(name: string, country: string): string { + const slug = normalize(name) + const code = country.toLowerCase() + + return `${slug}.${code}` + } +} + +function normalize(name: string) { + return name + .replace(/^@/gi, 'At') + .replace(/^&/i, 'And') + .replace(/\+/gi, 'Plus') + .replace(/\s-(\d)/gi, ' Minus$1') + .replace(/[^a-z\d]+/gi, '') +} diff --git a/scripts/core/index.ts b/scripts/core/index.ts index b52c75ce..b32c7fe6 100644 --- a/scripts/core/index.ts +++ b/scripts/core/index.ts @@ -2,3 +2,4 @@ export * from './csv' export * from './issueParser' export * from './issueLoader' export * from './csvParser' +export * from './idCreator' diff --git a/scripts/db/update.ts b/scripts/db/update.ts index c80a577e..39fdef9a 100644 --- a/scripts/db/update.ts +++ b/scripts/db/update.ts @@ -1,4 +1,4 @@ -import { CSV, IssueLoader, CSVParser } from '../core' +import { CSV, IssueLoader, CSVParser, IDCreator } from '../core' import { Channel, Blocked, Issue } from '../models' import { DATA_DIR } from '../constants' import { Storage, Collection } from '@freearhey/core' @@ -8,6 +8,7 @@ let channels = new Collection() const processedIssues = new Collection() async function main() { + const idCreator = new IDCreator() const dataStorage = new Storage(DATA_DIR) const parser = new CSVParser() @@ -20,8 +21,8 @@ async function main() { const loader = new IssueLoader() await removeChannels({ loader }) - await editChannels({ loader }) - await addChannels({ loader }) + await editChannels({ loader, idCreator }) + await addChannels({ loader, idCreator }) await blockChannels({ loader }) await unblockChannels({ loader }) @@ -65,7 +66,7 @@ async function removeChannels({ loader }: { loader: IssueLoader }) { }) } -async function editChannels({ loader }: { loader: IssueLoader }) { +async function editChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) { const issues = await loader.load({ labels: ['channels:edit,approved'] }) issues.forEach((issue: Issue) => { const data = issue.data @@ -80,7 +81,7 @@ async function editChannels({ loader }: { loader: IssueLoader }) { if (data.has('name') || data.has('country')) { const name = data.get('name') || found.name const country = data.get('country') || found.country - channelId = generateChannelId(name, country) + channelId = idCreator.create(name, country) } found.update({ @@ -107,13 +108,13 @@ async function editChannels({ loader }: { loader: IssueLoader }) { }) } -async function addChannels({ loader }: { loader: IssueLoader }) { +async function addChannels({ loader, idCreator }: { loader: IssueLoader; idCreator: IDCreator }) { const issues = await loader.load({ labels: ['channels:add,approved'] }) issues.forEach((issue: Issue) => { const data = issue.data if (data.missing('name') || data.missing('country')) return - const channelId = generateChannelId(data.get('name'), data.get('country')) + const channelId = idCreator.create(data.get('name'), data.get('country')) const found: Channel = channels.first((channel: Channel) => channel.id === channelId) if (found) return @@ -182,13 +183,3 @@ async function blockChannels({ loader }: { loader: IssueLoader }) { processedIssues.push(issue) }) } - -function generateChannelId(name: string, country: string): string { - const slug = name - .replace(/\+/gi, 'Plus') - .replace(/^@/gi, 'At') - .replace(/[^a-z\d]+/gi, '') - country = country.toLowerCase() - - return `${slug}.${country}` -} diff --git a/scripts/db/validate.ts b/scripts/db/validate.ts index 174461c9..578987a4 100644 --- a/scripts/db/validate.ts +++ b/scripts/db/validate.ts @@ -1,9 +1,8 @@ import { Collection, Storage, File, Dictionary, Logger } from '@freearhey/core' import { DATA_DIR } from '../constants' -import { transliterate } from 'transliteration' import { program } from 'commander' import Joi from 'joi' -import { CSVParser } from '../core' +import { CSVParser, IDCreator } from '../core' import chalk from 'chalk' program.argument('[filepath]', 'Path to file to validate').parse(process.argv) @@ -217,28 +216,15 @@ function findDuplicatesBy(rows: { [key: string]: string }[], key: string) { function validateChannelId(row: { [key: string]: string }, i: number) { const errors = new Collection() - const name = normalize(row.name) - const code = row.country.toLowerCase() - const expected = `${name}.${code}` + const expectedId = new IDCreator().create(row.name, row.country) - if (expected !== row.id) { + if (expectedId !== row.id) { errors.push({ line: i + 2, message: `"${row.id}" must be derived from the channel name "${row.name}" and the country code "${row.country}"` }) } - function normalize(name: string) { - const translit = transliterate(name) - - return translit - .replace(/^@/i, 'At') - .replace(/^&/i, 'And') - .replace(/\+/gi, 'Plus') - .replace(/\s-(\d)/gi, ' Minus$1') - .replace(/[^a-z\d]+/gi, '') - } - return errors }