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 🙏

© 2025 – Pkg Stats / Ryan Hefner

like-a-symbol

v1.0.1

Published

Alternative to ES6 Symbol

Downloads

16

Readme

like-a-symbol

An alternative to EcmaScript 6 Symbol that can be chained with strings and used with JSON.stringify(...);

Reason

Symbol() function in JavaScript has some features that can be annoying:

const s = Symbol('S');
console.log('I like the symbol ' + s);

// Throws: Uncaught TypeError: Cannot convert a Symbol value to a string
const vegetable = {label: 'pear', type: Symbol('pear')};
console.log(JSON.stringify(vegetable));
// Outputs: "{"label":"pear"}", type field is lost.

Installation

npm install --save like-a-symbol

Usage

<script src="https://unpkg.com/like-a-symbol@1.0.0/src/index.js"></script>
<script>
  const pear = LikeASymbol('pear');
</script>

or

import { LikeASymbol } from 'like-a-symbol';
const pear = LikeASymbol('pear');

Syntax

LikeASymbol([description])

Parameters: description: String | optional

Optional, string. A description of the symbol which can be used for debugging.

Description

You can use LikeASymbol to emulate part of the behavior of the ES6 Symbol() avoiding exceptions when the symbol is put inside a string for debug reasons.

In addition Symbols are not serialized with JSON.stringify(...), whereas with LikeASymbol its string representation will be serialized and can be parsed back using LikeASymbol.fromString(value) function.

const s1 = LikeASymbol();
const s2 = LikeASymbol('foo');

LikeASymbol('foo') === LikeASymbol('foo'); // false
console.log('Symbol: ' + LikeASymbol('Foo')); // Symbol: LikeASymbol(Foo)
LikeASymbol('Foo').toString() === 'LikeASymbol(Foo)'; // true

JSON.stringify({ label: 'pear', type: LikeASymbol('pear') }); // {"label":"pear","type":"LikeASymbol(pear)"}

Methods

LikeASymbol.fromString(value)

Returns LikeASymbol instance from a valid string representation.

Throws a SyntaxError if the given string value is not a valid representation of a LikeASymbol instance.

LikeASymbol.isParsable(value)

Returns true if value is a parsable string representation of the symbol. Returns false otherwise.

Keep in mind

Keep in mind that LikeASymbol is not a Symbol, so function like Object.getOwnPropertySymbols() will not return any property defined with LikeASymbol. Also LikeASymbol('foo') == 'LikeASymbol(foo)' will return true.

Symbol() is designed to be unique and not comparable with strings, whereas LikeASymbol is designed to be loosely linked to its string representation.

Symbol.for(key) and Symbol.keyFor(symbol) are not ported to LikeASymbol. If you need them, you probably want to use the ECS6 Symbol, and not LikeASymbol.

If you look for a Symbol replacement that works in older browers, google "Symbol polyfill".