SCRIPTING:

Natasha Henstridge
MISCELLANEOUS FUNCTIONS #3

I include here a few miscellaneous functions: routines mostly shaped over time when reading of a particular request on newsgroups and the alike. Some are simple, some are powerful.
January 2004
{ @ }

The model above is Natasha Henstridge
LOADS OF NATASHA HENSTRIDGE ON THE NET


MISCELLANEOUS FUNCTIONS
A few functions for various purposes of text formatting

The functions I feature are mostly text oriented functions, for which other pretty akin ones you may really want to check out are the wordWrapper and the fragmentString.

Van Gogh
Van Gogh, still nature, 1888
/ / / / / THE textFormatter
/ / / / / ITS CODEZ: / / / / /
  lines:
/ / / / / WHAT IT DOES:

This function is
CASE SENSITIVE.

To make this function work you need to define in your script (that is: you have to include an additional function too) also the function named:
imatch.
For your convenience its code is reproduced here:


Provide an input text as a string.
Joan Miro painting
a Joan Miro painting: a hair persecuted by two planets (!), 1968
The textFormatter function can then format this input string so to return the same string in a different format: such reshaped string is arranged into a set of lines of a given length, plus you can determine which char to put at the end of each line and which at the beginning of each line.
Basically, it is an emulation of the format an email acquires once it is quoted in a "reply email" environment.

This text formatter includes what I called the breakers management: the management of these breakers is a tricky issue that many scripters, upon tackling their first text formatting script would never expect to find in their way: but it does, as it did in my way, and may end up making of an allegedly carefree script a true nightmare that daunts you till dawn - as it did with me.
The question and the issue is: if you are to find a newline char in your input, how can you avoid that your script would produce lines spanning throughout manifold lines (indeed a tautology) simply because the function is counting in as a regular char whatever newline char it meets too? And what if you want to make this more flexible, so to manage more than just the newline char exception?

The default breaker in my function is the newline char "\n" yet you can pass to the function as many breakers as you want providing them in an Array meant to be passed as the last argument of the function (glance the code if you want). If for some reason you want to override even the default breaker, pass this argument as an empty array.
A breaker means, in my conception, a specific type of char or family of chars that, upon being met during the parsing process, must produce the following:
  1. The line which was currently built up is considered as concluded.
    It can still add any part of the string before the breaker only if such string can be added to it without exceeding the fixed maxLength: if it does, the part of the string before the breakers will make a line of its own, even if it is significantly shorter than the maxLenght.
    Basically, breakers when met impose sudden cut to the lines, as sort of exceptions.
  2. The breaker itself gets now added (and not discarded) soon after the maybe "prematurely" terminated previous line - and is added obviously enough unmodified, namely as it is regardless of what the breaker can be.
  3. A fresh brand new line count starts.
IMPORTANT NOTE: all new lines cumulate two hidden chars namely both the new line char traditionally represented as "\n") and a carriage return char (traditionally represented as "\r") which do count as valid chars though invisible to human eyes. That is, a new line environment would actually look like this:
line1\r
\nline2

Note the presence of the \r and \n chars that are revealed.
The textFormatter function removes all the carriage returns chars so to avoid strange invisible char influencing the formatting in an apparently unexplainable way (hidden chars are not easily accounted for!).
If you have reasons to want to overcome this, in order to achieve the removal of this feature you have to locate in the code the line that says:
aLine=aLine.replace("\r","");
and remove it manually.
/ / / / /
ARGUMENTS:
/ / / / /
RETURN:
  1. input: a string.
  2. maxLength: cannot be lower than number 1, and it sets the max length each line can have: a line can be accommodated to have an inferior length if the next word in the queue is too long, yet it can never produce a line whose amount of chars is higher than the number you prescribe here.
    Note that if the number is too short you end up with a formatting where at each line you have just one word.
  3. divisor: a string: the input gets split (if it was not passed as an array) by this divisor, and when the newly formatted version is produced, each split element is "rejoined" still using this divisor argument as the joining element. Defaults to a white space.
  4. newLine: the char you want as a new line; it defaults to "\n".
    If you want it to be another thing, like for instance a <br> html tag, you will have to pass that as a string.
    It is not counted in as far as the max length of the line is concerned.
  5. quoter: the char you want each new line to start with: it defaults to nothing, so if you want it to be something, you have to pass it as such.
    If you set it, the carriage-return char is counted in as far as the max length of the line is concerned.
  6. NOremoveLastDivisor: if passed as 1, doesn't remove eventually trailing divisors (such as the added white space) from the end of each line; by default the trailing white spaces (the default divisor) is eliminated from the end of each line.
  7. breakers: an Array (or nothing). It defaults to an array of one entry which is a "\n" new line char.
    For what this argument is for, see this function explanation section above titled «What it does».
