All files / utils dogUtils.js

100% Statements 53/53
100% Branches 14/14
100% Functions 9/9
100% Lines 50/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93  1x 11x 11x 1x 1x   10x 10x 1x 1x   9x     1x 3x 1x   2x 2x     5x 2x 1x   1x       1x 5x 5x       1x 1x 1x 1x 1x 1x 1x       1x 2x   2x     4x 2x 1x   1x 1x 1x       1x 3x 1x   2x 2x     5x 2x 1x   1x 1x 1x     1x                        
// get dogs from local storage
const get = () => {
    const dogValue = localStorage.getItem("dogs");
    if (dogValue === undefined) {
        const dogCollection = { nextId: 1, dogs: [] }
        return set(dogCollection);
    }
    const dogCollection = JSON.parse(dogValue);
    if (dogCollection === null) {
        const dogCollection = { nextId: 1, dogs: [] }
        return set(dogCollection);
    }
    return dogCollection;
};
 
const getById = (id) => {
    if (id === undefined) {
        return { "error": "id is a required parameter" };
    }
    const dogCollection = get();
    const dogs = dogCollection.dogs;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = dogs.findIndex((r) => r.id == id);
    if (index === -1) {
        return { "error": `dog with id ${id} not found` };
    }
    return { dog: dogs[index] };
}
 
// set dogs in local storage
const set = (dogCollection) => {
    localStorage.setItem("dogs", JSON.stringify(dogCollection));
    return dogCollection;
};
 
// add a dog to local storage
const add = (dog) => {
    const dogCollection = get();
    dog = { ...dog, id: dogCollection.nextId };
    dogCollection.nextId++;
    dogCollection.dogs.push(dog);
    set(dogCollection);
    return dog;
};
 
// update a dog in local storage
const update = (dog) => {
    const dogCollection = get();
 
    const dogs = dogCollection.dogs;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = dogs.findIndex((r) => r.id == dog.id);
    if (index === -1) {
        return { "error": `dog with id ${dog.id} not found` };
    }
    dogs[index] = dog;
    set(dogCollection);
    return { dogCollection: dogCollection };
};
 
// delete a dog from local storage
const del = (id) => {
    if (id === undefined) {
        return { "error": "id is a required parameter" };
    }
    const dogCollection = get();
    const dogs = dogCollection.dogs;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = dogs.findIndex((r) => r.id == id);
    if (index === -1) {
        return { "error": `dog with id ${id} not found` };
    }
    dogs.splice(index, 1);
    set(dogCollection);
    return { dogCollection: dogCollection };
};
 
const dogUtils = {
    get,
    getById,
    add,
    update,
    del
};
 
export { dogUtils };