File size: 1,187 Bytes
a6dc518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { StringReader, StringWriter } from './strings';
import { decodeInteger, encodeInteger, semicolon } from './vlq';

export type MappingIndex = number;
export type RangeMappings = MappingIndex[][];

export function decodeRangeMappings(input: string): RangeMappings {
  const { length } = input;
  const reader = new StringReader(input);
  const rangeMappings: RangeMappings = [];

  do {
    const semi = reader.indexOf(';');
    const indices: MappingIndex[] = [];
    let index = 0;

    while (reader.pos < semi) {
      index += decodeInteger(reader);
      indices.push(index);
    }

    rangeMappings.push(indices);
    reader.pos = semi + 1;
  } while (reader.pos <= length);

  return rangeMappings;
}

export function encodeRangeMappings(decoded: RangeMappings): string {
  if (decoded.length === 0) return '';

  const writer = new StringWriter();

  for (let i = 0; i < decoded.length; i++) {
    const indices = decoded[i];
    if (i > 0) writer.write(semicolon);

    let index = 0;
    for (let j = 0; j < indices.length; j++) {
      const offset = indices[j];
      encodeInteger(writer, offset - index);
      index = offset;
    }
  }

  return writer.flush();
}