// wink-nlp-utils
// NLP Functions for amplifying negations, managing elisions,
// creating ngrams, stems, phonetic codes to tokens and more.
//
// Copyright (C) GRAYPE Systems Private Limited
//
// This file is part of “wink-nlp-utils”.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// ## string
// ### returnQuotedTextExtractor
/**
*
* Returns a function that extracts all occurrences of every quoted text
* between the `lq` and the `rq` characters from its argument. This argument
* must be of type string.
*
* @alias helper#returnQuotedTextExtractor
* @param {string} [lq='"'] the left quote character.
* @param {string} [rq='"'] the right quote character.
* @return {function} that will accept an input string argument and return an
* array of all substrings that are quoted between `lq` and `rq`.
* @example
* var extractQuotedText = returnQuotedTextExtractor();
* extractQuotedText( 'Raise 2 issues - "fix a bug" & "run tests"' );
* // -> [ 'fix a bug', 'run tests' ]
*/
var returnQuotedTextExtractor = function ( lq, rq ) {
var // Index variable for *for-loop*
i,
// Set defaults for left quote, if required.
lq1 = ( ( lq && ( typeof lq === 'string' ) ) ? lq : '"' ),
// Extracts its length
lqLen = lq1.length,
// The regular expression is created here.
regex = null,
// The string containing the regular expression builds here.
rgxStr = '',
// Set defaults for right quote, if required.
rq1 = ( ( rq && ( typeof rq === 'string' ) ) ? rq : lq1 ),
// Extract its length.
rqLen = rq1.length;
// Build `rgxStr`
for ( i = 0; i < lqLen; i += 1 ) rgxStr += '\\' + lq1.charAt( i );
rgxStr += '.*?';
for ( i = 0; i < rqLen; i += 1 ) rgxStr += '\\' + rq1.charAt( i );
// Create regular expression.
regex = new RegExp( rgxStr, 'g' );
// Return the extractor function.
return ( function ( s ) {
if ( !s || ( typeof s !== 'string' ) ) return null;
var // Extracted elements are captured here.
elements = [],
// Extract matches with quotes
matches = s.match( regex );
if ( !matches || ( matches.length === 0 ) ) return null;
// Collect elements after removing the quotes.
for ( var k = 0, kmax = matches.length; k < kmax; k += 1 ) {
elements.push( matches[ k ].substr( lqLen, matches[ k ].length - ( rqLen + lqLen ) ) );
}
return ( elements );
} );
}; // returnQuotedTextExtractor()
module.exports = returnQuotedTextExtractor;