It returns either false if something went wrong (like for instance inconsistent outputs), or a string.

Van Gogh
Van Gogh: fenced field at dawn, 1889
/ / / / / THE TEST FORM:
milla jovovich

The model above is Milla Jovovich
MILLA JOVOVICH ONLINE

divisor: maxLength:
newLine: quoter:
NOremoveLastDivisor
To overcome a test form issue namely to make the output visible, you should not set the newLine argument as a string "\n" but at most as an actual carriage return, namely hitting the keyboard Enter once focused the mouse pointer within the quoter text area: this is why the newLine field has been arranged as a multiline textarea instead than as a mere unilinear text input field. Anyway the textFormatter function already defaults to a new line so you can leave the carriageRetrun field untouched.
breakers (add separating them with a white space: the test form will produce in the background the suitable array to pass as breakers argument by splitting such input string by a white space. The function default breakers already include a "\n"):


input:

 
  add line length:
Van Gogh
Van Gogh, sower at dusk, 1888

/ / / / / THE stringExtractor
/ / / / / ITS CODEZ: / / / / /
  lines:
/ / / / / WHAT IT DOES:

To make this function work you need to define in your script (that is: you have to include an additional function too) also the function named:
imatch.
For your convenience its code is reproduced here, yet if by chance you want to know how it works you have to click the link above for the documentation:


francois boucher painting
a Francois Boucher painting: portrait of Madame de Pomapdour, 1756
Provided an input text as a string the function can do the following: finds out the match of a given parameter PARAM1 within the string (trivial instance, you want to find the word "hallo", and such word is PARAM1), then it finds out in the input the match of a second given parameter PARAM2 (if any) before the position of PARAM1; eventually, can find out a third parameter PARAM3 in the input string after the position of PARAM1; consequently, it can extract this whole segment and return it as an independent string subunit.

