prototypeextender
v0.0.1
Published
Extends the prototypes of Strings, Arrays
Downloads
1
Readme
#Prototype Extender
A small collection of extensions to the prototypes of String, Array, and Number. Kind of like underscore, but simpler and it directly affects the prototype.
#Methods
##String prototype
####Reverse
Takes a string and reverses the order of characters.
'abc'.reverse(); //cba
####replaceLast
Replaces the last occurrence of a substring
'abca'.replaceLast('a'); //abc
####replaceAll
Replaces all occurrences of a substring
'abca'.replaceLast('a'); //bc
####findOcc
Finds the amount of times a substing occurs. Takes an optional allow overlapping argument that defaults to true.
'sss'.findOcc('ss'); //1
'sss'.findOcc('ss', true); //2
####contains
Returns true if a string has a substring.
'abca'.contains('a'); //true
'abca'.contains('f'); //false
####splitFirst
Splits a string at the first occurence of a word.
'dabcad'.splitFirst('a'); //['d', 'bcad']
####splitLast
Splits a string at the last occurence of a word.
'dabcad'.splitLast('a'); //['dabc', 'd']
####is
Takes a list and checks if the string is an item of that list.
'b'.is(['a', 'b', 'c']); //true
'd'.is(['a', 'b', 'c']); //false
####firstWord
Returns the first word of a string.
'once upon a time there was a very small cat named steve'.firstWord(); //once
####cut
Cuts a string between substrings.
'abcdef'.cut('c'); //def
'abcdef'.cut('b', 'd'); //c
####loop
Loops over a string. Provides the letter, index of letter, and persistent variable that defaults to an empty string.
console.log(
'abcdef'.loop(function(item, i, persist){
return persist + item.toUpperCase() + i;
});
} //A1B2C3D4E5F6
##Array prototype
####invPop
Pop returns the last item of string. invPop returns everything but the last item.
['a','b','c'].invPop(); //['a', 'b']
####invShift
Returns everything but the first item.
['a','b','c'].invShift(); //['b', 'c']
####replace
Replaces all occurences of a string in every item of the list.
['ab','b','cd'].replace('b'); //['a','','cd']
####invPop
Splits a list.
['aba','b','c'].split('b'); //['a', 'a', '', '','c']
####clean
Removes falsy values.
['a', 'a', '', false, 1, undefined, null, '','c'].clean(); //['a', 'a', 'c']
####replaceItem
Compares every item and replaces it.
['ab','b','c'].replaceItem('b', 12); //['ab', 12, 'c']
####whereIs
indexOf for lists.
['a','b','c'].whereIs('b'); //1
####loop
Loop for lists.
console.log(
'abcdef'.split('').loop(function(item, i, persist){
return persist + item.toUpperCase() + i;
});
} //A1B2C3D4E5F6
##Number Prototype
####loop
I think you get the idea by now. Gives the nmber and persistent, but no index for this one.
console.log(
7.0.loop(function(i, persist){
return persist + i.toString();
});
} //1234567