helper-meth
Helpful methods on String, Array, Object, Number in Javascript
- Improved version of Array's
includes
method, introduced in ES6. - Deep/Nested checking of values inside Array and Object. Using recursion.
- Has shorthand methods
- Some method's inspiration/naming taken from ruby language.
Methods
- Generic Methods
- .is_n()
- .is_i()
- .is_float()
- .is_s()
- .is_a()
- .is_o()
- .is_f()
- .is_json()
- .to_json()
- .present()
- .empty()
- .count()
- .equals() (Checks if String, Integer, Boolean or Object are equals, Deep Checking)
- Object Specific Methods
- Object.to_query()
- Object.has() (Key exists or Key-Value pair exists in Object)
- Object.hasValue() (Value exists in any key in Object)
- Array Specific Methods
- Array.equals()
- Array.includes() (Enhanced versin of ES6's include method)
- Array.to_sentence()
- String Specific Methods
.is_n()
Returns true if variable is an Number (Integer/Float). Also accepts optional arugments: true
, if strict type checking is required in case of string:
var num = 1;var float1 = 13;var str = "1";num1 // return truefloat1 // return truestr // returns truestr // returns false
.is_i()
Returns true if variable is an Integer/Number. Also accepts optional arugments: true
, if strict type checking is required in case of string:
var num = 21;var str = "Hello world!";num // return truestr // returns false
.is_float()
Return true if variable is a Float. Also accepts optional arugments: true
, if strict type checking is required in case of string:
var num1 = 21;var num2 = 21;num1; // return true`num2; // returns false
.is_s()
Return true if variable is a string.
var str = "Hello world!";str; // returns true
.is_a()
Return true if variable is an Array.
var arr = 1 2 3;arr; // returns true
.is_o()
Return true if variable is an Object.
var obj = hello: "World";obj; // returns true
.is_f()
Return true if variable is a Function.
var {};func; // returns true
.is_json()
Return true if variable JSON.
var json = '{"hello":"world"}';json; // returns true
.to_json()
Convert Array or Objects to JSON type
var json = "hello":"world";json; // returns stringify version of variable
.present()
Check if Array has elements, String has characters, Object has keys. In short, they are not empty.
var str1 = "Hello";var str2 = "";str1; // Returns truestr2; // Returns false var arr1 = 1 2;var arr2 = ;var arr1 // Returns truevar arr2 // Returns false var obj1 = "hello":"world";var obj2 = {};var obj1 // Returns truevar obj2 // Returns false
.empty()
Inverse of .present()
method. it checks if Array, String or Object is empty.
var str1 = "";str // Returns true
.count()
Count the number occurrence of an item/character in an Array or String.
var str = "Hello World!";str; // returns: 2 var arr = 1 2 4 1 4 4;arr //returns 3
.equals()
Returns true if String or Number is equal.
var num1 = 1;var num2 = 12;var str = "1"; num1 // returns true;num1 // returns false;num2 // returns true;str // returns truestr // returns false
Object.to_query()
Converts object to query parameter string.
var obj = hello: "world" goodbye: "world";obj;// Outputs: "hello=world&goodbye=world"
Object.has()
Returns true if the Key or Key-Value pair exists in the Object.
Takes two argument: First key_name
, Second value_name
(Optional).
var obj = hello: "World!";obj; // returns: trueobj; // returns: falseobj // returns true // Deep Nested Checkingvar obj1 = hello: 'world!' good: bye: 'world';obj1
Object.hasValue()
Returns true if the Value exists in the Object.
var obj = hello: 'world!' good: bye: 'world';obj; // return trueobj; // return trueobj; // return false
Array.equals()
Checks if two Arrays are same. This works for nested array as well.
1 2 // return true1 2 3 4 // return true1 2 // return false
Array.includes()
ES6 has .includes()
method, but this method fails if we check if array element is present in an Array:
1 2 1 // ES6 version will return false
New implementation will use .equals()
method and will check if another Array is present in Array.
1 2 // return true1 2 3 4 // return true1 2 // return false
Array.to_sentence()
Converts an array to human readable sentence:
'John' "Johny" "Jenna"; // Output: "John, Johny and Jenna"
This method accepts two parameter: last_word_connector
, where you can specify which word will connect the last word with rest of the Array values
'John' "Johny" "Jenna";// Output: "John, Johny or Jenna"
Another parameter is: word separator
, instead of default ,
:
'John' "Johny" "Jenna";// Output: "John Johny or Jenna"
String.to_a()
Converts String to Array:
"Hello" // returns: ["H", "e", "l", "l", "o"]
String.to_o()
Convert string to Object:
"Hello" // returns: {H: "H", e: "e", l: "l", o: "o"}
String.includes()
returns true if string contains substring
"Hello World!" // return true "This World!" // return false
String.upcase()
Converts string charactores to Uppercase:
"Hello" // returns: HELLO
String.downcase()
Converts string charactores to Lowercase:
"HeeLLO" // returns: heello
String.titleize()
Converts first letter of word to Uppercase. Optional argument: true, if passed will convert all words in a sentence to titlecase else only the first letter.
"hello world!" // returns: "Hello world!""hello world!" // returns: "Hello World!"
String.pluralize()
Converts a string to customizable plural text based on number passed as argument. This is not grammatically perfect, that is why added second argument: pluralText
which will get return if the number is greater than 1;
"size" // returns: "sizes""size" // returns: "size""size" // returns: "so many sizes""size" // returns: "size"