core.js

Summary
core.js
bbop.coreBBOP language extensions to JavaScript.
Variables
globalCapture the global object for later reference.
Functions
namespaceCreate a namespace (chained object) in the global environment.
requireThrow an error unless a specified namespace is defined.
cropCrop a string nicely.
foldFold 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.
mergeMerge a pair of hashes together, the second hash getting precedence.
get_keysGet the hash keys from a hash/object, return as an array.
hashifyReturns a hash form of the argument array/list.
is_sameReturns true if it things the two incoming arguments are value-wise the same.
what_isReturn the string best guess for what the input is, null if it can’t be identified.
is_arrayReturn the best guess (true/false) for whether or not a given object is being used as an array.
is_hashReturn the best guess (true/false) for whether or not a given object is being used as a hash.
is_emptyReturn true/false on whether or not the object in question has any items of interest (iterable?)
is_definedReturn true/false on whether or not the passed object is defined.
eachImplement a simple iterator so I don’t go mad.
pareTake an array or hash and pare it down using a couple of functions to what we want.
cloneClone an object down to its atoms.
to_stringEssentially add standard ‘to string’ interface to the string class and as a stringifier interface to other classes.
dumpDump an object to a string form as best as possible.
has_interfaceCheck to see if all top-level objects in a namespace supply an “interface”.
get_assembleAssemble an object into a GET-like query.
first_splitAttempt to return a two part split on the first occurrence of a character.
url_parametersReturn the parameters part of a URL.
resourcifyConvert a string into something consistent for urls (getting icons, etc.)
uuidRFC 4122 v4 compliant UUID generator.
numeric_sort_ascendingA sort function to put numbers in ascending order.
numeric_sort_descendingA sort function to put numbers in descending order.
extendWhat seems to be a typical idiom for subclassing in JavaScript.

bbop.core

BBOP language extensions to JavaScript.

Purpose: Helpful basic utilities and operations to fix common needs in JS.

Summary
Variables
globalCapture the global object for later reference.
Functions
namespaceCreate a namespace (chained object) in the global environment.
requireThrow an error unless a specified namespace is defined.
cropCrop a string nicely.
foldFold 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.
mergeMerge a pair of hashes together, the second hash getting precedence.
get_keysGet the hash keys from a hash/object, return as an array.
hashifyReturns a hash form of the argument array/list.
is_sameReturns true if it things the two incoming arguments are value-wise the same.
what_isReturn the string best guess for what the input is, null if it can’t be identified.
is_arrayReturn the best guess (true/false) for whether or not a given object is being used as an array.
is_hashReturn the best guess (true/false) for whether or not a given object is being used as a hash.
is_emptyReturn true/false on whether or not the object in question has any items of interest (iterable?)
is_definedReturn true/false on whether or not the passed object is defined.
eachImplement a simple iterator so I don’t go mad.
pareTake an array or hash and pare it down using a couple of functions to what we want.
cloneClone an object down to its atoms.
to_stringEssentially add standard ‘to string’ interface to the string class and as a stringifier interface to other classes.
dumpDump an object to a string form as best as possible.
has_interfaceCheck to see if all top-level objects in a namespace supply an “interface”.
get_assembleAssemble an object into a GET-like query.
first_splitAttempt to return a two part split on the first occurrence of a character.
url_parametersReturn the parameters part of a URL.
resourcifyConvert a string into something consistent for urls (getting icons, etc.)
uuidRFC 4122 v4 compliant UUID generator.
numeric_sort_ascendingA sort function to put numbers in ascending order.
numeric_sort_descendingA sort function to put numbers in descending order.
extendWhat seems to be a typical idiom for subclassing in JavaScript.

Variables

global

bbop.core.global

Capture the global object for later reference.

Used by namespace and require.

TODO: There is a temporary workaround for NodeJS here TODO: Creates loop; problem?

Also see

namespace requires

Functions

namespace

bbop.core.namespace = function()

Create a namespace (chained object) in the global environment.

Parameters: An arbitrary number of strings.

Returns: Nothing.  Side-effects: this function extends the global object for easy namespace creation.

Also See: require

require

bbop.core.require = function()

Throw an error unless a specified namespace is defined.

Parameters: An arbitrary number of strings.

Returns: Nothing.  Side-effects: throws an error if the namespace defined by the strings is not currently found.

Also See: namespace

crop

bbop.core.crop = function(str,
lim,
suff)

Crop a string nicely.

Parameters

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: Nothing.  Side-effects: throws an error if the namespace defined by the strings is not currently found.

fold

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.

Parameters

default_hashTemplate hash.
arg_hashArgument hash to match.

Returns: A new hash.

Also see: merge

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.

Parameters

older_hashfirst pass
newer_hashsecond pass

Returns: A new hash.

Also see: fold

get_keys

bbop.core.get_keys = function (arg_hash)

Get the hash keys from a hash/object, return as an array.

Parameters

arg_hashthe hash in question

Returns: an array of keys

hashify

