Skip to content

Commit

Permalink
change printWidth to 80 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
smeijer committed Apr 14, 2020
1 parent ec0ca1e commit 5223c43
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'off',
curly: ['error', 'all'],
},
settings: {
react: {
version: 'detect',
},
},
};
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
printWidth: 80,
tabWidth: 2,
};
9 changes: 8 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ export const ARROW_UP_KEY = 38;
export const ARROW_LEFT_KEY = 37;
export const ARROW_RIGHT_KEY = 39;

export const SPECIAL_KEYS = [ENTER_KEY, ESCAPE_KEY, ARROW_DOWN_KEY, ARROW_UP_KEY, ARROW_LEFT_KEY, ARROW_RIGHT_KEY];
export const SPECIAL_KEYS = [
ENTER_KEY,
ESCAPE_KEY,
ARROW_DOWN_KEY,
ARROW_UP_KEY,
ARROW_LEFT_KEY,
ARROW_RIGHT_KEY,
];
28 changes: 22 additions & 6 deletions src/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export function createElement<T extends HTMLElement = HTMLElement>(
Object.keys(attributes).forEach((key) => {
if (typeof attributes[key] === 'function') {
// IE doesn't support startsWith
const type = (key.indexOf('on') === 0 ? key.substr(2).toLowerCase() : key) as keyof HTMLElementEventMap;
const type = (key.indexOf('on') === 0
? key.substr(2).toLowerCase()
: key) as keyof HTMLElementEventMap;
el.addEventListener(type, attributes[key] as () => void);
} else {
el.setAttribute(key, attributes[key] as string);
Expand All @@ -32,7 +34,10 @@ export function stopPropagation(event: Event) {
event.stopPropagation();
}

export function createScriptElement<T = object>(url: string, cb: string): Promise<T> {
export function createScriptElement<T = object>(
url: string,
cb: string,
): Promise<T> {
const script = createElement('script', null, document.body);
script.setAttribute('type', 'text/javascript');

Expand All @@ -47,9 +52,13 @@ export function createScriptElement<T = object>(url: string, cb: string): Promis
});
}

export const cx = (...classNames: (string | undefined)[]): string => classNames.filter(Boolean).join(' ').trim();
export const cx = (...classNames: (string | undefined)[]): string =>
classNames.filter(Boolean).join(' ').trim();

export function addClassName(element: Element, className: string | string[]): void {
export function addClassName(
element: Element,
className: string | string[],
): void {
if (!element || !element.classList) {
return;
}
Expand All @@ -63,7 +72,10 @@ export function addClassName(element: Element, className: string | string[]): vo
});
}

export function removeClassName(element: Element, className: string | string[]): void {
export function removeClassName(
element: Element,
className: string | string[],
): void {
if (!element || !element.classList) {
return;
}
Expand All @@ -77,7 +89,11 @@ export function removeClassName(element: Element, className: string | string[]):
});
}

export function replaceClassName(element: Element, find: string, replace: string): void {
export function replaceClassName(
element: Element,
find: string,
replace: string,
): void {
removeClassName(element, find);
addClassName(element, replace);
}
54 changes: 44 additions & 10 deletions src/leafletControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import ResultList from './resultList';
import debounce from './lib/debounce';

import { createElement, addClassName, removeClassName } from './domUtils';
import { ENTER_KEY, SPECIAL_KEYS, ARROW_UP_KEY, ARROW_DOWN_KEY, ESCAPE_KEY } from './constants';
import {
ENTER_KEY,
SPECIAL_KEYS,
ARROW_UP_KEY,
ARROW_DOWN_KEY,
ESCAPE_KEY,
} from './constants';

const defaultOptions = () => ({
position: 'topleft',
Expand Down Expand Up @@ -37,7 +43,14 @@ const defaultOptions = () => ({
});

const wasHandlerEnabled = {};
const mapHandlers = ['dragging', 'touchZoom', 'doubleClickZoom', 'scrollWheelZoom', 'boxZoom', 'keyboard'];
const mapHandlers = [
'dragging',
'touchZoom',
'doubleClickZoom',
'scrollWheelZoom',
'boxZoom',
'keyboard',
];

const Control = {
initialize(options) {
Expand All @@ -49,7 +62,13 @@ const Control = {
...options,
};

const { style, classNames, searchLabel, autoComplete, autoCompleteDelay } = this.options;
const {
style,
classNames,
searchLabel,
autoComplete,
autoCompleteDelay,
} = this.options;
if (style !== 'button') {
this.options.classNames.container += ` ${options.style}`;
}
Expand Down Expand Up @@ -100,7 +119,11 @@ const Control = {
true,
);
input.addEventListener('keydown', (e) => this.selectResult(e), true);
input.addEventListener('keydown', (e) => this.clearResults(e, true), true);
input.addEventListener(
'keydown',
(e) => this.clearResults(e, true),
true,
);
}

form.addEventListener('mouseenter', (e) => this.disableHandlers(e), true);
Expand All @@ -120,7 +143,9 @@ const Control = {

if (style === 'bar') {
const { form } = this.searchElement.elements;
const root = map.getContainer().querySelector('.leaflet-control-container');
const root = map
.getContainer()
.querySelector('.leaflet-control-container');

const container = createElement('div', 'leaflet-control-geosearch bar');
container.appendChild(form);
Expand Down Expand Up @@ -186,7 +211,9 @@ const Control = {
},

selectResult(event) {
if ([ENTER_KEY, ARROW_DOWN_KEY, ARROW_UP_KEY].indexOf(event.keyCode) === -1) {
if (
[ENTER_KEY, ARROW_DOWN_KEY, ARROW_UP_KEY].indexOf(event.keyCode) === -1
) {
return;
}

Expand All @@ -208,7 +235,8 @@ const Control = {
}

// eslint-disable-next-line no-bitwise
const next = event.code === 'ArrowDown' ? ~~list.selected + 1 : ~~list.selected - 1;
const next =
event.code === 'ArrowDown' ? ~~list.selected + 1 : ~~list.selected - 1;
// eslint-disable-next-line no-nested-ternary
const idx = next < 0 ? max : next > max ? 0 : next;

Expand Down Expand Up @@ -325,12 +353,16 @@ const Control = {
const { retainZoomLevel, animateZoom } = this.options;

const resultBounds = new L.LatLngBounds(result.bounds);
const bounds = resultBounds.isValid() ? resultBounds : this.markers.getBounds();
const bounds = resultBounds.isValid()
? resultBounds
: this.markers.getBounds();

if (!retainZoomLevel && resultBounds.isValid()) {
this.map.fitBounds(bounds, { animate: animateZoom });
} else {
this.map.setView(bounds.getCenter(), this.getZoom(), { animate: animateZoom });
this.map.setView(bounds.getCenter(), this.getZoom(), {
animate: animateZoom,
});
}
},

Expand All @@ -342,7 +374,9 @@ const Control = {

export default function LeafletControl(...options) {
if (!L || !L.Control || !L.Control.extend) {
throw new Error('Leaflet must be loaded before instantiating the GeoSearch control');
throw new Error(
'Leaflet must be loaded before instantiating the GeoSearch control',
);
}

const LControl = L.Control.extend(Control);
Expand Down
6 changes: 5 additions & 1 deletion src/lib/hasShape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default function hasShape(keys: string[], exact: boolean, object: object): boolean {
export default function hasShape(
keys: string[],
exact: boolean,
object: object,
): boolean {
if (exact && keys.length !== Object.keys(object).length) {
return false;
}
Expand Down
19 changes: 15 additions & 4 deletions src/providers/__tests__/bingProvider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ describe('BingProvider', () => {
const callbackName = `BING_JSONP_CB_${now}`;

beforeAll(() => {
fetch.mockResponse(async () => ({ body: `${callbackName}(${JSON.stringify(fixtures)})` }));
fetch.mockResponse(async () => ({
body: `${callbackName}(${JSON.stringify(fixtures)})`,
}));

// eslint-disable-next-line @typescript-eslint/no-var-requires
jest.spyOn(require('../../domUtils'), 'createScriptElement').mockImplementation(jest.fn(async () => fixtures));
const domUtils = require('../../domUtils');

jest
.spyOn(domUtils, 'createScriptElement')
.mockImplementation(jest.fn(async () => fixtures));
});

afterAll(() => {
Expand All @@ -43,8 +50,12 @@ describe('BingProvider', () => {
const result = results[0];

expect(result.label).toBeTruthy();
expect(result.x).toEqual(fixtures.resourceSets[0].resources[0].point.coordinates[1]);
expect(result.y).toEqual(fixtures.resourceSets[0].resources[0].point.coordinates[0]);
expect(result.x).toEqual(
fixtures.resourceSets[0].resources[0].point.coordinates[1],
);
expect(result.y).toEqual(
fixtures.resourceSets[0].resources[0].point.coordinates[0],
);
expect(result.bounds[0][0]).toBeGreaterThan(result.bounds[0][1]);
expect(result.bounds[1][0]).toBeGreaterThan(result.bounds[1][1]);
expect(result.bounds[0][0]).toBeLessThan(result.bounds[1][0]);
Expand Down
17 changes: 14 additions & 3 deletions src/providers/bingProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import AbstractProvider, { EndpointArgument, ParseArgument, SearchArgument, SearchResult } from './provider';
import AbstractProvider, {
EndpointArgument,
ParseArgument,
SearchArgument,
SearchResult,
} from './provider';
import { createScriptElement } from '../domUtils';

export interface RequestResult {
Expand Down Expand Up @@ -39,7 +44,10 @@ export interface RawResult {
matchCodes: string[];
}

export default class BingProvider extends AbstractProvider<RequestResult, RawResult> {
export default class BingProvider extends AbstractProvider<
RequestResult,
RawResult
> {
searchUrl = 'https://dev.virtualearth.net/REST/v1/Locations';

endpoint({ query, jsonp }: EndpointArgument & { jsonp: string }): string {
Expand Down Expand Up @@ -68,7 +76,10 @@ export default class BingProvider extends AbstractProvider<RequestResult, RawRes

async search({ query }: SearchArgument): Promise<SearchResult<RawResult>[]> {
const jsonp = `BING_JSONP_CB_${Date.now()}`;
const json = await createScriptElement<RequestResult>(this.endpoint({ query, jsonp }), jsonp);
const json = await createScriptElement<RequestResult>(
this.endpoint({ query, jsonp }),
jsonp,
);

return this.parse({ data: json });
}
Expand Down
14 changes: 11 additions & 3 deletions src/providers/esriProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import AbstractProvider, { EndpointArgument, ParseArgument, SearchResult } from './provider';
import AbstractProvider, {
EndpointArgument,
ParseArgument,
SearchResult,
} from './provider';

interface RequestResult {
spatialReference: { wkid: number; latestWkid: number };
Expand All @@ -19,8 +23,12 @@ interface RawResult {
};
}

export default class EsriProvider extends AbstractProvider<RequestResult, RawResult> {
searchUrl = 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find';
export default class EsriProvider extends AbstractProvider<
RequestResult,
RawResult
> {
searchUrl =
'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find';

endpoint({ query }: EndpointArgument): string {
const params = typeof query === 'string' ? { text: query } : query;
Expand Down
12 changes: 10 additions & 2 deletions src/providers/googleProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import AbstractProvider, { EndpointArgument, LatLng, ParseArgument, SearchResult } from './provider';
import AbstractProvider, {
EndpointArgument,
LatLng,
ParseArgument,
SearchResult,
} from './provider';

export interface RequestResult {
results: RawResult[];
Expand Down Expand Up @@ -28,7 +33,10 @@ export interface RawResult {
types: string[];
}

export default class GoogleProvider extends AbstractProvider<RequestResult, RawResult> {
export default class GoogleProvider extends AbstractProvider<
RequestResult,
RawResult
> {
searchUrl = 'https://maps.googleapis.com/maps/api/geocode/json';

endpoint({ query }: EndpointArgument) {
Expand Down
4 changes: 3 additions & 1 deletion src/providers/locationIQProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import OpenStreetMapProvider, { OpenStreetMapProviderOptions } from './openStreetMapProvider';
import OpenStreetMapProvider, {
OpenStreetMapProviderOptions,
} from './openStreetMapProvider';

export default class LocationIQProvider extends OpenStreetMapProvider {
constructor(options: OpenStreetMapProviderOptions) {
Expand Down
26 changes: 22 additions & 4 deletions src/providers/openCageProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import AbstractProvider, { EndpointArgument, LatLng, ParseArgument, SearchResult } from './provider';
import AbstractProvider, {
EndpointArgument,
LatLng,
ParseArgument,
SearchResult,
} from './provider';

export interface RequestResult {
results: RawResult[];
Expand Down Expand Up @@ -44,8 +49,18 @@ export interface RawResult {
speed_in: string;
};
sun: {
rise: { apparent: number; astronomical: number; civil: number; nautical: number };
set: { apparent: number; astronomical: number; civil: number; nautical: number };
rise: {
apparent: number;
astronomical: number;
civil: number;
nautical: number;
};
set: {
apparent: number;
astronomical: number;
civil: number;
nautical: number;
};
};
timezone: {
name: string;
Expand All @@ -64,7 +79,10 @@ export interface RawResult {
geometry: LatLng;
}

export default class OpenCageProvider extends AbstractProvider<RequestResult, RawResult> {
export default class OpenCageProvider extends AbstractProvider<
RequestResult,
RawResult
> {
searchUrl = 'https://api.opencagedata.com/geocode/v1/json';

endpoint({ query }: EndpointArgument) {
Expand Down
Loading

0 comments on commit 5223c43

Please sign in to comment.