Merge pull request #30266 from chrfrasco/master

@types/kdbush - support more array constructors
This commit is contained in:
Armando Aguirre
2018-11-05 12:37:28 -08:00
committed by GitHub
2 changed files with 28 additions and 6 deletions
+15 -2
View File
@@ -1,14 +1,27 @@
// Type definitions for kdbush 1.0
// Project: https://github.com/mourner/kdbush
// Definitions by: DenisCarriere <https://github.com/DenisCarriere>
// Christian Scott <https://github.com/chrfrasco>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type Points = number[][];
type Get<T> = (point: T) => number;
type ArrayType = typeof Int32Array | typeof Array;
type ArrayType =
typeof Int8Array
| typeof Int16Array
| typeof Int32Array
| typeof Float32Array
| typeof Float64Array
| typeof Array;
declare function kdbush(points: Points): kdbush.KDBush<Points>;
declare function kdbush<T>(points: T[], getX: Get<T>, getY: Get<T>, nodeSize?: number, ArrayType?: ArrayType): kdbush.KDBush<T>;
declare function kdbush<T>(
points: T[],
getX: Get<T>,
getY: Get<T>,
nodeSize?: number,
ArrayType?: ArrayType
): kdbush.KDBush<T>;
declare namespace kdbush {
class KDBush<T> {
ids: number[];
+13 -4
View File
@@ -1,10 +1,19 @@
import * as kdbush from 'kdbush';
import { KDBush } from 'kdbush';
import * as kdbush from "kdbush";
import { KDBush } from "kdbush";
// API
const points = [[110, 60], [130, 40]];
const index = kdbush(points);
// Specify array constructor
const pick = (index: number) => (point: number[]) => point[index];
kdbush(points, pick(0), pick(1), 64, Int8Array);
kdbush(points, pick(0), pick(1), 64, Int16Array);
kdbush(points, pick(0), pick(1), 64, Int32Array);
kdbush(points, pick(0), pick(1), 64, Float32Array);
kdbush(points, pick(0), pick(1), 64, Float64Array);
kdbush(points, pick(0), pick(1), 64, Array);
// properties
index.nodeSize;
index.points;
@@ -20,8 +29,8 @@ index.within(10, 10, 5);
index.within(10, 10, 5).map(id => points[id]);
// custom points (object)
const xy = [{x: 110, y: 60}, {x: 130, y: 40}];
const index2: KDBush<{x: number, y: number}> = kdbush(xy, p => p.x, p => p.y);
const xy = [{ x: 110, y: 60 }, { x: 130, y: 40 }];
const index2: KDBush<{ x: number; y: number }> = kdbush(xy, p => p.x, p => p.y);
index2.points[0].x;
index2.points[0].y;
index2.points.map(p => [p.x, p.y]);