src/string.js

Method capitalize

Parameters:

  • obj must be a String.
    (String to capitalize)

  • all must be a Boolean.
    ([Optional] Capitalize each word)

Returns a String
(Capitalized String)

capitalize : function ( obj, all ) { all = ( all === true ); var result; if ( all ) { result = string.explode( obj, " " ).map( function ( i ) { return i.charAt( 0 ).toUpperCase() + i.slice( 1 ); }).join(" "); } else { result = obj.charAt( 0 ).toUpperCase() + obj.slice( 1 ); } return result; },

Method escape

Escapes meta characters within a string

Parameters:

  • obj must be a String.
    (String to escape)

Returns a String
(Escaped string)

escape : function ( obj ) { return obj.replace( /[\-\[\]{}()*+?.,\\\^\$|#\s]/g, "\\$&" ); },

Method explode

Splits a string on comma, or a parameter, and trims each value in the resulting Array

Parameters:

  • obj must be a String.
    (String to capitalize)

  • arg must be a String.
    (String to split on)

Returns an Array
(Array of the exploded String)

explode : function ( obj, arg ) { arg = arg || ","; return string.trim( obj ).split( new RegExp( "\\s*" + arg + "\\s*" ) ); },

Method hyphenate

Replaces all spaces in a string with dashes

Parameters:

  • obj must be a String.
    (String to hyphenate)

  • camel must be a Boolean.
    ([Optional] Hyphenate camelCase)

Returns a String
(String with dashes instead of spaces)

hyphenate : function ( obj, camel ) { var result = string.trim( obj ).replace( /\s+/g, "-" ); if ( camel === true ) { result = result.replace( /([A-Z])/g, "-$1" ).toLowerCase(); } return result; },

Method isAlphaNum

Tests if a string is alpha-numeric

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isAlphaNum : function ( obj ) { return validate.test( {alphanum: obj} ).pass; },

Method isBoolean

Tests if a string is a boolean

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isBoolean : function ( obj ) { return validate.test( {"boolean": obj} ).pass; },

Method isDate

Tests if a string a date

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isDate : function ( obj ) { return validate.test( {date: obj} ).pass; },

Method isDomain

Tests if a string is a domain

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isDomain : function ( obj ) { return validate.test( {domain: obj} ).pass; },

Method isEmail

Tests if a string is an email address

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isEmail : function ( obj ) { return validate.test( {email: obj} ).pass; },

Method isEmpty

Tests if a string is empty

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isEmpty : function ( obj ) { return ( string.trim( obj ) === "" ); },

Method isIP

Tests if a string is an IP address

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isIP : function ( obj ) { return validate.test( {ip: obj} ).pass; },

Method isInt

Tests if a string is an integer

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isInt : function ( obj ) { return validate.test( {integer: obj} ).pass; },

Method isNumber

Tests if a string is a number

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isNumber : function ( obj ) { return validate.test( {number: obj} ).pass; },

Method isPhone

Tests if a string is a phone number

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isPhone : function ( obj ) { return validate.test( {phone: obj} ).pass; },

Method isUrl

Tests if a string is a URL

Parameters:

  • obj must be a String.
    (String to test)

Returns a Boolean
(Result of test)

isUrl : function ( obj ) { return validate.test( {url: obj} ).pass; },

Method singular

Returns singular form of the string

Parameters:

  • obj must be a String.
    (String to transform)

Returns a String
(Transformed string)

singular : function ( obj ) { return obj.replace( /oe?s$/, "o" ).replace( /ies$/, "y" ).replace( /ses$/, "se" ).replace( /s$/, "" ); },

Method toCamelCase

Transforms the case of a String into CamelCase

Parameters:

  • obj must be a String.
    (String to capitalize)

Returns a String
(Camel case String)

toCamelCase : function ( obj ) { var s = string.trim( obj ).replace( /\.|_|-|\@|\[|\]|\(|\)|\#|\$|\%|\^|\&|\*|\s+/g, " " ).toLowerCase().split( regex.space_hyphen ), r = []; array.each( s, function ( i, idx ) { r.push( idx === 0 ? i : string.capitalize( i ) ); }); return r.join( "" ); },

Method trim

Trims the whitespace around a String

Parameters:

  • obj must be a String.
    (String to capitalize)

Returns a String
(Trimmed String)

trim : function ( obj ) { return obj.replace( /^(\s+|\t+)|(\s+|\t+)$/g, "" ); },

Method unCamelCase

Uncamelcases the String

Parameters:

  • obj must be a String.
    (String to uncamelcase)

Returns a String
(Uncamelcased String)

unCamelCase : function ( obj ) { return string.trim( obj.replace( /([A-Z])/g, " $1" ).toLowerCase() ); },

Method uncapitalize

Uncapitalizes the String

Parameters:

  • obj must be a String.
    (String to uncapitalize)

Returns a String
(Uncapitalized String)

uncapitalize : function ( obj ) { obj = string.trim( obj ); return obj.charAt( 0 ).toLowerCase() + obj.slice( 1 ); },

Method unhyphenate

Replaces all hyphens with spaces

Parameters:

  • obj must be a String.
    (String to unhypenate)

  • caps must be a Boolean.
    ([Optional] True to capitalize each word)

Returns a String
(Unhyphenated String)

unhyphenate : function ( obj, caps ) { if ( caps !== true ) { return string.explode( obj, "-" ).join( " " ); } else { return string.explode( obj, "-" ).map( function ( i ) { return string.capitalize( i ); }).join( " " ); } } };