Cateva functii utile in JavaScript (1)

In ideea ca i-ar putea folosi cuiva care vrea sa aiba si in JavaScript ceva asemanator cu bibliotecile de rutine din C. Mai intai sunt niste functii care calculeaza diverse chestii:

[sourcecode language=’jscript’]
/* —————– NUMBERS ——————- */
//random number from 1 to maxValue
function getRandom(maxValue){
retValue = Math.round(Math.random()*(maxValue-1))+1;
return retValue;
}
//convert a number lower than 256 to hex
function toHex(val){
/* the first version (usefull for older browsers)
var chars = “0123456789abcdef”;
var i = val % 16;
var j = (val – i) / 16;
return “” + chars.charAt (j) + chars.charAt (i);
*/
return val.toString(16)
}
//convert from hex
function fromHex(val){
var retValue= parseInt(val, 16);
return retValue;
}
//compute |number|
function modulus(value){
if (value < 0){ value = 0 - value }; return value; } //percent function percent(percVal, value){ return Math.round((value * percVal) / 100); } /* -------------- STRINGS -------------------- */ //check if a particular expresion occurs in a string //(this doesn't use regular expresions) function checkFor(whatCheck){ if (this.indexOf(whatCheck) != -1){ return true; }else{ return false; } } //same as the above, but you can use regular expresions; function regCheckFor(whatFind){ temp= this.match(whatFind); if (temp == null){ return false; }else{ return true; } } //replace a substring (without regular expresions) function stringReplace(whatFind, replaceWithWhat){ tempString= this.split(whatFind) retValue= tempString.join(replaceWithWhat) return retValue } function regStringReplace(whatFind, replaceWithWhat){ pattern= new RegExp(whatFind, "g") retValue= this.replace(pattern, replaceWithWhat) return retValue } //substract a substring function stringSubstract(whatSubstract){ this.stringReplace(whatSubstract, "") } //and now the methods: String.prototype.check= (is.ns4 || is.ie4) ? regCheckFor : checkFor String.prototype.repl= (is.ns4 || is.ie4) ? regStringReplace : stringReplace String.prototype.substract= stringSubstract /* this was used instead of whatString.replace(whatFind, replaceWithWhat): findString= whatFind.substring(1, whatFind.lastIndexOf("/")); optionsString= (whatFind.lastIndexOf("/") != null) ? whatFind.substring(whatFind.lastIndexOf("/")+1, whatFind.length) : ""; pattern= new RegExp(findString, optionsString); tempText= new String (whatString); newText= tempText.replace(pattern, replaceWhithWhat); return newText; */ //this saves a little typing in some cases; function glue(){ retValue= "" for (var i = 0; i < glue.arguments.length ; i++) { retValue= retValue + glue.arguments[i] } return retValue } /* -------------- COLORS --------------- */ colNames= new Array('alice blue', 'antique white', 'aqua', 'aqua marine', 'azure', 'beige', 'bisque', 'black', 'blanched almond', 'blue', 'blue violet', 'brown', 'burly wood', 'cadet blue', 'chartreuse', 'chocolate', 'coral', 'corn flower blue', 'corn silk', 'crimson', 'cyan', 'dark blue', 'dark cyan', 'dark golden rod', 'dark gray', 'dark green', 'dark khaki', 'dark magenta', 'dark olive green', 'dark orange', 'dark orchid', 'dark red', 'dark salmon', 'dark sea green', 'dark slate blue', 'dark slate gray', 'dark turquoise', 'dark violet', 'deep pink', 'deep sky blue', 'dim gray', 'dodger blue', 'fire brick', 'floral white', 'forest green', 'fuchsia', 'gainsboro', 'ghost white', 'gold', 'golden rod', 'gray', 'green', 'green yellow', 'honey dew', 'hot pink', 'indian red', 'indigo', 'ivory', 'khaki', 'lavender', 'lavender blush', 'lawn green', 'lemon chiffon', 'light blue', 'light coral', 'light cyan', 'light golden rod yellow', 'light green', 'light grey', 'light pink', 'light salmon', 'light sea green', 'light sky blue', 'light slate gray', 'light steel blue', 'light yellow', 'lime', 'lime green', 'linen', 'magenta', 'maroon', 'medium aqua marine', 'medium blue', 'medium orchid', 'medium purple', 'medium sea green', 'medium slate blue', 'medium spring green', 'medium turquoise', 'medium violet red', 'midnight blue', 'mint cream', 'misty rose', 'moccasin', 'navajo white', 'navy', 'old lace', 'olive', 'olive drab', 'orange', 'orange red', 'orchid', 'pale golden rod', 'pale green', 'pale turquoise', 'pale violet red', 'papaya whip', 'peach puff', 'peru', 'pink', 'plum', 'powder blue', 'purple', 'red', 'rosy brown', 'royal blue', 'saddle brown', 'salmon', 'sandy brown', 'sea green', 'sea shell', 'sienna', 'silver', 'sky blue', 'slate blue', 'slate gray', 'snow', 'spring green', 'steel blue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'white smoke', 'yellow', 'yellow green'); colCodes= new Array('#f0f8ff', '#faebd7', '#00ffff', '#7fffd4', '#f0ffff', '#f5f5dc', '#ffe4c4', '#000000', '#ffebcd', '#0000ff', '#8a2be2', '#a52a2a', '#deb887', '#5f9ea0', '#7fff00', '#d2691e', '#ff7f50', '#6495ed', '#fff8dc', '#dc143c', '#00ffff', '#00008b', '#008b8b', '#b8860b', '#a9a9a9', '#006400', '#bdb76b', '#8b008b', '#556b2f', '#ff8c00', '#9932cc', '#8b0000', '#e9967a', '#8fbc8f', '#483d8b', '#2f4f4f', '#00ced1', '#9400d3', '#ff1493', '#00bfff', '#696969', '#1e90ff', '#b22222', '#fffaf0', '#228b22', '#ff00ff', '#dcdcdc', '#f8f8ff', '#ffd700', '#daa520', '#808080', '#008000', '#adff2f', '#f0fff0', '#ff69b4', '#cd5c5c', '#4b0082', '#fffff0', '#f0e68c', '#e6e6fa', '#fff0f5', '#7cfc00', '#fffacd', '#add8e6', '#f08080', '#e0ffff', '#fafad2', '#90ee90', '#d3d3d3', '#ffb6c1', '#ffa07a', '#20b2aa', '#87cefa', '#778899', '#b0c4de', '#ffffe0', '#00ff00', '#32cd32', '#faf0e6', '#ff00ff', '#800000', '#66cdaa', '#0000cd', '#ba55d3', '#9370db', '#3cb371', '#7b68ee', '#00fa9a', '#48d1cc', '#c71585', '#191970', '#f5fffa', '#ffe4e1', '#ffe4b5', '#ffdead', '#000080', '#fdf5e6', '#808000', '#6b8e23', '#ffa500', '#ff4500', '#da70d6', '#eee8aa', '#98fb98', '#afeeee', '#db7093', '#ffefd5', '#ffdab9', '#cd853f', '#ffc0cb', '#dda0dd', '#b0e0e6', '#800080', '#ff0000', '#bc8f8f', '#4169e1', '#8b4513', '#fa8072', '#f4a460', '#2e8b57', '#fff5ee', '#a0522d', '#c0c0c0', '#87ceeb', '#6a5acd', '#708090', '#fffafa', '#00ff7f', '#4682b4', '#d2b48c', '#008080', '#d8bfd8', '#ff6347', '#40e0d0', '#ee82ee', '#f5deb3', '#ffffff', '#f5f5f5', '#ffff00', '#9acd32'); function convertNameToCode(colN){ colN= colN.toLowerCase(); for (var i= 0; i < 140 ; i++){ if (colN == colNames[i]){ colN= colCodes[i]; } } return colN; } function convertCodeToName(colC){ colC= colC.toLowerCase(); for (var i= 0; i < 140 ; i++){ if (colC == colCodes[i]){ colC= colNames[i]; } } return colC; } [/sourcecode] Nu sunt copiate de nicaieri, desi unele dintre ele sunt probabil utilizate si de alti oameni care se joaca cu JavaScript.

1 thought on “Cateva functii utile in JavaScript (1)

  1. Pingback: Cateva functii utile in JavaScript (2) « Gramo’s World

Comments are closed.