UNPKG

@cloudbase/js-sdk

Version:
84 lines (83 loc) 3.03 kB
var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import { SYMBOL_GEO_LINE_STRING } from '../helper/symbol'; import { Point } from './point'; import { isArray, isNumber } from '../utils/type'; var LineString = (function () { function LineString(points) { if (!isArray(points)) { throw new TypeError("\"points\" must be of type Point[]. Received type ".concat(typeof points)); } if (points.length < 2) { throw new Error('"points" must contain 2 points at least'); } points.forEach(function (point) { if (!(point instanceof Point)) { throw new TypeError("\"points\" must be of type Point[]. Received type ".concat(typeof point, "[]")); } }); this.points = points; } LineString.prototype.parse = function (key) { var _a; return _a = {}, _a[key] = { type: 'LineString', coordinates: this.points.map(function (point) { return point.toJSON().coordinates; }) }, _a; }; LineString.prototype.toJSON = function () { return { type: 'LineString', coordinates: this.points.map(function (point) { return point.toJSON().coordinates; }) }; }; LineString.validate = function (lineString) { var e_1, _a; if (lineString.type !== 'LineString' || !isArray(lineString.coordinates)) { return false; } try { for (var _b = __values(lineString.coordinates), _c = _b.next(); !_c.done; _c = _b.next()) { var point = _c.value; if (!isNumber(point[0]) || !isNumber(point[1])) { return false; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return true; }; LineString.isClosed = function (lineString) { var firstPoint = lineString.points[0]; var lastPoint = lineString.points[lineString.points.length - 1]; if (firstPoint.latitude === lastPoint.latitude && firstPoint.longitude === lastPoint.longitude) { return true; } }; Object.defineProperty(LineString.prototype, "_internalType", { get: function () { return SYMBOL_GEO_LINE_STRING; }, enumerable: false, configurable: true }); return LineString; }()); export { LineString };