-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
64 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const fieldMatchers = [/pro.*nouns?/i, "pronomen"]; | ||
|
||
/** | ||
* Tries to extract the pronouns for the given status. | ||
* This is done by searching for pronoun fields that match the {@see fieldMatchers}. | ||
* | ||
* If found, it sanitizes and returns the value of said field. | ||
* | ||
* @param {any} status | ||
* @returns {string|null} Author pronouns if found. Otherwise returns null. | ||
*/ | ||
export function extractFromStatus(status) { | ||
// get account from status and pull out fields | ||
const account = status.account; | ||
const fields = account.fields; | ||
|
||
let pronouns; | ||
for (const field of fields) { | ||
// TODO: add ranking of fields | ||
if (pronouns) break; | ||
|
||
for (const matcher of fieldMatchers) { | ||
if (typeof matcher === "string" && field.name.toLowerCase().includes(matcher)) { | ||
pronouns = field.value; | ||
} else if (field.name.match(matcher)) { | ||
pronouns = field.value; | ||
} | ||
} | ||
} | ||
if (!pronouns) return null; | ||
return pronouns; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { suite } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
import * as pronouns from "../src/libs/pronouns.js"; | ||
|
||
const extract = suite("field extraction"); | ||
const validFields = [ | ||
"pronoun", | ||
"pronouns", | ||
"PRONOUNS", | ||
"professional nouns", | ||
"pronomen", | ||
"Pronouns / Pronomen", | ||
]; | ||
|
||
for (const field of validFields) { | ||
extract(`${field} is extracted`, () => { | ||
const result = pronouns.extractFromStatus({ | ||
account: { | ||
fields: [{ name: field, value: "pro/nouns" }], | ||
}, | ||
}); | ||
assert.equal("pro/nouns", result); | ||
}); | ||
} | ||
|
||
extract.run(); |