Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework @turf/random & @turf/turf export modules #995

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/turf-difference/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/// <reference types="geojson" />

import {Polygon, MultiPolygon, Feature} from '@turf/meta'

export type Input = Feature<Polygon|MultiPolygon> | Polygon | MultiPolygon;
export type Output = Feature<Polygon|MultiPolygon> | null;
import { Polygon, MultiPolygon, Feature } from '@turf/helpers'

/**
* http://turfjs.org/docs/#difference
*/
export default function difference(polygon1: Input, polygon2: Input): Output;
export default function difference(
polygon1: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
polygon2: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon
): Feature<Polygon | MultiPolygon> | null;

2 changes: 2 additions & 0 deletions packages/turf-helpers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type BBox = [number, number, number, number];
export type Units = 'miles' | 'nauticalmiles' | 'degrees' | 'radians' | 'inches' | 'yards' | 'meters' | 'metres' | 'kilometers' | 'kilometres';
export type Geometry = 'Point' | 'LineString' | 'Polygon' | 'MultiPoint' | 'MultiLineString' | 'MultiPolygon';
export type Grid = 'point' | 'square' | 'hex' | 'triangle';
export type Types = Geometry | 'Feature' | 'FeatureCollection'

// GeoJSON Geometry Types
export type Position = GeoJSON.Position;
Expand All @@ -31,6 +32,7 @@ export interface ExtendedFeatureCollection<Feat extends Feature<any>> {
type: 'FeatureCollection';
features: Feat[];
}
export type AllGeoJSON = Feature<any> | FeatureCollection<any> | FeatureGeometryCollection | GeometryObject | GeometryCollection;

/**
* http://turfjs.org/docs/#feature
Expand Down
19 changes: 10 additions & 9 deletions packages/turf-invariant/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// <reference types="geojson" />

export type GeometryObject = GeoJSON.GeometryObject;
export type GeometryCollection = GeoJSON.GeometryCollection;
export type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>;
export type FeatureCollection<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>;
export type StringGeomTypes = 'Point' | 'LineString' | 'Polygon' | 'MultiPoint' | 'MultiLineString' | 'MultiPolygon' | 'GeometryCollection'
export type StringTypes = StringGeomTypes | 'Feature' | 'FeatureCollection'
import {
GeometryObject,
GeometryCollection,
Feature,
FeatureCollection,
Geometry,
Types
} from '@turf/helpers'

/**
* http://turfjs.org/docs/#getcoords
Expand Down Expand Up @@ -45,4 +45,5 @@ export function getGeom(geojson: GeometryCollection | GeometryObject | Feature<a
/**
* http://turfjs.org/docs/#gettype
*/
export function getType(geojson: GeometryCollection | GeometryObject | Feature<any> | FeatureCollection<any>): StringTypes;
export function getType(geojson: GeometryObject | Feature<any>): Geometry;
export function getType(geojson: GeometryCollection | GeometryObject | Feature<any> | FeatureCollection<any>): Types;
10 changes: 4 additions & 6 deletions packages/turf-invariant/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isNumber } from '@turf/helpers';

/**
* Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
*
Expand All @@ -16,9 +18,7 @@ export function getCoord(obj) {
var coordinates = getCoords(obj);

// getCoord() must contain at least two numbers (Point)
if (coordinates.length > 1 &&
typeof coordinates[0] === 'number' &&
typeof coordinates[1] === 'number') {
if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
return coordinates;
} else {
throw new Error('Coordinate is not a valid Point');
Expand Down Expand Up @@ -69,9 +69,7 @@ export function getCoords(obj) {
* @returns {boolean} true if Array contains a number
*/
export function containsNumber(coordinates) {
if (coordinates.length > 1 &&
typeof coordinates[0] === 'number' &&
typeof coordinates[1] === 'number') {
if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/turf-invariant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "*",
"benchmark": "*",
"rollup": "*",
"tape": "*",
"@std/esm": "*",
"uglify-js": "*"
},
"dependencies": {},
"dependencies": {
"@turf/helpers": "^5.0.0"
},
"@std/esm": {
"esm": "js",
"cjs": true
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-invariant/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ test('invariant -- containsNumber', t => {
t.equals(invariant.containsNumber([[1, 1], [1, 1]]), true);
t.equals(invariant.containsNumber([[[1, 1], [1, 1]], [1, 1]]), true);

//# Ensure recusive call handles Max callstack exceeded
//# Ensure recursive call handles Max callstack exceeded
t.throws(() => {
invariant.containsNumber(['1', 1]);
invariant.containsNumber(['foo', 1]);
}, /coordinates must only contain numbers/, 'Must only contain numbers');
t.end();
});
Expand Down
12 changes: 7 additions & 5 deletions packages/turf-invariant/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
lineString,
polygon,
geometryCollection,
featureCollection} from '@turf/helpers'
featureCollection,
Geometry,
Types
} from '@turf/helpers'
import {
getCoord,
getCoords,
Expand All @@ -13,9 +16,8 @@ import {
collectionOf,
containsNumber,
getGeom,
getType,
StringGeomTypes,
StringTypes} from './'
getType
} from './'

