SCRIPTING:

Pamela Anderson

MISCELLANEOUS FUNCTIONS #1
I include here miscellaneous functions: routines mostly shaped over time when reading of a particular request on newsgroups and the alike. Some are simplier, some powerful.
August 2002
{ @ }



Miscella Subroutines
Each soubroutine is fully documented in what it does and how it can work. Most of them have been shaped so to accomodate a plurality of actions all affecting the same topic. All have a Test Form so you can make sure you know how they work before using them. I call it cool!

Here are your miscella functions, each with its documentation and test form.
But before introducing them, just let me add a quick synopsis of other files located at United Scripters where you can find other miscellaneous functions included as auxiliary ones in within more relevant and bigger function documentations; all these snippets are extremely simple and trivial, I mention them here simply to fit a logic consistency which may call for such a a summary in a file which after all is devoted to miscellaneous stuff:

A group of Arrays: convert them to one Array which holds each of these arrays as one of its branches.
Snippets to help sorting processes to be tackled properly
Make an array on the fly whose each entry holds the same value.
Completely delete an entry from an Array making sure no remains whatsoever may still linger around.
Add value (n) to an hexadecimal (h) couple, returning the new hexadecimal
Make sure that in an input Array are included only those values which are included in a compared Array, and produce an output similar to the input array but where all the values not present in the compared Array have been sifted away.

A NOTE
I work entirely alone and it's a hack of a hell of coding and scripting and html and dhtml designing to make things as comprehensive and of high quality as possible for you, and I get no money for this job (there are still out there a few guys who do not need the smell of money in order to get interest in something, you see; although if they were to work for you instead than for themselves, they'd want to be payed as well, puzzling enough).
If by chance you find a bug, be understanding and kind, if you're reasonably sure it is such (that is, not a bad management of a function and its arguments by you), contact me and lemme know 'bout it.


PERCENT

Calculates the percentage represented by a number on a given whole.
Can perform the opposite, returning a number which is the numerical equivalent of the given percentage on a given whole. Very usful for Dhtml resizings too.
Arguments
 number 
  1. Works in connection with whole: if whole is a length and number is another length, the returned number is the percentage that number represents of whole.
  2. Conversely, if number is meant to be a percentage and whole is a length, the returned number is the length (out of the whole) which amounts to the given percentage passed as number.
 whole 
See above.
 inverse 
If none or zero, applies #1. If number (say 1), applies #2
 rounder 
A power of ten (10, 100, 1000...). Rounds by that. Defaults to 100 (two floating digits, if any).
The code
The Test Form
N:» W:»
[ inverse? » ]

ROUNDER

Given a number, produces an Array which holds a variety of different floating formats
Arguments
 number 
A number.
The function would return an Array of as many as 4 entries. Therefore:
  1. rounder(number)[0]:
    the number rounded by the next argument named approx
  2. rounder(number)[1]:
    The component of a nubmer before the decimal floating fraction (if any).
  3. rounder(number)[2]:
    The decimal floating fraction (if any) of the number after being rounded by approx.
  4. rounder(number)[3]:
    The decimal floating fraction (if any) of the number without it being rounded by approx.
 approx 
Defaults to 10. Must be a power of 10 (10, 100, 1000, 10000...) and rounds by that.
The code
The Test Form
N:»


PROPERTY TO MENU

Basically, it prints an Array to a drop down menu. Can print to the menu the properties of an Object (such as the images locations in a document). In both cases it unfolds to an Array the properties of the passed object.
Alternatively, can just return such an array of an unfolded properties without printing it to any menu.
Arguments
 array 
Either an Array or, more typically, a reference to an Object, excluding its properties (such as length or similar). Instance:
document.images.
This argument is mandatory
 arrayProperty 
A String.
Must be the property to inspect such as, for images:
"src"
This argument is mandatory
 menuObject 
A Object reference to a select menu without reference to options; example:
document.formName.menuName
if passed as zero, the function won't print but just return an Array of the values of the unfolded property arrayProperty for the array object.
 myTexts 
If none (zero), does nothing and by default if printing on a menu, the printed texts would be the values of the unfolded properties.
If passed as a String, it will show on the menu texts the given string plus an increasing number starting with 1.
if passed as an Array of texts, for each unfolded property the text will be the corresponding myTexts entry. The length of such array must be the same of the length of the array argument.
 shorten 
A String. If passed, would shorten the values of the unfolded properties to a substring starting from the char next such value if present.
If passed as number 1, it defaults to: "/"
If passed as zero doesn't shorten at all.
The code
The Test Form
ARRAY PROPERTY MENU OBJECT
SHORTEN MY TEXTS
TEST MENU below:


MANAGE SELECT

It can perform upon a menu nearly everything a menu can undergo.
Upon a selection on a drop down menu, either eliminates the selection and returns the [value,text] of the deleted option, or just returns the latter [value,text] without deleting it.
A similar task (but focused on Arrays: deleting an Array entry) is performed by the short but powerful subroutine named unset
It can eliminate an option also without a selection being performed.
Alternatively, completely redraws the menu from a given index with given values/texts.
Alternatively, inserts a group of new options to a given offset.
Can append and pre-pend to a menu.
Arguments
 field 
An Object, namely a reference to the form menu without including any of its properties nor options; example:
document.formName.menuName
 sel 
Optional. If passed must be a Number, and should be typically used by passing also the third argument justReturn. Passing sel means that the deleted entry will be the one whose index correspond to sel (that is: also without it having been selected!), and therefore the returned array of two entries which are the [value,text] pair will be the one corresponding to the index given by sel. By default sel is gonna be the selected index (if any), and zero (first option) if no selection is on. In case sel is passed but exceeds the amount of available options, its default is the last index.
 
If you do mean to pass sel as index zero, pass zero in between quotes or the function would assume it is meant as boolean false:
"0"
 justReturn 
If set it either:
  1. Just returns the [value,text] pair of the selected option (and of option zero if no selection has been performed or instituted by default in the given menu) without deleting the selected option.
  2. Just returns the [value,text] pair of the option whose index is equal to sel (obviously provided sel is passed) without deleting the selected option.
 textsArray 
If such an argument is passed, it must be an Array, and would overcome all the previous ones. It would set to zero the options present in field Object and would redraw them using as texts for each new option the entries of the textsArray argument, whereas for the values of each new option it will use the entries of the valuesArray argument (if passed must be an Array as well) or if no valuesArray is passed, it would use textsArray again even to assign the values, and not just the texts, for each given newly reshaped option.
If such argument or valuesArray ar passed, the function always returns boolean false
 valuesArray 
See above.
 redrawFrom 
Defaults to zero. If passed as a Number, it would start adding the new options from the option index correponfing to the passed number: for instance if 1, would start redrawing after the first option.
Does nothing if either textsArray or valuesArray or both are not passed, for it would have nothing to redraw the field with.
Its value is a close range: if you pass, say, 1, well it would redraw including (overwriting, that is) the option whose index is 1.
To append to the menu, pass redrawFrom as the length of the options present in the menu.
Conversely, to prepend, pass redrawFrom as zero, (and still pass textsArray or valuesArray or both) and set the next argument isInsertion as number 1.
 isInsertion 
See the last statement in the previous argument.
 
Moreover, if this is passed as a positive number, it would force the function to use redrawFrom as a point of insertion (close range): instance if it is 1, after 0 it will insert and after having inserted it would add the original ones to the tail starting with 1.
The code
The Test Form
FIELD TEXTS ARRAY VALUES ARRAY
Enable textsArray? »
Enable valuesArray? »
SEL JUST RETURN REDRAW FROM
is Insertion? »
TEST MENU below:


COMMENT STRIPPER

Strips all the comments either form a javaScript passed as text, or by Html passed as text (javaScript doesn't handle files).
Can obfuscate (in such case the function will always removes one line comments by JavaScript, BUT you MUST be sure all the statements ends with a semicolon ";").
Returns the text without the comments and all the found comments as well.
Arguments
 text 
Is the input text which has to bee stripped of the comments. May be, for instance, the innerHTML property of some document section, or a function.toString() statement.
 isHTML 
Pass it as 1 if the first argument is not javaScript code (which comments are, namely, either // or /**/) but html code and therefore its comments are shaped as: <!-- -->
 
The whole function returns an Array of 3 entries:
  1. commentStripper(args)[0] : the original text without the commments (unless -se further on- the argument named keepComments is passed as 1)
  2. commentStripper(args)[1] [furtherIndex] : In fact it is an Array itself, whose each entry is one of the MULTILINE comments found in the javaScript.
  3. commentStripper(args)[2] [furtherIndex] : In fact it is an Array itself, whose each entry is one of the SINGLE LINE comments found.
 oneLineOutput 
If passed as 1, produces all the input text as a flush extending on one single line. Forcibly removes // comments by javaScripts, and you have to be sure all the statements end with the semicolon or the linear flush may mix up commands/keywords.
 keepComments 
Doesn't eliminate the comments from the first entry.
The code
The Test Form
Include below the code of one of these functions? »
isHTML? »
oneLineOutput? »
keepComments? »

Stripped text
Multiline comments
Single Line comments

expunger

Given an input Array or String, and given an optional comparative Array or String of chars, returns those chars/properties which are in common, and those which are unique to the input, and those which are unique to the comparison chars.
To this category of tasks belongs also hasher
Arguments
 input 
Either an Array of chars (or Objects) or a String of chars.
 
The function returns an Array of 3 entries, each of them an Array itself: the first holds all the entries which are in common between input and next argument named compare; the second and the third those chars which are not in common.
  1. expunger(param)[0] [furtherIndex] : in common
  2. expunger(param)[1] [furtherIndex] : not in common, derived from input: that is, are unique in input.
  3. expunger(param)[2] [furtherIndex] : not in common, derived from compare: that is, are unique in compare.
The returned outputs do not include duplicates.
 compare 
If not set (or passed as zero), defaults to a long list of chars and numbers, including punctuations. Otherwise it will be the passed object, which must be either an Array of chars or objects, or a string of chars.
 caseInsensitive 
If passed as 1, would perform the expunge process lower-casing all the chars. Therefore it applies only if you're positive either you're dealing with Strings or with an Array whose entries are string or stringed versions of numbers or punctuations.
 splitInput  splitCompare
If you pass them, the former would be used to split the input by the passed parameter (which must be a String); the same applies to the latter.
They default to an empty string, which is the most likely to fit your intentions.
The code
The Test Form

[Optional: -> USE THIS! » ]
[case insensitive? » ]

expunger
expunger(args)[1]; expunger(args)[2]
COMMON
expunger(args)[0];
UNIQUE IN INPUT

UNIQUE IN COMPARE

COMMON IN BOTH


REVERSER

Given an input String or Array, it reverses it all after splitting it (if a string) by a given parameter.
Arguments
 input 
Either a String or an Array.
 splitter 
Optional. The parameter it splits the string by. if input is String, returns String; if Array, just reverses and returns Array.
The code
The Test Form
splitter: »