11var _ = require ( '../util' )
22var Cache = require ( '../cache' )
33var pathCache = new Cache ( 1000 )
4- var identRE = exports . identRE = / ^ [ $ _ a - z A - Z ] + [ \w $ ] * $ /
4+ exports . identRE = / ^ [ $ _ a - z A - Z ] + [ \w $ ] * $ /
55
66// actions
77var APPEND = 0
@@ -229,25 +229,6 @@ function parsePath (path) {
229229}
230230}
231231
232- /**
233- * Format a accessor segment based on its type.
234- *
235- *@param {String } key
236- *@return {Boolean }
237- */
238-
239- function formatAccessor ( key ) {
240- if ( identRE . test ( key ) ) { // identifier
241- return '.' + key
242- } else if ( + key === key >>> 0 ) { // bracket index
243- return '[' + key + ']'
244- } else if ( key . charAt ( 0 ) === '*' ) {
245- return '[o' + formatAccessor ( key . slice ( 1 ) ) + ']'
246- } else { // bracket string
247- return '["' + key . replace ( / " / g, '\\"' ) + '"]'
248- }
249- }
250-
251232/**
252233 * Compiles a getter function with a fixed path.
253234 * The fixed path getter supresses errors.
@@ -257,8 +238,22 @@ function formatAccessor (key) {
257238 */
258239
259240exports . compileGetter = function ( path ) {
260- var body = 'return o' + path . map ( formatAccessor ) . join ( '' )
261- return new Function ( 'o' , body )
241+ return function get ( obj ) {
242+ var original = obj
243+ var segment
244+ for ( var i = 0 , l = path . length ; i < l ; i ++ ) {
245+ segment = path [ i ]
246+ if ( segment . charAt ( 0 ) === '*' ) {
247+ segment = original [ segment . slice ( 1 ) ]
248+ }
249+ obj = obj [ segment ]
250+ if ( i === l - 1 ) {
251+ return obj
252+ } else if ( ! _ . isObject ( obj ) ) {
253+ return
254+ }
255+ }
256+ }
262257}
263258
264259/**