/**
* Fixtures
Expand All @@ -40,7 +42,7 @@ invariant.getGeom(pt.geometry)
/**
* invariant.getType
*/
const type: StringTypes = invariant.getType(pt)
const type: Geometry = invariant.getType(pt)
getType(gc)
invariant.getType(gc)
invariant.getType(line)
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-isobands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import fs from 'fs';
import load from 'load-json-file';
import write from 'write-json-file';
import random from '@turf/random';
import { randomPolygon } from '@turf/random';
import envelope from '@turf/envelope';
import { lineString } from '@turf/helpers';
import { getCoords } from '@turf/invariant';
Expand Down Expand Up @@ -68,7 +68,7 @@ test('isobands', t => {
test('isobands -- throws', t => {
const points = pointGrid([-70.823364, -33.553984, -70.473175, -33.302986], 5);

t.throws(() => isobands(random('polygon'), [1, 2, 3]), 'invalid points');
t.throws(() => isobands(randomPolygon(), [1, 2, 3]), 'invalid points');
t.throws(() => isobands(points, ''), 'invalid breaks');
t.throws(() => isobands(points, [1, 2, 3], {zProperty: 'temp', isobandProperties: 'hello' }), 'invalid options');

Expand Down
6 changes: 3 additions & 3 deletions packages/turf-isolines/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import test from 'tape';
import path from 'path';
import load from 'load-json-file';
import write from 'write-json-file';
import random from '@turf/random';
import envelope from '@turf/envelope';
import pointGrid from '@turf/point-grid';
import { randomPolygon } from '@turf/random';
import { getCoords } from '@turf/invariant';
import matrixToGrid from 'matrix-to-grid';
import { lineString } from '@turf/helpers';
import { lineString, polygon } from '@turf/helpers';
import isolines from '.';

const directories = {
Expand Down Expand Up @@ -58,7 +58,7 @@ test('isolines', t => {
test('isolines -- throws', t => {
const points = pointGrid([-70.823364, -33.553984, -70.473175, -33.302986], 5);

t.throws(() => isolines(random('polygon'), [1, 2, 3]), 'invalid points');
t.throws(() => isolines(randomPolygon()), 'invalid points');
t.throws(() => isolines(points), /breaks is required/);
t.throws(() => isolines(points, 'string'), /breaks must be an Array/);
t.throws(() => isolines(points, [1, 2, 3], 5), /zProperty must be a string/);
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-isolines/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random from '@turf/random'
import { randomPoint } from '@turf/random'
import isolines from './'

const points = random('point', 100, {
const points = randomPoint(100, {
bbox: [0, 30, 20, 50]
})
for (let i = 0; i < points.features.length; i++) {
Expand Down
45 changes: 19 additions & 26 deletions packages/turf-meta/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
/// <reference types="geojson" />

export type Point = GeoJSON.Point;
export type LineString = GeoJSON.LineString;
export type Polygon = GeoJSON.Polygon;
export type MultiPoint = GeoJSON.MultiPoint;
export type MultiLineString = GeoJSON.MultiLineString;
export type MultiPolygon = GeoJSON.MultiPolygon;
export type FeatureCollection<Geom extends GeometryObject> = GeoJSON.FeatureCollection<Geom>;
export type Feature<Geom extends GeometryObject> = GeoJSON.Feature<Geom>;
export type GeometryObject = GeoJSON.GeometryObject;
export type GeometryCollection = GeoJSON.GeometryCollection;
export type Geoms = Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon;
export type Lines = LineString | Polygon | MultiLineString | MultiPolygon;
export type AllGeoJSON = Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection;
export interface FeatureGeometryCollection extends GeoJSON.Feature<any> {
geometry: GeometryCollection;
}
export interface ExtendedFeatureCollection<Feat extends Feature<any>> {
type: 'FeatureCollection';
features: Feat[];
}
import {
Point,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
FeatureCollection,
Feature,
GeometryObject,
GeometryCollection,
AllGeoJSON,
FeatureGeometryCollection,
ExtendedFeatureCollection
} from '@turf/helpers';

/**
* http://turfjs.org/docs/#coordreduce
Expand Down Expand Up @@ -140,16 +133,16 @@ export function segmentEach(
/**
* http://turfjs.org/docs/#linereduce
*/
export function lineReduce<Reducer extends any>(
geojson: Feature<Lines> | Lines,
export function lineReduce<Reducer extends any, Geom extends LineString | MultiLineString | Polygon | MultiPolygon>(
geojson: Feature<Geom> | Geom,
callback: (previousValue?: Reducer, currentLine?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number) => Reducer,
initialValue?: Reducer
): Reducer;

/**
* http://turfjs.org/docs/#lineeach
*/
export function lineEach(
geojson: Feature<Lines> | Lines,
export function lineEach<Geom extends LineString | MultiLineString | Polygon | MultiPolygon>(
geojson: Feature<Geom> | Geom,
callback: (currentLine?: Feature<LineString>, featureIndex?: number, featureSubIndex?: number) => void
): void;
5 changes: 3 additions & 2 deletions packages/turf-meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@turf/helpers": "*",
"@turf/random": "*",
"benchmark": "*",
"rollup": "*",
"tape": "*",
"@std/esm": "*",
"uglify-js": "*"
},
"dependencies": {},
"dependencies": {
"@turf/helpers": "^5.0.0"
},
"@std/esm": {
"esm": "js",
"cjs": true
Expand Down
6 changes: 2 additions & 4 deletions packages/turf-projection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import {
GeometryCollection
} from '@turf/helpers';

type Types = Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection

/**
* http://turfjs.org/docs/#toMercator
*/
export function toMercator<T extends Types>(
export function toMercator<T extends Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection>(
geojson: T,
options?: {
mutate?: boolean
Expand All @@ -20,7 +18,7 @@ export function toMercator<T extends Types>(
/**
* http://turfjs.org/docs/#toWgs84
*/
export function toWgs84<T extends Types>(
export function toWgs84<T extends Feature<any> | FeatureCollection<any> | GeometryObject | GeometryCollection>(
geojson: T,
options?: {
mutate?: boolean
Expand Down
50 changes: 35 additions & 15 deletions packages/turf-random/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
/// <reference types="geojson" />
import { BBox, FeatureCollection, Feature, Point, LineString, Polygon, Position } from '@turf/helpers';

export type Points = GeoJSON.FeatureCollection<GeoJSON.Point>;
export type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>;
export type Features = GeoJSON.FeatureCollection<any>;
export type BBox = Array<number>;

export interface Options {
bbox?: BBox
num_vertices?: number
max_radial_length?: number
}
/**
* http://turfjs.org/docs/#randomposition
*/
export function randomPosition(bbox?: BBox | {bbox?: BBox}): Position

/**
* http://turfjs.org/docs/#random
* http://turfjs.org/docs/#randompoint
*/
declare function random(type?: 'point' | 'points', count?: number, options?: Options): Points;
declare function random(type?: 'polygon' | 'polygons', count?: number, options?: Options): Polygons;
export function randomPoint(
count?: number,
options?: {
bbox?: BBox
}
): FeatureCollection<Point>

export default random;
/**
* http://turfjs.org/docs/#randomlinestring
*/
export function randomLineString(
count?: number,
options?: {
bbox?: BBox,
num_vertices?: number,
max_length?: number,
max_rotation?: number
}
): FeatureCollection<LineString>

/**
* http://turfjs.org/docs/#randompolygon
*/
export function randomPolygon(
count?: number,
options?: {
bbox?: BBox,
num_vertices?: number,
max_radial_length?: number
}
): FeatureCollection<LineString>
Loading