Miscellaneous Functions

© 2012, Martin Rinehart


new_object = add_objects( first_object, second_object );

Makes a shallow copy of first_object and 'adds' (adds or overwrites existing) all properties of second_object to the new object.


shallow_copy_of_object = copy_object( old_object );

Makes a shallow copy of an object. A shallow copy makes a copy of properties that have primitive values (like booleans and numbers) but copies values that are references (such as arrays and other objects).


today_as_formatted_string = date2dd_Mon_yyyy( new Date() );

Format date as dd-Mon-yyyy (e.g. (08-Aug-2012'). Month abbreviations are hard-coded American English.


array = div_points( low, high, ndivisions );

Return the points that divide a range into a number of divisions. Example: [ 3, 6 ] === div_points( 0, 9, 3 ). Used in morphing.


fader( msg, left, top, fade_millis, delay_millis );

Display a message, briefly. Creates a div. Places the msg in the div. Puts the div at left, top. Waits for (optional) delay_millis). Displays the message, fading out over fade_millis. Detaches and discards the div.

WARNING: Uses default data object. See the code.


left_side = leftmost( array, number );

Copies leftmost (beginning elements) number of elements in array to new array.


inverted = invert_object( object );

Each "name:value: pair in object becomes a "value:name" pair in the inverted version. Values are coerced to string as needed.


object_attach_props( div.style, more_styles_object );

object_attach_props( div.style, styles_objects_array );

Add properties to an object from another object or from an array of other objects.


array = object_props_to_array( object );

Return array of values from an object ('values' only from 'name/value' pairs).

The outnum() Function

Designed for an outline written like this:

foo = '''
    Main 1
        Sub 1
        Sub 2
            Minor 1
            Minor 2
    Main 2

Converted to JavaScript array of strings (by editor macros) like this:

foo = [
'    Intro',
'    Main 1',
'        Sub 1',
'        Sub 2',
'            Minor 1',
'            Minor 2',
'    Main 2'

This code will produce an array of arrays, like this:

foo = [
[[2],'Main 1'],
[[2,1],'Sub 1'],
[[2,2],'Sub 2'],
[[2,2,1],'Minor 1'],
[[2,2,2],'Minor 2'],
[[3],'Main 2']

Each of the row arrays has two elements: an array of outline numbers and the outline topic (without tabs). The length of the outline array is equal to the original number of tabs. The value of each number is the count of topics, including itself, that follow the last preceding less-indented topic.

The input may be arrays (strings plus additional values), like this:

foo = [
['Title', xxx],
['    Intro', xxx],
['    Main 1', xxx],
['        Sub 1', xxx],
['        Sub 2', xxx],
['            Minor 1', xxx],
['            Minor 2', xxx],
['    Main 2', xxx]

('xxx' is any number of additional values.) This will produce:

foo = [
[[],'Title', xxx],
[[1],'Intro', xxx],
[[2],'Main 1', xxx],
[[2,1],'Sub 1', xxx],
[[2,2],'Sub 2', xxx],
[[2,2,1],'Minor 1', xxx],
[[2,2,2],'Minor 2', xxx],
[[3],'Main 2', xxx]


'Object is: ' + to_string( object );

'Array begins: ' + to_string( array, array_max );

Provides a sensible toString()-like look at values, strings, arrays, functions and other objects. Calls itself recursively for array elements and object values.

Strings are displayed in quotes, arrays in '[]' and other objects in '{}'.

Optional array_max argument limits arrays to displaying the first array_max elements. This applies recursively to array elements of other arrays and array values in other objects.

Feedback: MartinRinehart at gmail dot com

# # #