update-section
Advanced tools
Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "update-section", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Updates a section inside a file with newer content while removing the old content.", | ||
@@ -5,0 +5,0 @@ "main": "update-section.js", |
159
README.md
@@ -90,1 +90,160 @@ # update-section [![build status](https://secure.travis-ci.org/thlorenz/update-section.png)](http://travis-ci.org/thlorenz/update-section) | ||
MIT | ||
<!-- START docme generated API please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN docme TO UPDATE --> | ||
<div> | ||
<div class="jsdoc-githubify"> | ||
<section> | ||
<article> | ||
<div class="container-overview"> | ||
<dl class="details"> | ||
</dl> | ||
</div> | ||
<dl> | ||
<dt> | ||
<h4 class="name" id="updateSection"><span class="type-signature"></span>updateSection<span class="signature">(content, section, matchesStart, matchesEnd)</span><span class="type-signature"> → {String}</span></h4> | ||
</dt> | ||
<dd> | ||
<div class="description"> | ||
<p>Updates the content with the given section. </p> | ||
<p>If previous section is found it is replaced. | ||
Otherwise the section is appended to the end of the content.</p> | ||
</div> | ||
<h5>Parameters:</h5> | ||
<table class="params"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Type</th> | ||
<th class="last">Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td class="name"><code>content</code></td> | ||
<td class="type"> | ||
<span class="param-type">String</span> | ||
</td> | ||
<td class="description last"><p>that may or may not include a previously added section</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>section</code></td> | ||
<td class="type"> | ||
<span class="param-type">String</span> | ||
</td> | ||
<td class="description last"><p>the section to update</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>matchesStart</code></td> | ||
<td class="type"> | ||
<span class="param-type">function</span> | ||
</td> | ||
<td class="description last"><p>when called with a line needs to return true iff it is the section start line</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>matchesEnd</code></td> | ||
<td class="type"> | ||
<span class="param-type">function</span> | ||
</td> | ||
<td class="description last"><p>when called with a line needs to return true iff it is the section end line</p></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<dl class="details"> | ||
<dt class="tag-source">Source:</dt> | ||
<dd class="tag-source"><ul class="dummy"> | ||
<li> | ||
<a href="https://github.com/thlorenz/update-section/blob/master/update-section.js">update-section.js</a> | ||
<span>, </span> | ||
<a href="https://github.com/thlorenz/update-section/blob/master/update-section.js#L44">lineno 44</a> | ||
</li> | ||
</ul></dd> | ||
</dl> | ||
<h5>Returns:</h5> | ||
<div class="param-desc"> | ||
<p>content with updated section</p> | ||
</div> | ||
<dl> | ||
<dt> | ||
Type | ||
</dt> | ||
<dd> | ||
<span class="param-type">String</span> | ||
</dd> | ||
</dl> | ||
</dd> | ||
<dt> | ||
<h4 class="name" id="updateSection::parse"><span class="type-signature"></span>updateSection::parse<span class="signature">(lines, matchesStart, matchesEnd)</span><span class="type-signature"> → {object}</span></h4> | ||
</dt> | ||
<dd> | ||
<div class="description"> | ||
<p>Finds the start and end lines that match the given criteria. | ||
Used by update-section itself.</p> | ||
<p>Use it if you need to get information about where the matching content is located.</p> | ||
</div> | ||
<h5>Parameters:</h5> | ||
<table class="params"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Type</th> | ||
<th class="last">Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td class="name"><code>lines</code></td> | ||
<td class="type"> | ||
<span class="param-type">Array.<string></span> | ||
</td> | ||
<td class="description last"><p>the lines in which to look for matches</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>matchesStart</code></td> | ||
<td class="type"> | ||
<span class="param-type">function</span> | ||
</td> | ||
<td class="description last"><p>when called with a line needs to return true iff it is the section start line</p></td> | ||
</tr> | ||
<tr> | ||
<td class="name"><code>matchesEnd</code></td> | ||
<td class="type"> | ||
<span class="param-type">function</span> | ||
</td> | ||
<td class="description last"><p>when called with a line needs to return true iff it is the section end line</p></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<dl class="details"> | ||
<dt class="tag-source">Source:</dt> | ||
<dd class="tag-source"><ul class="dummy"> | ||
<li> | ||
<a href="https://github.com/thlorenz/update-section/blob/master/update-section.js">update-section.js</a> | ||
<span>, </span> | ||
<a href="https://github.com/thlorenz/update-section/blob/master/update-section.js#L3">lineno 3</a> | ||
</li> | ||
</ul></dd> | ||
</dl> | ||
<h5>Returns:</h5> | ||
<div class="param-desc"> | ||
<p>with the following properties: hasStart, hasEnd, startIdx, endIdx</p> | ||
</div> | ||
<dl> | ||
<dt> | ||
Type | ||
</dt> | ||
<dd> | ||
<span class="param-type">object</span> | ||
</dd> | ||
</dl> | ||
</dd> | ||
</dl> | ||
</article> | ||
</section> | ||
</div> | ||
*generated with [docme](https://github.com/thlorenz/docme)* | ||
</div> | ||
<!-- END docme generated API please keep comment here to allow auto update --> |
'use strict'; | ||
/** | ||
* Updates the content with the given section. | ||
* Finds the start and end lines that match the given criteria. | ||
* Used by update-section itself. | ||
* | ||
* If previous section is found it is replaced. | ||
* Otherwise the section is appended to the end of the content. | ||
* Use it if you need to get information about where the matching content is located. | ||
* | ||
* @name updateSection | ||
* @name updateSection::parse | ||
* @function | ||
* @param {String} content that may or may not include a previously added section | ||
* @param {String} section the section to update | ||
* @param {Array.<string>} lines the lines in which to look for matches | ||
* @param {Function} matchesStart when called with a line needs to return true iff it is the section start line | ||
* @param {Function} matchesEnd when called with a line needs to return true iff it is the section end line | ||
* @return {String} content with updated section | ||
* @return {object} with the following properties: hasStart, hasEnd, startIdx, endIdx | ||
*/ | ||
var updateSection = module.exports = function updateSection(content, section, matchesStart, matchesEnd) { | ||
if (!content) return section; | ||
var lines = content.split('\n') | ||
, startIdx = -1 | ||
function parse(lines, matchesStart, matchesEnd) { | ||
var startIdx = -1 | ||
, endIdx = -1 | ||
@@ -27,4 +23,2 @@ , hasStart = false | ||
if (!lines.length) return section; | ||
for (var i = 0; i < lines.length; i++) { | ||
@@ -42,5 +36,2 @@ line = lines[i] | ||
// no previous section found in content not just append | ||
if (!hasStart) return content + '\n\n' + section; | ||
// no end, correct it to be all remaining lines after start | ||
@@ -52,8 +43,38 @@ if (!hasEnd) { | ||
return { hasStart: hasStart, hasEnd: hasEnd, startIdx: startIdx, endIdx: endIdx }; | ||
} | ||
/** | ||
* Updates the content with the given section. | ||
* | ||
* If previous section is found it is replaced. | ||
* Otherwise the section is appended to the end of the content. | ||
* | ||
* @name updateSection | ||
* @function | ||
* @param {String} content that may or may not include a previously added section | ||
* @param {String} section the section to update | ||
* @param {Function} matchesStart when called with a line needs to return true iff it is the section start line | ||
* @param {Function} matchesEnd when called with a line needs to return true iff it is the section end line | ||
* @return {String} content with updated section | ||
*/ | ||
exports = module.exports = function updateSection(content, section, matchesStart, matchesEnd) { | ||
if (!content) return section; | ||
var lines = content.split('\n') | ||
if (!lines.length) return section; | ||
var info = parse(lines, matchesStart, matchesEnd); | ||
// no previous section found in content not just append | ||
if (!info.hasStart) return content + '\n\n' + section; | ||
var sectionLines = section.split('\n') | ||
, dropN = endIdx - startIdx + 1; | ||
, dropN = info.endIdx - info.startIdx + 1; | ||
[].splice.apply(lines, [ startIdx, dropN ].concat(sectionLines)) | ||
[].splice.apply(lines, [ info.startIdx, dropN ].concat(sectionLines)) | ||
return lines.join('\n'); | ||
} | ||
exports.parse = parse; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15878
247
248