From ed8bf676775cae9e7b54f8ed848919491f0c5371 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 13 Jun 2018 15:29:03 -0400 Subject: [PATCH 1/2] Update turf-kinks to typescript --- packages/turf-kinks/.gitignore | 0 packages/turf-kinks/bench.js | 10 +-- packages/turf-kinks/index.d.ts | 36 ++++++---- packages/turf-kinks/{index.js => index.ts} | 83 ++++++++++++++-------- packages/turf-kinks/package.json | 26 +++---- packages/turf-kinks/test.js | 14 ++-- packages/turf-kinks/tsconfig.json | 17 +++++ packages/turf-kinks/tslint.json | 12 ++++ 8 files changed, 126 insertions(+), 72 deletions(-) create mode 100644 packages/turf-kinks/.gitignore rename packages/turf-kinks/{index.js => index.ts} (65%) create mode 100644 packages/turf-kinks/tsconfig.json create mode 100644 packages/turf-kinks/tslint.json diff --git a/packages/turf-kinks/.gitignore b/packages/turf-kinks/.gitignore new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/turf-kinks/bench.js b/packages/turf-kinks/bench.js index 9bf4142ee4..e10f16c577 100644 --- a/packages/turf-kinks/bench.js +++ b/packages/turf-kinks/bench.js @@ -1,8 +1,8 @@ -import fs from 'fs'; -import path from 'path'; -import load from 'load-json-file'; -import Benchmark from 'benchmark'; -import kinks from './'; +const fs = require('fs'); +const path = require('path'); +const load = require('load-json-file'); +const Benchmark = require('benchmark'); +const kinks = require('./').default; const suite = new Benchmark.Suite('turf-kinks'); diff --git a/packages/turf-kinks/index.d.ts b/packages/turf-kinks/index.d.ts index 69051386ac..f9dfe266f0 100644 --- a/packages/turf-kinks/index.d.ts +++ b/packages/turf-kinks/index.d.ts @@ -1,16 +1,24 @@ -import { - LineString, - MultiLineString, - Polygon, - MultiPolygon, - Point, - FeatureCollection, - Feature -} from '@turf/helpers' - +import { Feature, FeatureCollection, LineString, MultiLineString, MultiPolygon, Point, Polygon } from "@turf/helpers"; /** - * http://turfjs.org/docs/#kinks + * Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring}, + * {@link MultiPolygon|multi-polygon} or {@link Polygon|polygon} and + * returns {@link Point|points} at all self-intersections. + * + * @name kinks + * @param {Feature} featureIn input feature + * @returns {FeatureCollection} self-intersections + * @example + * var poly = turf.polygon([[ + * [-12.034835, 8.901183], + * [-12.060413, 8.899826], + * [-12.03638, 8.873199], + * [-12.059383, 8.871418], + * [-12.034835, 8.901183] + * ]]); + * + * var kinks = turf.kinks(poly); + * + * //addToMap + * var addToMap = [poly, kinks] */ -export default function kinks( - featureIn: Feature | T -): FeatureCollection; +export default function kinks(featureIn: Feature | T): FeatureCollection; diff --git a/packages/turf-kinks/index.js b/packages/turf-kinks/index.ts similarity index 65% rename from packages/turf-kinks/index.js rename to packages/turf-kinks/index.ts index aaaca20112..dfe1f8954e 100644 --- a/packages/turf-kinks/index.js +++ b/packages/turf-kinks/index.ts @@ -1,7 +1,18 @@ -import { point } from '@turf/helpers'; +import { point } from "@turf/helpers"; +import { + Feature, + FeatureCollection, + LineString, + MultiLineString, + MultiPolygon, + Point, + Polygon, +} from "@turf/helpers"; /** - * Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring}, {@link MultiPolygon|multi-polygon}, or {@link Polygon|polygon} and returns {@link Point|points} at all self-intersections. + * Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring}, + * {@link MultiPolygon|multi-polygon} or {@link Polygon|polygon} and + * returns {@link Point|points} at all self-intersections. * * @name kinks * @param {Feature} featureIn input feature @@ -20,35 +31,38 @@ import { point } from '@turf/helpers'; * //addToMap * var addToMap = [poly, kinks] */ -function kinks(featureIn) { - var coordinates; - var feature; - var results = { - type: 'FeatureCollection', - features: [] +export default function kinks( + featureIn: Feature | T, +): FeatureCollection { + let coordinates: any; + let feature: any; + const results: FeatureCollection = { + type: "FeatureCollection", + features: [], }; - if (featureIn.type === 'Feature') { + if (featureIn.type === "Feature") { feature = featureIn.geometry; } else { feature = featureIn; } - if (feature.type === 'LineString') { + if (feature.type === "LineString") { coordinates = [feature.coordinates]; - } else if (feature.type === 'MultiLineString') { + } else if (feature.type === "MultiLineString") { coordinates = feature.coordinates; - } else if (feature.type === 'MultiPolygon') { + } else if (feature.type === "MultiPolygon") { coordinates = [].concat.apply([], feature.coordinates); - } else if (feature.type === 'Polygon') { + } else if (feature.type === "Polygon") { coordinates = feature.coordinates; } else { - throw new Error('Input must be a LineString, MultiLineString, ' + - 'Polygon, or MultiPolygon Feature or Geometry'); + throw new Error("Input must be a LineString, MultiLineString, " + + "Polygon, or MultiPolygon Feature or Geometry"); } - coordinates.forEach(function (line1) { - coordinates.forEach(function (line2) { - for (var i = 0; i < line1.length - 1; i++) { - // start iteration at i, intersections for k < i have already been checked in previous outer loop iterations - for (var k = i; k < line2.length - 1; k++) { + coordinates.forEach((line1: any) => { + coordinates.forEach((line2: any) => { + for (let i = 0; i < line1.length - 1; i++) { + // start iteration at i, intersections for k < i have already + // been checked in previous outer loop iterations + for (let k = i; k < line2.length - 1; k++) { if (line1 === line2) { // segments are adjacent and always share a vertex, not a kink if (Math.abs(i - k) === 1) { @@ -67,7 +81,7 @@ function kinks(featureIn) { } } - var intersection = lineIntersects(line1[i][0], line1[i][1], line1[i + 1][0], line1[i + 1][1], + const intersection: any = lineIntersects(line1[i][0], line1[i][1], line1[i + 1][0], line1[i + 1][1], line2[k][0], line2[k][1], line2[k + 1][0], line2[k + 1][1]); if (intersection) { results.features.push(point([intersection[0], intersection[1]])); @@ -79,16 +93,29 @@ function kinks(featureIn) { return results; } - // modified from http://jsfiddle.net/justin_c_rounds/Gd2S2/light/ -function lineIntersects(line1StartX, line1StartY, line1EndX, line1EndY, line2StartX, line2StartY, line2EndX, line2EndY) { - // if the lines intersect, the result contains the x and y of the intersection (treating the lines as infinite) and booleans for whether line segment 1 or line segment 2 contain the point - var denominator, a, b, numerator1, numerator2, - result = { +function lineIntersects( + line1StartX: any, + line1StartY: any, + line1EndX: any, + line1EndY: any, + line2StartX: any, + line2StartY: any, + line2EndX: any, + line2EndY: any) { + // if the lines intersect, the result contains the x and y of the + // intersection (treating the lines as infinite) and booleans for whether + // line segment 1 or line segment 2 contain the point + let denominator; + let a; + let b; + let numerator1; + let numerator2; + const result = { x: null, y: null, onLine1: false, - onLine2: false + onLine2: false, }; denominator = ((line2EndY - line2StartY) * (line1EndX - line1StartX)) - ((line2EndX - line2StartX) * (line1EndY - line1StartY)); if (denominator === 0) { @@ -124,5 +151,3 @@ function lineIntersects(line1StartX, line1StartY, line1EndX, line1EndY, line2Sta return false; } } - -export default kinks; diff --git a/packages/turf-kinks/package.json b/packages/turf-kinks/package.json index 2a327c65c6..745b23135a 100644 --- a/packages/turf-kinks/package.json +++ b/packages/turf-kinks/package.json @@ -1,22 +1,19 @@ { "name": "@turf/kinks", - "version": "5.1.5", + "version": "6.0.0", "description": "turf kinks module", - "main": "main.js", - "module": "main.es.js", + "main": "index", "types": "index.d.ts", "files": [ "index.js", - "index.d.ts", - "main.js", - "main.es.js" + "index.d.ts" ], "scripts": { - "pretest": "rollup -c ../../rollup.config.js", - "test": "node -r @std/esm test.js", - "posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js", - "bench": "node -r @std/esm bench.js", + "prepare": "tsc", + "pretest": "tsc", + "test": "node test.js", + "bench": "node bench.js", "docs": "node ../../scripts/generate-readmes" }, "repository": { @@ -35,19 +32,14 @@ }, "homepage": "https://github.com/Turfjs/turf", "devDependencies": { - "@std/esm": "*", - "@turf/meta": "6.x", + "@turf/meta": "*", "benchmark": "*", "load-json-file": "*", - "rollup": "*", + "typescript": "*", "tape": "*", "write-json-file": "*" }, "dependencies": { "@turf/helpers": "6.x" - }, - "@std/esm": { - "esm": "js", - "cjs": true } } diff --git a/packages/turf-kinks/test.js b/packages/turf-kinks/test.js index 6c40ba08cf..4a61f0986d 100644 --- a/packages/turf-kinks/test.js +++ b/packages/turf-kinks/test.js @@ -1,10 +1,10 @@ -import test from 'tape'; -import fs from 'fs'; -import path from 'path'; -import load from 'load-json-file'; -import write from 'write-json-file'; -import { featureEach } from '@turf/meta'; -import kinks from '.'; +const test = require('tape'); +const fs = require('fs'); +const path = require('path'); +const load = require('load-json-file'); +const write = require('write-json-file'); +const { featureEach } = require('@turf/meta'); +const kinks = require('.').default; const directories = { in: path.join(__dirname, 'test', 'in') + path.sep, diff --git a/packages/turf-kinks/tsconfig.json b/packages/turf-kinks/tsconfig.json new file mode 100644 index 0000000000..91cffcbf55 --- /dev/null +++ b/packages/turf-kinks/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + + /* Module Resolution Options */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + }, + "files": [ + "index.ts" + ] +} \ No newline at end of file diff --git a/packages/turf-kinks/tslint.json b/packages/turf-kinks/tslint.json new file mode 100644 index 0000000000..a54537158a --- /dev/null +++ b/packages/turf-kinks/tslint.json @@ -0,0 +1,12 @@ +{ + "defaultSeverity": "error", + "extends": [ + "tslint:recommended" + ], + "jsRules": {}, + "rules": { + "object-literal-sort-keys": false, + "max-line-length": false + }, + "rulesDirectory": [] +} \ No newline at end of file From 1063a4b983023abfa597dfad5b550e5f3fec8e84 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 13 Jun 2018 15:29:31 -0400 Subject: [PATCH 2/2] add gitignore --- packages/turf-bbox/tsconfig.json | 5 ++++- packages/turf-kinks/.gitignore | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/turf-bbox/tsconfig.json b/packages/turf-bbox/tsconfig.json index 230fe2dd4f..91cffcbf55 100644 --- a/packages/turf-bbox/tsconfig.json +++ b/packages/turf-bbox/tsconfig.json @@ -10,5 +10,8 @@ /* Module Resolution Options */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ - } + }, + "files": [ + "index.ts" + ] } \ No newline at end of file diff --git a/packages/turf-kinks/.gitignore b/packages/turf-kinks/.gitignore index e69de29bb2..945ce43a90 100644 --- a/packages/turf-kinks/.gitignore +++ b/packages/turf-kinks/.gitignore @@ -0,0 +1 @@ +index.js \ No newline at end of file