Skip to content

Instantly share code, notes, and snippets.

@corpix
Last active September 27, 2015 14:37
Show Gist options
  • Save corpix/1285604 to your computer and use it in GitHub Desktop.
Save corpix/1285604 to your computer and use it in GitHub Desktop.
String.esplit function
var str = 'fooooo1#foo1:1,bfooooo1#foo1:1,arrrr2#barr2:2',
slice = [].slice;
String.prototype.esplit = function(){
if(this.length == 0)
return [];
var args = slice.apply(arguments)
, options = {
delimiter: ','
}
if(args.length > 2 && args[args.length - 1] instanceof Object) {
var _options = args.pop();
for(var i in _options){
options[i] = _options[i];
}
}
args = args.reverse();
for(var i in args) {
if(i < args.length -1) {
args[i] = {
symbol: args[i].substr(0, 1),
arg: args[i].substr(1)
}
}
}
var str = this.split(options.delimiter)
, result = [];
str.forEach(function(str, i){
var r = {};
for(var j in args) {
var a = args[j]
, firstIndex = (j != args.length - 1) ? (str.indexOf(a.symbol) + 1) : 0
, secondIndex = (j == 0) ? str.length : str.indexOf(args[j - 1].symbol);
r[a instanceof Object ? a.arg : a] = str.substring(firstIndex, secondIndex);
}
result.push(r);
});
return result;
}
console.log('result', str.esplit('label', '#tag', ':id'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment