@natsuneko-laboratory/react-native-twitter-text
v0.1.0
Published
A React Native plugin that provides Twitter's twitter-text parsing library via native Objective-C and Java implementations. Especially fast for CJK text compared to the JavaScript implementation.
Downloads
46
Readme
@natsuneko-laboratory/react-native-twitter-text
A React Native plugin that provides Twitter's twitter-text parsing library via native Objective-C and Java implementations. Especially fast for CJK text compared to the JavaScript implementation.
Features
- Tweet parsing with weighted character count (v3 configuration)
- Entity extraction (URLs, hashtags, @mentions, cashtags, lists)
- Tweet validation
- Synchronous API via React Native Turbo Modules (New Architecture)
Installation
npm install @natsuneko-laboratory/react-native-twitter-text
cd ios && pod installExpo
npx expo install @natsuneko-laboratory/react-native-twitter-textAdd the plugin to your app.json:
{
"expo": {
"plugins": ["@natsuneko-laboratory/react-native-twitter-text"]
}
}Then run prebuild:
npx expo prebuildUsage
Parse a tweet
import { parseTweet } from '@natsuneko-laboratory/react-native-twitter-text';
const result = parseTweet('Hello, world!');
// {
// weightedLength: 13,
// permillage: 46,
// isValid: true,
// displayTextRange: { start: 0, end: 13 },
// validTextRange: { start: 0, end: 13 },
// }Extract entities
import { extractEntities } from '@natsuneko-laboratory/react-native-twitter-text';
const entities = extractEntities(
'@jack Check out #ReactNative https://reactnative.dev'
);
// [
// { type: 'mention', value: '@jack', range: { start: 0, end: 5 } },
// { type: 'hashtag', value: '#ReactNative', range: { start: 16, end: 28 } },
// { type: 'url', value: 'https://reactnative.dev', range: { start: 29, end: 52 } },
// ]Extract specific entity types
import {
extractURLs,
extractHashtags,
extractMentions,
extractMentionsOrLists,
extractCashtags,
extractReplyScreenname,
} from '@natsuneko-laboratory/react-native-twitter-text';
extractURLs('Visit https://example.com');
extractHashtags('Hello #world');
extractMentions('Hi @user!');
extractMentionsOrLists('@user/my-list'); // includes listSlug
extractCashtags('Buy $AAPL');
extractReplyScreenname('@user hello'); // '@user' or nullValidate tweets
import {
isValidTweet,
isValidHashtag,
tweetLength,
} from '@natsuneko-laboratory/react-native-twitter-text';
isValidTweet('Hello!'); // true
isValidTweet(''); // false
isValidHashtag('#ReactNative'); // true
tweetLength('Hello!'); // 6API Reference
Types
type EntityType = 'url' | 'hashtag' | 'mention' | 'listname' | 'cashtag';
interface TextRange {
start: number;
end: number;
}
interface Entity {
type: EntityType;
value: string;
range: TextRange;
listSlug?: string; // present for 'listname' entities
}
interface ParseResults {
weightedLength: number;
permillage: number;
isValid: boolean;
displayTextRange: TextRange;
validTextRange: TextRange;
}Functions
| Function | Return Type | Description |
| ------------------------------ | ---------------- | ------------------------------------------------------------------- |
| parseTweet(text) | ParseResults | Parse a tweet and return weighted length, validity, and text ranges |
| extractEntities(text) | Entity[] | Extract all entities (URLs, hashtags, mentions, cashtags) |
| extractURLs(text) | Entity[] | Extract URLs |
| extractHashtags(text) | Entity[] | Extract hashtags |
| extractMentions(text) | Entity[] | Extract @mentions |
| extractMentionsOrLists(text) | Entity[] | Extract @mentions and list references |
| extractCashtags(text) | Entity[] | Extract cashtags ($SYMBOL) |
| extractReplyScreenname(text) | string \| null | Extract the replied-to screen name |
| isValidTweet(text) | boolean | Check if a tweet is valid |
| isValidHashtag(text) | boolean | Check if text is a valid hashtag |
| tweetLength(text) | number | Get the weighted length of a tweet |
Requirements
- React Native >= 0.76 (New Architecture / Turbo Modules)
- iOS >= 13.4
- Android minSdk >= 24
Contributing
Disclaimer
This project is not affiliated with, endorsed by, or sponsored by Twitter, Inc. (now X Corp.) or any of its subsidiaries. "twitter-text" refers to the open-source library published under the Apache 2.0 license.
License
- MIT and Apache-2.0 and BSD-2-Clause (see LICENSE file for details)
