README
pocketwrench
An eclectic collection of JavaScript utilities
Module Overview
Pocketwrench is a collection of JavaScript utility functions that I've found useful over the years. The collection is pretty eclectic, and reflects the various problems I've needed to solve on a bunch of different projects. The functions are split into different namespaces dependent on what they cover. For example, there is an 'array' namespace containing all the functions that operate on arrays. I've not attached any new functions to the built in object prototypes because the functions are often only applicable in certain circumstances.
The namespaces are:
- array - Utility functions that operate on arrays of objects.
- browser - Functions to aid interaction with the browser.
- math - Mathematical functions.
- object - Utility functions that operate on objects.
Usage
array Namespace
findFirstByField(array, fieldName, valueToSearchFor)
Find the first occurrence of an object with key fieldName and value valueToSearchFor.
var pocketwrench = require('pocketwrench');
var testCollection = [
{name: 'Fred', age: 25},
{name: 'Bob', age: 40},
{name: 'Sally', age: 42},
{name: 'Alice', age: 34},
{name: 'Jane', age: 25}
];
pocketwrench.array.findFirstByField(testCollection, 'name', 'Sally');
==> {name: 'Sally', age: 42}
findByField(array, fieldName, valueToSearchFor)
Find all the objects with a key fieldName that have a value valueToSearchFor.
pocketwrench.array.findFirstByField(testCollection, 'age', 25);
==> [{name: 'Fred', age: 25}, {name: 'Jane', age: 25}]
findFirstIndexByField(array, fieldName, valueToSearchFor)
Find the index of the first occurrence of an object with key fieldName and value valueToSearchFor.
pocketwrench.array.findFirstIndexByField(testCollection, 'name', 'Sally');
==> 2
sortByField(array, fieldName, desc)
Sort an array of objects by attribute fieldName. desc is a boolean indicating the sort direction, true sorts descending, false (or omitting the argument altogeter) sorts ascending.
pocketwrench.array.sortByField(testCollection, 'name', false);
==> [
{name: 'Alice', age: 34},
{name: 'Bob', age: 40},
{name: 'Fred', age: 25},
{name: 'Jane', age: 25},
{name: 'Sally', age: 42}
];
mostFrequentElement(collection, fieldName)
Find the most frequency occurring value for fieldName.
pocketwrench.array.mostFrequentElement(testCollection, 'age')
==> 25
countOccurrences(collection, fieldName, value)
Count the number of times value occurrs in fieldName.
pocketwrench.array.countOccurrences(testCollection, 'age', 25)
==> 2
uniqueValuesByField(collection, fieldName)
Return a list of unique values in fieldName.
pocketwrench.array.uniqueValuesByField(testCollection, 'age')
==> [25, 40, 42, 34]
uniqueValues(collection)
Return a list of unique values in a simple array.
pocketwrench.array.uniqueValues([1, 2, 1, 3, 2, 4, 2, 5, 1])
==> [1, 2, 3, 4, 5]
browser Namespace
getQuerystringValues()
Gets the query string as an object.
pocketwrench.browser.getQuerystringValues()
==> {
'key1' : 'value1',
'key2' : 'value2',
'key3' : 'value3'
}
getQuerystringValue(keyName)
Get the value from the query string that corresponds to keyName.
pocketwrench.browser.getQuerystringValue('key1')
==> 'value1'
math Namespace
logx(number, base)
Return the log of number to the base base.
pocketwrench.math.logx(8, 2)
==> 3
object Namespace
isDictionary(obj)
Returns true if obj is a dictionary, otherwise returns false.
pocketwrench.object.isDictionary([1,2,3])
==> false
pocketwrench.object.isDictionary({ 'a' : 1, 'b' : 2 })
==> true