Text

The Text protocol:

protocol Text {
  func countLines(_ string: String) -> Int
 
  func countCharacters(_ string: String) -> Int
 
  func countWords(_ string: String) -> Int
 
  func capitalize(_ string: String) -> String
 
  func lowercase(_ string: String) -> String
 
  func uppercase(_ string: String) -> String
 
  func getUUID() -> String
 
  func isLink(_ string: String) -> Bool
 
  func findLinks(_ string: String) -> [String]
 
  func URLEncode(_ string: String) -> String
 
  func URLDecode(_ string: String) -> String
 
  func encodeAsBase64(_ string: String) -> String
 
  func decodeFromBase64(_ string: String) -> String?
}

$text.countLines

const lines = $text.countLines('hello world');
$alert.info('Text Stats', `Lines: ${lines}`); // Lines: 1

Count lines in string.

$text.countCharacters

const chars = $text.countCharacters('hello world');
$alert.info('Text Stats', `Characters: ${chars}`); // Characters: 11

Count characters in string.

$text.countWords

const words = $text.countWords('hello world');
$alert.info('Text Stats', `Words: ${words}`); // Words: 2

Count words in string.

$text.capitalize

const capitalized = $text.capitalize('hello world'); // Hello World

Capitalize string.

$text.lowercase

const lowercased = $text.lowercase('HELLO WORLD'); // hello world

Convert string to lowercase.

$text.uppercase

const uppercased = $text.uppercase('hello world'); // HELLO WORLD

Convert string to uppercase.

$text.getUUID

const uuid = $text.getUUID(); // 21889372-7DCD-41C1-9E21-05B4EA26E25F

Generate a UUID string.

$text.isLink

const isLink = $text.isLink('https://okjson.aoo'); // true

Test if string is a link.

$text.findLinks

const links = $text.findLinks('Visit our website at https://okjson.app'); // ["https://okjson.app"]

Find links in string with Regular Expression.

$text.URLEncode

URL encode string

const encoded = $text.URLEncode('OK JSON is a fine app.'); // OK%20JSON%20is%20a%20fine%20app%2E

$text.URLDecode

URL decode string.

const decoded = $text.URLDecode('OK%20JSON%20is%20a%20fine%20app%2E'); // OK JSON is a fine app.

$text.encodeAsBase64

Encode input as Base64 string.

const encoded = $text.encodeAsBase64('OK JSON'); // T0sgSlNPTg==

$text.decodeFromBase64

Decode input from Base64 string. The output will be undefined if the string cannot be decoded.

const decoded = $text.decodeFromBase64('T0sgSlNPTg=='); // OK JSON