{"id":13991887,"url":"https://github.com/kitspace/electro-grammar","last_synced_at":"2025-07-22T14:32:33.558Z","repository":{"id":48257117,"uuid":"91369995","full_name":"kitspace/electro-grammar","owner":"kitspace","description":":zap: A parser for electronic component descriptions","archived":false,"fork":false,"pushed_at":"2024-04-18T12:59:10.000Z","size":739,"stargazers_count":278,"open_issues_count":16,"forks_count":12,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-07-07T12:36:18.908Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://kitspace.github.io/electro-grammar/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kitspace.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-05-15T18:17:14.000Z","updated_at":"2025-02-08T21:24:11.000Z","dependencies_parsed_at":"2022-08-29T21:11:40.780Z","dependency_job_id":"8ec7c27b-0d4b-4504-87ef-a82ea6812983","html_url":"https://github.com/kitspace/electro-grammar","commit_stats":{"total_commits":135,"total_committers":5,"mean_commits":27.0,"dds":0.0444444444444444,"last_synced_commit":"8916e92e708fdf7c57abc1ea8d2ebb6e6087b572"},"previous_names":["monostable/electro-grammar"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/kitspace/electro-grammar","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitspace%2Felectro-grammar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitspace%2Felectro-grammar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitspace%2Felectro-grammar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitspace%2Felectro-grammar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kitspace","download_url":"https://codeload.github.com/kitspace/electro-grammar/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitspace%2Felectro-grammar/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265502828,"owners_count":23777925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T14:01:39.300Z","updated_at":"2025-07-22T14:32:33.202Z","avatar_url":"https://github.com/kitspace.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Electro Grammar\n\n[⚡ demo](https://kitspace.github.io/electro-grammar/)\n\n[![npm](https://img.shields.io/npm/v/electro-grammar.svg?maxAge=3600)](https://www.npmjs.com/package/electro-grammar)\n\nThis is a parser using [Nearley](http://nearley.js.org/) that defines a grammar for describing generic electronic components such as surface mount resistors, capacitors and LEDs.\nA function to match the result to parts in the [Common Parts Library][CPL] is also provided.\n\n```\nnpm install electro-grammar\n```\n\n\n```js\nconst {parse, matchCPL} = require('electro-grammar')\n```\n\n## Where is this used?\n\n- [1-click BOM browser extension](https://1clickbom.com)\n- [Kitspace BOM Builder](https://github.com/kitspace/bom-builder) (currently in alpha)\n- [electron-lang](https://github.com/electron-lang/electron) for [`@cpl`](https://github.com/electron-lang/electron/blob/master/docs/reference.md#cpldescr)\n\n## Parsing\n\n### Capacitors\nParses capacitance, package size, characteristic, tolerance and voltage rating for capacitors.\n\n```js\n\u003e parse('100nF 0603 C0G 10% 25V')\n{ type: 'capacitor',\n  capacitance: 1e-7,\n  size: '0603',\n  characteristic: 'C0G',\n  tolerance: 10,\n  voltage_rating: 25 }\n```\n\nFor [class 1][CLASS-1] ceramic names and EIA letter codes are understood.\nFor [class 2][CLASS-2] only EIA letter codes are understood.\nIn both cases only EIA letter codes are returned.\n\n```js\n\u003e parse('10pF C0G/NP0')\n{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }\n\u003e parse('10pF NP0')\n{ type: 'capacitor', capacitance: 1e-11, characteristic: 'C0G' }\n\u003e parse('10pF X7R')\n{ type: 'capacitor', capacitance: 1e-11, characteristic: 'X7R' }\n```\n\n### Resistors\nParses resistance, package size, tolerance and power rating for resistors.\n\n```js\n\u003e parse('1k 0805 5% 125mW')\n{ type: 'resistor',\n  resistance: 1000,\n  size: '0805',\n  tolerance: 5,\n  power_rating: 0.125 }\n```\n\nElectro-grammar supports several different ways to express resistance.\n\n```js\n\u003e parse('1.5k')\n{ type: 'resistor', resistance: 1500 }\n\u003e parse('1k5')\n{ type: 'resistor', resistance: 1500 }\n\u003e parse('500R')\n{ type: 'resistor', resistance: 500 }\n\u003e parse('1500 ohm')\n{ type: 'resistor', resistance: 1500 }\n\u003e parse('1500.0 ohm')\n{ type: 'resistor', resistance: 1500 }\n\u003e parse('1500 Ω')\n{ type: 'resistor', resistance: 1500 }\n\u003e parse('resistor 1500')\n{ type: 'resistor', resistance: 1500 }\n```\n\n### LEDs\n\nLEDs need to include the word 'LED' or 'led'.\n\n```js\n\u003e parse('LED red')\n{ type: 'led', color: 'red' }\n\u003e parse('LED 0603')\n{ type: 'led', size: '0603' }\n\u003e parse('green led 1206')\n{ type: 'led', color: 'green', size: '1206' }\n```\n\n\n### Parsing Details\n\nConverts all units to floating point numbers.\n\n```js\n\u003e parse('100nF')\n{ type: 'capacitor', capacitance: 1e-7 }\n\u003e parse('0.1uF')\n{ type: 'capacitor', capacitance: 1e-7 }\n```\n\nThe order of the terms doesn't matter.\n\n```js\n\u003e parse('1% 0603 1uF')\n{ type: 'capacitor'\n  capacitance: 0.000001,\n  tolerance: 1,\n  size: \"0603\" }\n\u003e parse('0603 1% 1uF')\n{ type: 'capacitor',\n  capacitance: 0.000001,\n  tolerance: 1,\n  size: \"0603\" }\n```\n\nIf no match is found an empty object is returned.\n\n```js\n\u003e parse('')\n{}\n\u003e parse('NE555P')\n{}\n```\n\nBut invalid input types will throw.\n\n```js\n\u003e parse({})\nTypeError: str.split is not a function\n```\n\nText that is not part of the grammar is simply ignored.\n\n```js\n\u003e parse('NE555P 1uF')\n{ type: 'capacitor', capacitance: 0.000001 }\n\u003e parse('these words 1k are ignored 0805')\n{ type: 'resistor', resistance: 1000, size: '0805' }\n```\n\nYou can use metric package sizes as long as you make it clear by using the `metric` keyword.\nOutput for package sizes is always in imperial.\n\n```js\n\u003e parse('1k metric 0603')\n{ type: 'resistor', resistance: 1000, size: '0201' }\n\u003e parse('1k 0603 metric')\n{ type: 'resistor', resistance: 1000, size: '0201' }\n```\n\nYou 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'\n\n```js\n\u003e parse('capacitor 1u')\n{ type: 'capacitor', capacitance: 0.000001 }\n\u003e parse('c 1u')\n{ type: 'capacitor', capacitance: 0.000001 }\n\u003e parse('resistor 100')\n{ type: 'resistor', resistance: 100 }\n\u003e parse('res 100')\n{ type: 'resistor', resistance: 100 }\n\u003e parse('r 100')\n{ type: 'resistor', resistance: 100 }\n```\n\n## CPL Matching\n`matchCPL` tries to find as many matches as it can from the [Common Parts Library][CPL] and returns an array of CPL IDs.\nYou could match these against [CPL data][CPL-Data] or search for them on Octopart to get exact part numbers.\nIf no matches are found or the function is given invalid input an empty array is returned.\n\n```js\n\u003e c = parse('0.1uF 0805 25V')\n{ type: 'capacitor',\n  capacitance: 1e-7,\n  size: '0805',\n  voltage_rating: 25 }\n\u003e matchCPL(c)\n[ 'CPL-CAP-X7R-0805-100NF-50V' ]\n\n\u003e r = parse('10k 0603')\n{ type: 'resistor', resistance: 10000, size: '0603' }\n\u003e matchCPL(r)\n[ 'CPL-RES-0603-10K-0.1W' ]\n\n\u003e // I don't think it's possible to make such a resistor\n\u003e r = parse('1k 1000000W')\n{ type: 'resistor', resistance: 1000, power_rating: 1000000 }\n\u003e matchCPL(r)\n[]\n\n\u003e matchCPL({invalid: 'input'})\n[]\n\n\u003e matchCPL(null)\n[]\n```\n\n## Roadmap\n\nWe are currently working on v2 of Electro Grammar which will have parsers in many more languages:\n\n### v1\n\n- JavaScript only\n- Capacitors, resistors and LEDs (SMD only)\n- Lax parser only (any-order, ignores invalid input)\n\n### v2\n\n- Work in progress!\n- Uses Antlr4: JavaScript (API compatible with v1), Python, Java, C (\u0026 C++), Go\n- Capacitors, resistors, LEDs, diodes, transistors (SMD \u0026 through-hole)\n- Strict and lax parser\n\nHead to the [issue tracker][ISSUES] or the [Gitter Room][CHAT] if you want to help or need to know more details.\n\n## License\n\nElectro Grammar is MIT licensed. It can be freely used in open source and\npropietary work as long as you include the copyright notice in all copies. See\nthe [LICENSE.md][LICENSE] file for details.\n\n[CPL]: https://octopart.com/common-parts-library#Resistors\n[CPL-DATA]: https://github.com/octopart/CPL-Data\n[BADGE]: https://travis-ci.org/kitspace/electro-grammar.svg?branch=master\n[BUILD]: https://travis-ci.org/kitspace/electro-grammar\n[CLASS-1]: https://en.wikipedia.org/wiki/Ceramic_capacitor#Class_1_ceramic_capacitors\n[CLASS-2]: https://en.wikipedia.org/wiki/Ceramic_capacitor#Class_2_ceramic_capacitors\n[ISSUES]: https://github.com/kitspace/electro-grammar/issues\n[CHAT]: https://gitter.im/monostable/electro-grammar\n[LICENSE]: https://github.com/kitspace/electro-grammar/blob/master/LICENSE.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkitspace%2Felectro-grammar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkitspace%2Felectro-grammar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkitspace%2Felectro-grammar/lists"}