Skip to content

Commit

Permalink
feat: implement spanish views parsers (es-ES, es-419, es-US)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrednav authored Apr 5, 2024
1 parent 0e038b0 commit 2b01808
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
4 changes: 2 additions & 2 deletions public/_locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@
"description": "Text to display for the descending 'sort by channel name' option"
},
"sortType_views_label_asc": {
"message": "Visualizaciones (Ascendente)",
"message": "Vistas (Ascendente)",
"description": "Text to display for the ascending 'sort by views' option"
},
"sortType_views_label_desc": {
"message": "Visualizaciones (Descendente)",
"message": "Vistas (Descendente)",
"description": "Text to display for the descending 'sort by views' option"
},
"sortType_uploadDate_label_asc": {
Expand Down
15 changes: 3 additions & 12 deletions src/modules/sorting/sort-by-upload-date/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { elementSelectors } from "src/shared/data/element-selectors";
import { EnUploadDateParser } from "./parsers/en";
import { ZhCnUploadDateParser } from "./parsers/zh_CN";

const PARSERS_BY_LOCALE = {
"en": EnUploadDateParser,
"en-GB": EnUploadDateParser,
"en-IN": EnUploadDateParser,
"en-US": EnUploadDateParser,
"zh-Hans-CN": ZhCnUploadDateParser
};
import { getSupportedLocales, getUploadDateParser } from "./parsers";

export class SortByUploadDateStrategy {
static supportedLocales = Object.keys(PARSERS_BY_LOCALE);
static supportedLocales = getSupportedLocales();

/**
* Sorts a list of videos by their upload date
Expand Down Expand Up @@ -56,7 +47,7 @@ export class UploadDateParserContext {

/** @param {string} locale */
setParser(locale) {
const Parser = PARSERS_BY_LOCALE[locale] ?? EnUploadDateParser;
const Parser = getUploadDateParser(locale);
this.parser = new Parser();
}

Expand Down
17 changes: 17 additions & 0 deletions src/modules/sorting/sort-by-upload-date/parsers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { EnUploadDateParser } from "./en";
import { ZhHansCnUploadDateParser } from "./zh-Hans-CN";

const UPLOAD_DATE_PARSERS_BY_LOCALE = {
"en": EnUploadDateParser,
"en-GB": EnUploadDateParser,
"en-US": EnUploadDateParser,
"zh-Hans-CN": ZhHansCnUploadDateParser
};

/** @param {string} locale */
export const getUploadDateParser = (locale) => {
return UPLOAD_DATE_PARSERS_BY_LOCALE[locale] ?? EnUploadDateParser;
};

export const getSupportedLocales = () =>
Object.keys(UPLOAD_DATE_PARSERS_BY_LOCALE);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class ZhCnUploadDateParser {
export class ZhHansCnUploadDateParser {
/** @param {Element} videoInfo */
parse(videoInfo) {
const secondsByUnit = {
Expand Down
14 changes: 3 additions & 11 deletions src/modules/sorting/sort-by-views/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { elementSelectors } from "src/shared/data/element-selectors";
import { EnViewsParser } from "./parsers/en";
import { ZhCnViewsParser } from "./parsers/zh_CN";

const PARSERS_BY_LOCALE = {
"en": EnViewsParser,
"en-GB": EnViewsParser,
"en-US": EnViewsParser,
"zh-Hans-CN": ZhCnViewsParser
};
import { getSupportedLocales, getViewsParser } from "./parsers";

export class SortByViewsStrategy {
static supportedLocales = Object.keys(PARSERS_BY_LOCALE);
static supportedLocales = getSupportedLocales();

/**
* Sorts a list of videos by their view count
Expand Down Expand Up @@ -55,7 +47,7 @@ export class ViewsParserContext {

/** @param {string} locale */
setParser(locale) {
const Parser = PARSERS_BY_LOCALE[locale] ?? EnViewsParser;
const Parser = getViewsParser(locale);
this.parser = new Parser();
}

Expand Down
25 changes: 25 additions & 0 deletions src/modules/sorting/sort-by-views/parsers/es-ES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class EsViewsParser {
/** @param {Element} videoInfo */
parse(videoInfo) {
const viewsElement = videoInfo.firstElementChild;
const [value, unit] = viewsElement.textContent
.trim()
.toLowerCase()
.replaceAll(/\s/g, " ")
.split(" ");

const baseViews = parseFloat(value.replace(",", "."));

if (isNaN(baseViews)) {
return 0;
}

if (unit === "k") {
return Math.round(baseViews * 1000);
} else if (unit === "m") {
return Math.round(baseViews * 1_000_000);
} else {
return Math.round(baseViews);
}
}
}
20 changes: 20 additions & 0 deletions src/modules/sorting/sort-by-views/parsers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EnViewsParser } from "./en";
import { EsViewsParser } from "./es-ES";
import { ZhHansCnViewsParser } from "./zh-Hans-CN";

const VIEWS_PARSERS_BY_LOCALE = {
"en": EnViewsParser,
"en-GB": EnViewsParser,
"en-US": EnViewsParser,
"es-ES": EsViewsParser,
"es-419": EsViewsParser,
"es-US": EsViewsParser,
"zh-Hans-CN": ZhHansCnViewsParser
};

/** @param {string} locale */
export const getViewsParser = (locale) => {
return VIEWS_PARSERS_BY_LOCALE[locale] ?? EnViewsParser;
};

export const getSupportedLocales = () => Object.keys(VIEWS_PARSERS_BY_LOCALE);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class ZhCnViewsParser {
export class ZhHansCnViewsParser {
/** @param {Element} videoInfo */
parse(videoInfo) {
const viewsElement = videoInfo.firstElementChild;
Expand Down

0 comments on commit 2b01808

Please sign in to comment.