UNPKG

single-trailing-newline

Version:

Ensure a string has a single trailing newline based off it's dominant newline character.

15 lines (10 loc) 348 B
'use strict' var detectNewline = require('detect-newline') module.exports = function singleTrailingNewline (str) { if (typeof str !== 'string') { throw new TypeError('single-trailing-newline expected a string') } var newlineChar = detectNewline(str) var trimmedStr = str.replace(/(\r?\n)*$/, '') return trimmedStr + newlineChar }