section-manager
v1.0.2
Published
Manage string sections
Downloads
1
Readme
Section Manager
Lightweight section management of multi-line Strings, with no external dependencies.
Installation
npm install section-manager
, oryarn add section-manager
Example
import { StringSectionManager } from 'section-manager'
const testString =
`Hello
World!
<-- MySection
Test
--> MySection
Bye!`
const stringSectionManager = new StringSectionManager(testString)
const updatedSection = ['One', 'Two', 'Three']
stringSectionManager.findAndUpdateSection('MySection', updatedSection)
console.log(stringSectionManager.toString())
/* Output
Hello
World!
<-- MySection
One
Two
Three
--> MySection
Bye!
*/
API
- This module exports
StringSectionManager
andSectionManager
- It is recommended to use
StringSectionManager
as its constructor takes in astring
as an argument and automatically assigns it to the internal array
- It is recommended to use
StringSectionManager
StringSectionManager(fileString: string, options: SectionManagerOptions): StringSectionManager
SectionManager
StringSectionManager(options: SectionManagerOptions): SectionManager
type SectionManagerOptions = {
padding?: boolean,
sectionSyntax?: {
start: (name) => string,
end: (name) => string
}
}
padding
: controls whether or not there should be a new line after the section start and before the section end- This setting can be overridden on the update methods
sectionSyntax
: gives the developer control over the syntax for delimiting each section- Defaults:
- start:
(name) => `<-- ${ name }`
- end:
(name) => `--> ${ name }`
- start:
- Defaults:
Methods
findAndUpdateSection
findAndUpdateSection(section: string, sectionContentArray: string[], padding?: boolean): void
prependToSection
prependToSection(section: string, sectionContentArray: string[]): void
appendToSection
appendToSection(section: string, sectionContentArray: string[]): void
toString
toString(): string
The following are internal methods, but are exposed for cases that require manual control
findSection
findSection(section: string): {
startSection: number,
endSection: number,
indentChars: number,
section: string[]
}
updateSection
updateSection(options: UpdateSectionOptions): void
type UpdateSectionOptions = {
startSection: number,
endSection: number,
indentChars: string,
sectionContentArray: string[],
padding?: boolean
}
setFileArray
setFileArray(fileArray: string[]): void
getFileArray
getFileArray(): string[]
Extending
You can extend the behavior by extending the base class SectionManager
. For example, as shown in https://github.com/eeyang92/file-section-manager
import * as fs from 'fs'
import { SectionManager } from 'section-manager'
export class FileSectionManager extends SectionManager {
absolutePathFileName: string
constructor(absolutePathFileName: string, options: SectionManagerOptions) {
super(options)
if (!fs.existsSync(absolutePathFileName) ){
throw new Error('File not found')
}
this.absolutePathFileName = absolutePathFileName
this.readSync()
}
readSync() {
const fileString = fs.readFileSync(this.absolutePathFileName).toString()
this.setFileArray(fileString.split('\n'))
}
writeSync() {
fs.writeFileSync(this.absolutePathFileName, this.getFileArray().join('\n'))
}
}