mirror of https://github.com/ElemeFE/element
[util]: Update getPropByPath to better support brace lookups and object keys with special characters.
parent
fa18776b4b
commit
c9f9dffee7
|
@ -149,10 +149,6 @@
|
|||
if (!model || !this.prop) { return; }
|
||||
|
||||
let path = this.prop;
|
||||
if (path.indexOf(':') !== -1) {
|
||||
path = path.replace(/:/, '.');
|
||||
}
|
||||
|
||||
return getPropByPath(model, path, true).v;
|
||||
},
|
||||
isRequired() {
|
||||
|
@ -234,10 +230,6 @@
|
|||
let model = this.form.model;
|
||||
let value = this.fieldValue;
|
||||
let path = this.prop;
|
||||
if (path.indexOf(':') !== -1) {
|
||||
path = path.replace(/:/, '.');
|
||||
}
|
||||
|
||||
let prop = getPropByPath(model, path, true);
|
||||
|
||||
this.validateDisabled = true;
|
||||
|
|
|
@ -46,10 +46,15 @@ export const getValueByPath = function(object, prop) {
|
|||
|
||||
export function getPropByPath(obj, path, strict) {
|
||||
let tempObj = obj;
|
||||
path = path.replace(/\[(\w+)\]/g, '.$1');
|
||||
path = path.replace(/^\./, '');
|
||||
let matcher = /(?:\[['"])(.+)(?=['"]\])|(\w+)(?!['"]\])/g;
|
||||
let match;
|
||||
let keyArr = [];
|
||||
|
||||
do {
|
||||
match = matcher.exec(path);
|
||||
if (match) { keyArr.push(match[1] || match[0]); }
|
||||
} while (match);
|
||||
|
||||
let keyArr = path.split('.');
|
||||
let i = 0;
|
||||
for (let len = keyArr.length; i < len - 1; ++i) {
|
||||
if (!tempObj && !strict) break;
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import { getPropByPath } from 'element-ui/src/utils/util';
|
||||
|
||||
describe('utils.getPropByPath', () => {
|
||||
it('getPropByPath', () => {
|
||||
|
||||
const target = {
|
||||
mountains: {
|
||||
'Mt. Fiji': 1,
|
||||
'Mountain: Mt. Blanca': 2,
|
||||
'mt fiji': 3,
|
||||
'fiji': 4,
|
||||
'A Very \'Strange\' ["key"]': 5
|
||||
}
|
||||
};
|
||||
|
||||
expect(getPropByPath(target, 'mountains[\'Mt. Fiji\']').v).to.equal(1);
|
||||
expect(getPropByPath(target, 'mountains[\'Mountain: Mt. Blanca\']').v).to.equal(2);
|
||||
expect(getPropByPath(target, 'mountains[\'mt fiji\']').v).to.equal(3);
|
||||
expect(getPropByPath(target, 'mountains["Mt. Fiji"]').v).to.equal(1);
|
||||
expect(getPropByPath(target, 'mountains["Mountain: Mt. Blanca"]').v).to.equal(2);
|
||||
expect(getPropByPath(target, 'mountains["mt fiji"]').v).to.equal(3);
|
||||
expect(getPropByPath(target, 'mountains[fiji]').v).to.equal(4);
|
||||
expect(getPropByPath(target, 'mountains.fiji').v).to.equal(4);
|
||||
expect(getPropByPath(target, 'mountains["A Very \'Strange\' ["key"]"]').v).to.equal(5);
|
||||
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue