core.js | |
bbop.core | BBOP language extensions to JavaScript. |
Functions | |
crop | Crop a string nicely. |
fold | 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. |
merge | Merge a pair of hashes together, the second hash getting precedence. |
get_keys | Get the hash keys from a hash/object, return as an array. |
hashify | Returns a hash form of the argument array/list. |
is_same | Returns true if it things the two incoming arguments are value-wise the same. |
what_is | Return the string best guess for what the input is, null if it can’t be identified. |
is_array | Return the best guess (true/false) for whether or not a given object is being used as an array. |
is_hash | Return the best guess (true/false) for whether or not a given object is being used as a hash. |
is_empty | Return true/false on whether or not the object in question has any items of interest (iterable?) |
is_defined | Return true/false on whether or not the passed object is defined. |
each | Implement a simple iterator so I don’t go mad. |
pare | Take an array or hash and pare it down using a couple of functions to what we want. |
clone | Clone an object down to its atoms. |
to_string | Essentially add standard ‘to string’ interface to the string class and as a stringifier interface to other classes. |
dump | Dump an object to a string form as best as possible. |
has_interface | Check to see if all top-level objects in a namespace supply an “interface”. |
get_assemble | Assemble an object into a GET-like query. |
first_split | Attempt to return a two part split on the first occurrence of a character. |
url_parameters | Return the parameters part of a URL. |
resourcify | Convert a string into something consistent for urls (getting icons, etc.) |
uuid | RFC 4122 v4 compliant UUID generator. |
numeric_sort_ascending | A sort function to put numbers in ascending order. |
numeric_sort_descending | A sort function to put numbers in descending order. |
dequote | Remove the quotes from a string. |
ensure | Make sure that a substring exists at the beginning or end (or both) of a string. |
chomp | Trim the leading and trailing whitespace from a string. |
splode | Break apart a string on certain delimiter. |
extend | What seems to be a typical idiom for subclassing in JavaScript. |
BBOP language extensions to JavaScript.
Purpose: Helpful basic utilities and operations to fix common needs in JS.
Functions | |
crop | Crop a string nicely. |
fold | 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. |
merge | Merge a pair of hashes together, the second hash getting precedence. |
get_keys | Get the hash keys from a hash/object, return as an array. |
hashify | Returns a hash form of the argument array/list. |
is_same | Returns true if it things the two incoming arguments are value-wise the same. |
what_is | Return the string best guess for what the input is, null if it can’t be identified. |
is_array | Return the best guess (true/false) for whether or not a given object is being used as an array. |
is_hash | Return the best guess (true/false) for whether or not a given object is being used as a hash. |
is_empty | Return true/false on whether or not the object in question has any items of interest (iterable?) |
is_defined | Return true/false on whether or not the passed object is defined. |
each | Implement a simple iterator so I don’t go mad. |
pare | Take an array or hash and pare it down using a couple of functions to what we want. |
clone | Clone an object down to its atoms. |
to_string | Essentially add standard ‘to string’ interface to the string class and as a stringifier interface to other classes. |
dump | Dump an object to a string form as best as possible. |
has_interface | Check to see if all top-level objects in a namespace supply an “interface”. |
get_assemble | Assemble an object into a GET-like query. |
first_split | Attempt to return a two part split on the first occurrence of a character. |
url_parameters | Return the parameters part of a URL. |
resourcify | Convert a string into something consistent for urls (getting icons, etc.) |
uuid | RFC 4122 v4 compliant UUID generator. |
numeric_sort_ascending | A sort function to put numbers in ascending order. |
numeric_sort_descending | A sort function to put numbers in descending order. |
dequote | Remove the quotes from a string. |
ensure | Make sure that a substring exists at the beginning or end (or both) of a string. |
chomp | Trim the leading and trailing whitespace from a string. |
splode | Break apart a string on certain delimiter. |
extend | What seems to be a typical idiom for subclassing in JavaScript. |
bbop.core.crop = function( str, lim, suff )
Crop a string nicely.
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 ‘’) |
Returns: Nothing. Side-effects: throws an error if the namespace defined by the strings is not currently found.
bbop.core.fold = function( default_hash, arg_hash )
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.
default_hash | Template hash. |
arg_hash | Argument hash to match. |
Returns: A new hash.
Also see: merge
bbop.core.merge = function( older_hash, newer_hash )
Merge a pair of hashes together, the second hash getting precedence. This is a superset of the keys both hashes.
older_hash | first pass |
newer_hash | second pass |
Returns: A new hash.
Also see: fold
bbop.core.is_same = function ( thing1, thing2 )
Returns true if it things the two incoming arguments are value-wise the same.
Currently only usable for simple (atomic single layer) hashes, atomic lists, boolean, null, number, and string values. Will return false otherwise.
thing1 | thing one |
thing2 | thing two |
Returns: boolean
bbop.core.what_is = function( in_thing )
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’.
in_thing | the thing in question |
Returns: a string
bbop.core.each = function( in_thing, in_function )
Implement a simple iterator so I don’t go mad. array - function(item, index) object - function(key, value)
TODO/BUG/WARNING?: This does not seem to work with the local function variable “arguments”.
in_thing | hash or array |
in_function | function to apply to elements |
n/a
bbop.core.pare = function( in_thing, filter_function, sort_function )
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.
in_thing | hash or array |
filter_function | hash (function(key, val)) or array (function(item, i)). This function must return boolean true or false. |
sort_function | function to apply to elements: function(a, b) This function must return an integer as the usual sort functions do. |
Returns: An array.
bbop.core.to_string = function( in_thing )
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.
in_thing | something |
Returns: string
Also See: dump
bbop.core.has_interface = function( iobj, interface_list )
Check to see if all top-level objects in a namespace supply an “interface”.
Mostly intended for use during unit testing.
iobj | the object/constructor in question |
interface_list | the list of interfaces (as a strings) we’re looking for |
Returns: boolean
TODO: Unit test this to make sure it catches both prototype (okay I think) and uninstantiated objects (harder/impossible?).
bbop.core.get_assemble = function( qargs )
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.
qargs | hash/object |
Returns: string
bbop.core.first_split = function( character, string )
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.
character | the character to split on |
string | the string to split |
list of first and second parts
bbop.core.resourcify = function( base, resource, extension )
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.
base | base url for the resource(s) |
resource | the filename or whatever to be transformed |
extension | [optional] the extension of the resource |
string
bbop.core.uuid = function()
RFC 4122 v4 compliant UUID generator. From: http://stackoverflow.com
n/a
string
bbop.core.numeric_sort_ascending = function( a, b )
A sort function to put numbers in ascending order.
Useful as the argument to .sort().
See: https://developer.mozilla.org
a | the first number |
b | the second number |
number of their relative worth
bbop.core.numeric_sort_descending = function( a, b )
A sort function to put numbers in descending order.
Useful as the argument to .sort().
See: https://developer.mozilla.org
a | the first number |
b | the second number |
number of their relative worth
bbop.core.ensure = function( str, add, place )
Make sure that a substring exists at the beginning or end (or both) of a string.
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) |
a new string with the property enforced
bbop.core.extend = function( subclass, baseclass )
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
subclass | the subclass object |
superclass | the superclass object |
n/a
Crop a string nicely.
bbop.core.crop = function( str, lim, suff )
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.
bbop.core.fold = function( default_hash, arg_hash )
Merge a pair of hashes together, the second hash getting precedence.
bbop.core.merge = function( older_hash, newer_hash )
Get the hash keys from a hash/object, return as an array.
bbop.core.get_keys = function ( arg_hash )
Returns a hash form of the argument array/list.
bbop.core.hashify = function ( list )
Returns true if it things the two incoming arguments are value-wise the same.
bbop.core.is_same = function ( thing1, thing2 )
Return the string best guess for what the input is, null if it can’t be identified.
bbop.core.what_is = function( in_thing )
Return the best guess (true/false) for whether or not a given object is being used as an array.
bbop.core.is_array = function( in_thing )
Return the best guess (true/false) for whether or not a given object is being used as a hash.
bbop.core.is_hash = function( in_thing )
Return true/false on whether or not the object in question has any items of interest (iterable?)
bbop.core.is_empty = function( in_thing )
Return true/false on whether or not the passed object is defined.
bbop.core.is_defined = function( in_thing )
Implement a simple iterator so I don’t go mad.
bbop.core.each = function( in_thing, in_function )
Take an array or hash and pare it down using a couple of functions to what we want.
bbop.core.pare = function( in_thing, filter_function, sort_function )
Clone an object down to its atoms.
bbop.core.clone = function( thing )
Essentially add standard ‘to string’ interface to the string class and as a stringifier interface to other classes.
bbop.core.to_string = function( in_thing )
Dump an object to a string form as best as possible.
bbop.core.dump = function( thing )
Check to see if all top-level objects in a namespace supply an “interface”.
bbop.core.has_interface = function( iobj, interface_list )
Assemble an object into a GET-like query.
bbop.core.get_assemble = function( qargs )
Attempt to return a two part split on the first occurrence of a character.
bbop.core.first_split = function( character, string )
Return the parameters part of a URL.
bbop.core.url_parameters = function( url )
Convert a string into something consistent for urls (getting icons, etc.)
bbop.core.resourcify = function( base, resource, extension )
RFC 4122 v4 compliant UUID generator.
bbop.core.uuid = function()
A sort function to put numbers in ascending order.
bbop.core.numeric_sort_ascending = function( a, b )
A sort function to put numbers in descending order.
bbop.core.numeric_sort_descending = function( a, b )
Remove the quotes from a string.
bbop.core.dequote = function( str )
Make sure that a substring exists at the beginning or end (or both) of a string.
bbop.core.ensure = function( str, add, place )
Trim the leading and trailing whitespace from a string.
bbop.core.chomp = function( str )
Break apart a string on certain delimiter.
bbop.core.splode = function( str, delimiter )
What seems to be a typical idiom for subclassing in JavaScript.
bbop.core.extend = function( subclass, baseclass )