1.0.2 • Published 6 years ago

nbsp v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

nbsp

Nbsp is a JavaScript module that replaces normal spaces with non-breaking ones. The configuration allows to declare a list of words/characters which trailing space will be replaced with non-breaking one. It's also possible to replace spaces after strings that are within a chars length range.

Installation

Install with npm:

npm install nbsp

Install with yarn:

yarn add nbsp

Usage

Basic usage

import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textElement = document.querySelector('p')
  nbsp(textElement)
})

Parameters

NameDescriptionTypeDefault
texta source element or a plain textHTMLelement, String-
opts-Object-
opts.wordsreplace a space after any of this words/charactersArray[]
opts.minreplace a space after any string within the min-max rangeNumber0
opts.maxreplace a space after any string within the min-max rangeNumber0
opts.removeWidowsreplace a space before last wordBooleanfalse
import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textElement = document.querySelector('p')
  
  nbsp(textElement, {
    min: 1,
    max: 1,
    removeWidows: true
  })
})

If you don't want to modify element's node values you can pass element's content as the text argument.

import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textContent = document.querySelector('p').innerHTML
  
  const result = nbsp(textContent, {
    min: 1,
    max: 1,
    removeWidows: true
  })
})

License

nbsp.js is licensed under MIT license.