1.0.6 • Published 9 years ago

js-extensions v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Classes

new Object()

Placeholder for native Object class. The methods below are extension methods.

Object.values(obj) ⇒ Object

Returns an array of values of a given object's own enumerable properties, in the same order as that provided by a for...in loop.

Kind: static method of Object
Returns: Object - The array of property values.

ParamTypeDescription
obj*The object whose enumerable own properties are to be returned.

Object.clone(obj) ⇒ Object

Makes a deep copy of an object

Kind: static method of Object
Returns: Object - The cloned object.

ParamTypeDescription
obj*The object to clone.

Object.equals(obj, other) ⇒ boolean

Makes a deep copy of an object

Kind: static method of Object
Returns: boolean - True if both objects are equal. False, otherwise.

ParamTypeDescription
obj*The first object to compare.
otherThe second object to compare.

Object.hashCode(obj) ⇒ int

Generates a hash code for an Object.

Kind: static method of Object
Returns: int - The hashcode of the Object.

ParamTypeDescription
obj*The object to generate a hashcode from.

Object.isNullOrUndefined(obj) ⇒ boolean

Determines if an Object is null or undefined.

Kind: static method of Object
Returns: boolean - True if the Object is null or undefined. False otherwise.

ParamTypeDescription
obj*The Object to test.

Object.merge(params) ⇒ *

Creates an object that includes members from the source objects

Kind: static method of Object
Returns: * - A new object that combines the source objects.

ParamTypeDescription
params*The object to copy merge.

JSON

Kind: global class

new JSON()

Placeholder for native JSON class. The methods below are extension methods.

JSON.safeStringify(str, replacer, spaces, cycleRepeater) ⇒ String

Safely serialize an object. Circular references will be tagged.

Kind: static method of JSON
Returns: String - The serialized version of the object.

ParamTypeDescription
str*The object to stringify.
replacerfunctionThe custom replace method. This param is optional.
spacesnumberThe number of spaces to user to 'prettify' the serialized string. This param is optional.
cycleRepeaterfunctionThe custom replace method for circular references. This param is optional

Example

var result = JSON.safeStringify(sobeObj); // Serialize, not 'pretty'.<br/>
var result = JSON.safeStringify(sobeObj, 2); // Serialize, 'prettify' using 2 spaces.<br/>
var result = JSON.safeStringify(sobeObj, myReplacer);<br/>
var result = JSON.safeStringify(sobeObj, myReplacer, spaces);<br/>
var result = JSON.safeStringify(sobeObj, myReplacer, spaces, myCycleReplacer);

JSON.serializer(replacer, cycleRepeater) ⇒ function

Create a replacer function for stringify that will can handle circular references.

Kind: static method of JSON
Returns: function - Returns a serializer function that can be used by stringify.

ParamTypeDescription
replacerfunctionThe custom replace method. This param is optional.
cycleRepeaterfunctionThe custom eplace method for circular references. This param is optional

String

Kind: global class

new String()

Placeholder for the native String class. The methods below are extension methods.

string.clone() ⇒ String

Returns a new string containing a copy of the current string.

Kind: instance method of String
Returns: String - A copy of the current string.
Example

var newString = 'Hello World'.clone();

string.endsWith(suffix) ⇒ boolean

Determines if the string instance ends with another string.

Kind: instance method of String
Returns: boolean - True if string instance ends with the suffix string. False otherwise.

ParamTypeDescription
suffixStringThe string to check for.

Example

var endsWith = 'string'.endsWith('ng');

string.equals(other) ⇒ boolean

Compares the current string to another string.

Kind: instance method of String
Returns: boolean - True if the current and other strings are equal. False otherwise.

ParamTypeDescription
otherStringThe string to compare.

Example

var isEqual = 'string'.equals('string');

string.format(params) ⇒ String

Formats a string using the current string as the format string. Numbered tokens are used to insert values e.g. 'Format string {0}, {1}, {2}'. The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter.

Kind: instance method of String
Returns: String - The formatted string.

ParamTypeDescription
params*One or more parameters to format into the format string.

Example

'Hello, my name is {0} {1}.'.format('John', 'Doe');

string.hashCode() ⇒ String

Generates a hashcode for the current string.

Kind: instance method of String
Returns: String - The hashcode for the current string.
Example

'Hello World'.hashCode();

