1. update-section
Updates a section inside a file with newer content while removing the old content.
update-section
Package: update-section
Created by: thlorenz
Last modified: Tue, 28 Jun 2022 05:37:48 GMT
Version: 0.3.3
License: MIT
Downloads: 312,770
Repository: https://github.com/thlorenz/update-section

Install

npm install update-section
yarn add update-section

update-section build status

testling badge

Updates a section inside a file with newer content while removing the old content.

 var updateSection = require('update-section');

var original = [
    '# Some Project'
  , ''
  , 'Does a bunch of things'
  , ''
  , 'START -- GENERATED GOODNESS'
  , 'this was painstakingly generated'
  , 'as was this'
  , 'END -- GENERATED GOODNESS' , ''
  , ''
  , '## The End'
  , ''
  , 'Til next time'
].join('\n');

var update = [
    'START -- GENERATED GOODNESS'
  , 'this was painstakingly re-generated'
  , 'and we added another line'
  , 'here'
  , 'END -- GENERATED GOODNESS'
].join('\n');

function matchesStart(line) {
  return (/START -- GENERATED GOODNESS/).test(line);  
}

function matchesEnd(line) {
  return (/END -- GENERATED GOODNESS/).test(line);  
}

var updated = updateSection(original, update, matchesStart, matchesEnd);
console.log(updated);

Output

# Some Project

Does a bunch of things

START -- GENERATED GOODNESS
this was painstakingly re-generated
and we added another line
here
END -- GENERATED GOODNESS

## The End

Til next time

Installation

npm install update-section

API

updateSection(content, section, matchesStart, matchesEnd)

/**
 * 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
 */

License

MIT

updateSection(content, section, matchesStart, matchesEnd, top) → {String}

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.

Parameters:
Name Type Description
content String

that may or may not include a previously added section

section String

the section to update

matchesStart function

when called with a line needs to return true iff it is the section start line

matchesEnd function

when called with a line needs to return true iff it is the section end line

top boolean

forces the section to be added at the top of the content if a replacement couldn't be made

Source:
Returns:

content with updated section

Type
String

updateSection::parse(lines, matchesStart, matchesEnd) → {object}

Finds the start and end lines that match the given criteria. Used by update-section itself.

Use it if you need to get information about where the matching content is located.

Parameters:
Name Type Description
lines Array.<string>

the lines in which to look for matches

matchesStart function

when called with a line needs to return true iff it is the section start line

matchesEnd function

when called with a line needs to return true iff it is the section end line

Source:
Returns:

with the following properties: hasStart, hasEnd, startIdx, endIdx

Type
object

generated with docme

Dependencies

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin