2022-12-17 20:19:42 +00:00
|
|
|
package funkin.util.tools;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A static extension which provides utility functions for Strings.
|
|
|
|
*/
|
|
|
|
class StringTools
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts a string to title case. For example, "hello world" becomes "Hello World".
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param value The string to convert.
|
|
|
|
* @return The converted string.
|
|
|
|
*/
|
|
|
|
public static function toTitleCase(value:String):String
|
|
|
|
{
|
2024-03-13 01:34:50 +00:00
|
|
|
var words:Array<String> = value.split(' ');
|
|
|
|
var result:String = '';
|
2023-01-23 03:25:45 +00:00
|
|
|
for (i in 0...words.length)
|
|
|
|
{
|
|
|
|
var word:String = words[i];
|
|
|
|
result += word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
|
|
|
|
if (i < words.length - 1)
|
|
|
|
{
|
2024-03-13 01:34:50 +00:00
|
|
|
result += ' ';
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2022-12-17 20:19:42 +00:00
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
2022-12-17 20:19:42 +00:00
|
|
|
|
2024-03-12 03:42:32 +00:00
|
|
|
/**
|
|
|
|
* Strip a given prefix from a string.
|
|
|
|
* @param value The string to strip.
|
|
|
|
* @param prefix The prefix to strip. If the prefix isn't found, the original string is returned.
|
|
|
|
* @return The stripped string.
|
|
|
|
*/
|
|
|
|
public static function stripPrefix(value:String, prefix:String):String
|
|
|
|
{
|
|
|
|
if (value.startsWith(prefix))
|
|
|
|
{
|
|
|
|
return value.substr(prefix.length);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip a given suffix from a string.
|
|
|
|
* @param value The string to strip.
|
|
|
|
* @param suffix The suffix to strip. If the suffix isn't found, the original string is returned.
|
|
|
|
* @return The stripped string.
|
|
|
|
*/
|
|
|
|
public static function stripSuffix(value:String, suffix:String):String
|
|
|
|
{
|
|
|
|
if (value.endsWith(suffix))
|
|
|
|
{
|
|
|
|
return value.substr(0, value.length - suffix.length);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts a string to lower kebab case. For example, "Hello World" becomes "hello-world".
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param value The string to convert.
|
|
|
|
* @return The converted string.
|
|
|
|
*/
|
|
|
|
public static function toLowerKebabCase(value:String):String
|
|
|
|
{
|
2024-03-13 01:34:50 +00:00
|
|
|
return value.toLowerCase().replace(' ', '-');
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2022-12-17 20:19:42 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts a string to upper kebab case, aka screaming kebab case. For example, "Hello World" becomes "HELLO-WORLD".
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param value The string to convert.
|
|
|
|
* @return The converted string.
|
|
|
|
*/
|
|
|
|
public static function toUpperKebabCase(value:String):String
|
|
|
|
{
|
2024-03-13 01:34:50 +00:00
|
|
|
return value.toUpperCase().replace(' ', '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The regular expression to sanitize strings.
|
|
|
|
*/
|
|
|
|
static final SANTIZE_REGEX:EReg = ~/[^-a-zA-Z0-9]/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all instances of symbols other than alpha-numeric characters (and dashes)from a string.
|
|
|
|
* @param value The string to sanitize.
|
|
|
|
* @return The sanitized string.
|
|
|
|
*/
|
|
|
|
public static function sanitize(value:String):String
|
|
|
|
{
|
|
|
|
return SANTIZE_REGEX.replace(value, '');
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the string data as JSON and returns the resulting object.
|
2023-08-22 08:27:30 +00:00
|
|
|
* This is here so you can use `string.parseJSON()` when `using StringTools`.
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2024-03-13 01:34:50 +00:00
|
|
|
* TODO: Remove this and replace with `json2object`
|
|
|
|
* @param value The
|
2023-01-23 03:25:45 +00:00
|
|
|
* @return The parsed object.
|
|
|
|
*/
|
|
|
|
public static function parseJSON(value:String):Dynamic
|
|
|
|
{
|
|
|
|
return SerializerUtil.fromJSON(value);
|
|
|
|
}
|
2022-12-17 20:19:42 +00:00
|
|
|
}
|