feat: parse abbreviated counts like "59 млн" and "571 тыс."

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
IgorVolochay
2026-10-25 12:04:55 +03:00
co-authored by Claude Sonnet 5.5
parent 39af9a3111
commit edb450d4e6
2 changed files with 95 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
const UNITS = new Map([
['k', 1e3], ['тыс', 1e3], ['тысяча', 1e3], ['тысячи', 1e3], ['тысяч', 1e3], ['thousand', 1e3],
['m', 1e6], ['млн', 1e6], ['миллион', 1e6], ['миллиона', 1e6], ['миллионов', 1e6], ['million', 1e6],
['b', 1e9], ['млрд', 1e9], ['миллиард', 1e9], ['миллиарда', 1e9], ['миллиардов', 1e9], ['billion', 1e9],
]);
// "просмотров", "подписчиков", "views", "subscribers" and their other forms at the end of the text.
const COUNT_WORDS = /\s*(просмотров|просмотра|просмотр|подписчиков|подписчика|подписчики|подписчик|views?|subscribers?)$/;
const GROUPED_INTEGER = /^\d{1,3}(?:[ ,.]\d{3})+$/;
const DECIMAL = /^\d+(?:[.,]\d+)?$/;
// Reads "59 млн", "571 тыс.", "1.2M", "12 345" and the like; the whole text must be a count.
export function parseCount(text) {
if (typeof text !== 'string') return null;
const normalized = text.replace(/\s+/g, ' ').trim().toLowerCase().replace(COUNT_WORDS, '').trim();
const match = /^(\d[\d ,.]*?)(?: ?([a-zа-яё]+)\.?)?$/.exec(normalized);
if (!match) return null;
const [, number, unit] = match;
if (unit === undefined) {
if (/^\d+$/.test(number)) return Number(number);
return GROUPED_INTEGER.test(number) ? Number(number.replace(/\D/g, '')) : null;
}
const multiplier = UNITS.get(unit);
if (multiplier === undefined || !DECIMAL.test(number)) return null;
return Math.round(Number(number.replace(',', '.')) * multiplier);
}
export function parseViews(text) {
if (typeof text !== 'string') return null;
if (/^\s*(no|нет) (views|просмотров)\s*$/i.test(text)) return 0;
return parseCount(text);
}
+62
View File
@@ -0,0 +1,62 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseCount, parseViews } from '../src/lib/numbers.js';
test('parseCount understands Russian abbreviations', () => {
assert.equal(parseCount('59 млн'), 59000000);
assert.equal(parseCount('59 млн'), 59000000); // non-breaking space, as YouTube renders it
assert.equal(parseCount('571 тыс.'), 571000);
assert.equal(parseCount('1,8 млн'), 1800000);
assert.equal(parseCount('3,4 млрд'), 3400000000);
assert.equal(parseCount('123 тысячи'), 123000);
assert.equal(parseCount('1,8 миллиона'), 1800000);
assert.equal(parseCount('146 миллионов'), 146000000);
});
test('parseCount understands English abbreviations', () => {
assert.equal(parseCount('1.2M'), 1200000);
assert.equal(parseCount('1.2m'), 1200000);
assert.equal(parseCount('123K'), 123000);
assert.equal(parseCount('1.5B'), 1500000000);
assert.equal(parseCount('2.5 million'), 2500000);
});
test('parseCount reads plain and grouped integers', () => {
assert.equal(parseCount('37'), 37);
assert.equal(parseCount('0'), 0);
assert.equal(parseCount('12 345'), 12345);
assert.equal(parseCount('12 345'), 12345);
assert.equal(parseCount('1,234,567'), 1234567);
assert.equal(parseCount('1.234.567'), 1234567);
});
test('parseCount ignores trailing "views" and "subscribers" words', () => {
assert.equal(parseCount('59 миллионов просмотров'), 59000000);
assert.equal(parseCount('1 просмотр'), 1);
assert.equal(parseCount('571 тыс. подписчиков'), 571000);
assert.equal(parseCount('1.2M views'), 1200000);
assert.equal(parseCount('1.2M subscribers'), 1200000);
});
test('parseCount rejects anything that is not wholly a count', () => {
for (const text of ['', 'Wun Wun', '10 л. назад', '2 г. назад', '7 дн. назад', '3 нед. назад', '1,2 тыс. зрителей', '5 Minute Crafts', 'abc', '1.2', '1,5']) {
assert.equal(parseCount(text), null, text);
}
for (const value of [null, undefined, 123, {}]) {
assert.equal(parseCount(value), null, String(value));
}
});
test('parseViews adds the "no views" cases', () => {
assert.equal(parseViews('Нет просмотров'), 0);
assert.equal(parseViews('No views'), 0);
assert.equal(parseViews('59 млн'), 59000000);
assert.equal(parseViews('1,8 млн просмотров'), 1800000);
assert.equal(parseViews('1.2M views'), 1200000);
});
test('parseViews rejects dates and names', () => {
for (const text of ['10 л. назад', 'Wun Wun', '2 years ago', null, undefined]) {
assert.equal(parseViews(text), null, String(text));
}
});