string.isNullOrEmpty() ⇒ boolean

Determine if the current string instance is null, undefined, or zero length.

Kind: instance method of String
Returns: boolean - True if null, undefined, or zero length. False otherwise.

string.ltrim() ⇒ String

Creates a copy of the current string with the leading whitespace removed.

Kind: instance method of String
Returns: String - The current string with the leading spaces removed.
Example

'   Hello World'.ltrim();

string.md5() ⇒ String

Generates a MD5 message digest for the current string instance.

Kind: instance method of String
Returns: String - The MD5 message digest for the current string instance.
Example

'password'.md5();

string.print(params)

Prints a formatted string using the current string as the format string.

Kind: instance method of String
See: String.format

ParamTypeDescription
params*One or more parameters to format into the format string.

Example

'Hello, my name is {0} {1}.'.print('John', 'Doe');

string.printf(params)

Prints a formatted string using the current string as the format string.

Kind: instance method of String
See: String.sprintf

ParamTypeDescription
params*One or more parameters to format into the format string.

Example

'Hello, my name is %s %s, I am %d years old.'.printf('John', 'Doe', 46);

string.rtrim() ⇒ String

Creates a copy of the current string with the trailing whitespace removed.

Kind: instance method of String
Returns: String - The current string with the trailing spaces removed.
Example

'Hello World   '.rtrim();

string.sprintf(params) ⇒ String

Formats a string using the current string as the format string. The format string uses tokens e.g. 'Format string %s, %d, %f'

Kind: instance method of String
Returns: String - The formatted string.
See: String.sprintf

ParamTypeDescription
params*One or more parameters to format into the format string.

Example

'Hello, my name is %s %s, I am %d years old.'.sprintf('John', 'Doe', 46);

string.startsWith(suffix) ⇒ boolean

Determines if the string instance starts with another string.

Kind: instance method of String
Returns: boolean - True if string instance starts with the preffix string. False otherwise.

ParamTypeDescription
suffixStringThe string to check for.

Example

var startsWith = String.startsWith('string', 'st');

string.toBoolean() ⇒ boolean

Converts the current string instance to a boolean.

Kind: instance method of String
Returns: boolean - True if the string is 'true' or 'yes'. False otherwise.
Example

var isTrue = 'true'.toBoolean();

string.toCamelCase() ⇒ String

Creates a copy of the current string converted to camel case based on word breaks. The words may be separated with dash, underscore, period, or space.

Kind: instance method of String
Returns: String - The current string converted to camel case.
Example

'hello_world'.toCamelCase();

string.toTitleCase() ⇒ String

Creates a copy of the current string converted to title case.

Kind: instance method of String
Returns: String - The current string converted to title case.
Example

'hello world'.toTitleCase();

String.EMPTY : String

An empty string.

Kind: static constant of String

String.clone(string) ⇒ String

Makes a deep copy of a string.

Kind: static method of String
Returns: String - The cloned string.

ParamTypeDescription
stringStringThe string to copy.

Example

var string2 = String.clone('copy this string');

String.endsWith(string, suffix) ⇒ boolean

Determines if a string ends with another string.

Kind: static method of String
Returns: boolean - True if string ends with the suffix string. False otherwise.

ParamTypeDescription
stringStringThe string to check.
suffixStringThe string to check for.

Example

var endsWith = String.endsWith('string', 'ng');

String.equals(string, other) ⇒ boolean

Compares two strings for equality.

Kind: static method of String
Returns: boolean - True if strings are equal. False otherwise.

ParamTypeDescription
stringStringThe first string to compare.
otherStringThe second string to compare.

Example

var isEqual = String.equals('string', 'string');

String.format(format, params) ⇒ String

Formats a string using numbered tokens e.g. 'Format string {0}, {1}, {2}'.

Kind: static method of String
Returns: String - The formatted string.

ParamTypeDescription
formatStringThe format for the string.
params*One or more parameters to format into the format string.

Example

String.format('Hello, my name is {0} {1}.', 'John', 'Doe');

String.generatePassword(length, inclNumbers, inclSymbols) ⇒ string

Generate a password.

Kind: static method of String
Returns: string - A new randomly generated password.

ParamTypeDescription
lengthintLength of the new password.
inclNumbersbooleanWhether to include numbers in the new password.
inclSymbolsbooleanWhether to include symbols in the new password.

