Skip to content

Commit

Permalink
Merge pull request Turfjs#1417 from Turfjs/update-kinks-ts
Browse files Browse the repository at this point in the history
Update kinks to Typescript
  • Loading branch information
DenisCarriere authored Jun 13, 2018
2 parents bc3ae8e + 1063a4b commit b069f15
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 73 deletions.
5 changes: 4 additions & 1 deletion packages/turf-bbox/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
1 change: 1 addition & 0 deletions packages/turf-kinks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.js
10 changes: 5 additions & 5 deletions packages/turf-kinks/bench.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
36 changes: 22 additions & 14 deletions packages/turf-kinks/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<LineString|MultiLineString|MultiPolygon|Polygon>} featureIn input feature
* @returns {FeatureCollection<Point>} 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<T extends LineString | MultiLineString | Polygon | MultiPolygon>(
featureIn: Feature<T> | T
): FeatureCollection<Point>;
export default function kinks<T extends LineString | MultiLineString | Polygon | MultiPolygon>(featureIn: Feature<T> | T): FeatureCollection<Point>;
83 changes: 54 additions & 29 deletions packages/turf-kinks/index.js → packages/turf-kinks/index.ts
Original file line number Diff line number Diff line change
@@ -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<LineString|MultiLineString|MultiPolygon|Polygon>} featureIn input feature
Expand All @@ -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<T extends LineString | MultiLineString | Polygon | MultiPolygon>(
featureIn: Feature<T> | T,
): FeatureCollection<Point> {
let coordinates: any;
let feature: any;
const results: FeatureCollection<Point> = {
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) {
Expand All @@ -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]]));
Expand All @@ -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) {
Expand Down Expand Up @@ -124,5 +151,3 @@ function lineIntersects(line1StartX, line1StartY, line1EndX, line1EndY, line2Sta
return false;
}
}

export default kinks;
26 changes: 9 additions & 17 deletions packages/turf-kinks/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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
}
}
14 changes: 7 additions & 7 deletions packages/turf-kinks/test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 17 additions & 0 deletions packages/turf-kinks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
12 changes: 12 additions & 0 deletions packages/turf-kinks/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"object-literal-sort-keys": false,
"max-line-length": false
},
"rulesDirectory": []
}

0 comments on commit b069f15

Please sign in to comment.