Skip to content

Commit

Permalink
Merge pull request #66 from EveripediaNetwork/fix-simplified-the-file…
Browse files Browse the repository at this point in the history
…-structure-and-exports

Simplified the file structure and exports
  • Loading branch information
Royal-lobster authored Oct 1, 2024
2 parents d3e98d0 + 589e5ea commit 7afba29
Show file tree
Hide file tree
Showing 12 changed files with 3,005 additions and 203 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-timers-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@everipedia/iq-utils': major
---

Simplifies the file structure and variable and method function names from exports
2,863 changes: 2,863 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ export const IDEAL_MEDIA_COUNT = 5;
export const IDEAL_TAGS_COUNT = 3;
export const IDEAL_SUMMARY_LENGTH = 100;
export const IDEAL_SOCIAL_MEDIA_COUNT = 4;

export const WHITELISTED_DOMAINS = [
"youtube.com/watch",
"youtu.be",
"vimeo.com",
"alpha.everipedia.org/wiki",
"beta.everipedia.org/wiki",
"iq.wiki/wiki",
"ipfs.everipedia.org/ipfs",
];

export const WHITELISTED_LINK_NAMES = ["YOUTUBE@VID", "DUNE@EMBED"];
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from "./lib/wikiScore";
export * from "./lib/isDeepEqual";
export * from "./lib/checkWikiValidity";
export * from "./types/wiki";
export * from "./types/wikiBuilder";
export * from "./lib/wiki-score";
export * from "./lib/check-deep-equal";
export * from "./lib/check-wiki-validity";
export * from "./data/constants";
export * from "./schema/wiki.schema"
export * from "./schema";
File renamed without changes.
35 changes: 14 additions & 21 deletions src/lib/checkWikiValidity.ts → src/lib/check-wiki-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import {
WIKI_CONTENT_MIN_WORDS,
WIKI_SUMMARY_MAX_LENGTH,
WIKI_TITLE_MAX_LENGTH,
WHITELISTED_DOMAINS,
WHITELISTED_LINK_NAMES,
} from "../data/constants";
import {
CommonMetaIdsEnum,
MediaSourceEnum,
MediaTypeEnum,
type Wiki,
} from "../schema/wiki.schema";
import { whiteListedDomains, whiteListedLinkNames } from "../types/wiki";
import { CommonMetaIds, MediaSource, MediaType, type Wiki } from "../schema";

/**
* Counts the number of words in a given string.
Expand Down Expand Up @@ -50,15 +46,15 @@ export const areContentLinksVerified = (content: string) => {
if (
linkText &&
linkUrl &&
whiteListedLinkNames.includes(linkText) &&
WHITELISTED_LINK_NAMES.includes(linkText) &&
!isValidUrl(linkUrl)
) {
return true;
}

if (linkUrl && !linkUrl.startsWith("#")) {
const validDomainPattern = new RegExp(
`^https?://(www\\.)?(${whiteListedDomains.join("|")})`,
`^https?://(www\\.)?(${WHITELISTED_DOMAINS.join("|")})`,
);
return validDomainPattern.test(linkUrl);
}
Expand All @@ -77,13 +73,13 @@ export const isMediaValid = (wiki: Wiki) => {

const isMediaContentValid = wiki.media.every((media) => {
if (
media.source === MediaSourceEnum.Enum.IPFS_IMG ||
media.source === MediaSourceEnum.Enum.IPFS_VID
media.source === MediaSource.Enum.IPFS_IMG ||
media.source === MediaSource.Enum.IPFS_VID
) {
return media.id.length === IPFS_HASH_LENGTH;
}

if (media.source === MediaSourceEnum.Enum.YOUTUBE) {
if (media.source === MediaSource.Enum.YOUTUBE) {
const youtubePattern =
/^.*(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/|watch\?v=)([^#&?]*).*/;
return (
Expand All @@ -92,17 +88,15 @@ export const isMediaValid = (wiki: Wiki) => {
);
}

if (media.source === MediaSourceEnum.Enum.VIMEO) {
if (media.source === MediaSource.Enum.VIMEO) {
return media.id === `https://vimeo.com/${media.name}`;
}

return media.type
? Object.values(MediaTypeEnum).includes(media.type)
: true;
return media.type ? MediaType.options.includes(media.type) : true;
});

const iconMediaCount = wiki.media.filter(
(media) => media.type === MediaTypeEnum.Enum.ICON,
(media) => media.type === MediaType.Enum.ICON,
).length;

