npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

encode-decode

v1.0.0

Published

A Javascript object to encode and/or decode html characters using HTML or Numeric entities that handles double or partial encoding

Downloads

1,090

Readme

Encode Decode

A Javascript object to encode and/or decode html characters using HTML or Numeric entities that handles double or partial encoding.

This is an npm published version of Robert Reid's original - full credit to Robert.

Example

Modified from the original documentation

var Encoder = require('encode-decode');

// set the type of encoding to numerical entities e.g & instead of &
Encoder.EncodeType = "numerical";

// or to set it to encode to html entities e.g & instead of &
Encoder.EncodeType = "entity";

// HTML encode text from an input element
// This will prevent double encoding.
var encoded = Encoder.htmlEncode(document.getElementById('input'));

// To encode but to allow double encoding which means any existing entities such as
// & will be converted to &
var dblEncoded = Encoder.htmlEncode(document.getElementById('input'),true);

// Decode the now encoded text
var decoded = Encoder.htmlDecode(encoded);

// Check whether the text still contains HTML/Numerical entities
var containsEncoded = Encoder.hasEncoded(decoded);

Usage

Taken from the original documentation

There are a number of useful functions within the object which I will outline here:

  • HTML2Numerical: Converts HTML entities to their numerical equivalents.
  • NumericalToHTML: Converts numerical entities to their HTML equivalents.
  • numEncode: Numerically encodes unicode characters.
  • htmlDecode: Decodes HTML encoded text to its original state.
  • htmlEncode: Encodes HTML to either numerical or HTML entities. This is determined by the EncodeType property.
  • XSSEncode: Encodes the basic characters used in XSS attacks to malform HTML.
  • correctEncoding: Corrects any double encoded ampersands.
  • stripUnicode: Removes all unicode characters.
  • hasEncoded: Returns true if a string contains html encoded entities within it.

Motivation

Taken from the original documentation

One of the things that I have found strange about Javascript is its lack of inbuilt functions to handle HTML encoding and decoding. Most server side languages have this functionality built into them but Javascript has escape, encodeURIComponent, encodeURI, unescape, decodeURIComponent and decodeURI functions which are aimed at making strings portable and for encoding URIs and URI parameters but there is no function for HTML encoding.

Now you may think well there's not much demand for a Javascript HTMLEncode and HTMLDecode function as any textual content that needs encoding should be done server-side before the HTML page is rendered and I would have agreed with you not long ago. However I have started working more and more with AJAX and especially RSS feeds and other client side delivered content such as Googles AJAX APIs and I have found more and more the need to reformat content delivered from external sources especially by HTML encoding or decoding content client side using Javascript.

For more details about reformating content with Javascript and the problems associated with simple replace statements you can read my related blog article.

License

Dual GPL v2 or MIT licence.