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

ts-key-enum

v3.0.13

Published

A TypeScript string enum for compile-time safety when working with event.key

Downloads

333,122

Readme

ts-key-enum

A TypeScript string enum for compile-time safety when working with event.key.

Install

npm install ts-key-enum --save
yarn add ts-key-enum

Purpose

Tired of referencing keyboard keys with a string?

onKeyPress = (ev) => {

    // whoops, it's actually ArrowLeft!
    if (ev.key === 'LeftArrow') {
        ...
    }
}

Me too. With this module, you can do this instead (in a TypeScript file):

onKeyPress = (ev) => {

    // much better
    if (ev.key === Key.ArrowLeft) {
        ...
    }
}

Background

This is similar to the ts-keycode-enum module, but it provides an enum with string values that correspond with the event.key values instead of number values that correspond to the deprecated event.which and event.keyCode values.

Usage

To use this module, import the Key enum at the top of your TypeScript file:

import { Key } from 'ts-key-enum';

You can now use the enum value in place of key strings throughout the file:

// if (ev.key === 'Escape') { ... }
if (ev.key === Key.Escape) { ... }

See Key.enum.d.ts for a complete list of available keys. This file is auto-generated from the list of keys found at MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values.

What's included

The Key enum contains values for all standard non-printable keys such as "CapsLock", "Backspace", and "AudioVolumeMute". The enum does not contain values for printable keys such as "a", "A", "#", "é", or "¿", simply because the list of possible values is too vast to include in a single enum. To test for printable values, simply use a string comparison:

if (ev.key === 'é') { ... }

v2 vs v3

This package is published as two versions on NPM: v2.x and v3.x.

v2

v2.x defines the Key enum as a "basic" enum:

export enum Key { ... }

The end result is a JavaScript object that contains every enum value. You can see this object here: https://gitlab.com/nfriend/ts-key-enum/blob/v2/dist/js/Key.enum.js.

v3

v3.x defines the Key enum as a const enum:

export const enum Key { ... }

This allows the enum's definition to live entirely in the definition file https://gitlab.com/nfriend/ts-key-enum/blob/master/Key.enum.d.ts. Consumers can use this enum without including all of the enum's values in their own JavaScript bundle; only the values referenced in their code will be injected into their output file.

Which version should I use?

It's advisable to use v3.x unless you have a reason to use v2.x. Some reasons you may need to use v2.x:

  • You are using this package as a JavaScript module (not a TypeScript module).
  • You are using TypeScript < 1.4, which doesn't support const enum.
  • You are using @babel/plugin-transform-typescript, which does not support const enum.
    • See #2 and https://github.com/babel/babel/issues/8741
    • Alternatively, use babel-plugin-const-enum to convert const enums to regular enum in your development environment, allowing you to reap the benefits of const enums in your production build (see https://www.npmjs.com/package/babel-plugin-const-enum#transform-removeconst-default).

Building

To build this module yourself, first install its dependencies using:

npm install

Next, run the scraper script (scrapeMDNForKeys.ts) using:

npm run scrape

This will overwrite Key.enum.d.ts with the updated list of keys found in MDN.

Verify that the enum builds without any TypeScript errors:

npm run build

Finally, make sure the enum passes all linters:

npm run lint

License

MIT