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

electro-grammar

v1.3.0

Published

Parser electronic components descriptions such as surface-mount resistors and capacitors.

Downloads

115

Readme

Electro Grammar

⚡ demo

npm travis chat

This is a parser using Nearley that defines a grammar for describing generic electronic components such as surface mount resistors, capacitors and LEDs. A function to match the result to parts in the Common Parts Library is also provided.

npm install electro-grammar
const {parse, matchCPL} = require('electro-grammar')

Where is this used?

Parsing

Capacitors

Parses capacitance, package size, characteristic, tolerance and voltage rating for capacitors.

> parse('100nF 0603 C0G 10% 25V')
{ type: 'capacitor',
  capacitance: 1e-7,
  size: '0603',
  characteristic: 'C0G',
  tolerance: 10,
  voltage_rating: 25 }

For class 1 ceramic names and EIA letter codes are understood. For class 2 only EIA letter codes are understood. In both cases only EIA letter codes are returned.

> parse('10pF C0G/NP0')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }
> parse('10pF NP0')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }
> parse('10pF X7R')
{ type: 'capacitor', capacitance: 1e-11, characteristic: 'X7R' }

Resistors

Parses resistance, package size, tolerance and power rating for resistors.

> parse('1k 0805 5% 125mW')
{ type: 'resistor',
  resistance: 1000,
  size: '0805',
  tolerance: 5,
  power_rating: 0.125 }

Electro-grammar supports several different ways to express resistance.

> parse('1.5k')
{ type: 'resistor', resistance: 1500 }
> parse('1k5')
{ type: 'resistor', resistance: 1500 }
> parse('500R')
{ type: 'resistor', resistance: 500 }
> parse('1500 ohm')
{ type: 'resistor', resistance: 1500 }
> parse('1500.0 ohm')
{ type: 'resistor', resistance: 1500 }
> parse('1500 Ω')
{ type: 'resistor', resistance: 1500 }
> parse('resistor 1500')
{ type: 'resistor', resistance: 1500 }

LEDs

LEDs need to include the word 'LED' or 'led'.

> parse('LED red')
{ type: 'led', color: 'red' }
> parse('LED 0603')
{ type: 'led', size: '0603' }
> parse('green led 1206')
{ type: 'led', color: 'green', size: '1206' }

Parsing Details

Converts all units to floating point numbers.

> parse('100nF')
{ type: 'capacitor', capacitance: 1e-7 }
> parse('0.1uF')
{ type: 'capacitor', capacitance: 1e-7 }

The order of the terms doesn't matter.

> parse('1% 0603 1uF')
{ type: 'capacitor'
  capacitance: 0.000001,
  tolerance: 1,
  size: "0603" }
> parse('0603 1% 1uF')
{ type: 'capacitor',
  capacitance: 0.000001,
  tolerance: 1,
  size: "0603" }

If no match is found an empty object is returned.

> parse('')
{}
> parse('NE555P')
{}

But invalid input types will throw.

> parse({})
TypeError: str.split is not a function

Text that is not part of the grammar is simply ignored.

> parse('NE555P 1uF')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('these words 1k are ignored 0805')
{ type: 'resistor', resistance: 1000, size: '0805' }

You can use metric package sizes as long as you make it clear by using the metric keyword. Output for package sizes is always in imperial.

> parse('1k metric 0603')
{ type: 'resistor', resistance: 1000, size: '0201' }
> parse('1k 0603 metric')
{ type: 'resistor', resistance: 1000, size: '0201' }

You can give it a hint to the type of component by starting off with that. For instance you can leave of F or farad if you start off with 'capacitor' or 'c'

> parse('capacitor 1u')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('c 1u')
{ type: 'capacitor', capacitance: 0.000001 }
> parse('resistor 100')
{ type: 'resistor', resistance: 100 }
> parse('res 100')
{ type: 'resistor', resistance: 100 }
> parse('r 100')
{ type: 'resistor', resistance: 100 }

CPL Matching

matchCPL tries to find as many matches as it can from the Common Parts Library and returns an array of CPL IDs. You could match these against CPL data or search for them on Octopart to get exact part numbers. If no matches are found or the function is given invalid input an empty array is returned.

> c = parse('0.1uF 0805 25V')
{ type: 'capacitor',
  capacitance: 1e-7,
  size: '0805',
  voltage_rating: 25 }
> matchCPL(c)
[ 'CPL-CAP-X7R-0805-100NF-50V' ]

> r = parse('10k 0603')
{ type: 'resistor', resistance: 10000, size: '0603' }
> matchCPL(r)
[ 'CPL-RES-0603-10K-0.1W' ]

> // I don't think it's possible to make such a resistor
> r = parse('1k 1000000W')
{ type: 'resistor', resistance: 1000, power_rating: 1000000 }
> matchCPL(r)
[]

> matchCPL({invalid: 'input'})
[]

> matchCPL(null)
[]

Roadmap

We are currently working on v2 of Electro Grammar which will have parsers in many more languages:

v1

  • JavaScript only
  • Capacitors, resistors and LEDs (SMD only)
  • Lax parser only (any-order, ignores invalid input)

v2

  • Work in progress!
  • Uses Antlr4: JavaScript (API compatible with v1), Python, Java, C (& C++), Go
  • Capacitors, resistors, LEDs, diodes, transistors (SMD & through-hole)
  • Strict and lax parser

Head to the issue tracker or the Gitter Room if you want to help or need to know more details.

License

Electro Grammar is MIT licensed. It can be freely used in open source and propietary work as long as you include the copyright notice in all copies. See the LICENSE.md file for details.