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

@type-magic/color-identity

v1.1.0

Published

Typescript library for working with the Magic color identity system

Downloads

11

Readme

type-magic 🧙

Typescript library for working with the Magic color identity system

Table of Contents

Features

  • Type safety and IDE autocomplete for Magic color identity strings
  • Use the ColorIdentityString type to enforce the color identity syntax for strings
  • The static API of ColorIdentity offers methods for working with color identities such as add, subtract, contains, gte, subColorIdentities and many more
  • Instances of ColorIdentity provide a fluent interface for the same methods

Usage Examples

Installation in a npm project

Install the library in any npm project and import from @type-magic/color-identity:

Usage example

ColorIdentityString type

// Define a ColorIdentityString to get strong typing for wubrg strings
const myId: ColorIdentityString = 'wubrg'
const myOtherId: ColorIdentityString = 'wbg'
const myThirdId: ColorIdentityString = ''
// const doesntCompile: ColorIdentityString = 'xyz' // Error!

Static API of ColorIdentity

// Use the static ColorIdentity API to work with color identity strings
ColorIdentity.add('wub', 'brg') // returns 'wubrg'
ColorIdentity.contains('wubr', 'br') // returns true
ColorIdentity.lt('u', 'ub') // returns true
ColorIdentity.components('wub') // returns ['', 'w', 'u', 'b']
ColorIdentity.subColorIdentities('wg') // returns ['', 'g', 'w', 'wg']

Create ColorIdentity objects

const myObj = new ColorIdentity('wubrg') // only accepts ColorIdentityString input
const myOtherObj = ColorIdentity.parse('wubrg') // accepts any string input and throws if invalid
const myThirdObj = ColorIdentity.parse('invalid') // Error!

Fluent interface API of ColorIdentity objects

myObj
  .subtract(ColorIdentity.FULL)
  .add('wbg')
  .subtract('b')
  .subColorIdentities() // ['', 'g', 'w', 'wg']

The cid tagged template function

// Regular syntax
const myCid = cid`wubrg`
// With template interpolation
const interpolationValue = "wu"
const myOtherCid = cid`${interpolationValue}brg`
// Syntax ok, but will throw an error when executed
const myThirdCid = cid`will compile but throw error`

What is a Tagged Template?

A tagged template in Typescript is a template string tagged with a function. The template string behaves just like a regular Typescript template string. However, instead of the regular string interpolation, the tag function must implement how the template is supposed to be parsed. In short, a tagged template is a special way to parse template strings.

In the case of the cid function, it actually does nothing more than a regular template string would. It receives the TemplateStringsArray and the args, then joins them and calls ColorIdentity.parse with the input. The cid function is basically just a shortcut or syntactic sugar for calling ColorIdentity.parse. However, this can be used in very interesting ways, like for example an IDE plugin for syntax highlighting for cid tagged templates.

Development

The scripts section of the package.json file contains everything that is needed for working on this library.

| Script | Description | |--------------|--------------------------------------------------------------------------------------------------------------------------------------------------| | clean | Deletes the dist/ and coverage/ directories that are generated when building or testing the project. | | build | Builds the project using tsc . This build is used for active development and debugging purposes. | | build:prod | Builds the project, but swaps out the build configuration file ( tsconfig.prod.json ). This build is used for public releases of this library. | | test | Runs all tests for the project with code coverage using jest. | | test:watch | Starts the jest unit test runner in watch mode. | | lint | Scans all typescript files for linting errors as specified in .eslintrc.js . | | lint:fix | Runs the lint check and fixes each error if possible. | | prepublish | An npm hook that is invoked whenever a version of this library is about to be npm published. |