<!-- Importing of text utilities Text.js provided by MyersDaily. For documentation see https://www.angelfire.com/yt/jmyers/bin/pod/Text.shtml. --> <script type="text/javascript" src="https://www.angelfire.com/yt/jmyers/free/Text.js"> </script>
var Text = { as:function(a1) { return '' + (a1 || '') }, is:function(a1) { return '' + a1 === a1 }, no:function(a1) { a1 = isNaN(a1=Number(a1)) ? parseFloat(a1) : a1; return isNaN(a1) ? 0 : a1; }, ex:function(a1,a2) { var mo = Math.floor(a2/2); if (mo > 1) return this.ex(a1 + a1, mo) + (a2 % 2 && a1 || ''); var fo = ''; while (a2 > 0) { fo += a1; a2-- } return fo; }, pad:function(a1,a2,a3) { return this.is(a1) ? a1 + this.ex(a3 || ' ', a2 - a1.length) : this.ex(a3 || ' ', a1 - ('' + a2).length) + a2; }, trim:function(a1,a2,a3) { a1 = this.as(a1).replace(a3 || /^\s+|\s+$/g, ''); return a2 && a1.length > a2 ? a1.substr(0,a2-1) + '\u2026' : a1; }, line:function(a1) { var OS = navigator.platform, NL = OS.indexOf('Mac') != -1 && '\r' || OS.indexOf('Win') != -1 && '\r\n' || '\n'; return this.as(a1) + NL; }, code:function(a1) { return (a1=this.as(a1)) ? "unescape('"+escape(a1)+"')" : "''" } }
All functions are available through the Text object, i.e. Text.function(arguments). They are helpful in formatting text, conversion to numbers, value comparisons, newline resolution, conversion to evaluable code, etc.
Eight built-in functions are provided. The as() and no() functions convert to and from strings/numbers respectively. The is() function confirms a value as text, and the ex() function expands a compressed string value. The pad() and trim() functions pad and trim text, and the line() and code() functions help work with newlines and create evaluable code from text.
Conversion of data from one form to another, or determining the identity of a value is very important in many applications. If someone types in 5 and 10 into text boxes, you want to make sure that if, for instance, they clicked "Add" that the answer given would be 5 + 10, rather than '5' + '10' -- the answer an unsuspecting coder would end up with. For another example, did the user type in 'null' as their username, or click Cancel and create a null response from the prompt dialog?
Ensuring the representation of a value as text can be critical! The text.as() function converts values to text, but non-textual "false" values such as null, false, 0, or undefined will return an empty string instead of an ugly word.
The no() function does the opposite, converting strings to numbers (including conversion of standard dates to their representation in milliseconds).
The is() function returns a Boolean value of whether or not its argument is truly a string
.Formatting text is certainly an obvious task. Padding and cleaning up of text is an important job that is done well by these functions. Think of the last time you've gotten a handwritten receipt at checkout, or done addition without lining up decimal places. Once you've used them you'll find that doing without is like using carbon paper instead of a copier.
The ex() expansion function expands short text into a longer string. It makes it easy to create strings of an exact length.
The pad() function pads text to either the right or left margin. Using this makes it easy to arrange prices in columns along the decimal point, etc. Note that the filler should be one character in length.
"Trimming" out whitespace or other characters is important to clean up input before you can use checking functions like credit card checksum operations or telephone number validation. The optional second and third arguments specify a maximum length and an override for the default elimination of whitespace characters, respectively.
The functions relate to the ASCII table, making correct newline characters (an OS dependent function) and establishing the character codes for easy creation of code on-the-fly.
The line() function returns the correct newline for the platform it runs on. The newline is added to the end of the string if specified, or returned alone
.The code() function converts a line into its representation in JavaScript, allowing you to create code on-the-fly from arbitrary input.
Text.as() == '', Text.as(5000) === '5000', Text.as('text') == 'text';/*
A "non-true" value is a value that would evaluate as false by Boolean(). A logical || (or) comparison is made -- value || '', eliminating dirty strings like 'undefined'.
The following statements would be true:
Text.as(value) == (value-as-string), Text.as() == '', Text.as(non-true-value) == '';*/
Text.is(Text.as()) == true, Text.is(Text.no()) == false;/*
Simple but valuable, I'm sure I don't need to explain this one.
The following statements would be true:
Text.is(string) == true, Text.is(non-string) == false;*/
Text.ex('#', 15) == '###############';/*
Text.ex() is a recursively optimized function. [ Note:
If this were not so, to shorten run time you would nest calls to Text.ex() when you could. For example, Text.ex(Text.ex('hello', 45), 10) runs much faster than simply Text.ex('hello', 450). (6-7 ms as compared to 1-2, but still a difference).
]
However, the function now uses a powerful algorithm to enable it to create huge strings almost instantaneously. The following is an excerpt from the performance notes I made when optimizing. (Obviously there is a huge improvement in time usage.) The optimal string length is a power of two.
Old version 318 ms for 10,000 dashes, and 1411 for 20,000. New version 6 and 17 ms respectively for the same amounts. NEW version 2 and 3 ms respectively, for the same amounts, or 161 ms for 1,000,000!
As you can see the function uses the powerful technique of recursion to expand the string explosively rather than by unary addition of characters. In fact, 13,369,344 dashes were once generated in under 2 seconds!
*/ /*Text.pad(10, '$3.00') == ' $3.00', Text.pad('$3.00', 10) == '$3.00 ' (string to the right -- as the second argument), and Text.pad('#', 20, '-') == '#-------------------' (string to the left -- as the first argument);
Text.pad() determines whether to pad right|left by whether the first argument is really text. Remember that it pads the string onto its side with padding of the given length.
Basically, if the first argument is a string then that string will be padded to the left with filler on the right until the specified length. Otherwise the string given as the second argument will be padded right with filler on the left to the specified length. Take note that strings with lengths greater than the indicated padding margin are not truncated.
*/Text.trim(' hello there ') == 'hello there'; Text.trim('zipcode = 67154-9718', 6, /\D+/g).substr(0, 5) == '67154'; Text.trim('zipcode = 67154-9718', 10, /[^-\d]+/g) == '67154-9718';/*
Whatever is matched by the optional regular expression (which defaults to match all leading and trailing whitespace) will be deleted.
The second example above does the following:
Knock out all non-digits; limit length to 5. If we had run it like this -- Text.trim('zipcode = 67154-9718', 5, /\D+/g) -- it would have returned '6715' and the ellipsis (Unicode 2026), indicating visually that the value was truncated.
We got the whole zip (i.e. 67154-9718) by changing the second and third arguments to 10 and /[^-\d]+/g respectively.
*/These tools provide valuable textual capabilities able to be used by various Internet documents. Import the script into your document in a separate script element, above any other scripts which use the functions contained in Text.js.
Using these tools, its easy to make super cool functions like the following number-to-dollar function.
function Cash(N) { N = Math.round(Text.no(N)*100); return '$' + Math.floor(N/100) + '.' + Text.pad(2, Math.abs(N%100), '0'); }
Now Cash(12) == '$12.00', Cash(0.263432) == '$0.26', and Cash(0.006) == '$0.01' -- see how cool the Text library is?! (And it's only 917 bytes too.)
MyersDaily created by Joseph K. Myers 1999-2000. Free hosting by Angelfire!!Terms-of-Service |