return (
Expand All @@ -129,9 +123,8 @@ export const isAnyMediaUploading = (wiki: Wiki) =>
export const isEventUrlMissing = (wiki: Wiki) => {
if (wiki.tags.some((tag) => tag.id === "Events")) {
const referencesData =
wiki.metadata.find(
(meta) => meta.id === CommonMetaIdsEnum.Enum.references,
)?.value || "[]";
wiki.metadata.find((meta) => meta.id === CommonMetaIds.Enum.references)
?.value || "[]";
const references = JSON.parse(referencesData);
return !references.some(
(item: { description: string }) =>
Expand Down Expand Up @@ -164,7 +157,7 @@ export const isSummaryTooLong = (wiki: Wiki) =>
*/
export const hasNoCitations = (wiki: Wiki) => {
const references = wiki.metadata.find(
(meta) => meta.id === CommonMetaIdsEnum.Enum.references,
(meta) => meta.id === CommonMetaIds.Enum.references,
);
return !references?.value || references.value.length === 0;
};
Expand Down
58 changes: 38 additions & 20 deletions src/lib/helpers/wiki.helpers.ts → src/lib/wiki-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import type { z } from "zod";

import { IPFS_HASH_LENGTH, MAX_MEDIA_COUNT } from "../../data/constants";
import {
CommonMetaIdsEnum,
IPFS_HASH_LENGTH,
MAX_MEDIA_COUNT,
WHITELISTED_DOMAINS,
WHITELISTED_LINK_NAMES,
} from "../data/constants";
import {
CommonMetaIds,
type Media,
MediaSourceEnum,
MediaTypeEnum,
type TagEnum,
} from "../../schema/wiki.schema";
import { whiteListedDomains, whiteListedLinkNames } from "../../types/wiki";
MediaSource,
MediaType,
type Tag,
} from "../schema";

/**
* Counts the number of words in a given string.
* @param text - The input string to count words from.
* @returns The number of words in the string.
*/

export const countWords = (text: string) =>
text.split(" ").filter((word) => word !== "").length;

Expand All @@ -31,16 +35,21 @@ export const isValidUrl = (urlString: string) => {
}
};

/**
* Validates the media content in a wiki, ensuring that the media items have valid IDs and sources.
* @param media - The array of media items to validate.
* @returns True if all media items have valid content, false otherwise.
*/
export const validateMediaContent = (media: Media[]) => {
return media.every((item) => {
if (
item.source === MediaSourceEnum.Enum.IPFS_IMG ||
item.source === MediaSourceEnum.Enum.IPFS_VID
item.source === MediaSource.Enum.IPFS_IMG ||
item.source === MediaSource.Enum.IPFS_VID
) {
return item.id.length === IPFS_HASH_LENGTH;
}

if (item.source === MediaSourceEnum.Enum.YOUTUBE) {
if (item.source === MediaSource.Enum.YOUTUBE) {
const youtubePattern =
/^.*(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/|watch\?v=)([^#&?]*).*/;
return (
Expand All @@ -49,31 +58,40 @@ export const validateMediaContent = (media: Media[]) => {
);
}

if (item.source === MediaSourceEnum.Enum.VIMEO) {
if (item.source === MediaSource.Enum.VIMEO) {
return item.id === `https://vimeo.com/${item.name}`;
}

return item.type ? item.type in MediaTypeEnum : true;
return item.type ? item.type in MediaType : true;
});
};

/**
* Validates the count of media items in a wiki, ensuring that the total count does not exceed a maximum limit and that there is at most one icon media item.
* @param media - The array of media items to validate.
* @returns True if the media count is valid, false otherwise.
*/
export const validateMediaCount = (media: Media[]) => {
const iconMediaCount = media.filter(
(item) => item.type === MediaTypeEnum.Enum.ICON,
(item) => item.type === MediaType.Enum.ICON,
).length;
return media.length <= MAX_MEDIA_COUNT && iconMediaCount <= 1;
};

/**
* Validates the event wiki data, ensuring that if the wiki has a "Events" tag, it also has a reference with the description "Event Link" and at least one event.
* @param wiki - The wiki data to validate.
* @returns True if the event wiki data is valid, false otherwise.
*/
export const validateEventWiki = (wiki: {
tags: { id: z.infer<typeof TagEnum> }[];
tags: { id: z.infer<typeof Tag> }[];
metadata: { id: string; value?: string }[];
events?: unknown[];
}): boolean => {
if (wiki.tags.some((tag) => tag.id === "Events")) {
const referencesData =
wiki.metadata.find(
(meta) => meta.id === CommonMetaIdsEnum.Enum.references,
)?.value || "[]";
wiki.metadata.find((meta) => meta.id === CommonMetaIds.Enum.references)
?.value || "[]";
const references: { description: string }[] = JSON.parse(
referencesData,
) as { description: string }[];
Expand All @@ -100,15 +118,15 @@ export const areContentLinksVerified = (content: string) => {
if (
linkText &&
linkUrl &&
whiteListedLinkNames.includes(linkText) &&
WHITELISTED_LINK_NAMES.includes(linkText) &&
!isValidUrl(linkUrl)
) {
return true;
}

if (linkUrl && !linkUrl.startsWith("#")) {
const validDomainPattern = new RegExp(
`^https?://(www\\.)?(${whiteListedDomains.join("|")})`,
`^https?://(www\\.)?(${WHITELISTED_DOMAINS.join("|")})`,
);
return validDomainPattern.test(linkUrl);
}
Expand Down
12 changes: 3 additions & 9 deletions src/lib/wikiScore.ts → src/lib/wiki-score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import {
SUMMARY_SCORE_WEIGHT,
TAGS_SCORE_WEIGHT,
} from "../data/constants";
import {
CommonMetaIdsEnum,
type Wiki,
WikiPossibleSocialsList,
} from "../schema/wiki.schema";
import { CommonMetaIds, type Wiki } from "../schema";

const contentQuality = (wordCount: number): number => {
const scoreMin = 0.0;
Expand Down Expand Up @@ -114,7 +110,7 @@ const getWikiInternalLinks = (content: string): number => {

const getWikiCitationLinks = (wiki: Wiki) => {
const rawWikiReferences = wiki.metadata.find(
(m) => m.id === CommonMetaIdsEnum.Enum.references,
(m) => m.id === CommonMetaIds.Enum.references,
)?.value;

if (
Expand All @@ -133,9 +129,7 @@ const getSocialsCount = (wiki: Wiki): number => {
let socialsCount = 0;
for (const meta of wiki.metadata) {
if (
WikiPossibleSocialsList.includes(
meta.id as keyof typeof CommonMetaIdsEnum.enum,
)
CommonMetaIds.options.includes(meta.id as keyof typeof CommonMetaIds.enum)
) {
if (meta.value) {
socialsCount += 1;
Expand Down
Loading

0 comments on commit 7afba29

Please sign in to comment.