| 1 |
'use strict'; |
| 2 |
|
| 3 |
;!function(undefined) { |
| 4 |
|
| 5 |
var ObjectPath = { |
| 6 |
parse: function(str){ |
| 7 |
if(typeof str !== 'string'){ |
| 8 |
throw new TypeError('ObjectPath.parse must be passed a string'); |
| 9 |
} |
| 10 |
|
| 11 |
var i = 0; |
| 12 |
var parts = []; |
| 13 |
var d, b, q, c; |
| 14 |
while (i < str.length){ |
| 15 |
d = str.indexOf('.', i); |
| 16 |
b = str.indexOf('[', i); |
| 17 |
|
| 18 |
|
| 19 |
if (d === -1 && b === -1){ |
| 20 |
parts.push(str.slice(i, str.length)); |
| 21 |
i = str.length; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
else if (b === -1 || (d !== -1 && d < b)) { |
| 26 |
parts.push(str.slice(i, d)); |
| 27 |
i = d + 1; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
else { |
| 32 |
if (b > i){ |
| 33 |
parts.push(str.slice(i, b)); |
| 34 |
i = b; |
| 35 |
} |
| 36 |
q = str.slice(b+1, b+2); |
| 37 |
if (q !== '"' && q !=='\'') { |
| 38 |
c = str.indexOf(']', b); |
| 39 |
if (c === -1) c = str.length; |
| 40 |
parts.push(str.slice(i + 1, c)); |
| 41 |
i = (str.slice(c + 1, c + 2) === '.') ? c + 2 : c + 1; |
| 42 |
} else { |
| 43 |
c = str.indexOf(q+']', b); |
| 44 |
if (c === -1) c = str.length; |
| 45 |
while (str.slice(c - 1, c) === '\\' && b < str.length){ |
| 46 |
b++; |
| 47 |
c = str.indexOf(q+']', b); |
| 48 |
} |
| 49 |
parts.push(str.slice(i + 2, c).replace(new RegExp('\\'+q,'g'), q)); |
| 50 |
i = (str.slice(c + 2, c + 3) === '.') ? c + 3 : c + 2; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
return parts; |
| 55 |
}, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
stringify: function(arr, quote){ |
| 60 |
|
| 61 |
if(!Array.isArray(arr)) |
| 62 |
arr = [arr.toString()]; |
| 63 |
|
| 64 |
quote = quote === '"' ? '"' : '\''; |
| 65 |
|
| 66 |
return arr.map(function(n){ return '[' + quote + (n.toString()).replace(new RegExp(quote, 'g'), '\\' + quote) + quote + ']'; }).join(''); |
| 67 |
}, |
| 68 |
|
| 69 |
normalize: function(data, quote){ |
| 70 |
return ObjectPath.stringify(Array.isArray(data) ? data : ObjectPath.parse(data), quote); |
| 71 |
}, |
| 72 |
|
| 73 |
|
| 74 |
registerModule: function(angular) { |
| 75 |
angular.module('ObjectPath', []).provider('ObjectPath', function(){ |
| 76 |
this.parse = ObjectPath.parse; |
| 77 |
this.stringify = ObjectPath.stringify; |
| 78 |
this.normalize = ObjectPath.normalize; |
| 79 |
this.$get = function(){ |
| 80 |
return ObjectPath; |
| 81 |
}; |
| 82 |
}); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
if (typeof define === 'function' && define.amd) { |
| 88 |
define(function() { |
| 89 |
return {ObjectPath: ObjectPath}; |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
else if (typeof exports === 'object') { |
| 95 |
exports.ObjectPath = ObjectPath; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
else { |
| 100 |
window.ObjectPath = ObjectPath; |
| 101 |
} |
| 102 |
|
| 103 |
}(); |