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.
- 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)
npm install @natsuneko-laboratory/react-native-twitter-text
cd ios && pod installnpx 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 prebuildimport { 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 },
// }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 } },
// ]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 nullimport {
isValidTweet,
isValidHashtag,
tweetLength,
} from '@natsuneko-laboratory/react-native-twitter-text';
isValidTweet('Hello!'); // true
isValidTweet(''); // false
isValidHashtag('#ReactNative'); // true
tweetLength('Hello!'); // 6type 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;
}| 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 |
- React Native >= 0.76 (New Architecture / Turbo Modules)
- iOS >= 13.4
- Android minSdk >= 24
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.
- MIT and Apache-2.0 and BSD-2-Clause (see LICENSE file for details)