@oat-sa/tao-item-runner-qti
Version:
TAO QTI Item Runner modules
80 lines (74 loc) • 2.88 kB
JavaScript
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2016 (original work) Open Assessment Technologies SA ;
*/
var regexChar = /\^\[\\s\\S\]\{\d+\,(\d+)\}\$/,
regexWords = /\^\(\?\:\(\?\:\[\^\\s\\:\\!\\\?\\\;\\\…\\\€\]\+\)\[\\s\\:\\!\\\?\\;\\\…\\\€\]\*\)\{\d+\,(\d+)\}\$/;
var patternMaskHelper = {
/**
* Parse the pattern string and according to the given type, try to extract the associate number
*
* @param {String} pattern - the pattern string to be parsed
* @param {String} type - words or chars
* @returns {*}
*/
parsePattern: function parsePattern(pattern, type) {
if (pattern === undefined || pattern === null) {
return null;
}
if (type === 'words') {
//expre = /^(?:(?:[^\s\:\!\?\;\…\€]+)[\s\:\!\?\;\…\€]*){0,3}$/;
var result = pattern.match(regexWords);
if (result !== null && result.length > 1) {
return result[1];
} else {
return null;
}
} else if (type === 'chars') {
// This is the original regExp generated by our code
// expre = /^[\s\S]{0,10}$/;
// and we will try to extract the top limit from it with this regexp
// which is mostly just escaped version of the first one.
var result = pattern.match(regexChar);
if (result !== null && result.length > 1) {
return result[1];
} else {
return null;
}
} else {
return null;
}
},
/**
* Reverse function of parsePattern for word type
*
* @param {Number} max
* @returns {string}
*/
createMaxWordPattern: function createMaxWordPattern(max) {
return '^(?:(?:[^\\s\\:\\!\\?\\;\\…\\€]+)[\\s\\:\\!\\?\\;\\…\\€]*){0,' + max.toString() + '}$';
},
/**
* Reverse function of parsePattern for char type
*
* @param {Number} max
* @returns {string}
*/
createMaxCharPattern: function createMaxCharPattern(max) {
return '^[\\s\\S]{0,' + max.toString() + '}$';
}
};
export default patternMaskHelper;