String.hashCode(str) ⇒ String

Generates a hashcode for the current string.

Kind: static method of String
Returns: String - The hashcode for the current string.

ParamTypeDescription
strStringThe string to hash.

Example

'Hello World'.hashCode();

String.ltrim(str) ⇒ String

Creates a copy of the current string with the leading whitespace removed.

Kind: static method of String
Returns: String - The current string with the leading spaces removed.

ParamTypeDescription
strStringThe string to trim.

Example

'   Hello World'.ltrim();

String.isNullOrEmpty(string) ⇒ boolean

Determine if a string is null, undefined, or zero length.

Kind: static method of String
Returns: boolean - True if null, undefined, or zero length. False otherwise.

ParamTypeDescription
stringStringThe string to check.

String.md5(str) ⇒ String

Generates a MD5 message digest for the current string instance.

Kind: static method of String
Returns: String - The MD5 message digest for the current string instance.

ParamTypeDescription
strStringThe string to compute digest.

Example

'password'.md5();

String.print(format, params)

Prints a formatted string.

Kind: static method of String
See: String.format

ParamTypeDescription
formatStringThe format for the string.
params*One or more parameters to format into the format string.

Example

String.print('Hello, my name is {0} {1}.', 'John', 'Doe');

String.printf(format, params)

Prints a formatted string.

Kind: static method of String
See: String.sprintf

ParamTypeDescription
formatStringThe format for the string.
params*One or more parameters to format into the format string.

Example

String.printf('Hello, my name is %s %s, I am %d years old.', 'John', 'Doe', 46);

String.rtrim(str) ⇒ String

Creates a copy of the current string with the trailing whitespace removed.

Kind: static method of String
Returns: String - The current string with the trailing spaces removed.

ParamTypeDescription
strStringThe string to trim.

Example

'Hello World   '.rtrim();

String.sprintf(format, params) ⇒ String

Formats a string using tokens e.g. 'Format string %s, %d, %f'.

Kind: static method of String
Returns: String - The formatted string.

ParamTypeDescription
formatThe format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order: An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the - sign is used on a number if it's negative. This specifier forces positive numbers to have the + sign attached as well. An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified. An optional number, a width specifier that says how many characters (minimum) this conversion should result in. An optional precision specifier in the form of a period (`.') followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string. A type specifier that says what type the argument data should be treated as. Possible types: % - a literal percent character. No argument is required. b - the argument is treated as an integer, and presented as a binary number. c - the argument is treated as an integer, and presented as the character with that ASCII value. d - the argument is treated as an integer, and presented as a (signed) decimal number. e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point. E - like %e but uses uppercase letter (e.g. 1.2E+2). f - the argument is treated as a float, and presented as a floating-point number (locale aware). F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). g - shorter of %e and %f. G - shorter of %E and %f. o - the argument is treated as an integer, and presented as an octal number. s - the argument is treated as and presented as a string. u - the argument is treated as an integer, and presented as an unsigned decimal number. x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).
params*One or more parameters to format into the format string.

String.startsWith(string, suffix) ⇒ boolean

Determines if the string starts with another string.

Kind: static method of String
Returns: boolean - True if string starts with the preffix string. False otherwise.

ParamTypeDescription
stringStringThe string to check.
suffixStringThe string to check for.

Example

var startsWith = 'string'.startsWith(, 'st');

String.toBoolean(string) ⇒ boolean

Converts a string to a boolean.

Kind: static method of String
Returns: boolean - True if the string is 'true' or 'yes'. False otherwise.

ParamTypeDescription
stringStringThe string to convert to boolean.

Example

var isTrue = String.toBoolean('true');

String.toCamelCase(str) ⇒ String

Creates a copy of the current string converted to camel case based on word breaks. The words may be separated with dash, underscore, period, or space.

Kind: static method of String
Returns: String - The current string converted to camel case.

ParamTypeDescription
strStringTHe string to convert case.

Example

'hello_world'.toCamelCase();

String.toTitleCase(str) ⇒ String

Creates a copy of the current string converted to title case.

Kind: static method of String
Returns: String - The current string converted to title case.

ParamTypeDescription
strStringThe string to convert case.

Example

'hello world'.toTitleCase();
1.0.6

9 years ago

1.0.5

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago