Skip to content

Commit

Permalink
update READMEs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Oct 16, 2021
1 parent 45414d5 commit 45ea175
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 64 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.md
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier.disableLanguages": ["md"]
}
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ A [variable-length quantity](http://en.wikipedia.org/wiki/Variable-length_quanti
npm install vlq
```

...or...

```bash
bower install vlq
```

...or grab the vlq.js file and include it with a `<script src='vlq.js'>` tag.


## Usage

Expand All @@ -43,17 +35,17 @@ bower install vlq
`vlq.encode` accepts an integer, or an array of integers, and returns a string:

```js
vlq.encode( 123 ); // '2H';
vlq.encode([ 123, 456, 789 ]); // '2HwcqxB'
vlq.encode(123); // '2H';
vlq.encode([123, 456, 789]); // '2HwcqxB'
```

### Decoding

`vlq.decode` accepts a string and always returns an array:

```js
vlq.decode( '2H' ); // [ 123 ]
vlq.decode( '2HwcqxB' ); // [ 123, 456, 789 ]
vlq.decode('2H'); // [123]
vlq.decode('2HwcqxB'); // [123, 456, 789]
```


Expand Down
98 changes: 46 additions & 52 deletions sourcemaps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ And CoffeeScript would (if you asked it to) generate a sourcemap like this:
"file": "helloworld.js",
"sources": [
"helloworld.coffee"
],
],
"names": [],
"mappings": "AAAA;AAAA,EAAA,OAAO,CAAC,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA"
}
Expand All @@ -44,48 +44,44 @@ Each line in the generated JavaScript (`helloworld.js`) is represented as a seri

```js
mappings = 'AAAA;AAAA,EAAA,OAAO,CAAC,GAAR,CAAY,aAAZ,CAAA,CAAA;AAAA';
vlqs = mappings.split( ';' ).map( function ( line ) {
return line.split( ',' );
});
vlqs = mappings.split(';').map(line => line.split(','));

[
// line 0 of helloworld.js (everything is zero-based)
[ 'AAAA' ],
// line 0 of helloworld.js (everything is zero-based)
['AAAA'],

// line 1
[ 'AAAA', 'EAAA', 'OAAO', 'CAAC', 'GAAR', 'CAAY', 'aAAZ', 'CAAA', 'CAAA' ],
// line 1
['AAAA', 'EAAA', 'OAAO', 'CAAC', 'GAAR', 'CAAY', 'aAAZ', 'CAAA', 'CAAA'],

// line 2
[ 'AAAA' ]
// line 2
['AAAA']
]
```

Using vlq.js to decode each segment, we can convert that into the following:

```js
decoded = vlqs.map( function ( line ) {
return line.map( vlq.decode );
});
decoded = vlqs.map(line => line.map(vlq.decode));

[
// line 0
[ [ 0, 0, 0, 0 ] ],
[[0, 0, 0, 0]],

// line 1
[
[ 0, 0, 0, 0 ],
[ 2, 0, 0, 0 ],
[ 7, 0, 0, 7 ],
[ 1, 0, 0, 1 ],
[ 3, 0, 0, -8 ],
[ 1, 0, 0, 12 ],
[ 13, 0, 0, -12 ],
[ 1, 0, 0, 0 ],
[ 1, 0, 0, 0 ]
[0, 0, 0, 0],
[2, 0, 0, 0],
[7, 0, 0, 7],
[1, 0, 0, 1],
[3, 0, 0, -8],
[1, 0, 0, 12],
[13, 0, 0, -12],
[1, 0, 0, 0],
[1, 0, 0, 0]
],

// line 2
[ [ 0, 0, 0, 0 ] ]
[[0, 0, 0, 0]]
]
```

Expand All @@ -100,22 +96,20 @@ Each segment has 4 *fields* in this case, though the [spec](https://docs.google.
We can now decode our mappings a bit further:

```js
var sourceFileIndex = 0, // second field
sourceCodeLine = 0, // third field
sourceCodeColumn = 0, // fourth field
nameIndex = 0; // fifth field

decoded = decoded.map( function ( line ) {
var generatedCodeColumn = 0; // first field - reset each time
let sourceFileIndex = 0; // second field
let sourceCodeLine = 0; // third field
let sourceCodeColumn = 0; // fourth field
let nameIndex = 0; // fifth field

return line.map( function ( segment ) {
var result;
decoded = decoded.map(line => {
let generatedCodeColumn = 0; // first field - reset each time

return line.map(segment => {
generatedCodeColumn += segment[0];

result = [ generatedCodeColumn ];
const result = [generatedCodeColumn];

if ( segment.length === 1 ) {
if (segment.length === 1) {
// only one field!
return result;
}
Expand All @@ -124,11 +118,11 @@ decoded = decoded.map( function ( line ) {
sourceCodeLine += segment[2];
sourceCodeColumn += segment[3];

result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn );
result.push(sourceFileIndex, sourceCodeLine, sourceCodeColumn);

if ( segment.length === 5 ) {
if (segment.length === 5) {
nameIndex += segment[4];
result.push( nameIndex );
result.push(nameIndex);
}

return result;
Expand All @@ -137,23 +131,23 @@ decoded = decoded.map( function ( line ) {

[
// line 0
[ [ 0, 0, 0, 0 ] ],
[[0, 0, 0, 0]],

// line 1
[
[ 0, 0, 0, 0 ],
[ 2, 0, 0, 0 ],
[ 9, 0, 0, 7 ],
[ 10, 0, 0, 8 ],
[ 13, 0, 0, 0 ],
[ 14, 0, 0, 12 ],
[ 27, 0, 0, 0 ],
[ 28, 0, 0, 0 ],
[ 29, 0, 0, 0 ]
[0, 0, 0, 0],
[2, 0, 0, 0],
[9, 0, 0, 7],
[10, 0, 0, 8],
[13, 0, 0, 0],
[14, 0, 0, 12],
[27, 0, 0, 0],
[28, 0, 0, 0],
[29, 0, 0, 0]
],

// line 2
[ [ 0, 0, 0, 0 ] ]
[[0, 0, 0, 0]]
]
```

Expand All @@ -163,18 +157,18 @@ The first and third lines don't really contain any interesting information. But
// line 1 (the second line - still zero-based, remember)
[
// Column 0 of line 1 corresponds to source file 0, line 0, column 0
[ 0, 0, 0, 0 ],
[0, 0, 0, 0],

// Column 2 of line 1 also corresponds to 0, 0, 0! In other words, the
// two spaces before `console` in helloworld.js don't correspond to
// anything in helloworld.coffee
[ 2, 0, 0, 0 ],
[2, 0, 0, 0],

// Column 9 of line 1 corresponds to 0, 0, 7. Taken together with the
// previous segment, this means that columns 2-9 of line 1 in the
// generated helloworld.js file correspond to columns 0-7 of line 0
// in the original helloworld.coffee
[ 9, 0, 0, 7 ],
[9, 0, 0, 7],

...
]
Expand Down

0 comments on commit 45ea175

Please sign in to comment.