@alicd/templateparser
Version:
walle template parser
41 lines (34 loc) • 1.03 kB
JavaScript
/**
* @license
* Copyright Alibaba Group and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import config from '../config';
// output type reg.
const outputTag = new RegExp(
'^\\' + config.expressStartTag + '(.+?)\\' + config.expressEndTag + '$'
);
const jsonTag = new RegExp('^\\{(.+?)\\}$');
const spreadAttribute = config.spreadAttribute;
class Express {
test(str = '') {
const isOutput = outputTag.test(str);
const isExpress = jsonTag.test(str);
const isSpreadAttribute = spreadAttribute.test(str);
return isOutput || isExpress || isSpreadAttribute;
}
filter(str = '') {
if (str.match(spreadAttribute)) {
return str.match(spreadAttribute)[1];
} else if (str.match(outputTag)) {
return str.match(outputTag)[1];
} else if (str.match(jsonTag)) {
return str.match(jsonTag)[1];
} else {
return str;
}
}
}
export default new Express();