Module: bbop-core

bbop-core

BBOP language extensions to JavaScript, complimenting Underscore.js. Purpose: Helpful basic utilities and operations to fix common needs in JS.
Source:

Classes

logger

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
str string the string to ensure that has the property
Source:
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
str the string to crop
lim the final length to crop to (optional, defaults to 10)
suff the string to add to the end (optional, defaults to '')
Source:
Returns:
cropped string
Type
string

<static> dequote(str) → {string}

Remove the quotes from a string.
Parameters:
Name Type Description
str string the string to dequote
Source:
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
str the string to ensure that has the property
add the string to check for (and possibly add)
place *[optional]* "front"|"back", place to ensure (defaults to both)
Source:
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
subclass the subclass object
superclass the superclass object
Source:

<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_hash object Template hash.
arg_hash object 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
qargs hash/object
Source:
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_hash the hash in question
Source:
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
iobj the object/constructor in question
interface_list the list of interfaces (as a strings) we're looking for
Source:
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
list Array the list to convert
Source:
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_thing the thing in question
Source:
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_thing the thing in question
Source:
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_thing the thing in question
Source:
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_hash first pass
newer_hash second 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
a number the first number
b number the second number
Source:
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
a number the first number
b number the second number
Source:
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_thing Array | Object hash or array
filter_function function hash (function(key, val)) or array (function(item, i)); this function must return boolean true or false.
sort_function function function to apply to elements: function(a, b); this function must return an integer as the usual sort functions do.
Source:
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
len the number of random character to return.
Source:
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
base base url for the resource(s)
resource the filename or whatever to be transformed
extension *[optional]* the extension of the resource
Source:
Returns:
string
Type
string

<static> splode(str, delimiter) → {Array}

Break apart a string on certain delimiter.
Parameters:
Name Type Description
str the string to ensure that has the property
delimiter *[optional]* either a string or a simple regexp; defaults to ws
Source:
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_thing any something
Source:
See:
  • module:bbop-core.dump
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
url url (or similar string)
Source:
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
Source:
Returns:
string
Type
string

clone(thing) → {any}

(Deep) clone an object down to its atoms.
Parameters:
Name Type Description
thing any whatever
Source:
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_thing something
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
character String the character to split on
string String the string to split
Source:
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_thing the thing in question
Source:
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_thing any the thing in question
Source:
Returns:
string
Type
string