A collection of utility functions
Methods
-
getUniqueChars(string)
-
Returns an array of all the unique characters in a string. Works with both UTF-8 and UTF-16 encoding.
Parameters:
Name Type Description string
String The string to retrieve unique characters from Returns:
Returns an array of the unique characters- Type
- Array
-
sanitize(string, substitutions)
-
Performs a series of substitutions on a string, replacing unwanted characters with the desired ones (or nothing, meaning the character is simply removed everywhere it occurs). NB: This function cannot properly handle cases where the output of one substitution is also the input of another subsitition (e.g. if you pass it both `ch` => `c` and `c` => `k`).
Parameters:
Name Type Description string
String The string to sanitize substitutions
Object An object whose attributes are the characters you wish to replace, and values are the characters you wish to replace them with Returns:
Returns the sanitized string, with substitutions made- Type
- String
-
tokenize(string, delimiters, punctuation)
-
Removes punctuation from a string, and then tokenizes the string, returning an array of tokens.
Parameters:
Name Type Description string
String The string to tokenize delimiters
Array An array of characters to tokenize the string with punctuation
Array An array of punctuation characters to remove from the string Returns:
Returns an array of tokens- Type
- Array
-
transliterate(string, substitutions)
-
Converts a string from one writing system to another.
Parameters:
Name Type Description string
String The string to transliterate. substitutions
Object A hash of substitutions to make on the string. Each key in the object should be a string to replace, and its value should be the string to replace it with. Returns:
Returns the transliterated string- Type
- String
-
<inner> checkProp(item, type, prop)
-
Checks whether an item is valid for the specified type, and returns a generic error message with the provided property name if not.
Parameters:
Name Type Description item
Any The item to check type
String The type to check for prop
String The name of the property to use in the error message Returns:
Returns the original value if no error is thrown- Type
- Any
-
<inner> createElement(type, props)
-
Creates a new DOM element, and sets its properties
Parameters:
Name Type Description type
String The type of element to create (e.g. 'div', 'p') props
Object An object of properties and their values to assign to the new element Returns:
Returns the new DOM Node- Type
- Object
-
<inner> isAbbr(string)
-
Tests whether a string is a valid abbreviation according to the DLx Abbreviation schema.
Parameters:
Name Type Description string
String The string to test Returns:
Returns true if the string is a valid abbreviation, false otherwise- Type
- Boolean
-
<inner> isDateString(string)
-
Tests whether a string is a valid ISO Internet Date-Time string.
Parameters:
Name Type Description string
String The string to test Returns:
Returns true if the string is a valid date- Type
- Boolean
-
<inner> isUrl(string)
-
Tests whether a string is a valid URI. Code taken from http://stackoverflow.com/questions/8667070/javascript-regular-expression-to-validate-url/15855457#15855457
Parameters:
Name Type Description string
String The string to validate Returns:
Returns true if the string is a valid URI, false otherwise- Type
- Boolean
-
<inner> toDOM(html)
-
Takes an HTML string and converts it to DOM nodes
Parameters:
Name Type Description html
String an HTML string Returns:
returns an array of DOM nodes- Type
- Array