Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
fix(errors): precise error messages on some bad usage
Browse files Browse the repository at this point in the history
fixes #229
  • Loading branch information
vvo committed Jun 20, 2016
1 parent 4950f61 commit dc8f0e1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,46 @@ import './places.scss';
import clearIcon from './icons/clear.svg';
import pinIcon from './icons/address.svg';

const errors = {
multiContainers:
`Algolia Places: 'container' must point to a single <input> element.
Example: instantiate the library twice if you want to bind two <inputs>.
See https://community.algolia.com/places/documentation.html#api-options-container`,
badContainer:
`Algolia Places: 'container' must point to an <input> element.
See https://community.algolia.com/places/documentation.html#api-options-container`
};

export default function places(options) {
const {
container,
style,
autocompleteOptions: userAutocompleteOptions = {}
} = options;

// multiple DOM elements targeted
if (container instanceof NodeList) {
if (container.length > 1) {
throw new Error(errors.multiContainers);
}

// if single node NodeList received, resolve to the first one
return places({container: container[0], ...options});
}

// container sent as a string, resolve it for multiple DOM elements issue
if (typeof container === 'string') {
const resolvedContainer = document.querySelectorAll(container);
return places({container: resolvedContainer, ...options});
}

// if not an <input>, error
if (!(container instanceof HTMLInputElement)) {
throw new Error(errors.badContainer);
}

const placesInstance = new EventEmitter();

const autocompleteOptions = {
Expand Down

0 comments on commit dc8f0e1

Please sign in to comment.