All files / str keyPathSplit.js

100% Statements 12/12
100% Branches 4/4
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                      41x 39x 39x 66x 66x 66x 16x   66x 72x     39x     3x  
/**
 * 解析对象路径为一个数组
 * @method str/keyPathSplit
 * @param {String} 对象路径
 * @returns {Array} 对象路径数组
 * @example
 * var $keyPathSplit = require('@spore-ui/kit/packages/str/keyPathSplit');
 * console.info($keyPathSplit('[1].a[1][2]b.c'); // ['1', 'a', '1', '2', 'b', 'c']
 */
 
function keyPathSplit(xpath) {
  if (typeof xpath !== 'string') return [];
  var arrXpath = [];
  xpath.split('.').forEach(function (itemPath) {
    var strItem = itemPath.replace(/\[([^[\]]+)\]/g, '.$1');
    var itemArr = strItem.split('.');
    if (itemArr[0] === '') {
      itemArr.shift();
    }
    itemArr.forEach(function (item) {
      arrXpath.push(item);
    });
  });
  return arrXpath;
}
 
module.exports = keyPathSplit;