JSVerbalExpressions
JSVerbalExpressions copied to clipboard
Different test result between ES6 import VerEx and local VerEx
Hello, I am using JSVerbalExpressions to extract the specified format of strings, when the VerEx test() result returns true, some code will be implemented.
But I found a strange problem when I try to separate the format from a different file in ES6 style.
The following code is trying to import a file that included formats that represent in VerEx, then call the test() function from the imported Object variable.
In Format.js
import VerEx from 'verbal-expressions'
let testResult = VerEx().startOfLine().anythingBut('.').find('-').anything().then('.').anything().endOfLine().test('CT-CP-RF-03.CWS.TEMP.1')
export { testResult };
export default {
Format01: {
description: "Metadata with floor information and following [Lo-cat-ion].[element] rule",
example: "AHU-S-3F-02.CHW.FLOW",
separator_lv1: ".",
separator_lv2: ".",
regExp: VerEx().startOfLine().anythingBut('.').find('-').anything().then('.').anything().endOfLine()
},
Format02: {
description: "",
example: "",
separator_lv1: "",
separator_lv2: "",
regExp: VerEx().startOfLine().anythingBut('.').maybe('-').anything().then(':').anythingBut('.-').endOfLine()
},
}
I did many tests in the following file, between local define VerEx and import VerEx, including
- check regExp string
- test('Hardcode String') by local VerEx, test('Hardcode String') in import file local, test('Hardcode String') by import VerEx
- check parameter storing value equality before test() check parameter storing value equality after test()
- test(parameter) by local VerEx, test(parameter) by import VerEx <---- this is the problem
even some tricky thing like
5. force copy string var string_copy = (' ' + metadata).slice(1);
In formatter.js
import Format, {testResult} from './data/Format'
format(metadata) {
//5.
var string_copy = (' ' + metadata).slice(1);
if(metadata == 'CT-CP-RF-03.CWS.TEMP.1'){
//1. check regExp string
//import regExp /^(?:[^\.]*)(?:-)(?:.*)(?:\.)(?:.*)$/gm
console.log('import regExp ',this.formatList.Format01.regExp);
//local regExp /^(?:[^\.]*)(?:-)(?:.*)(?:\.)(?:.*)$/gm
console.log('local regExp ',VerEx().startOfLine().anythingBut('.').find('-').anything().then('.').anything().endOfLine());
//2.
//local test("CT-CP-RF-03.CWS.TEMP.1"): true
console.log('local test("CT-CP-RF-03.CWS.TEMP.1"): ', VerEx().startOfLine().anythingBut('.').find('-').anything().then('.').anything().endOfLine().test('CT-CP-RF-03.CWS.TEMP.1'));
//import local testResult("CT-CP-RF-03.CWS.TEMP.1"): true
console.log('import local testResult("CT-CP-RF-03.CWS.TEMP.1"): ', testResult);
//import test("CT-CP-RF-03.CWS.TEMP.1"): true
console.log('import test("CT-CP-RF-03.CWS.TEMP.1"): ', this.formatList.Format01.regExp.test('CT-CP-RF-03.CWS.TEMP.1'));
//3.
//Before test(): CT-CP-RF-03.CWS.TEMP.1
console.log('Before test(): ',metadata);
//Before test(): true true
console.log('Before test(): ',metadata == 'CT-CP-RF-03.CWS.TEMP.1', metadata === 'CT-CP-RF-03.CWS.TEMP.1');
//4.
//local test(metadata): true
console.log('local test(metadata): ',VerEx().startOfLine().anythingBut('.').find('-').anything().then('.').anything().endOfLine().test(metadata));
//import test(metadata): false
console.log('import test(metadata): ',this.formatList.Format01.regExp.test(metadata));
//3.
//After test(): CT-CP-RF-03.CWS.TEMP.1
console.log('After test(): ',metadata);
//After test(): true true
console.log('After test(): ',metadata == 'CT-CP-RF-03.CWS.TEMP.1', metadata === 'CT-CP-RF-03.CWS.TEMP.1');
}
but when I try to use VerEx that import from the other file to test() the parameter (in 4.), it will return false although I checked it is equal with the hardcode string (not all of the parameter having this problem)
Test Case: Tested hardcode string === parameter Local VerEx + hardcode string = pass Local VerEx + parameter = pass Import VerEx + hardcode string = pass Import VerEx + parameter = fail