11# CSS Tokenizer
22
33[ <img alt =" npm version " src =" https://img.shields.io/npm/v/@csstools/tokenizer.svg " height =" 20 " >] ( https://www.npmjs.com/package/@csstools/tokenizer )
4- [ <img alt =" build status " src =" https://img.shields.io/travis/csstools/tokenizer/master .svg " height =" 20 " >] ( https://travis-ci.org/github/csstools/tokenizer )
4+ [ <img alt =" build status " src =" https://img.shields.io/travis/csstools/tokenizer/main .svg " height =" 20 " >] ( https://travis-ci.org/github/csstools/tokenizer )
55[ <img alt =" code coverage " src =" https://img.shields.io/codecov/c/github/csstools/tokenizer " height =" 20 " >] ( https://codecov.io/gh/csstools/tokenizer )
66[ <img alt =" issue tracker " src =" https://img.shields.io/github/issues/csstools/tokenizer.svg " height =" 20 " >] ( https://github.com/csstools/tokenizer/issues )
77[ <img alt =" pull requests " src =" https://img.shields.io/github/issues-pr/csstools/tokenizer.svg " height =" 20 " >] ( https://github.com/csstools/tokenizer/pulls )
@@ -29,17 +29,17 @@ npm install @csstools/tokenizer
2929Tokenize CSS in JavaScript:
3030
3131``` js
32- import cssTokenizer from ' @csstools/tokenizer'
32+ import { tokenizer } from ' @csstools/tokenizer'
3333
34- const tokens = Array .from (cssTokenizer (cssText)) // an array of css tokens
34+ const tokens = Array .from (tokenizer (cssText)) // an array of css tokens
3535```
3636
3737Tokenize CSS in _ classical_ NodeJS:
3838
3939``` js
40- const cssTokenizer = require (' @csstools/tokenizer' )
40+ const { tokenizer } = require (' @csstools/tokenizer' )
4141
42- let iterator = cssTokenizer (cssText), iteration
42+ let iterator = tokenizer (cssText), iteration
4343
4444while (! (iteration = iterator ()).done ) {
4545 console .log (iteration .value ) // logs an individual css token
@@ -51,9 +51,9 @@ Tokenize CSS in client-side scripts:
5151``` html
5252<script type =" module" >
5353
54- import cssTokenizer from ' https://unpkg.com/@csstools/tokenizer?module'
54+ import { tokenizer } from ' https://unpkg.com/@csstools/tokenizer?module'
5555
56- const tokens = Array .from (cssTokenizer (cssText)) // an array of css tokens
56+ const tokens = Array .from (tokenizer (cssText)) // an array of css tokens
5757
5858 </script >
5959```
@@ -78,16 +78,25 @@ The CSS tokenizer separates a string of CSS into tokens represented as an array.
7878 /** Position in the string at which the token was retrieved. */
7979 number ,
8080
81- /** Negative number that identifies the kind of token. */
82- number ,
83-
84- /** Opening token content, like the opening of a comment or the quotation mark of a string. */
81+ /** Number identifying the kind of token. */
82+ | - 2 // Comment
83+ | - 3 // Space
84+ | - 4 // Identifier
85+ | - 5 // Function
86+ | - 6 // At-Identifier
87+ | - 7 // Hash
88+ | - 8 // String
89+ | - 9 // Numeric
90+ | number // Delimiter (character code)
91+ ,
92+
93+ /** Lead content, like the opening of a comment or the quotation mark of a string. */
8594 string ,
8695
87- /** Main token content, like the numbers before a unit, or the letters after an at-sign. */
96+ /** Main content, like the numbers before a unit, or the letters after an at-sign. */
8897 string ,
8998
90- /** Closing token content, like the unit of a number, or the closing of a comment. */
99+ /** Tail content, like the unit of a number, or the closing of a comment. */
91100 string ,
92101]
93102```
@@ -104,22 +113,23 @@ The **first** number represents the position at which the token was read. The **
104113
105114## Benchmarks
106115
107- As of September 26, 2020 , these benchmarks were averaged from my local machine:
116+ As of May 11, 2021 , these benchmarks were averaged from my local machine:
108117
109118```
110119Benchmark: Tailwind CSS
111120 ┌──────────────────────────────────────────────────┬───────┬────────┬────────┐
112121 │ (index) │ ms │ ms/50k │ tokens │
113122 ├──────────────────────────────────────────────────┼───────┼────────┼────────┤
114- │ PostCSS x 12.38 ops/sec ±3.33 % (35 runs sampled) │ 80.79 │ 6.97 │ 579896 │
115- │ Develop x 12.07 ops/sec ±1.54 % (34 runs sampled) │ 82.86 │ 6.17 │ 670972 │
123+ │ PostCSS x 11.86 ops/sec ±2.64 % (35 runs sampled) │ 84.31 │ 4.52 │ 933299 │
124+ │ Develop x 14.98 ops/sec ±1.04 % (42 runs sampled) │ 66.75 │ 3.53 │ 946324 │
116125 └──────────────────────────────────────────────────┴───────┴────────┴────────┘
126+
117127Benchmark: Bootstrap
118128 ┌────────────────────────────────────────────────┬──────┬────────┬────────┐
119129 │ (index) │ ms │ ms/50k │ tokens │
120130 ├────────────────────────────────────────────────┼──────┼────────┼────────┤
121- │ PostCSS x 230 ops/sec ±0.50 % (89 runs sampled) │ 4.35 │ 4.37 │ 49807 │
122- │ Develop x 142 ops/sec ±0.76 % (80 runs sampled) │ 7.04 │ 5.92 │ 59502 │
131+ │ PostCSS x 369 ops/sec ±0.12 % (95 runs sampled) │ 2.71 │ 2.77 │ 49006 │
132+ │ Develop x 272 ops/sec ±0.10 % (93 runs sampled) │ 3.68 │ 3.22 │ 57141 │
123133 └────────────────────────────────────────────────┴──────┴────────┴────────┘
124134```
125135
0 commit comments