QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Data with Prefix

Adds a prefix and postfix to data, with a fallback value.

  • data
  • prefix
  • postfix
  • format

Install

npx shadcn@latest add https://quicksnip.dev/r/data-with-prefix.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

const dataWithPrefix = (data, fallback = '-', prefix = '', postfix = '') => {
  return data ? `${prefix}${data}${postfix}` : fallback;
};

Usage

dataWithPrefix('123', '-', '(', ')'); // Returns: '(123)'
dataWithPrefix('', '-', '(', ')'); // Returns: '-'
dataWithPrefix('Hello', 'N/A', 'Mr. ', ''); // Returns: 'Mr. Hello'
dataWithPrefix(null, 'N/A', 'Mr. ', ''); // Returns: 'N/A'