js-methods for JavaScript bundles a lot of extensions to the language's core classes to simplify development for the browser or for Node.js. This extensions doesn't depend on any other code or overwrite existing methods.
If you need 'string' and 'array' methods, put this lines to your script.
<script src="https://raw.github.com/harrydeluxe/js-methods/master/string.min.js"></script>
<script src="https://raw.github.com/harrydeluxe/js-methods/master/array.min.js"></script>
Installation
npm install --production js-methods
require('js-methods'); // Now you have all methods to dispose. Try it!
"javascript".md5(); // 'de9b9ed78d7e2e1dceeffee780e2f919'
Extends string prototype with the following methods: camelize, capitalize, collapseSpaces, format, ltrim, pad, remove, repeat, reverse, rtrim, sprintf, stripTags, trim, truncate
The string.min.js
is super lightweight (< 1k Gzipped).
Converts a string separated by dashes and/or underscores into a camelCase equivalent. For instance, 'java-script' would be converted to 'javaScript'.
"java-script".camelize(); // "javaScript"
"java_script_methods".camelize(); // "javaScriptMethods"
"java_script-methods".camelize(); // "javaScriptMethods"
Capitalizes the first letter of a string and downcases all the others.
"my name is harry".capitalize(); // "My Name Is Harry"
Converts all adjacent whitespace characters to a single space.
" My name is Harry! ".collapseSpaces(); // "My name is Harry!"
Substitute substrings from an array into a format String that includes '%1'-type specifiers
"My %0 is %1".format(['name','Harry']); // "My name is Harry"
Removes leading whitespace.
" My name is Harry!".ltrim(); // "My name is Harry!"
"/this/is/a/path/".ltrim("/"); // "this/is/a/path/"
Pad a string up to a given length. Padding characters are added to the left of the string.
"22".pad(4, '#'); // "##22"
"22".pad(4, '#', 1); // "22##"
Removes a specified length of characters from a given postion.
"JavaScript".remove(4, 6); // "Java"
Returns 'str' repeated 'count' times, optionally placing 'separator' between each repetition
"Hello".repeat(5); // "HelloHelloHelloHelloHello"
"Hello".repeat(5,'#'); // "Hello#Hello#Hello#Hello#Hello"
Reverses the characters in the specified string.
"Hello".reverse(); // "olleH"
Removes trailing whitespace.
"My name is Harry! ".rtrim(); // "My name is Harry!"
"/this/is/a/path//".rtrim("/"); // "/this/is/a/path"
Returns a string with all HTML tags stripped.
"<div id='type'>JavaScript</div>".stripTags(); // "JavaScript"
Returns a String with with leading and trailing whitespace removed.
" My name is Harry! ".trim(); // "My name is Harry!"
"/this/is/a/path//".trim("/"); // "this/is/a/path"
Returns a string that is no longer than a certain length.
"JavaScript ".truncate(5); // "Ja..."
"JavaScript ".truncate(5, "#"); // "Java#"
Extends string prototype with the following methods: encodeBase64, decodeBase64
Encodes a string using Base64.
"JavaScript".encodeBase64(); // "SmF2YVNjcmlwdA=="
Decodes a Base64 encoded string to a byte string.
"SmF2YVNjcmlwdA==".decodeBase64(); // "JavaScript"
Extends string prototype with the following method: md5
Returns the md5 hash of the given string.
"JavaScript".md5(); // "686155af75a60a0f6e9d80c1f7edd3e9"
Extends string prototype with the following method: utf8
Encodes a multi-byte string into utf-8 multiple single-byte characters.
"Straße".encodeUTF8(); // "StraÃ�e"
Decodes a utf-8 encoded string back into multi-byte characters.
"StraÃ�e".decodeUTF8(); // "Straße"
Char extensions convert between characters and decimal Numeric Character References. Extends string prototype with the following methods: char2dec, dec2char
Convert Character to Decimal.
"JavaScript".char2dec(); // "JavaScript"
Convert Decimal to Character.
"JavaScript".dec2char(); // "JavaScript"
Extends array prototype with the following methods: contains, every, exfiltrate, filter, forEach, getRange, inArray, indexOf, insertAt, map, randomize, removeAt, some, unique
Returns true if every element in 'elements' is in the array.
[1, 2, 1, 4, 5, 4].contains([1, 2, 4]); // true
Returns the array without the elements in 'elements'.
[1, 2, 1, 4, 5, 4].exfiltrate([1, 2, 4]); // 5
Tests whether all elements in the array pass the test implemented by the provided function.
[22, 72, 16, 99, 254].every(function(element, index, array) {
return element >= 15;
}); // true
[12, 72, 16, 99, 254].every(function(element, index, array) {
return element >= 15;
}); // false
Creates a new array with all elements that pass the test implemented by the provided function.
[12, 5, 8, 1, 44].filter(function(element, index, array) {
return element >= 10;
}); // [12, 44];
Executes a provided function once per array element.
var stuff = "";
["Java", "Script"].forEach(function(element, index, array) {
stuff += element;
}); // "JavaScript";
Returns a range of items in this collection.
[1, 2, 1, 4, 5, 4].getRange(2, 4); // [1, 4, 5]
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
[12, 5, 8, 5, 44].indexOf(5); // 1
[12, 5, 8, 5, 44].indexOf(5, 2); // 3
Checks if a given subject can be found in the array.
[12, 5, 7, 5].inArray(7); // true
[12, 5, 7, 5].inArray(9); // false
Inserts an item at the specified index in the array
['dog', 'cat', 'horse'].insertAt(2, 'mouse'); // ['dog', 'cat', 'mouse', 'horse']
Creates a new array with the results of calling a provided function on every element in this array.
["my", "Name", "is", "HARRY"].map(function(element, index, array) {
return element.toUpperCase();
}); // ["MY", "NAME", "IS", "HARRY"]
[1, 4, 9].map(Math.sqrt); // [1, 2, 3]
Remove an item from a specified index in the array.
['dog', 'cat', 'mouse', 'horse'].removeAt(2); // ['dog', 'cat', 'horse']
Randomize the order of the elements in the Array.
[2, 3, 4, 5].randomize(); // ??? [5, 2, 3, 4] randomized result
Tests whether some element in the array passes the test implemented by the provided function.
[101, 199, 250, 200].some(function(element, index, array) {
return element >= 100;
}); // true;
[101, 99, 250, 200].some(function(element, index, array) {
return element >= 100;
}); // true;
Returns a new array that contains all unique elements of this array.
[1, 2, 1, 4, 5, 4].unique(); // [1, 2, 4, 5]
Have a bug? Please create an issue here on GitHub!