Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
#37 add inner delimeter method
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonLi8 committed Jan 20, 2020
1 parent 266c8d8 commit 257e452
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Generator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright © 2019-2020 Brandon Li. All rights reserved.

/**
* File Generator encapsulation that retrieves and validates values from package.json and replaces placeholder strings
* from a template file with these values. The result is then outputted in a specified output file.
* File Generator encapsulation that retrieves, registers, and validates replacement values and replaces placeholder
* strings from a template file with these values. The result is then outputted in a specified output file.
*
* ## Background
* - A placeholder string is a string used in template files (see ../../templates) to indicate a string that changes
Expand Down
21 changes: 21 additions & 0 deletions src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ module.exports = ( () => {
return fileContent.split( /\r?\n/ );
},

/**
* Gets ALL substrings between delimiters of a string. For instance,
* Util.getInnerDelimeterStrings( 'Foo <div> Bar </div><div>Doe</div>', '<div>', '</div>' ) -> [ ' Bar ', 'Doe' ].
*
* NOTE: this method doesn't work well for nested delimiters like '<div>H<div>B</div></div>'. Only use this method
* for delimiters that you know will 100% not have nested delimiters inside them.
* @public
*
* @param {String} string - the string to search for the strings in between delimiters
* @param {String} start - the start delimiter (e.g. '<div>')
* @param {String} end - the end delimiter (e.g. '</div>')
*/
getInnerDelimeterStrings( string, start, end ) {

// First split the string with the inner delimiter strings with a regular expression
const innerDelimterStrings = string.match( new RegExp( `${ start }(.*?)${ end }`, 'gm' ) );

// Remove the delimiters from the inner strings and return it.
return innerDelimterStrings.map( innerString => innerString.replace( start, '' ).replace( end, '' ) );
},

/**
* Convenience method to pluralize a word, adding the number in front. For example,
* Util.pluralize( 'dog', 2 ) -> '2 dogs'.
Expand Down

0 comments on commit 257e452

Please sign in to comment.