Subroutine to fragment an Array
|
|
This subroutine will fragment the array and if the length of the array cannot be divided by the given size the last fragment will rank only the remaining entries
|
First your subroutine to fragment an Array (all comments on how to use it in the 3rd section):
Subroutine to fragment a String
|
|
This subroutine will fragment a string and if the length of the string cannot be divided by the given size the last fragment will rank only the remaining entries
|
Now your subroutine to fragment a String (all comments on how to use it in the next section):
Comments
|
|
How they work and how get invoked
|
Here is how it works, you need to know to make it work properly:
- You give to it two arguments: array which is the array to fragment, and size which is the size that each fragment must have.
- Then it initialize the output: you must be aware that the output will be an array of array: that is, an array whose each entry will be an array of the specified size ranking entries from the input array argument. Thus:
- When you invoke the function you will get as a return an array whose each entry is an array of the given size, thus you can invoke the function either adding an index or without an index
- If you add no index, an alert box would display all the entries in a subsequent stream: do not get fooled by the appearances, since they are really a concatenated list of arrays! In fact:
- If you give an argument you can invoke as follows:
fragmentArray(array,5)[0] whereas you can use a size whatever number, and as an index too: of course, if you invoke and index such as, for instance, [4] and the returned array length is inferior than 4, you'd get an error, so maybe before sorting the indexes you'd check whether the length of the output matches with the desired index, statements such as: fragmentArray(array,size).length
- Then we check to see if size argument is, by chance, zero: in such a case we assume you want no entries, thus we return a simple array of no length
- On the other hand if the specified size is higher than the array argument length we return an array of array, whose unique entry is the whole original array passed as argument, thus
fragmentArray(array,5)[0] would yield only entry [0] and it will have appended the array passed as argument (whose size, necessarily, will be inferior to the size argument)
- If on the other hand the size is shorter than the array length, we start fragmenting it.
- If at a certain point the subroutine realizes the last fragment would be shorter than the provided size, it will append as last entry an array whose length will be inferior to the size
- To get entries from a single fragment, either you
- invoke them such as (for instance):
fragmentArray(array,7)[1][4] - Or you assign each entry to new variables and then you get the entries from the new variable, such as:
var aFragment=fragmentArray(array,7)[0]; alert(aFragment[3])
- The fragmentString() function will behave like the fragmentArray() one, only it gets a and will yield an array whose each entry is not an array but a string of the desired length (size argument) with the last entry string length accommodated to the available size (that is: same reasoning as point #6)
Here is an example for you made on the following test array, divided by a size of 3:
var testArray=new Array(0,1,2,3,4,5,6,7,8,9,10)
Test the fragment String function (beware white spaces count in as valid characters in the output):
fragmentString(, );
Subroutine to Wrap a String
|
|
This subroutine will search for matches of a given pattern in a string and would wrap it with the given before and after arguments.
|
Here is the subroutine which can word wrap elements in within a string: It is a powerful script, and for this purpose probably the most complete out there as far as javaScripts for free are concerned.
It can
- Word wrap before and after a match, or only before (pre-pending) or only after (appending), accordingly to the way you pass the arguments named exactly before and after, as you're to see.
- Word wrap whole words.
- Word wrap specific subsegments in within words.
- Word wrap UNspecific subsegments in within unknwon words.
First the code, then the explanations.
In order to appreciate the workings of this nifty function, you may want to check how the replace method works in JavaScript Regular Expressions.
Anyway: the commitment of this subroutine is to take in a string as its string argument and to check in it all (all by default) the instances of the given regexp argument (as we're to see this can be either a string or a regular expression), and it would wrap each found instance with the passed before argument before it and the passed after argument
after it.
If no before and/or no after argument(s) are passed they default to empty strings. Be warned that if you pass zero, the function would assume you want to add either before or after each found instance of the regexp argument in within the string the passed number zero itself! therefore to bypass an argument, the best way in this function is to pass it as an empty string: ""
wordWrapper() ARGUMENTS |
|
STRING |
Just the input string to manipulate. The function returns a copy of it, namely does not affect the original by default. |
REGEXP
Renoir: Path in high lawn, 1874
|
It is the text you're searching for.
It can be either:
- a string: as such must be passed in between quotes.
- a Regular Expression: as such it would appear like any regular expression would look like: instance:
/\w{5}/
If you do not know what regular expressions are, I insist you can find a well crafted file at: JavaScript Regular Expressions.
If it is a regular expression, you can add to it your modifier flags, such as for instance:
/\w{5}/g
to flag you want a global search of the possible matches.
If you forget passing the flags but you do pass the flag argument (see further on for more on it), the function would work out a way to add the flags to your regular expression even if they were not there in your regular expression!
In case you pass it as a regular expression, you'd be aware the function uses the backreferencing capabilities of the regular expressions (see replace method in the file already previously linked), namely will wrap the text that is in within the first parenthesized item it finds in the regular expression: example:
/\w{4}/
If you forget the parenthesis in your regular expression, the function will add them assuming the whole expression is meant as the item to capture with backreferencing (note that if all this sounds mysterious to you, none the less it is very likely exactly what you meant to achieve!).
If the argument is a string, the function will eventually turn it into a regular expression and would parenthesize it all by itself. Cool, isn't it?
|
BEFORE |
Must be a string, which can also include html tags, no problem.
It is the text that will go before each found match. |
AFTER |
Must be a string, which can also include html tags, no problem.
It is the text that will go after each found match. |
FLAG
Monet: Camille in green, 1866
Monet: Breakfast on the lawn, 1865
|
If it is not passed at all, the function defaults to global and case insensitive, and would append it to regexp too if it is a regular expression 8that is, the flag argument if passed, overrides the modifiers you may have included -or not!- in the regexp argument if it was not a string but a full fledged regular expression.
If you want to add no modifiers, pass this argument as an empty string: ""
If you want to pass only some specific modfier(s), pass them as a string, example: "i"
that example would generate a flag for the regular expression making it only case nsensitive but not global too.
Or, conversely: "g"
that example would generate a flag for the regular expression making it only lobal but not case insensitive too.
Obviously, passing both "gi" would make the expressions both case insensitive and global as well.
Last, if this flag argument is passed as an empty string, this latest case would not override the flags included in the regexp if it was passed as a Regular Expression (would not strip them off if they were included in the regular expression, that is.)
Very technical: if perchance you have to word wrap text in within the input string which is parenthesized on its own - that is, for some reasons you're trying to word-wrap a text like: "(Hallo)" - then proceed as follows:
- Pass the regexp argument always as a Regular Expression: instance in our silly expression:
/(Hallo)/
- Escape the parenthesis meant to be searched for:
/(Hallo)/
- C R I T I C A L : Wrap all that is inside the forward slashes in within non escaped parenthesis:
/\(Hallo\)/
[ Why this latest step is, in this latest case, critical?
The replace method runs in the function using backtracking by the keyword $1: if the parenthesis inside the regular expressions are escaped and not included in within non escaped parenthesis, the replaceing method would get no element whatsoever to backtrack for backtracked elements should have been exactly the parenthesized (and non excaped) ones in the regular expression: consequently it would replace the matching text found in the input with... before++after: that is: $1 would be printed down as such, as a string! ]
This would positively word-wrap instances of "(Hallo)" like, say and regradless of how pointless the example may be:
Hallo world, I said (Hallo)
|
|
COMPLEX
Van Gogh: Oiran,
1887
|
This is an advanced feature indeed, insofar to take avail of it you must know how to craft or use regular expressions and how to pass/craft the second argument to the builtin javaScript function named replace().
It must be a String, namely in between quotes.
By passing this argument you completely override the before and after arguments.
In order to let you understand what it does, I have to make an example first: let's imagine you want to word wrap letters inside a word like LL inside:
HALLO
Let's imagine you want to show them in bold inside some html text, namely you have:
HALLO
and you want to get:
HA<strong>LL</strong>O
Well, to do that you have to capture (I insist: it is advanced, read the green box in the flags argument above at least, or the file on regular expressions just mentioned above) as many as 3 elements:
- HA
- LL
- O
In fact you do want to change the LL, but you don't want the HA and the O being stripped off but preserved!
Word wrapper cannot guess so much: but leaves you a way to include complex types of swappings by setting this argument complex. Of course, I could have included a way to let you wordWrap inside words trying to make it easier for you, but if I am to let in 3 captures, it means I'm on the road I'd wonder also: why not 4 or 5 or 6 or 7 then? So I had to decide for an universal approach.
If you do not capture all the three elements in such cases, but you capture only LL with only one set of parenthesis ( capturing is made by parenthesis, remember the green box above?), a replace method would do the following:
- replace(/HA(LL)O/gi, "<strong>$1</strong>")
- Grabs the HALLO as a whole
- Replaces it all with: <strong>LL</strong>
That's not what you want. What you want is this:
replace(/ (HA)(LL)(O)/gi, " $1<strong> $2</strong> $3")
Which does the following:
- Grabs a matching HALLO as a whole
- CAPTURES each: HA, LL and O
- Replaces
- replaces HA with HA (is $1)
- introduces a <strong>
- replaces LL with LL itself (it is $2)
- introduces a <strong>
- replaces O with O (it is $3)
Doing this, it preserves the whole HALLO but changes a section in it.
To achieve this you have then to pass the complex argument as the second argument meant to be hold by the replace method running inside wordWrapper. This also means that the regexp argument must be a full fledged regular expression (inclusive of modifiers like g or i if required) and obviously carrying as many round parenthesis sets as many the respective $ are, and must carry the right replacer around one of the $x instances.
So a correct expression for our example is:
wordWrapper(
"Hallo world Hallo",
/(HA)(LL)(O)/gi,
"" /*before: would just be ignored if complex is set*/,
""/*after: would just be ignored if complex is set*/,
"gi"/*flags: optional (override flags in regexp)*/,
"$1<strong>$2</strong>$3"
);
|
|
More on the complex argument
You may be wondering: what if I am to jumble the complex argument by a script and I don't know what it may look like as far as what goes before and what goes after are concerned?
Well, just initialize a foo variable meant to carry inside two variables meant to carry whatever the before and after are to be for the current run, and concatenate it all in it as a string by +=:
var foo="$1"+aVar1+"$2"+aVar2+"$3";
and then you pass this foo as the argument named complex.
Last but not least, you may be wondering the same as far as the regexp argument is concerned: namely: what if I want to match something I know but I've no clue what is before and after it?
A non trivial example is this: an user inserts in a field a text he/she wants to search for in a html file, and you want to return to him/her the highlighted matches in within whatever string element you may found instances of the searched text. Imagine an user searching for the word " script": you may want to report also words including the term, but you may know not what they may be in an unknown text. Or imagine you want to highlight all the letters L and you obviously have no clue where they may be.
Here is a possible regular expression that would meet this request:
/(\w*[^L])?(L)(\w*)/
The relative complex argument to pass and matching this regexp argument, can still and well be the same as in our latest example: $1<strong>$2</strong>$3
In the Regular Expression crafted above, the symbol \w* means whatever ( *) word \w amount (none too, if the L is at the beginning), followed by something which is NOT [^] an L ( Greed in Regular Expressions), all that optional ( ?) to allow for an isolated leading L, followed by an L followed by whatever optional amount of letters. Note that all the relevant items are parenthesized and therefore captured and suitable to be andled by replace as $1, $2, $3.
If also the regular expression would be built via script (for instance because you don't know beforehand what is to be searched for, wheter L or who knows what), build it as a mere string still concatenated by +=. Once completed such string via script, turn it into a regular expression by the compile method (on the compile method, I insist: Mastering Regular Expressions in Javascript) and pass the compiled version as the regexp argument (passing complex requires as nearly mandatory that the argument regexp is passed as a regular expression, not as a string).
So, what else can I do for you at UnitedScripters, my dear script raider? A lot of guys out there doing all this for you?
|
|