BBOP language extensions to JavaScript, complimenting Underscore.js.
Purpose: Helpful basic utilities and operations to fix common needs in JS.
Classes
Methods
-
<static> chomp(str) → {string}
-
Trim the leading and trailing whitespace from a string. Named differently so as not to confuse with JS 1.8.1's trim().
Parameters:
Name Type Description strstring the string to ensure that has the property Returns:
the trimmed string- Type
- string
-
<static> crop(str, lim, suff) → {string}
-
Crop a string nicely. Returns: Nothing. Side-effects: throws an error if the namespace defined by the strings is not currently found.
Parameters:
Name Type Description strthe string to crop limthe final length to crop to (optional, defaults to 10) suffthe string to add to the end (optional, defaults to '') Returns:
cropped string- Type
- string
-
<static> dequote(str) → {string}
-
Remove the quotes from a string.
Parameters:
Name Type Description strstring the string to dequote Returns:
the dequoted string (or the original string)- Type
- string
-
<static> ensure(str, add, place) → {string}
-
Make sure that a substring exists at the beginning or end (or both) of a string.
Parameters:
Name Type Description strthe string to ensure that has the property addthe string to check for (and possibly add) place*[optional]* "front"|"back", place to ensure (defaults to both) Returns:
a new string with the property enforced- Type
- string
-
<static> extend(subclass, superclass)
-
What seems to be a typical idiom for subclassing in JavaScript. This attempt has been scraped together from bits here and there and lucid explanations from Mozilla: https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Details_of_the_Object_Model https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_Revisited https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/new
Parameters:
Name Type Description subclassthe subclass object superclassthe superclass object -
<static> fold(default_hash, arg_hash) → {object}
-
Fold a pair of hashes together, using the first one as an initial template--only the keys in the default hash will be defined in the final hash--and the second hash getting precedence. The can be quite useful when defining functions--essentially allowing a limited default value system for arguments.
Parameters:
Name Type Description default_hashobject Template hash. arg_hashobject Argument hash to match. - Source:
- See:
Returns:
a new hash- Type
- object
-
<static> get_assemble(qargs) → {string}
-
Assemble an object into a GET-like query. You probably want to see the tests to get an idea of what this is doing. The last argument of double hashes gets quoted (Solr-esque), otherwise not. It will try and avoid adding additional sets of quotes to strings. This does nothing to make the produced "URL" in any way safe. WARNING: Not a hugely clean function--there are a lot of special cases and it could use a good (and safe) clean-up.
Parameters:
Name Type Description qargshash/object Returns:
string- Type
- string
-
<static> get_keys(arg_hash) → {Array}
-
Get the hash keys from a hash/object, return as an array.
Parameters:
Name Type Description arg_hashthe hash in question Returns:
an array of keys- Type
- Array
-
<static> has_interface(iobj, interface_list) → {boolean}
-
Check to see if all top-level objects in a namespace supply an "interface". Mostly intended for use during unit testing. TODO: Unit test this to make sure it catches both prototype (okay I think) and uninstantiated objects (harder/impossible?).
Parameters:
Name Type Description iobjthe object/constructor in question interface_listthe list of interfaces (as a strings) we're looking for Returns:
boolean- Type
- boolean
-
<static> hashify(list) → {object}
-
Returns a hash form of the argument array/list. For example ['a', 'b'] would become {'a': true, 'b': true} or [['a', '12'], ['b', '21']] would become {'a': '12', 'b': '21'}. Using mixed sub-lists is undefined.
Parameters:
Name Type Description listArray the list to convert Returns:
a hash- Type
- object
-
<static> is_array(in_thing) → {boolean}
-
Return the best guess (true/false) for whether or not a given object is being used as an array.
Parameters:
Name Type Description in_thingthe thing in question Returns:
boolean- Type
- boolean
-
<static> is_defined(in_thing) → {boolean}
-
Return true/false on whether or not the passed object is defined.
Parameters:
Name Type Description in_thingthe thing in question Returns:
boolean- Type
- boolean
-
<static> is_empty(in_thing) → {boolean}
-
Return true/false on whether or not the object in question has any items of interest (iterable?).
Parameters:
Name Type Description in_thingthe thing in question Returns:
boolean- Type
- boolean
-
<static> merge(older_hash, newer_hash) → {object}
-
Merge a pair of hashes together, the second hash getting precedence. This is a superset of the keys both hashes.
Parameters:
Name Type Description older_hashfirst pass newer_hashsecond pass - Source:
- See:
Returns:
a new hash- Type
- object
-
<static> numeric_sort_ascending(a, b) → {number}
-
A sort function to put numbers in ascending order. Useful as the argument to .sort(). See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
Parameters:
Name Type Description anumber the first number bnumber the second number Returns:
number of their relative worth- Type
- number
-
<static> numeric_sort_descending(a, b) → {number}
-
A sort function to put numbers in descending order. Useful as the argument to .sort(). See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
Parameters:
Name Type Description anumber the first number bnumber the second number Returns:
number of their relative worth- Type
- number
-
<static> pare(in_thing, filter_function, sort_function) → {Array}
-
Take an array or hash and pare it down using a couple of functions to what we want. Both parameters are optional in the sense that you can set them to null and they will have no function; i.e. a null filter will let everything through and a null sort will let things go in whatever order.
Parameters:
Name Type Description in_thingArray | Object hash or array filter_functionfunction hash (function(key, val)) or array (function(item, i)); this function must return boolean true or false. sort_functionfunction function to apply to elements: function(a, b); this function must return an integer as the usual sort functions do. Returns:
array- Type
- Array
-
<static> randomness(len) → {string}
-
Random number generator of fixed length. Return a random number string of length len.
Parameters:
Name Type Description lenthe number of random character to return. Returns:
string- Type
- string
-
<static> resourcify(base, resource, extension) → {string}
-
Convert a string into something consistent for urls (getting icons, etc.). Return a munged/hashed-down version of the resource. Assembles, converts spaces to underscores, and all lowercases.
Parameters:
Name Type Description basebase url for the resource(s) resourcethe filename or whatever to be transformed extension*[optional]* the extension of the resource Returns:
string- Type
- string
-
<static> splode(str, delimiter) → {Array}
-
Break apart a string on certain delimiter.
Parameters:
Name Type Description strthe string to ensure that has the property delimiter*[optional]* either a string or a simple regexp; defaults to ws Returns:
a list of separated substrings- Type
- Array
-
<static> to_string(in_thing) → {string}
-
Essentially add standard 'to string' interface to the string class and as a stringifier interface to other classes. More meant for output--think REPL. Only atoms, arrays, and objects with a to_string function are handled.
Parameters:
Name Type Description in_thingany something Returns:
string- Type
- string
-
<static> url_parameters(url) → {Array}
-
Return the parameters part of a URL. Unit tests make the edge cases clear.
Parameters:
Name Type Description urlurl (or similar string) Returns:
list of part lists- Type
- Array
-
<static> uuid() → {string}
-
RFC 4122 v4 compliant UUID generator. From: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
Returns:
string- Type
- string
-
clone(thing) → {any}
-
(Deep) clone an object down to its atoms.
Parameters:
Name Type Description thingany whatever Returns:
a new whatever- Type
- any
-
dump(in_thing) → {string}
-
Dump an object to a string form as best as possible. More meant for debugging. This is meant to be an Object walker. For a slightly different take (Object identification), see
. Parameters:
Name Type Description in_thingsomething - Source:
- See:
Returns:
string- Type
- string
-
first_split(character, string) → {Array}
-
Attempt to return a two part split on the first occurrence of a character. Returns '' for parts not found. Unit tests make the edge cases clear.
Parameters:
Name Type Description characterString the character to split on stringString the string to split Returns:
list of first and second parts- Type
- Array
-
is_hash(in_thing) → {boolean}
-
Return the best guess (true/false) for whether or not a given object is being used as a hash.
Parameters:
Name Type Description in_thingthe thing in question Returns:
boolean- Type
- boolean
-
what_is(in_thing) → {string}
-
Return the string best guess for what the input is, null if it can't be identified. In addition to the _is_a property convention, current core output strings are: 'null', 'array', 'boolean', 'number', 'string', 'function', and 'object'.
Parameters:
Name Type Description in_thingany the thing in question Returns:
string- Type
- string