-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorageUtils.js
More file actions
38 lines (34 loc) · 1.05 KB
/
Copy pathstorageUtils.js
File metadata and controls
38 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export const STORAGE_KEY = 'weather_app_saved_cities';
/**
* Get saved cities from localStorage
* @returns {Array<{name: string, lat: number, lon: number}>}
*/
export function getSavedCities() {
const saved = localStorage.getItem(STORAGE_KEY);
return saved ? JSON.parse(saved) : [];
}
/**
* Save a city to localStorage
* @param {Object} city - {name, lat, lon}
* @returns {boolean} - true if saved, false if duplicate
*/
export function saveCity(city) {
const cities = getSavedCities();
// Check key normalization (lowercase)
const exists = cities.some(c => c.name.toLowerCase() === city.name.toLowerCase());
if (!exists) {
cities.push(city);
localStorage.setItem(STORAGE_KEY, JSON.stringify(cities));
return true;
}
return false;
}
/**
* Remove a city
* @param {string} cityName
*/
export function removeCity(cityName) {
let cities = getSavedCities();
cities = cities.filter(c => c.name.toLowerCase() !== cityName.toLowerCase());
localStorage.setItem(STORAGE_KEY, JSON.stringify(cities));
}