Skip to content

Commit

Permalink
feat: set last number as episode
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Apr 15, 2023
1 parent 75acada commit 3239077
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
30 changes: 30 additions & 0 deletions packages/anitomy/src/parser/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ParserContext } from './context';
import { isMatchTokenCategory } from './utils';

import { TokenCategory, TokenFlag, findPrevToken } from '../token';
import { setEpisodeNumber } from './episode';

export const AnimeYearMin = 1900;
export const AnimeYearMax = 2100;
Expand Down Expand Up @@ -31,6 +35,32 @@ export function searchForIsolatedEpisodeNumber(context: ParserContext, tokens: n
}

export function searchForLastNumber(context: ParserContext, tokens: number[]) {
for (const it of tokens) {
// Assuming that episode number always comes after the title,
// the first token cannot be what we're looking for
if (it === 0) continue;
if (context.tokens[it].enclosed) continue;

// Ignore if it's the first non-enclosed, non-delimiter token
if (
context.tokens.slice(0, it).every((t) => t.enclosed || t.category === TokenCategory.Delimiter)
) {
continue;
}

const prevToken = findPrevToken(context.tokens, it, TokenFlag.NotDelimiter);
if (isMatchTokenCategory(TokenCategory.Unknown, context.tokens[prevToken])) {
const prevContent = context.tokens[prevToken].content;
if (prevContent === 'Movie' || prevContent === 'Part') {
continue;
}
}

// We'll use this number after all
if (setEpisodeNumber(context, context.tokens[it].content, context.tokens[it], true)) {
return true;
}
}
return false;
}

Expand Down
52 changes: 26 additions & 26 deletions packages/anitomy/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`parse > should work 1`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -72,7 +72,7 @@ exports[`parse > should work 3`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 10,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -105,7 +105,7 @@ exports[`parse > should work 4`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -138,7 +138,7 @@ exports[`parse > should work 5`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -171,7 +171,7 @@ exports[`parse > should work 6`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -204,7 +204,7 @@ exports[`parse > should work 7`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -270,7 +270,7 @@ exports[`parse > should work 9`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -303,7 +303,7 @@ exports[`parse > should work 10`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -336,7 +336,7 @@ exports[`parse > should work 11`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -369,7 +369,7 @@ exports[`parse > should work 12`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -402,7 +402,7 @@ exports[`parse > should work 13`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -435,7 +435,7 @@ exports[`parse > should work 14`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 12,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -468,7 +468,7 @@ exports[`parse > should work 15`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 12,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -501,7 +501,7 @@ exports[`parse > should work 16`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -534,7 +534,7 @@ exports[`parse > should work 17`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -567,7 +567,7 @@ exports[`parse > should work 18`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -600,7 +600,7 @@ exports[`parse > should work 19`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -633,7 +633,7 @@ exports[`parse > should work 20`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -666,7 +666,7 @@ exports[`parse > should work 21`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 27,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -699,7 +699,7 @@ exports[`parse > should work 22`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -732,7 +732,7 @@ exports[`parse > should work 23`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 13,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -765,7 +765,7 @@ exports[`parse > should work 24`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -798,7 +798,7 @@ exports[`parse > should work 25`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 1,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -831,7 +831,7 @@ exports[`parse > should work 26`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -864,7 +864,7 @@ exports[`parse > should work 27`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 2,
"numberAlt": undefined,
"title": undefined,
},
Expand Down Expand Up @@ -897,7 +897,7 @@ exports[`parse > should work 28`] = `
"term": "AAC",
},
"episode": {
"number": undefined,
"number": 13,
"numberAlt": undefined,
"title": undefined,
},
Expand Down

0 comments on commit 3239077

Please sign in to comment.