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

apphtmlspecialchars

v1.0.1

Published

Utility functions for encoding and decoding special HTML characters.

Downloads

69

Readme

Here's a comprehensive documentation for the encode and decode functions using Markdown format, tailored to fit your request. This version addresses the functionality and parameters, providing clarity on how to use the functions effectively.

markdown

Encode and Decode Functions Documentation

The encode and decode functions are utility methods designed for safely encoding and decoding string values, particularly in contexts where special characters must be securely represented (e.g., for database storage or network transmission). These functions accommodate both primitive types and nested objects/arrays.

encode

Purpose

The encode function recursively sanitizes and converts specific special characters in strings to their corresponding HTML entities. This process prevents potential issues with HTML rendering, data storage, or transmission.

Syntax

encode(data, listArray = null, operator = null)

Parameters

    data: The data to encode, which can be a string, array, or object. Nested objects and arrays are supported.
    listArray (optional): An array of field names to skip during encoding. Fields in this list will not be modified.
    operator (optional): A string that determines whether to include or exclude fields listed in listArray. Use + to include and - to exclude.

Return Value

The function returns the encoded version of the input data, with specified special characters replaced by their HTML entity equivalents.
Special Characters Encoded

The encode function converts the following characters:
Character	Encoded Value
&	&
<	<
>	>
"	"
'	'
{	{
}	}
~	~
`	`
,	,
/	/
\	\
(	(
)	)
[	[
]	]
Usage Example

javascript

const data = {
  name: "John & Jane",
  description: "<script>alert('hello');</script>",
  meta: { ignore: "<b>This is bold</b>", count: 5 }
};

const encodedData = encode(data, ["ignore"]);
console.log(encodedData);
/*
{
  name: "John &amp; Jane",
  description: "&lt;script&gt;alert(&#039;hello&#039;);&lt;/script&gt;",
  meta: { ignore: "<b>This is bold</b>", count: "5" }
}
*/

Notes

    listArray allows you to specify properties that should not be encoded. This is helpful if certain fields contain data that must remain in its original form.
    Non-string data types (like numbers) are converted to strings but are not encoded.

decode
Purpose

The decode function reverses the encoding performed by the encode function, converting HTML entities back to their original special characters. This functionality is essential for retrieving or displaying data in its original form.
Syntax

javascript

decode(data, listArray = null, operator = null)

Parameters

    data: The data to decode, which can be a string, array, or object. Nested objects and arrays are supported.
    listArray (optional): An array of field names to decode selectively. Only fields in this list will be decoded. If no fields are specified, all eligible fields will be decoded.
    operator (optional): A string that determines whether to include or exclude fields listed in listArray. Use + to include and - to exclude.

Return Value

The function returns the decoded version of the input data, with HTML entities replaced by their original special characters.
Special Characters Decoded

The decode function converts the following characters back to their original form:
Encoded Value	Character
&	&
<	<
>	>
"	"
'	'
{	{
}	}
~	~
`	`
,	,
/	/
\	\
(	(
)	)
[	[
]	]
Usage Example

javascript

const encodedData = {
  name: "John &amp; Jane",
  description: "&lt;script&gt;alert(&#039;hello&#039;);&lt;/script&gt;",
  meta: { ignore: "<b>This is bold</b>", count: "5" }
};

const decodedData = decode(encodedData, ["description"]);
console.log(decodedData);
/*
{
  name: "John &amp; Jane",
  description: "<script>alert('hello');</script>",
  meta: { ignore: "<b>This is bold</b>", count: "5" }
}
*/

Notes

    listArray allows you to specify fields for selective decoding, leaving other fields in their encoded form.
    Only fields containing HTML entities will be affected. Non-string fields remain unchanged.

Export

The module exports an object with the following structure:

javascript

export default { encode, decode };

Summary

    encode(data, listArray, operator): Encodes special characters in strings within the data structure. Fields in listArray are skipped during encoding based on the operator.
    decode(data, listArray, operator): Decodes HTML entities back to their original characters. Fields in listArray are selectively decoded based on the operator.

These functions provide a flexible and recursive approach to handle both encoding and decoding for complex data structures, ensuring safe representation of special characters.


This documentation provides a clear understanding of the functionality, usage, and structure of the `encode` and `decode` functions, making it easier for developers to utilize them effectively.