Created
January 15, 2017 01:28
-
-
Save dosstx/42c04c28ab74aef54eccfa07fed34f13 to your computer and use it in GitHub Desktop.
This file contains 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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.geojsonvt = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | |
'use strict'; | |
module.exports = clip; | |
/* clip features between two axis-parallel lines: | |
* | | | |
* ___|___ | / | |
* / | \____|____/ | |
* | | | |
*/ | |
function clip(features, scale, k1, k2, axis, intersect) { | |
k1 /= scale; | |
k2 /= scale; | |
var clipped = []; | |
for (var i = 0; i < features.length; i++) { | |
var feature = features[i], | |
geometry = feature.geometry, | |
type = feature.type, | |
min, max; | |
if (feature.min) { | |
min = feature.min[axis]; | |
max = feature.max[axis]; | |
if (min >= k1 && max <= k2) { // trivial accept | |
clipped.push(feature); | |
continue; | |
} else if (min > k2 || max < k1) continue; // trivial reject | |
} | |
var slices = type === 1 ? | |
clipPoints(geometry, k1, k2, axis) : | |
clipGeometry(geometry, k1, k2, axis, intersect, type === 3); | |
if (slices.length) { | |
// if a feature got clipped, it will likely get clipped on the next zoom level as well, | |
// so there's no need to recalculate bboxes | |
clipped.push({ | |
geometry: slices, | |
type: type, | |
tags: features[i].tags || null | |
}); | |
} | |
} | |
return clipped.length ? clipped : null; | |
} | |
function clipPoints(geometry, k1, k2, axis) { | |
var slice = []; | |
for (var i = 0; i < geometry.length; i++) { | |
var a = geometry[i], | |
ak = a[axis]; | |
if (ak >= k1 && ak <= k2) slice.push(a); | |
} | |
return slice; | |
} | |
function clipGeometry(geometry, k1, k2, axis, intersect, closed) { | |
var slices = []; | |
for (var i = 0; i < geometry.length; i++) { | |
var ak = 0, | |
bk = 0, | |
b = null, | |
points = geometry[i], | |
area = points.area, | |
dist = points.dist, | |
len = points.length, | |
a, j; | |
var slice = []; | |
for (j = 0; j < len - 1; j++) { | |
a = b || points[j]; | |
b = points[j + 1]; | |
ak = bk || a[axis]; | |
bk = b[axis]; | |
if (ak < k1) { | |
if ((bk > k2)) { // ---|-----|--> | |
slice.push(intersect(a, b, k1), intersect(a, b, k2)); | |
if (!closed) slice = newSlice(slices, slice, area, dist); | |
} else if (bk >= k1) slice.push(intersect(a, b, k1)); // ---|--> | | |
} else if (ak > k2) { | |
if ((bk < k1)) { // <--|-----|--- | |
slice.push(intersect(a, b, k2), intersect(a, b, k1)); | |
if (!closed) slice = newSlice(slices, slice, area, dist); | |
} else if (bk <= k2) slice.push(intersect(a, b, k2)); // | <--|--- | |
} else { | |
slice.push(a); | |
if (bk < k1) { // <--|--- | | |
slice.push(intersect(a, b, k1)); | |
if (!closed) slice = newSlice(slices, slice, area, dist); | |
} else if (bk > k2) { // | ---|--> | |
slice.push(intersect(a, b, k2)); | |
if (!closed) slice = newSlice(slices, slice, area, dist); | |
} | |
// | --> | | |
} | |
} | |
// add the last point | |
a = points[len - 1]; | |
ak = a[axis]; | |
if (ak >= k1 && ak <= k2) slice.push(a); | |
// close the polygon if its endpoints are not the same after clipping | |
if (closed && slice[0] !== slice[slice.length - 1]) slice.push(slice[0]); | |
// add the final slice | |
newSlice(slices, slice, area, dist); | |
} | |
return slices; | |
} | |
function newSlice(slices, slice, area, dist) { | |
if (slice.length) { | |
// we don't recalculate the area/length of the unclipped geometry because the case where it goes | |
// below the visibility threshold as a result of clipping is rare, so we avoid doing unnecessary work | |
slice.area = area; | |
slice.dist = dist; | |
slices.push(slice); | |
} | |
return []; | |
} | |
},{}],2:[function(require,module,exports){ | |
'use strict'; | |
module.exports = convert; | |
var simplify = require('./simplify'); | |
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data | |
function convert(data, tolerance) { | |
var features = []; | |
if (data.type === 'FeatureCollection') { | |
for (var i = 0; i < data.features.length; i++) { | |
convertFeature(features, data.features[i], tolerance); | |
} | |
} else if (data.type === 'Feature') { | |
convertFeature(features, data, tolerance); | |
} else { | |
// single geometry or a geometry collection | |
convertFeature(features, {geometry: data}, tolerance); | |
} | |
return features; | |
} | |
function convertFeature(features, feature, tolerance) { | |
var geom = feature.geometry, | |
type = geom.type, | |
coords = geom.coordinates, | |
tags = feature.properties, | |
i, j, rings; | |
if (type === 'Point') { | |
features.push(create(tags, 1, [projectPoint(coords)])); | |
} else if (type === 'MultiPoint') { | |
features.push(create(tags, 1, project(coords))); | |
} else if (type === 'LineString') { | |
features.push(create(tags, 2, [project(coords, tolerance)])); | |
} else if (type === 'MultiLineString' || type === 'Polygon') { | |
rings = []; | |
for (i = 0; i < coords.length; i++) { | |
rings.push(project(coords[i], tolerance)); | |
} | |
features.push(create(tags, type === 'Polygon' ? 3 : 2, rings)); | |
} else if (type === 'MultiPolygon') { | |
rings = []; | |
for (i = 0; i < coords.length; i++) { | |
for (j = 0; j < coords[i].length; j++) { | |
rings.push(project(coords[i][j], tolerance)); | |
} | |
} | |
features.push(create(tags, 3, rings)); | |
} else if (type === 'GeometryCollection') { | |
for (i = 0; i < geom.geometries.length; i++) { | |
convertFeature(features, { | |
geometry: geom.geometries[i], | |
properties: tags | |
}, tolerance); | |
} | |
} else { | |
console.warn('Unsupported GeoJSON type: ' + geom.type); | |
} | |
} | |
function create(tags, type, geometry) { | |
var feature = { | |
geometry: geometry, | |
type: type, | |
tags: tags || null, | |
min: [1, 1], // initial bbox values; | |
max: [0, 0] // note that all coords are in [0..1] range | |
}; | |
calcBBox(feature); | |
return feature; | |
} | |
function project(lonlats, tolerance) { | |
var projected = []; | |
for (var i = 0; i < lonlats.length; i++) { | |
projected.push(projectPoint(lonlats[i])); | |
} | |
if (tolerance) { | |
simplify(projected, tolerance); | |
calcSize(projected); | |
} | |
return projected; | |
} | |
function projectPoint(p) { | |
var sin = Math.sin(p[1] * Math.PI / 180), | |
x = (p[0] / 360 + 0.5), | |
y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); | |
return [x, y, 0]; | |
} | |
// calculate area and length of the poly | |
function calcSize(points) { | |
var area = 0, | |
dist = 0; | |
for (var i = 0, a, b; i < points.length - 1; i++) { | |
a = b || points[i]; | |
b = points[i + 1]; | |
area += a[0] * b[1] - b[0] * a[1]; | |
// use Manhattan distance instead of Euclidian one to avoid expensive square root computation | |
dist += Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]); | |
} | |
points.area = Math.abs(area / 2); | |
points.dist = dist; | |
} | |
// calculate the feature bounding box for faster clipping later | |
function calcBBox(feature) { | |
var geometry = feature.geometry, | |
min = feature.min, | |
max = feature.max; | |
if (feature.type === 1) calcRingBBox(min, max, geometry); | |
else for (var i = 0; i < geometry.length; i++) calcRingBBox(min, max, geometry[i]); | |
return feature; | |
} | |
function calcRingBBox(min, max, points) { | |
for (var i = 0, p; i < points.length; i++) { | |
p = points[i]; | |
min[0] = Math.min(p[0], min[0]); | |
max[0] = Math.max(p[0], max[0]); | |
min[1] = Math.min(p[1], min[1]); | |
max[1] = Math.max(p[1], max[1]); | |
} | |
} | |
},{"./simplify":4}],3:[function(require,module,exports){ | |
'use strict'; | |
module.exports = geojsonvt; | |
var convert = require('./convert'), // GeoJSON conversion and preprocessing | |
clip = require('./clip'), // stripe clipping algorithm | |
createTile = require('./tile'); // final simplified tile generation | |
function geojsonvt(data, options) { | |
return new GeoJSONVT(data, options); | |
} | |
function GeoJSONVT(data, options) { | |
options = this.options = extend(Object.create(this.options), options); | |
var debug = options.debug; | |
if (debug) console.time('preprocess data'); | |
var z2 = 1 << options.maxZoom, // 2^z | |
features = convert(data, options.tolerance / (z2 * options.extent)); | |
this.tiles = {}; | |
if (debug) { | |
console.timeEnd('preprocess data'); | |
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints); | |
console.time('generate tiles'); | |
this.stats = {}; | |
this.total = 0; | |
} | |
// start slicing from the top tile down | |
this.splitTile(features, 0, 0, 0); | |
if (debug) { | |
console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints); | |
console.timeEnd('generate tiles'); | |
console.log('tiles generated:', this.total, JSON.stringify(this.stats)); | |
} | |
} | |
GeoJSONVT.prototype.options = { | |
maxZoom: 14, // max zoom to preserve detail on | |
indexMaxZoom: 5, // max zoom in the tile index | |
indexMaxPoints: 100000, // max number of points per tile in the tile index | |
tolerance: 3, // simplification tolerance (higher means simpler) | |
extent: 4096, // tile extent | |
buffer: 64, // tile buffer on each side | |
debug: 0 // logging level (0, 1 or 2) | |
}; | |
GeoJSONVT.prototype.splitTile = function (features, z, x, y, cz, cx, cy) { | |
var stack = [features, z, x, y], | |
options = this.options, | |
debug = options.debug, | |
extent = options.extent, | |
buffer = options.buffer; | |
// avoid recursion by using a processing queue | |
while (stack.length) { | |
features = stack.shift(); | |
z = stack.shift(); | |
x = stack.shift(); | |
y = stack.shift(); | |
var z2 = 1 << z, | |
id = toID(z, x, y), | |
tile = this.tiles[id], | |
tileTolerance = z === options.maxZoom ? 0 : options.tolerance / (z2 * extent); | |
if (!tile) { | |
if (debug > 1) console.time('creation'); | |
tile = this.tiles[id] = createTile(features, z2, x, y, tileTolerance, z === options.maxZoom); | |
if (debug) { | |
if (debug > 1) { | |
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', | |
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified); | |
console.timeEnd('creation'); | |
} | |
var key = 'z' + z; | |
this.stats[key] = (this.stats[key] || 0) + 1; | |
this.total++; | |
} | |
} | |
// save reference to original geometry in tile so that we can drill down later if we stop now | |
tile.source = features; | |
// stop tiling if the tile is degenerate | |
if (isClippedSquare(tile.features, extent, buffer)) continue; | |
// if it's the first-pass tiling | |
if (!cz) { | |
// stop tiling if we reached max zoom, or if the tile is too simple | |
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue; | |
// if a drilldown to a specific tile | |
} else { | |
// stop tiling if we reached base zoom or our target tile zoom | |
if (z === options.maxZoom || z === cz) continue; | |
// stop tiling if it's not an ancestor of the target tile | |
var m = 1 << (cz - z); | |
if (x !== Math.floor(cx / m) && y !== Math.floor(cy / m)) continue; | |
} | |
// if we slice further down, no need to keep source geometry | |
tile.source = null; | |
if (debug > 1) console.time('clipping'); | |
// values we'll use for clipping | |
var k1 = 0.5 * buffer / extent, | |
k2 = 0.5 - k1, | |
k3 = 0.5 + k1, | |
k4 = 1 + k1, | |
tl, bl, tr, br, left, right; | |
tl = bl = tr = br = null; | |
left = clip(features, z2, x - k1, x + k3, 0, intersectX); | |
right = clip(features, z2, x + k2, x + k4, 0, intersectX); | |
if (left) { | |
tl = clip(left, z2, y - k1, y + k3, 1, intersectY); | |
bl = clip(left, z2, y + k2, y + k4, 1, intersectY); | |
} | |
if (right) { | |
tr = clip(right, z2, y - k1, y + k3, 1, intersectY); | |
br = clip(right, z2, y + k2, y + k4, 1, intersectY); | |
} | |
if (debug > 1) console.timeEnd('clipping'); | |
if (tl) stack.push(tl, z + 1, x * 2, y * 2); | |
if (bl) stack.push(bl, z + 1, x * 2, y * 2 + 1); | |
if (tr) stack.push(tr, z + 1, x * 2 + 1, y * 2); | |
if (br) stack.push(br, z + 1, x * 2 + 1, y * 2 + 1); | |
} | |
}; | |
GeoJSONVT.prototype.getTile = function (z, x, y) { | |
var options = this.options, | |
extent = options.extent, | |
debug = options.debug; | |
var id = toID(z, x, y); | |
if (this.tiles[id]) return transformTile(this.tiles[id], extent); | |
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y); | |
var z0 = z, | |
x0 = x, | |
y0 = y, | |
parent; | |
while (!parent && z0 > 0) { | |
z0--; | |
x0 = Math.floor(x0 / 2); | |
y0 = Math.floor(y0 / 2); | |
parent = this.tiles[toID(z0, x0, y0)]; | |
} | |
if (debug > 1) console.log('found parent tile z%d-%d-%d', z0, x0, y0); | |
// if we found a parent tile containing the original geometry, we can drill down from it | |
if (parent && parent.source) { | |
if (isClippedSquare(parent.features, options.extent, options.buffer)) return transformTile(parent, extent); | |
if (debug > 1) console.time('drilling down'); | |
this.splitTile(parent.source, z0, x0, y0, z, x, y); | |
if (debug > 1) console.timeEnd('drilling down'); | |
} | |
return transformTile(this.tiles[id], extent); | |
}; | |
function transformTile(tile, extent) { | |
if (!tile || tile.transformed) return tile; | |
var z2 = tile.z2, | |
tx = tile.x, | |
ty = tile.y, | |
i, j, k; | |
for (i = 0; i < tile.features.length; i++) { | |
var feature = tile.features[i], | |
geom = feature.geometry, | |
type = feature.type; | |
if (type === 1) { | |
for (j = 0; j < geom.length; j++) geom[j] = transformPoint(geom[j], extent, z2, tx, ty); | |
} else { | |
for (j = 0; j < geom.length; j++) { | |
var ring = geom[j]; | |
for (k = 0; k < ring.length; k++) ring[k] = transformPoint(ring[k], extent, z2, tx, ty); | |
} | |
} | |
} | |
tile.transformed = true; | |
return tile; | |
} | |
function transformPoint(p, extent, z2, tx, ty) { | |
var x = Math.round(extent * (p[0] * z2 - tx)), | |
y = Math.round(extent * (p[1] * z2 - ty)); | |
return [x, y]; | |
} | |
// checks whether a tile is a whole-area fill after clipping; if it is, there's no sense slicing it further | |
function isClippedSquare(features, extent, buffer) { | |
if (features.length !== 1) return false; | |
var feature = features[0]; | |
if (feature.type !== 3 || feature.geometry.length > 1) return false; | |
for (var i = 0; i < feature.geometry[0].length; i++) { | |
var p = feature.geometry[0][i]; | |
if ((p[0] !== -buffer && p[0] !== extent + buffer) || | |
(p[1] !== -buffer && p[1] !== extent + buffer)) return false; | |
} | |
return true; | |
} | |
function toID(z, x, y) { | |
return (((1 << z) * y + x) * 32) + z; | |
} | |
function intersectX(a, b, x) { | |
return [x, (x - a[0]) * (b[1] - a[1]) / (b[0] - a[0]) + a[1], 1]; | |
} | |
function intersectY(a, b, y) { | |
return [(y - a[1]) * (b[0] - a[0]) / (b[1] - a[1]) + a[0], y, 1]; | |
} | |
function extend(dest, src) { | |
for (var i in src) dest[i] = src[i]; | |
return dest; | |
} | |
},{"./clip":1,"./convert":2,"./tile":5}],4:[function(require,module,exports){ | |
'use strict'; | |
module.exports = simplify; | |
// calculate simplification data using optimized Douglas-Peucker algorithm | |
function simplify(points, tolerance) { | |
var sqTolerance = tolerance * tolerance, | |
len = points.length, | |
first = 0, | |
last = len - 1, | |
stack = [], | |
i, maxSqDist, sqDist, index; | |
// always retain the endpoints (1 is the max value) | |
points[first][2] = 1; | |
points[last][2] = 1; | |
// avoid recursion by using a stack | |
while (last) { | |
maxSqDist = 0; | |
for (i = first + 1; i < last; i++) { | |
sqDist = getSqSegDist(points[i], points[first], points[last]); | |
if (sqDist > maxSqDist) { | |
index = i; | |
maxSqDist = sqDist; | |
} | |
} | |
if (maxSqDist > sqTolerance) { | |
points[index][2] = maxSqDist; // save the point importance in squared pixels as a z coordinate | |
stack.push(first); | |
stack.push(index); | |
first = index; | |
} else { | |
last = stack.pop(); | |
first = stack.pop(); | |
} | |
} | |
} | |
// square distance from a point to a segment | |
function getSqSegDist(p, a, b) { | |
var x = a[0], y = a[1], | |
bx = b[0], by = b[1], | |
px = p[0], py = p[1], | |
dx = bx - x, | |
dy = by - y; | |
if (dx !== 0 || dy !== 0) { | |
var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy); | |
if (t > 1) { | |
x = bx; | |
y = by; | |
} else if (t > 0) { | |
x += dx * t; | |
y += dy * t; | |
} | |
} | |
dx = px - x; | |
dy = py - y; | |
return dx * dx + dy * dy; | |
} | |
},{}],5:[function(require,module,exports){ | |
'use strict'; | |
module.exports = createTile; | |
function createTile(features, z2, tx, ty, tolerance, noSimplify) { | |
var tile = { | |
features: [], | |
numPoints: 0, | |
numSimplified: 0, | |
numFeatures: 0, | |
source: null, | |
x: tx, | |
y: ty, | |
z2: z2, | |
transformed: false | |
}; | |
for (var i = 0; i < features.length; i++) { | |
tile.numFeatures++; | |
addFeature(tile, features[i], tolerance, noSimplify); | |
} | |
return tile; | |
} | |
function addFeature(tile, feature, tolerance, noSimplify) { | |
var geom = feature.geometry, | |
type = feature.type, | |
simplified = [], | |
sqTolerance = tolerance * tolerance, | |
i, j, ring, p; | |
if (type === 1) { | |
for (i = 0; i < geom.length; i++) { | |
simplified.push(geom[i]); | |
tile.numPoints++; | |
tile.numSimplified++; | |
} | |
} else { | |
// simplify and transform projected coordinates for tile geometry | |
for (i = 0; i < geom.length; i++) { | |
ring = geom[i]; | |
// filter out tiny polylines & polygons | |
if (!noSimplify && ((type === 2 && ring.dist < tolerance) || | |
(type === 3 && ring.area < sqTolerance))) { | |
tile.numPoints += ring.length; | |
continue; | |
} | |
var simplifiedRing = []; | |
for (j = 0; j < ring.length; j++) { | |
p = ring[j]; | |
// keep points with importance > tolerance | |
if (noSimplify || p[2] > sqTolerance) { | |
simplifiedRing.push(p); | |
tile.numSimplified++; | |
} | |
tile.numPoints++; | |
} | |
simplified.push(simplifiedRing); | |
} | |
} | |
if (simplified.length) { | |
tile.features.push({ | |
geometry: simplified, | |
type: type, | |
tags: feature.tags || null | |
}); | |
} | |
} | |
},{}]},{},[3])(3) | |
}); | |
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJzcmMvY2xpcC5qcyIsInNyYy9jb252ZXJ0LmpzIiwic3JjL2luZGV4LmpzIiwic3JjL3NpbXBsaWZ5LmpzIiwic3JjL3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ZlYXR1cmUubWluKSB7XG4gICAgICAgICAgICBtaW4gPSBmZWF0dXJlLm1pbltheGlzXTtcbiAgICAgICAgICAgIG1heCA9IGZlYXR1cmUubWF4W2F4aXNdO1xuXG4gICAgICAgICAgICBpZiAobWluID49IGsxICYmIG1heCA8PSBrMikgeyAvLyB0cml2aWFsIGFjY2VwdFxuICAgICAgICAgICAgICAgIGNsaXBwZWQucHVzaChmZWF0dXJlKTtcbiAgICAgICAgICAgICAgICBjb250aW51ZTtcbiAgICAgICAgICAgIH0gZWxzZSBpZiAobWluID4gazIgfHwgbWF4IDwgazEpIGNvbnRpbnVlOyAvLyB0cml2aWFsIHJlamVjdFxuICAgICAgICB9XG5cbiAgICAgICAgdmFyIHNsaWNlcyA9IHR5cGUgPT09IDEgP1xuICAgICAgICAgICAgICAgIGNsaXBQb2ludHMoZ2VvbWV0cnksIGsxLCBrMiwgYXhpcykgOlxuICAgICAgICAgICAgICAgIGNsaXBHZW9tZXRyeShnZW9tZXRyeSwgazEsIGsyLCBheGlzLCBpbnRlcnNlY3QsIHR5cGUgPT09IDMpO1xuXG4gICAgICAgIGlmIChzbGljZXMubGVuZ3RoKSB7XG4gICAgICAgICAgICAvLyBpZiBhIGZlYXR1cmUgZ290IGNsaXBwZWQsIGl0IHdpbGwgbGlrZWx5IGdldCBjbGlwcGVkIG9uIHRoZSBuZXh0IHpvb20gbGV2ZWwgYXMgd2VsbCxcbiAgICAgICAgICAgIC8vIHNvIHRoZXJlJ3Mgbm8gbmVlZCB0byByZWNhbGN1bGF0ZSBiYm94ZXNcbiAgICAgICAgICAgIGNsaXBwZWQucHVzaCh7XG4gICAgICAgICAgICAgICAgZ2VvbWV0cnk6IHNsaWNlcyxcbiAgICAgICAgICAgICAgICB0eXBlOiB0eXBlLFxuICAgICAgICAgICAgICAgIHRhZ3M6IGZlYXR1cmVzW2ldLnRhZ3MgfHwgbnVsbFxuICAgICAgICAgICAgfSk7XG4gICAgICAgIH1cbiAgICB9XG5cbiAgICByZXR1cm4gY2xpcHBlZC5sZW5ndGggPyBjbGlwcGVkIDogbnVsbDtcbn1cblxuZnVuY3Rpb24gY2xpcFBvaW50cyhnZW9tZXRyeSwgazEsIGsyLCBheGlzKSB7XG4gICAgdmFyIHNsaWNlID0gW107XG5cbiAgICBmb3IgKHZhciBpID0gMDsgaSA8IGdlb21ldHJ5Lmxlbmd0aDsgaSsrKSB7XG4gICAgICAgIHZhciBhID0gZ2VvbWV0cnlbaV0sXG4gICAgICAgICAgICBhayA9IGFbYXhpc107XG5cbiAgICAgICAgaWYgKGFrID49IGsxICYmIGFrIDw9IGsyKSBzbGljZS5wdXNoKGEpO1xuICAgIH1cbiAgICByZXR1cm4gc2xpY2U7XG59XG5cbmZ1bmN0aW9uIGNsaXBHZW9tZXRyeShnZW9tZXRyeSwgazEsIGsyLCBheGlzLCBpbnRlcnNlY3QsIGNsb3NlZCkge1xuXG4gICAgdmFyIHNsaWNlcyA9IFtdO1xuXG4gICAgZm9yICh2YXIgaSA9IDA7IGkgPCBnZW9tZXRyeS5sZW5ndGg7IGkrKykge1xuXG4gICAgICAgIHZhciBhayA9IDAsXG4gICAgICAgICAgICBiayA9IDAsXG4gICAgICAgICAgICBiID0gbnVsbCxcbiAgICAgICAgICAgIHBvaW50cyA9IGdlb21ldHJ5W2ldLFxuICAgICAgICAgICAgYXJlYSA9IHBvaW50cy5hcmVhLFxuICAgICAgICAgICAgZGlzdCA9IHBvaW50cy5kaXN0LFxuICAgICAgICAgICAgbGVuID0gcG9pbnRzLmxlbmd0aCxcbiAgICAgICAgICAgIGEsIGo7XG5cbiAgICAgICAgdmFyIHNsaWNlID0gW107XG5cbiAgICAgICAgZm9yIChqID0gMDsgaiA8IGxlbiAtIDE7IGorKykge1xuICAgICAgICAgICAgYSA9IGIgfHwgcG9pbnRzW2pdO1xuICAgICAgICAgICAgYiA9IHBvaW50c1tqICsgMV07XG4gICAgICAgICAgICBhayA9IGJrIHx8IGFbYXhpc107XG4gICAgICAgICAgICBiayA9IGJbYXhpc107XG5cbiAgICAgICAgICAgIGlmIChhayA8IGsxKSB7XG5cbiAgICAgICAgICAgICAgICBpZiAoKGJrID4gazIpKSB7IC8vIC0tLXwtLS0tLXwtLT5cbiAgICAgICAgICAgICAgICAgICAgc2xpY2UucHVzaChpbnRlcnNlY3QoYSwgYiwgazEpLCBpbnRlcnNlY3QoYSwgYiwgazIpKTtcbiAgICAgICAgICAgICAgICAgICAgaWYgKCFjbG9zZWQpIHNsaWNlID0gbmV3U2xpY2Uoc2xpY2VzLCBzbGljZSwgYXJlYSwgZGlzdCk7XG5cbiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKGJrID49IGsxKSBzbGljZS5wdXNoKGludGVyc2VjdChhLCBiLCBrMSkpOyAvLyAtLS18LS0+ICB8XG5cbiAgICAgICAgICAgIH0gZWxzZSBpZiAoYWsgPiBrMikge1xuXG4gICAgICAgICAgICAgICAgaWYgKChiayA8IGsxKSkgeyAvLyA8LS18LS0tLS18LS0tXG4gICAgICAgICAgICAgICAgICAgIHNsaWNlLnB1c2goaW50ZXJzZWN0KGEsIGIsIGsyKSwgaW50ZXJzZWN0KGEsIGIsIGsxKSk7XG4gICAgICAgICAgICAgICAgICAgIGlmICghY2xvc2VkKSBzbGljZSA9IG5ld1NsaWNlKHNsaWNlcywgc2xpY2UsIGFyZWEsIGRpc3QpO1xuXG4gICAgICAgICAgICAgICAgfSBlbHNlIGlmIChiayA8PSBrMikgc2xpY2UucHVzaChpbnRlcnNlY3QoYSwgYiwgazIpKTsgLy8gfCAgPC0tfC0tLVxuXG4gICAgICAgICAgICB9IGVsc2Uge1xuXG4gICAgICAgICAgICAgICAgc2xpY2UucHVzaChhKTtcblxuICAgICAgICAgICAgICAgIGlmIChiayA8IGsxKSB7IC8vIDwtLXwtLS0gIHxcbiAgICAgICAgICAgICAgICAgICAgc2xpY2UucHVzaChpbnRlcnNlY3QoYSwgYiwgazEpKTtcbiAgICAgICAgICAgICAgICAgICAgaWYgKCFjbG9zZWQp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gPSBzdGFjay5zaGlmdCgpO1xuICAgICAgICB6ID0gc3RhY2suc2hpZnQoKTtcbiAgICAgICAgeCA9IHN0YWNrLnNoaWZ0KCk7XG4gICAgICAgIHkgPSBzdGFjay5zaGlmdCgpO1xuXG4gICAgICAgIHZhciB6MiA9IDEgPDwgeixcbiAgICAgICAgICAgIGlkID0gdG9JRCh6LCB4LCB5KSxcbiAgICAgICAgICAgIHRpbGUgPSB0aGlzLnRpbGVzW2lkXSxcbiAgICAgICAgICAgIHRpbGVUb2xlcmFuY2UgPSB6ID09PSBvcHRpb25zLm1heFpvb20gPyAwIDogb3B0aW9ucy50b2xlcmFuY2UgLyAoejIgKiBleHRlbnQpO1xuXG4gICAgICAgIGlmICghdGlsZSkge1xuICAgICAgICAgICAgaWYgKGRlYnVnID4gMSkgY29uc29sZS50aW1lKCdjcmVhdGlvbicpO1xuXG4gICAgICAgICAgICB0aWxlID0gdGhpcy50aWxlc1tpZF0gPSBjcmVhdGVUaWxlKGZlYXR1cmVzLCB6MiwgeCwgeSwgdGlsZVRvbGVyYW5jZSwgeiA9PT0gb3B0aW9ucy5tYXhab29tKTtcblxuICAgICAgICAgICAgaWYgKGRlYnVnKSB7XG4gICAgICAgICAgICAgICAgaWYgKGRlYnVnID4gMSkge1xuICAgICAgICAgICAgICAgICAgICBjb25zb2xlLmxvZygndGlsZSB6JWQtJWQtJWQgKGZlYXR1cmVzOiAlZCwgcG9pbnRzOiAlZCwgc2ltcGxpZmllZDogJWQpJyxcbiAgICAgICAgICAgICAgICAgICAgICAgIHosIHgsIHksIHRpbGUubnVtRmVhdHVyZXMsIHRpbGUubnVtUG9pbnRzLCB0aWxlLm51bVNpbXBsaWZpZWQpO1xuICAgICAgICAgICAgICAgICAgICBjb25zb2xlLnRpbWVFbmQoJ2NyZWF0aW9uJyk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIHZhciBrZXkgPSAneicgKyB6O1xuICAgICAgICAgICAgICAgIHRoaXMuc3Rhd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y5sZW5ndGg7XG4gICAgICAgICAgICAgICAgY29udGludWU7XG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIHZhciBzaW1wbGlmaWVkUmluZyA9IFtdO1xuXG4gICAgICAgICAgICBmb3IgKGogPSAwOyBqIDwgcmluZy5sZW5ndGg7IGorKykge1xuICAgICAgICAgICAgICAgIHAgPSByaW5nW2pdO1xuICAgICAgICAgICAgICAgIC8vIGtlZXAgcG9pbnRzIHdpdGggaW1wb3J0YW5jZSA+IHRvbGVyYW5jZVxuICAgICAgICAgICAgICAgIGlmIChub1NpbXBsaWZ5IHx8IHBbMl0gPiBzcVRvbGVyYW5jZSkge1xuICAgICAgICAgICAgICAgICAgICBzaW1wbGlmaWVkUmluZy5wdXNoKHApO1xuICAgICAgICAgICAgICAgICAgICB0aWxlLm51bVNpbXBsaWZpZWQrKztcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgdGlsZS5udW1Qb2ludHMrKztcbiAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgc2ltcGxpZmllZC5wdXNoKHNpbXBsaWZpZWRSaW5nKTtcbiAgICAgICAgfVxuICAgIH1cblxuICAgIGlmIChzaW1wbGlmaWVkLmxlbmd0aCkge1xuICAgICAgICB0aWxlLmZlYXR1cmVzLnB1c2goe1xuICAgICAgICAgICAgZ2VvbWV0cnk6IHNpbXBsaWZpZWQsXG4gICAgICAgICAgICB0eXBlOiB0eXBlLFxuICAgICAgICAgICAgdGFnczogZmVhdHVyZS50YWdzIHx8IG51bGxcbiAgICAgICAgfSk7XG4gICAgfVxufVxuIl19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment