|
12 | 12 | } catch (e) {} |
13 | 13 |
|
14 | 14 | angular.module('schemaForm',deps); |
| 15 | + |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +;!function(undefined) { |
| 20 | + |
| 21 | + var ObjectPath = { |
| 22 | + parse: function(str){ |
| 23 | + if(typeof str !== 'string'){ |
| 24 | + console.log(str) |
| 25 | + throw new TypeError('ObjectPath.parse must be passed a string'); |
| 26 | + } |
| 27 | + |
| 28 | + var i = 0; |
| 29 | + var parts = []; |
| 30 | + var d, b, q, c; |
| 31 | + while (i < str.length){ |
| 32 | + d = str.indexOf('.', i); |
| 33 | + b = str.indexOf('[', i); |
| 34 | + |
| 35 | + // we've reached the end |
| 36 | + if (d === -1 && b === -1){ |
| 37 | + parts.push(str.slice(i, str.length)); |
| 38 | + i = str.length; |
| 39 | + } |
| 40 | + |
| 41 | + // dots |
| 42 | + else if (b === -1 || (d !== -1 && d < b)) { |
| 43 | + parts.push(str.slice(i, d)); |
| 44 | + i = d + 1; |
| 45 | + } |
| 46 | + |
| 47 | + // brackets |
| 48 | + else { |
| 49 | + if (b > i){ |
| 50 | + parts.push(str.slice(i, b)); |
| 51 | + i = b; |
| 52 | + } |
| 53 | + q = str.slice(b+1, b+2); |
| 54 | + if (q !== '"' && q !=='\'') { |
| 55 | + c = str.indexOf(']', b); |
| 56 | + if (c === -1) c = str.length; |
| 57 | + parts.push(str.slice(i + 1, c)); |
| 58 | + i = (str.slice(c + 1, c + 2) === '.') ? c + 2 : c + 1; |
| 59 | + } else { |
| 60 | + c = str.indexOf(q+']', b); |
| 61 | + if (c === -1) c = str.length; |
| 62 | + while (str.slice(c - 1, c) === '\\' && b < str.length){ |
| 63 | + b++; |
| 64 | + c = str.indexOf(q+']', b); |
| 65 | + } |
| 66 | + parts.push(str.slice(i + 2, c).replace(new RegExp('\\'+q,'g'), q)); |
| 67 | + i = (str.slice(c + 2, c + 3) === '.') ? c + 3 : c + 2; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return parts; |
| 72 | + }, |
| 73 | + |
| 74 | + // root === true : auto calculate root; must be dot-notation friendly |
| 75 | + // root String : the string to use as root |
| 76 | + stringify: function(arr, quote){ |
| 77 | + |
| 78 | + if(Array.isArray(arr) !== true) |
| 79 | + arr = [arr.toString()]; |
| 80 | + |
| 81 | + quote = quote === '"' ? '"' : '\''; |
| 82 | + |
| 83 | + return arr.slice().map(function(n){ return '[' + quote + (n.toString()).replace(new RegExp(quote, 'g'), '\\' + quote) + quote + ']'; }).join(''); |
| 84 | + }, |
| 85 | + |
| 86 | + normalize: function(str){ |
| 87 | + return this.stringify(this.parse(str)); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // AMD |
| 92 | + if (typeof define === 'function' && define.amd) { |
| 93 | + define(function() { |
| 94 | + return ObjectPath; |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + // CommonJS |
| 99 | + else if (typeof exports === 'object') { |
| 100 | + exports.ObjectPath = ObjectPath; |
| 101 | + } |
| 102 | + |
| 103 | + // Browser global. |
| 104 | + else { |
| 105 | + window.ObjectPath = ObjectPath; |
| 106 | + } |
| 107 | +}(); |
0 commit comments