Files

63 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));
}
});