JSPM

surround-string

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q20572F
  • License MIT

Adds a prefix and a suffix to a string — but only if the string isn’t empty.

Package Exports

  • surround-string

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (surround-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

surround-string

Adds a prefix and a suffix to a string — but only if the string isn’t empty.

Installation

Requires Node.js 4.0.0 or above.

npm i surround-string

API

The module exports a single function.

Parameters

  1. before (string): The prefix to add.
  2. str (string)
  3. after (string): The suffix to add.

Return Value

  • If str is empty, returns an empty string.
  • Otherwise, returns before + str + after.

Example

const surround = require('surround-string')

class Person {
  constructor (title, name) {
    this.title = title
    this.name = name
  }

  toString () {
    return surround('', this.title, ' ') + this.name
  }
}

(new Person('Mr.', 'Darcy')).toString() // 'Mr. Darcy'
(new Person('', 'Jane')).toString() // 'Jane'