1 | /*!
|
2 | * get-object <https://github.com/jonschlinkert/get-object>
|
3 | *
|
4 | * Copyright (c) 2014 Jon Schlinkert, contributors.
|
5 | * Licensed under the MIT License
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | var isNumber = require('is-number');
|
11 |
|
12 | module.exports = function getObject(obj, prop) {
|
13 | if (!prop) return obj;
|
14 | if (!obj) return {};
|
15 | var segs = String(prop).split(/[[.\]]/).filter(Boolean);
|
16 | var last = segs[segs.length - 1], res = {};
|
17 | while (prop = segs.shift()) {
|
18 | obj = obj[prop];
|
19 | if (!obj) return {};
|
20 | }
|
21 | if (isNumber(last)) return [obj];
|
22 | res[last] = obj;
|
23 | return res;
|
24 | };
|