Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/common/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ export const stripToInt = (string: string): number | null => {
return +string.replace(/[^0-9]/g, "");
};

/**
* Will try to parse shorthand numbers like:
* 100K, 100M, 100B numbers
*
* Otherwise simply parses string to int
*/
export const parseNumberRepresentation = (input: string | number | null) => {
const units = { K: 1000, M: 1000000, B: 1000000000 };
const regex = /(\d+(?:\.\d+)?)([KMB])/i;
if (typeof input === "number") return input;
if (!input) return null;
let parsed = stripToInt(input);

const match = input.toUpperCase().match(regex);
if (match && match.length >= 2) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, value, unit] = match;
const number = parseFloat(value);
const factor = units[unit as keyof typeof units];
parsed = number * factor;
}

return parsed;
};

export const getContinuationFromItems = (
items: YoutubeRawData,
accessors: string[] = ["continuationEndpoint"]
Expand Down
6 changes: 4 additions & 2 deletions src/youtube/VideoCompact/VideoCompactParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDuration, stripToInt, Thumbnails, YoutubeRawData } from "../../common";
import { getDuration, parseNumberRepresentation, Thumbnails, YoutubeRawData } from "../../common";
import { BaseChannel } from "../BaseChannel";
import { VideoCompact } from "./VideoCompact";

Expand Down Expand Up @@ -62,7 +62,9 @@ export class VideoCompactParser {
}
}

target.viewCount = stripToInt(viewCountText?.simpleText || viewCountText?.runs[0].text);
target.viewCount = parseNumberRepresentation(
viewCountText?.simpleText || viewCountText?.runs[0].text
);

return target;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/common/utils/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "jest-extended";

import { parseNumberRepresentation } from "../../../src/common/utils/helper";

describe("parseNumberRepresentation", () => {
it("should convert shorthand numbers", () => {
expect(parseNumberRepresentation("100K views")).toBe(100000);
expect(parseNumberRepresentation("2.3k views")).toBe(2300);
expect(parseNumberRepresentation("1.2m views")).toBe(1200000);
expect(parseNumberRepresentation("1B views")).toBe(1000000000);
expect(parseNumberRepresentation("256")).toBe(256);
});

it("should convert full numbers", () => {
expect(parseNumberRepresentation("120,000 views")).toBe(120000);
expect(parseNumberRepresentation("120500 views")).toBe(120500);
expect(parseNumberRepresentation("123456")).toBe(123456);
expect(parseNumberRepresentation("256,000,1235")).toBe(2560001235);
});
});