This is the Swift implementation of the twitter-text parsing library. The library has methods to parse Tweets and calculate length, validity, parse @mentions, #hashtags, URLs, and more.
Add twitter-text directly to your Package.swift file:
dependencies: [ // ... .package(url: "https://github.com/nysander/twitter-text.git", from:"0.0.1"), ], targets: [ .target(name: "Your App", dependencies: [ /// ... .product(name: "twitter-text", package: "twitter-text"), ]),
or via Xcode:
To run the Conformance test suite from the command line:
% swift test --enable-test-discovery
You can also run the tests from within Xcode itself. Open the project file and run the tests are you normally would (Cmd-U).
twitter-text 2.0 introduces configuration files that define how Tweets are parsed for length. This allows for backwards compatibility and flexibility going forward. Old-style traditional 140-character parsing is defined by the v1.json configuration file, whereas v2.json is updated for "weighted" Tweets where ranges of Unicode code points can have independent weights aside from the default weight. The sum of all code points, each weighted appropriately, should not exceed the max weighted length.
Some old methods from twitter-text 1.0 have been marked deprecated,
such as the various
+tweetLength: methods. The new API is based on the
- TwitterTextParser parseTweet(text:)
This method takes a string as input and returns a results object that
contains information about the string.
weightedLength: Int: the overall length of the tweet with code points
weighted per the ranges defined in the configuration file.
permillage: Int: indicates the proportion (per thousand) of the weighted
length in comparison to the max weighted length. A value > 1000
indicates input text that is longer than the allowable maximum.
isValid: Bool: indicates if input text length corresponds to a valid
displayTextRange: NSRange: An array of two unicode code point
indices identifying the inclusive start and exclusive end of the
displayable content of the Tweet. For more information, see
the description of
validDisplayTextRange: NSRange: An array of two unicode code point
indices identifying the inclusive start and exclusive end of the valid
content of the Tweet. For more information on the extended Tweet
payload see Tweet updates
Have a bug? Please create an issue here on GitHub!
Version 3.1.0 was used as reference. All test case JSON files used in this library are directly copied from said repository to keep consistency and being sure that results are the same.
Copyright 2020 Paweł Madej, and other contributors
Licensed under the MIT License