String.js

Contains String prototypes and Number prototypes.

Author

Valerio Proietti, http://mad4milk.net

License

MIT-style license.

Summary
String.jsContains String prototypes and Number prototypes.
StringA collection of The String Object prototype methods.
Properties
testTests a string with a regular expression.
toIntparses a string to an integer.
camelCaseConverts a hiphenated string to a camelcase string.
hyphenateConverts a camelCased string to a hyphen-ated string.
capitalizeConverts the first letter in each word of a string to Uppercase.
trimTrims the leading and trailing spaces off a string.
cleantrims (String.trim) a string AND removes all the double spaces in a string.
rgbToHexConverts an RGB value to hexidecimal.
hexToRgbConverts a hexidecimal color value to RGB.
rgbToHexsee String.rgbToHex, but as an array method.
hexToRgbsame as String.hexToRgb, but as an array method.
Numbercontains the internal method toInt.
Properties
toIntReturns this number; useful because toInt must work on both Strings and Numbers.

String

A collection of The String Object prototype methods.

Summary
Properties
testTests a string with a regular expression.
toIntparses a string to an integer.
camelCaseConverts a hiphenated string to a camelcase string.
hyphenateConverts a camelCased string to a hyphen-ated string.
capitalizeConverts the first letter in each word of a string to Uppercase.
trimTrims the leading and trailing spaces off a string.
cleantrims (String.trim) a string AND removes all the double spaces in a string.
rgbToHexConverts an RGB value to hexidecimal.
hexToRgbConverts a hexidecimal color value to RGB.
rgbToHexsee String.rgbToHex, but as an array method.
hexToRgbsame as String.hexToRgb, but as an array method.

Properties

test

Tests a string with a regular expression.

Arguments

regexa string or regular expression object, the regular expression you want to match the string with
paramsoptional, if first parameter is a string, any parameters you want to pass to the regex (‘g’ has no effect)

Returns

true if a match for the regular expression is found in the string, false if not.  See http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Objects:RegExp:test

Example

"I like cookies".test("cookie"); // returns true
"I like cookies".test("COOKIE", "i") // ignore case, returns true
"I like cookies".test("cake"); // returns false

toInt

parses a string to an integer.

Returns

either an int or “NaN” if the string is not a number.

Example

var value = "10px".toInt(); // value is 10

camelCase

Converts a hiphenated string to a camelcase string.

Example

"I-like-cookies".camelCase(); //"ILikeCookies"

Returns

the camel cased string

hyphenate

Converts a camelCased string to a hyphen-ated string.

Example

"ILikeCookies".hyphenate(); //"I-like-cookies"

capitalize

Converts the first letter in each word of a string to Uppercase.

Example

"i like cookies".capitalize(); //"I Like Cookies"

Returns

the capitalized string

trim

Trims the leading and trailing spaces off a string.

Example

"    i like cookies     ".trim() //"i like cookies"

Returns

the trimmed string

clean

trims (String.trim) a string AND removes all the double spaces in a string.

Returns

the cleaned string

Example

" i      like     cookies      \n\n".clean() //"i like cookies"

rgbToHex

Converts an RGB value to hexidecimal.  The string must be in the format of “rgb(255,255,255)” or “rgba(255,255,255,1)”;

Arguments

arrayboolean value, defaults to false.  Use true if you want the array [‘FF’,’33’,’00’] as output instead of “#FF3300”

Returns

hex string or array. returns “transparent” if the output is set as string and the fourth value of rgba in input string is 0.

Example

"rgb(17,34,51)".rgbToHex(); //"#112233"
"rgba(17,34,51,0)".rgbToHex(); //"transparent"
"rgb(17,34,51)".rgbToHex(true); //['11','22','33']

hexToRgb

Converts a hexidecimal color value to RGB.  Input string must be the hex color value (with or without the hash).  Also accepts triplets (‘333’);

Arguments

arrayboolean value, defaults to false.  Use true if you want the array [255,255,255] as output instead of “rgb(255,255,255)”;

Returns

rgb string or array.

Example

"#112233".hexToRgb(); //"rgb(17,34,51)"
"#112233".hexToRgb(true); //[17,34,51]

rgbToHex

see String.rgbToHex, but as an array method.

hexToRgb

same as String.hexToRgb, but as an array method.

Number

contains the internal method toInt.

Summary
Properties
toIntReturns this number; useful because toInt must work on both Strings and Numbers.

Properties

toInt

Returns this number; useful because toInt must work on both Strings and Numbers.

Trims the leading and trailing spaces off a string.
Converts an RGB value to hexidecimal.
Converts a hexidecimal color value to RGB.