https://github.com/critocrito/sugarcube-source-types
https://github.com/critocrito/sugarcube-source-types
data data-investigation
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/critocrito/sugarcube-source-types
- Owner: critocrito
- License: gpl-3.0
- Created: 2020-07-16T18:42:39.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-03-05T06:15:10.000Z (over 3 years ago)
- Last Synced: 2025-12-26T19:36:28.961Z (6 months ago)
- Topics: data, data-investigation
- Language: TypeScript
- Homepage: https://sugarcubetools.net
- Size: 987 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: license.txt
Awesome Lists containing this project
README
# Source Types for Sugarcube
> Utility functions to deal with different types of sources for Sugarcube.

[](https://codecov.io/gh/critocrito/sugarcube-source-types)

---
`@sugarcube/source-types` provides utility functions for the [Sugarcube Tools](https://sugarcubetools.net) distribution. The utility functions can parse, normalize and verify terms in respect to the type of the source. This packages is used across various parts of the Sugarcube distribution, such as [Ncube](https://github.com/critocrito/ncube), the [Sugarcube Discovery Extension](https://github.com/critocrito/sugarcube-discovery) and [Sugarcube](https://github.com/critocrito/sugarcube) itself.
## Contents
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [License](#license)
## Installation
```sh
npm install --save @sugarcube/source-types
```
## Usage
```javascript
import {sourceType} from "@sugarcube/source-types";
sourceType("I'm gibberish"); // undefined
sourceType("https://youtube.com/watch?v=wer23edsa"); // youtube_video
```
## API
Map source type for a term
- [`SourceType`: Valid types of sources.](#sourcetype-1)
- [`sourceType`: Determine source type for any term.](#sourcetype-2)
Source type predicates
- [`isTelegramChannel`: Determine if a term is a valid Telegram channel.](#istelegramchannel)
- [`isTwitterTweet`: Determine if a term is a valid Twitter tweet.](#istwittertweet)
- [`isTwitterFeed`: Determine if a term is a valid Twitter feed.](#istwitterfeed)
- [`isYoutubeVideo`: Determine if a term is a valid Youtube video.](#isyoutubevideo)
- [`isYoutubeChannel`: Determine if a term is a valid Youtube channel.](#isyoutubechannel)
- [`isHttpUrl`: Determine if a term is a valid HTTP url.](#ishttpurl)
Source type parsers
- [`parseTelegramChannel`: Extract a Telegram channel from a term.](#parsetelegramchannel)
- [`parseTweetId`: Extract a tweet id from a term.](#parsetweetid)
- [`parseTwitterUser`: Extract a Twitter user from a term.](#parsetwitteruser)
- [`parseYoutubeVideo`: Extract a Youtube video id from a term.](#parseyoutubevideo)
- [`parseYoutubeChannel`: Extract a Youtube channel id from a term.](#parseyoutubechannel)
- [`parseHttpUrl`: Parse a term into a valid HTTP url.](#parsehttpurl)
Normalize terms
- [`normalizeTelegramChannelUrl`: Turn a term into a normalized Telegram Channel
URL.](#normalizetelegramchannelurl)
- [`normalizeTwitterTweetUrl`: Turn a term into a normalized Twitter tweet URL.](#normalizetwittertweeturl)
- [`normalizeTwitterUserUrl`: Turn a term into a normalized Twitter feed URL.](#normalizetwitteruserurl)
- [`normalizeYoutubeVideoUrl`: Turn a term into a normalized Youtube video URL.](#normalizeyoutubevideourl)
- [`normalizeYoutubeChannelUrl`: Turn a term into a normalized Youtube channel URL.](#normalizeyoutubechannelurl)
- [`normalizeHttpUrl`: Turn a term into a normalized HTTP url.](#normalizehttpurl)
### `SourceType`
A string literal type containig valid source types.
- `telegram_channel`
- `twitter_tweet`
- `twitter_channel`
- `youtube_video`
- `youtube_channel`
- `http_url`
### `sourceType`
Detect the type of a source.
```
sourceType :: (term?: string) -> SourceType | undefined
```
### `isTelegramChannel`
Check if a term is a valid Telegram channel.
```
isTelegramChannel :: (term?: string) -> boolean
```
### `isTwitterTweet`
Check if a term is a valid Twitter tweet.
```
isTwitterTweet :: (term?: string) -> boolean
```
### `isTwitterFeed`
Check if a term is a valid Twitter channel.
```
isTwitterFeed :: (term?: string) -> boolean
```
### `isYoutubeVideo`
Check if a term is a valid Youtube video.
```
isYoutubeVideo :: (term?: string) -> boolean
```
### `isYoutubeChannel`
Check if a term is a valid Youtube channel.
```
isYoutubeChannel :: (term?: string) -> boolean
```
### `isHttpUrl`
Check if a term is a valid HTTP url.
```
isHttpUrl :: (term?: string) -> boolean
```
### `parseTelegramChannel`
Extract a Telegram channel from a term.
```
parseTelegramChannel :: (term?: string) -> string | undefined
```
```javascript
parseTelegramChannel("https://t.me/s/soscubamedia"); # soscubamedia
parseTelegramChannel("https://t.me/soscubamedia"); # soscubamedia
```
Channel names in the form of `@soscubamedia` are not valid since they are ambiguous with Twitter user handlers.
### `parseTweetId`
Extract a tweet id from a term.
```
parseTweetId :: (term?: string) -> string | undefined
```
### `parseTwitterUser`
Extract a Twitter user name from a term.
```
parseTwitterUser :: (term?: string) -> string | undefined
```
### `parseYoutubeVideo`
Extract a Youtube video id from a term.
```
parseYoutubeVideo :: (term?: string) -> string | undefined
```
### `parseYoutubeChannel`
Extract a Youtube channel id from a term.
```
parseYoutubeChannel :: (term?: string) -> string | undefined
```
### `parseHttpUrl`
Parse a term and return a HTTP url.
```
parseHttpUrl :: (term?: string) -> string | undefined
```
### `normalizeTelegramChannelUrl`
Parse a Telegram channel from a term and return a normalized Telegram channel URL.
```
normalizeTelegramChannelUrl :: (term?: string) -> string | undefined
```
### `normalizeTwitterTweetUrl`
Parse a Twitter tweet id from a term and return a normalized Twitter tweet URL.
```
normalizeTwitterTweetUrl :: (term?: string) -> string | undefined
```
### `normalizeTwitterUserUrl`
Parse a Twitter user name from a term and return a normalized Twitter feed URL.
```
normalizeTwitterUserUrl :: (term?: string) -> string | undefined
```
### `normalizeYoutubeVideoUrl`
Parse a Youtube Video id from a term and return a normalized Youtube video URL.
```
normalizeYoutubeVideoUrl :: (term?: string) -> string | undefined
```
### `normalizeYoutubeChannelUrl`
Parse a Youtube channel id from a term and return a normalized Youtube channel URL.
```
normalizeYoutubeChannelUrl :: (term?: string) -> string | undefined
```
### `normalizeHttpUrl`
Turn a term into a well formed HTTP url.
```
normalizeHttpUrl :: (term?: string) -> string | undefined
```
## License
[GPL 3.0 licensed](LICENSE)