Created
October 7, 2015 16:39
-
-
Save yurydelendik/1d4f7847d157cc43dfb3 to your computer and use it in GitHub Desktop.
Parsing source maps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | |
function decodeVLQ64(s) { | |
var i = 0; | |
var result = []; | |
var n = 0, shift = 0; | |
while (i < s.length) { | |
var j = base64Alphabet.indexOf(s[i++]); | |
if (j < 0) throw new Error('Bad base64 symbol'); | |
var more = !!(j & 0x20); | |
n = n | ((j & 0x1F) << shift); | |
if (!more) { | |
var sign = (n & 1); | |
n = sign ? -(n >>> 1) : (n >>> 1); | |
result.push(n); | |
n = 0; | |
shift = 0; | |
} else { | |
shift += 5; | |
if (shift > 27) throw new Error('Overflow'); | |
} | |
} | |
if (shift) { | |
throw new Error('Incomplete encoding'); | |
} | |
return result; | |
} | |
function parseMappings(mappings) { | |
var fields = {c: undefined, s: undefined, sl: undefined, sc: undefined, n: undefined}; | |
var lines = mappings.split(";").map(function (line) { | |
if (line === "") return null; | |
fields.c = undefined; | |
var groups = line.split(",").map(function (group) { | |
var parsed = decodeVLQ64(group); | |
var result = { | |
c: (fields.c = (fields.c === undefined ? parsed[0] : parsed[0] + fields.c)) | |
}; | |
if (parsed.length > 1) { | |
result.s = (fields.s = (fields.s === undefined ? parsed[1] : parsed[1] + fields.s)); | |
result.sl = (fields.sl = (fields.sl === undefined ? parsed[2] : parsed[2] + fields.sl)); | |
result.sc = (fields.sc = (fields.sc === undefined ? parsed[3] : parsed[3] + fields.sc)); | |
} | |
if (parsed.length > 4) { | |
result.n = (fields.n = (fields.n === undefined ? parsed[4] : parsed[4] + fields.n)); | |
} | |
return result; | |
}); | |
return groups; | |
}); | |
return lines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment