pmtpenza

Untitled

Jul 5th, 2019
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { StorageProvider } from './storage';
  3.  
  4. import { Api } from './api';
  5.  
  6. const cities: Array<any> = require('../data/city.list.json');
  7.  
  8. @Injectable()
  9. export class WeatherProvider {
  10. constructor(public api: Api, private storage: StorageProvider) {}
  11. query(endpoint: string, params?: any): any {
  12. return this.api.get(endpoint, params);
  13. }
  14. search(query: string) {
  15. if (query.length > 2) {
  16. const filter = this.queryToFilter(query);
  17. return cities.filter(cityItem => {
  18. for (const key in filter) {
  19. if (cityItem[key] === undefined || !cityItem[key].includes(filter[key])) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. });
  25. }
  26. }
  27.  
  28. queryToFilter(query: string) {
  29. const [name, country] = query.split(',').map((item) => item.trim());
  30. const filter = {name, country};
  31. Object.keys(filter).forEach(key => filter[key] === undefined && delete filter[key]);
  32. return filter;
  33. }
  34. }
Add Comment
Please, Sign In to add comment