insert-line

2019-02-13 admin

insert-line是什么

什么是insert-line,Node module for inserting lines to an existing file

insert-line使用教程帮助文档

insert-line

Node module for inserting lines to an existing file.

Usage

Instantiation

const insertLine = require('insert-line')

Examples

Appending a line asynchronously:

insertLine('./myfile.txt').append('Rocks').then(function(err) {
  var content = fs.readFileSync(destListPath, 'utf8')
  console.log(content)
})

Appending a line synchronously:

insertLine('./myfile.txt').appendSync('Rocks')
var content = fs.readFileSync(destListPath, 'utf8')
console.log(content)

Inserting a line at line number 3 asynchronously:

insertLine('./myfile.txt').content('This is a test').at(3).then(function(err) {
  var content = fs.readFileSync(destListPath, 'utf8')
  console.log(content)
})

Inserting a line at line number 3 synchronously:

insertLine('./myfile.txt').contentSync('This is a test').at(3)
var content = fs.readFileSync(destListPath, 'utf8')
console.log(content)

For more examples, refer to /test/test.js.

API

For async insertions, insertLine accepts a file path, after which you can call the chained methods:content() to specify the content and at() to specify the line number. Finally, you must call then() and pass your callback function.

If you need to prepend a line to a file, you can use the shortcut method prepend(), instead of calling content() and at(). If you need to append a line to a file, you can use the shortcut method append(), instead of calling content() and at().

content(), prepend(), and append() can be configured to specify the pad character, pad count, EOL character, and the option to overwrite the existing line.

contentSync(), prependSync(), and appendSync() are the synchronous version of content(), prepend(), and append(), respectively. These methods don’t need to be chained to then(), their insertion operation is executed on invoking at().

insertLine(<filePath>)

filePath: path to the file to insert the line.

.content([lineContent], [options])

lineContent: the content to be inserted at the line. If not specified, it defaults to an empty string. options: the options object supports the following options.

  • prepend: whether to prepend or not, defaults to false
  • append: whether to append or not, defaults to false
  • padding: padding amount, defaults to 0
  • padWith: character to be used for padding, defaults to ’ ',
  • eol: end of line character, defaults to os.EOL
  • overwrite: whether to overwrite the existing line, defaults to false

.contentSync([lineContent], [options])

Synchronous version of content(). then() is not required, insertion starts on calling at().

.at(<lineNumer>)

lineNumer: line number, where to insert the content. By default, the existing line will be pushed below. To overwrite the current line, specify overwrite: true, in the options object passed to content(), prepend(), or append().

If insert mode is sync (options.sync is set to true), then() is not called, instead the insertion operation starts at at().

.prepend([lineContent], [options])

Shortcut for adding a new line at the beginning of a file, with a combination of at() and content(). Refer to the documentation of .content() for details about lineContent and options.

.prependSync([lineContent], [options])

Synchronous version of prepend(). then() is not required, insertion starts on calling at().

.append([lineContent], [options])

Shortcut for adding a new line at the end of a file, with a combination of at() and content(). Refer to the documentation of .content() for details about lineContent and options.

.appendSync([lineContent], [options])

Synchronous version of append(). then() is not required, insertion starts on calling at().

.then(<callback>)

This method is only required while calling the async methods content(), prepend(), and append().

callback: callback function which will be executed after the line has been successfully inserted, or an error was encountered.

LICENSE

MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-53638.html

文章标题:insert-line

回到顶部