ponysmith.github.io

brevityNamespacing

Namespacing in brevity.js allows you to register multiple abbreviations with the same abbreviation text. This can come in handy on the off chance you have two identical abbreviations that have different meanings.

Namespaces in brevity.js work by using a colon (:) to add a namespace before the actual abbreviation. When brevity.js parses the abbreviation, everything to the left of the final colon will be considered a namespace and will be used for disambiguation purposes only. Everything to the right of the final colon is what will be printed out by brevity.js as the text for that abbr tag.

For example, let's say you were using the abbrevation WWF for two purposes:

  1. World Wrestling Federation
  2. World Wildlife Fund

In this case, you could namespace these abbrevations in the abbrevations array of your JS like so:

1
2
3
4
var abbrs = [
    { abbr: 'Pandas:WWF', title: 'World Wildlife Fund' },
    { abbr: 'HulkHogan:WWF', title: 'World Wrestling Federation' }
];

Then, in your HTML, you would simply type out the appropriate abbreviation (with namespace) and brevity.js will correctly convert it. Check out the sample code below to see it in action.

Code

Sample HTML

<!-- sample HTML -->
<p>Due to his love of pandas, Hulk Hogan (of HulkHogan:WWF fame) held a fundraiser for the Pandas:WWF.</p>

Sample Javascript

1
2
3
4
5
6
// sample JS
var abbrs = [
    { abbr: 'Pandas:WWF', title: 'World Wildlife Fund' },
    { abbr: 'HulkHogan:WWF', title: 'World Wrestling Federation' }
];
brevity(abbrs);

Result

Due to his love of pandas, Hulk Hogan (of HulkHogan:WWF fame) held a fundraiser for the Pandas:WWF.

Another use for namespacing

Another potential use for the namespacing capabilities of brevity.js is to have more control over when abbreviations get parsed. For example, let's say you want brevity.js to handle the term HTML, but you only want it to be abbreviated the first time it appears on the page. You might be able to use the root node option to accomplish this. But you could also use namespacing. All you would need to do is add a namespace (for example, convert). Then, any time you want brevity to handle the abbreviation, you would type convert:HTML and anytime you want the text to appear unchanged, you would simply type HTML. Easy peesy.