bbop.core.hashify = function (list)

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

listthe list to convert

Returns: a hash

is_same

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.

Parameters

thing1thing one
thing2thing two

Returns: boolean

what_is

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’.

Parameters

in_thingthe thing in question

Returns: a string

is_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 an array.

Parameters

in_thingthe thing in question

Returns: boolean

is_hash

bbop.core.is_hash = function(in_thing)

Return the best guess (true/false) for whether or not a given object is being used as a hash.

Parameters

in_thingthe thing in question

Returns: boolean

is_empty

bbop.core.is_empty = function(in_thing)

Return true/false on whether or not the object in question has any items of interest (iterable?).

Parameters

in_thingthe thing in question

Returns: boolean

is_defined

bbop.core.is_defined = function(in_thing)

Return true/false on whether or not the passed object is defined.

Parameters

in_thingthe thing in question

Returns: boolean

each

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”.

Parameters

in_thinghash or array
in_functionfunction to apply to elements

Returns

n/a

pare

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.

Parameters

in_thinghash or array
filter_functionhash (function(key, val)) or array (function(item, i)).  This function must return boolean true or false.
sort_functionfunction to apply to elements: function(a, b) This function must return an integer as the usual sort functions do.

Returns: An array.

clone

bbop.core.clone = function(thing)

Clone an object down to its atoms.

Parameters

thingwhatever

Returns: a new whatever

to_string

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.  Only atoms, arrays, and objects with a to_string function are handled.

Parameters

in_thingsomething

Returns: string

Also See: dump

dump

bbop.core.dump = function(thing)

Dump an object to a string form as best as possible.  More meant for debugging.  For a slightly different take, see to_string.

Parameters

in_thingsomething

Returns: string

Also See: to_string

has_interface

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.

Parameters

iobjthe object/constructor in question
interface_listthe 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?).

get_assemble

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.

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

qargshash/object

Returns: string

first_split

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.

Parameters

characterthe character to split on
stringthe string to split

Returns

list of first and second parts

url_parameters

bbop.core.url_parameters = function(url)

Return the parameters part of a URL.

Unit tests make the edge cases clear.

Parameters

urlurl (or similar string)

Returns

list of part lists

resourcify

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.

Parameters

basebase url for the resource(s)
resourcethe filename or whatever to be transformed
extension[optional] the extension of the resource

Returns

string

uuid

bbop.core.uuid = function()

RFC 4122 v4 compliant UUID generator.  From: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523

Parameters

n/a

Returns

string

numeric_sort_ascending

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/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

Parameters

athe first number
bthe second number

Returns

number of their relative worth

numeric_sort_descending

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/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

Parameters

athe first number
bthe second number

Returns

number of their relative worth

extend

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/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

subclassthe subclass object
superclassthe superclass object

Returns

n/a

bbop.core.global
Capture the global object for later reference.
bbop.core.namespace = function()
Create a namespace (chained object) in the global environment.
bbop.core.require = function()
Throw an error unless a specified namespace is defined.
bbop.core.crop = function(str,
lim,
suff)
Crop a string nicely.
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.
bbop.core.merge = function(older_hash,
newer_hash)
Merge a pair of hashes together, the second hash getting precedence.
bbop.core.get_keys = function (arg_hash)
Get the hash keys from a hash/object, return as an array.
bbop.core.hashify = function (list)
Returns a hash form of the argument array/list.
bbop.core.is_same = function (thing1,
thing2)
Returns true if it things the two incoming arguments are value-wise the same.
bbop.core.what_is = function(in_thing)
Return the string best guess for what the input is, null if it can’t be identified.
bbop.core.is_array = 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_hash = 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_empty = function(in_thing)
Return true/false on whether or not the object in question has any items of interest (iterable?)
bbop.core.is_defined = function(in_thing)
Return true/false on whether or not the passed object is defined.
bbop.core.each = function(in_thing,
in_function)
Implement a simple iterator so I don’t go mad.
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.
bbop.core.clone = function(thing)
Clone an object down to its atoms.
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.
bbop.core.dump = function(thing)
Dump an object to a string form as best as possible.
bbop.core.has_interface = function(iobj,
interface_list)
Check to see if all top-level objects in a namespace supply an “interface”.
bbop.core.get_assemble = function(qargs)
Assemble an object into a GET-like query.
bbop.core.first_split = function(character,
string)
Attempt to return a two part split on the first occurrence of a character.
bbop.core.url_parameters = function(url)
Return the parameters part of a URL.
bbop.core.resourcify = function(base,
resource,
extension)
Convert a string into something consistent for urls (getting icons, etc.)
bbop.core.uuid = function()
RFC 4122 v4 compliant UUID generator.
bbop.core.numeric_sort_ascending = function(a,
b)
A sort function to put numbers in ascending order.
bbop.core.numeric_sort_descending = function(a,
b)
A sort function to put numbers in descending order.
bbop.core.extend = function(subclass,
baseclass)
What seems to be a typical idiom for subclassing in JavaScript.
Close