Check the arguments to see the variations that are allowed, and check the returned array to see the wealth of data returned.
Also, remember that the function can accept both strings as input to be searched for as well as regular expressions. It is assumed you have some minimal knowledge about what regular expressions are and about the fact they start with a leading and trailing forward slash char; trivial example:
/h\w{5}/
that expression searches for a word starting with a lowercase h and followed by 5 more letters.
It is also assumed you know what modifiers are (the elements that can make a search global instead than local, whereas global means that the search doesn't stop as soon as it has found a first match, but goes on to detect all possible matches; if all this sounds very obscure and if you have no clue, do check the linked file and peruse it a bit).

If you're not familiar with regular expressions and you're going to use this script crafting your own regular expressions, I strongly recommend to you to check the previously linked file and read with particular care at least the sub-section that deals with compile uncovered, because it points out a variety of arguably unexpected but possible odd behaviours when passing regular expressions to a function either from form fields, or from handwritten regular expression codex, or as strings meant to be compiled as new regular expressions.
/ / / / /
ARGUMENTS:
/ / / / /
RETURN:
  1. input: a string.
  2. text: the text you're searching for. Either a string or a regular expression. The g modifier does nothing here because the search is by default performed only on the first found instance of text within the input.
  3. before: a text you want to locate and which should be present (if absent the function would assume the beginning of the input) in the substring that antecedes the position of argument text.
    This argument must be either a string or a regular expression: whatever the case the search will be performed always globally, even overriding the absence of a g modifier in case this argument was passed as a regular expression but forgetting to append the g modifier to it.
  4. after: a text you want to locate and which should be present (if absent the function would assume the end of the input) in the substring that follows the position of argument text.
    This argument must be either a string or a regular expression: whatever the case the search will be performed always globally, even overriding the absence of a g modifier in case this argument was passed as a regular expression but forgetting to append the g modifier to it.
  5. caseInsensitive: the function is by default case sensitive; setting this argument as 1 or whatever would implement all the searches as case insensitive.
    If you pas it as a string, you have to include manually in the string the i modifier, so you can even include the notorious multiline m modifier. The g modifier, I stress again, is included anyway by default.
  6. includeBefore: if passed as, say, 1 determines the inclusion of the found instance of the argument before: in fact such instance is by default stripped upon returning the results.
  7. includeAfter: if passed as, say, 1 determines the inclusion of the found instance of the argument after: in fact such instance is by default stripped upon returning the results.
  8. veryBefore: if passed as, say, 1 imposes that among all the possible found instances of the argument before, the one closer to the beginning of the input string is the one that should be taken in (otherwise by default the function takes in the instance closer to the position of the found argument text).
  9. veryAfter: if passed as, say, 1 imposes that among all the possible found instances of the argument after, the one closer to the end of the input string is the one that should be taken in (otherwise by default the function takes in the instance closer to the position of the found argument text).
It returns either false if something went wrong, or an array of 7 entries. Such entries are as follows:
  1. [0]: the numerical index of the position of argument text within the input.
  2. [1]: the instance of text argument itself (a string).
  3. [2]: the numerical index of the position of argument before within the input.
  4. [3]: the instance of before argument itself (a string).
  5. [4]: the numerical index of the position of argument after within the input. Beware, this does not report the index position of the first letter of the after argument, but the index position of its last letter plus 1.
  6. [5]: the instance of after argument itself (a string).
  7. [6]: the whole extracted substring, accordingly to the passed arguments.
All these entries are suitable to be empty strings in case some match is not found (simple case: an after argument searched for and yet not found would determine entries 4 and 5 being both empty strings.)
keith haring painting
a Keith Haring painting: key burglar, 1981
/ / / / / THE TEST FORM:
amber model

The model above is from ambermodel.com
AMBERMODEL.COM ONLINE
input:

 
If the following are meant to be regular Expressions, please to simplify do not include the leading and forward slashes (the Test Form will remove them anyway).
text:
before:
after:
caseInsensitive:
includeBefore: includeAfter:
veryBefore: veryAfter:

The model on the right is Angela Lindvall
ANGELA LINDVALL ONLINE
Truly Artistic Shot by Great Photographers:
MARCUS PIGGOTT & MERT ALAS ONLINE
Angela Lindvall by Marcus Piggott and Mert Alas

/ / / / / THE codeRedrawer
/ / / / / ITS CODEZ: / / / / /
  lines:
/ / / / / WHAT IT DOES:
keith haring painting
a Keith Haring painting: untitled, 1985
This apparently simple snippet can help you to do a nifty thing I have heard a few times sought for on newsgroups: rearranging the internal codex of an existing javascript function, and then recompiling it to make its new version executable.

You provide this function with a string input (if you pass to it a function, it will convert it into a string by agency of the built in method toString) and a text to search for within such input: the function will return to you, in the shape of an array, a rationalized disaggregation of the input, namely what is before the matched text sought for within input, what is after such match, and if the input string was a function it will return also the name of the function, its signature and its isolated arguments. That is, a fully disagrregation so that you can scramble with its components accordingly to your plan.

You can therefore rebuild the whole input string of the input code and you can thus reinitialize a brand new function with the same name after having, for instance, changed on the fly a small segment of its inner codex.
Since the searched for text can be a regular expression, all the warnings about regular expressions I gave to you in the "what id does" section of the stringExtractor function are fully valid here too.

Once you have rearranged the codeRedrawer returned splinters
keith haring painting
a Keith Haring painting: untitled, 1985
so to yield a different stringed source code by jumbling them, the way to override an older function named, say, foo, and make such newly jumbled code the new executable code of such foo function, is to reinitialize such variable and assigning to it the new Function constructor which accepts as its two arguments string data types (exactly the data type the codeRedrawer returns!): the first argument meant to be the arguments of the new Function itself (must be passed as a string but without the parenthesis that normally surround a function argument), the second argument meant to be the body of the function, namely:

foo=new Function(args, rearranged_by_codeRedrawer)

provided both args and rearranged_by_codeRedrawer are already string data type. You can also use one argument only, and that is meant to be the body of a function without arguments.
/ / / / /
ARGUMENTS:
/ / / / /
RETURN:
  1. input: a string, or a function which will then be converted by the codeRedrawer into a string.
  2. locate: a string or a regular expression, it is the element you are searching for inside input.
  3. variation: must be a string.
    It is assumed you may want either to add or to swap the located element with a variation, or that you simply want to add the variation either before or after some segments of the fragments returned. Thus you should pass this parameter merely to have a more consistent output, comprehensive of this variation too (that is, nothing would prevent you from not passing it and rearranging the output by inserting the variation, if any, from elsewhere; yet having it already arrayed in the returned array is, to my eyes, somewhat convenient).
kenny scharf painting
a Kenny Scharf painting: Felix on a pedestal, 1982
It returns either false if something went wrong or an Array of 8 entries:
  1. [0]: the code from the world function up to the first closed round bracket (namely, the signature so to say).
  2. [1]: the name of the function alone.
  3. [2]: the arguments, inclusive of both round brackets.
  4. [3]: the arguments, exclusive of both the round brackets.
  5. [4]: the code before the found match.
  6. [5]: the instance of the found match.
  7. [6]: the code after the found match.
  8. [7]: the variation argument (or an empty string if no variation argument was passed to the function)
/ / / / / THE TEST FORM:
Stacy Keibler

The model above is Stacy Keibler
STACY KEIBLER ONLINE
input:

 
If the following are meant to be regular Expressions, please to simplify do not include the leading and forward slashes (the Test Form will remove them anyway).
locate:
variation:
keith haring painting
a Keith Haring painting: untitled 1982

/ / / / / THE stringShortener
/ / / / / ITS CODEZ: / / / / /
  lines:
/ / / / / WHAT IT DOES:
Joan Miro painting
a Joan Miro painting: the wing of the lark, surrounded with golden bleu, rejoins with the poppy dozing on the prairie of diamonds, 1967
The function takes in a string, and given as a second parameter the max length you want it to have, it shortens it in the middle, returning the shortened string with interposed a default symbolism made of two round brackets and three dots.
That is, supposed you want to curtail to a maximum of 10 chars an input string that says:
"An example to curtail"
the stringShortener function will split in two subsegments so that reunited they won't exceed the 10 chars you set, as an example, as its max length, and in the section which has been omitted it inserts a (...) set.

The function, returns the curtailed string and the part that has been removed both.
/ / / / /
ARGUMENTS:
/ / / / /
RETURN:
  1. input: a string.
  2. max: a number, signifying the max length that the curtailed string must have. The function will produce a string whose length is exactly equal to such number excluding from the computation the added interposing separator (see next argument).
  3. separator: if none, it defaults to:
    (...)
    If you pass this argument as something different than zero, what you passed, whatever it is, will be used as the interposing replacer of the omitted substring in the curtailed string.
  4. exceedFromEnd: if the division meant to split the input in two halves leaves a remains, you can decide whether this asymmetrical char should be appended exceeding from the heading half of the curtailed string (default), or from its tail (in such case pass this argument as 1).

painting on the right: Van Gogh: olive garnering, 1889 »
It returns either false if something went wrong or an Array of 2 entries: entry [0] is the curtailed String, entry[1] the removed substring.
Van Gogh
/ / / / / THE TEST FORM:
Angel Cassidy
input:

 
max length:
separator:
from End?



remains:
The model above is Angel Cassidy
LOADS OF ANGEL CASSIDY ON THE NET
Van Gogh painting
a Van Gogh painting: mousme on a chair, 1888