js-extensions
v1.0.6
Published
Object and String extensions
Downloads
3
Maintainers
Readme
Classes
- Object
- new Object()
- .values(obj) ⇒ Object
- .clone(obj) ⇒ Object
- .equals(obj, other) ⇒ boolean
- .hashCode(obj) ⇒ int
- .isNullOrUndefined(obj) ⇒ boolean
- .merge(params) ⇒ *
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.
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | obj | * | The first object to compare. | | other | | The 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.
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | str | * | The object to stringify. | | replacer | function | The custom replace method. This param is optional. | | spaces | number | The number of spaces to user to 'prettify' the serialized string. This param is optional. | | cycleRepeater | function | The 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.
| Param | Type | Description | | --- | --- | --- | | replacer | function | The custom replace method. This param is optional. | | cycleRepeater | function | The custom eplace method for circular references. This param is optional |
String
Kind: global class
- String
- new String()
- instance
- .clone() ⇒ String
- .endsWith(suffix) ⇒ boolean
- .equals(other) ⇒ boolean
- .format(params) ⇒ String
- .hashCode() ⇒ String
- .isNullOrEmpty() ⇒ boolean
- .ltrim() ⇒ String
- .md5() ⇒ String
- .print(params)
- .printf(params)
- .rtrim() ⇒ String
- .sprintf(params) ⇒ String
- .startsWith(suffix) ⇒ boolean
- .toBoolean() ⇒ boolean
- .toCamelCase() ⇒ String
- .toTitleCase() ⇒ String
- static
- .EMPTY : String
- .clone(string) ⇒ String
- .endsWith(string, suffix) ⇒ boolean
- .equals(string, other) ⇒ boolean
- .format(format, params) ⇒ String
- .generatePassword(length, inclNumbers, inclSymbols) ⇒ string
- .hashCode(str) ⇒ String
- .ltrim(str) ⇒ String
- .isNullOrEmpty(string) ⇒ boolean
- .md5(str) ⇒ String
- .print(format, params)
- .printf(format, params)
- .rtrim(str) ⇒ String
- .sprintf(format, params) ⇒ String
- .startsWith(string, suffix) ⇒ boolean
- .toBoolean(string) ⇒ boolean
- .toCamelCase(str) ⇒ String
- .toTitleCase(str) ⇒ String
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.
| Param | Type | Description | | --- | --- | --- | | suffix | String | The 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.
| Param | Type | Description | | --- | --- | --- | | other | String | The 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.
| Param | Type | Description | | --- | --- | --- | | 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
| Param | Type | Description | | --- | --- | --- | | 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
| Param | Type | Description | | --- | --- | --- | | 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
| Param | Type | Description | | --- | --- | --- | | 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.
| Param | Type | Description | | --- | --- | --- | | suffix | String | The 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The string to check. | | suffix | String | The 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The first string to compare. | | other | String | The 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.
| Param | Type | Description | | --- | --- | --- | | format | String | The 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.
| Param | Type | Description | | --- | --- | --- | | length | int | Length of the new password. | | inclNumbers | boolean | Whether to include numbers in the new password. | | inclSymbols | boolean | Whether 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.
| Param | Type | Description | | --- | --- | --- | | str | String | The 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.
| Param | Type | Description | | --- | --- | --- | | str | String | The 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The 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.
| Param | Type | Description | | --- | --- | --- | | str | String | The string to compute digest. |
Example
'password'.md5();
String.print(format, params)
Prints a formatted string.
Kind: static method of String
See: String.format
| Param | Type | Description | | --- | --- | --- | | format | String | The 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
| Param | Type | Description | | --- | --- | --- | | format | String | The 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.
| Param | Type | Description | | --- | --- | --- | | str | String | The 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.
| Param | Type | Description | | --- | --- | --- | | format | | 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. 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The string to check. | | suffix | String | The 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.
| Param | Type | Description | | --- | --- | --- | | string | String | The 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.
| Param | Type | Description | | --- | --- | --- | | str | String | THe 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.
| Param | Type | Description | | --- | --- | --- | | str | String | The string to convert case. |
Example
'hello world'.toTitleCase();