I've found that it doesn't matter what project I'm working on, there are a few utilities that I always need and end up writing over and over again. Enough of that.
screwdriver.js contains the following utilities:
- startsWith
- endsWith
- format
- isEmailAddress
- isPhoneNumber
- shuffle
- max
- min
- isEmpty
- peek
- search
- isIdenticalTo
- duplicate
- isArray
- isEmpty
- size
- LinkedHashMap
Determines whether the string begins with the given substring.
str - The substring to search for.
bool - Whether or not the string starts with str.
'screwdriver'.startsWith('screw'); // true
Determines whether the string ends with the given substring.
str - The substring to search for.
bool - Whether or not the string ends with str.
'screwdriver'.endsWith('driver'); // true
Replaces placeholder strings with the arguments passed in. Placeholders are of the format {i} with i being the index of the argument with which to replace it.
args - Any number of string arguments to replace the corresponding placeholders.
string - The formatted string with all of the placeholders filled in with values.
'{0} out of {1}'.format(8, 10); // '8 out of 10'
Validates that the string is a properly formatted email address.
bool - Whether or not the string is a valid email address
'[email protected]'.isEmailAddress(); // true
Validates that the string is a properly formatted phone number.
bool - Whether or not the string is a valid phone number
'555 555 5555'.isPhoneNumber(); // true '(555) 555-5555'.isPhoneNumber(); // true '555-555-5555'.isPhoneNumber(); // true
Randomly shuffles the contents of the array in place.
array - The shuffled array. Note: the array is shuffled in place.
[1, 2, 3, 4].shuffle(); // [2, 4, 3, 1]
Gets the max value from the array.
object - The max value of the array.
[2, 4, 1, 3].max(); // 4
Gets the min value from the array.
object - The min value of the array.
[2, 4, 1, 3].min(); // 1
Determines whether the array has any elements.
bool - Whether or not the array has any elements.
[].isEmpty(); // true
Returns the element at the top of the stack without removing it
object - Element at the top of the stack. Null if the stack is empty.
[1, 2, 3, 4].peek(); // 4
Finds the 1-based index of the object in the stack.
int - 1-based index of the object in the stack calculated from the top of the stack. -1 if the item is not found in the stack.
[1, 2, 1, 3].search(3); // 1 [1, 2, 1, 3].search(1); // 2 [1, 2, 1, 3].search(0); // -1
Determines if the object is identical to obj
obj - The compare object.
bool - Whether or not the two objects have identical values. This is a recursive check.
{ a: 1 }.isIdenticalTo({ a: 1 }); // true
Creates a new object with identical recursive properties
object - A new object with all of the property values copied. The object is completely duplicated in memory.
var a = { a: 1 }.duplicate(); a.isIdenticalTo({ a: 1 }); // true a === { a: 1 }; // false
Determines whether the object has any children.
bool - Whether or not the object has any elements.
{}.isEmpty(); // true
Determines the number of children the object has
int - The number of children in the object
{ a: 1, b: 2 }.size(); // 2
Determines if the object is of type array
bool - Whether or not the object is an array
[].isArray(); // true {}.isArray(); // false
An ordered HashMap.
Empties the entire hashmap
lhm.put('key', 'value'); lhm.size(); // 1 lhm.clear(); lhm.size(); // 0
Gets the number of elements in the hashmap
int - The number of elements in the hashmap
lhm.size(); // 0 lhm.put('key', 'value'); lhm.size(); // 1
Determines if there are zero elements in the hashmap
bool - Whether or not there are zero elements in the hashmap
lhm.isEmpty(); // true lhm.put('key', 'value'); lhm.isEmpty(); // false lhm.clear(); lhm.isEmpty(); // true
Adds a key and value pair to the hashmap. The elements in the hashmap are stored in the order they were added.
key - key with which the specified value is to be associated value - value to be associated with the specified key
object - the previous value associated with key or null if there was no mapping for key
lhm.put('key', 'value'); // null lhm.get('key'); // 'value' lhm.put('key', 'value2'); // 'value' lhm.size(); // 1
Removes the value associated with this key.
key - key for which to remove the associated value
object - the value that just got removed or null if there was no mapping for the key
lhm.put('key', 'value'); lhm.get('key'); // 'value' lhm.get('key2'); // null
Gets the value associate with this key, or null if there is no mapping for the key.
key - key for which to search for an associated value
object - the value associated with key or null if there was no mapping for key
lhm.put('key', 'value'); lhm.get('key'); // 'value' lhm.get('key2'); // null
Determines if there is a value mapping for this key in the hashmap.
key - key to search for
bool - whether or not there is a value mapping for this key
lhm.containsKey('key'); // false lhm.put('key', 'value'); lhm.containsKey('key'); // true lhm.containsKey('key2'); // false
Retrieves the first value added to the hashmap
object - the first value added to the hashmap or null if the hashmap is empty
lhm.peekFirst(); // null lhm.put('key', 'value'); lhm.peekFirst(); // 'value' lhm.put('key2', 'value2'); lhm.peekFirst(); // 'value' lhm.remove('key'); lhm.peekFirst(); // 'value2'
Retrieves the last value added to the hashmap
object - the last value added to the hashmap or null if the hashmap is empty
lhm.peekLast(); // null lhm.put('key', 'value'); lhm.peekLast(); // 'value' lhm.put('key2', 'value2'); lhm.peekLast(); // 'value2' lhm.remove('key2'); lhm.peekLast(); // 'value'
Retrieves an ordered list of the added values
array - the ordered list of the added values
lhm.values(); // [] lhm.put('key', 'value'); lhm.put('key2', 'value2'); lhm.put('key3', 'value3'); lhm.values(); // ['value', 'value2', 'value3']