Skip to content

Commit

Permalink
Merge pull request #2 from JUGG097/add-array-inputs
Browse files Browse the repository at this point in the history
Add array inputs
  • Loading branch information
JUGG097 committed Jul 21, 2023
2 parents 316a9f5 + 26021d2 commit d7b0bd0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 14 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,49 @@
- Fetch key:value stored in storage
```js
// Using ES6 import syntax
import {retrieveValue} from 'retrievetokens';
import {retrieveValue, retrieveValues} from 'retrievetokens';

// Retrieve token from the local storage
// returns null or value (as a string)
const token = retrieveValue("valueKey", "local");
const tokenObj = retrieveValues(["key1", "key2"], "local");

// Retrieve token from the session storage
// returns null or value (as a string)
const token = retrieveValue("valueKey", "session");
const tokenObj = retrieveValues(["key1", "key2"], "session");
```

- Store key:value pair in storage
```js
// Using ES6 import syntax
import {addValue} from 'retrievetokens';
import {addValue, addValues} from 'retrievetokens';

// Store token in the local storage
// returns true or false (if it fails to store)
const token = addValue("key", "value", "local");
const tokenObj = addValues({ key1: "value", key2: "value2" }, "local");

// Store token in the session storage
// returns true or false (if it fails to store)
const token = addValue("key", "value", "session");
const tokenObj = addValues({ key1: "value", key2: "value2" }, "session");
```

- Delete stored key:value pair from storage
```js
// Using ES6 import syntax
import {deleteValue} from 'retrievetokens';
import {deleteValue, deleteValues} from 'retrievetokens';

// Delete token from the local storage
// returns true or false (if it fails to delete)
const token = deleteValue("valueKey", "local");
const tokenObj = deleteValues(["key1", "key2"], "local");

// Delete token from the session storage
// returns true or false (if it fails to delete)
const token = deleteValue("valueKey", "session");
const tokenObj = deleteValues(["key1", "key2"], "session");
```

## 📦 Releases <a name="release"></a>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "retrievetokens",
"version": "1.0.7",
"version": "1.0.8",
"description": "A simple package for interacting with web client storages like `localStorage`",
"source": "src/index.ts",
"main": "dist/index.js",
Expand Down
43 changes: 41 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
addValueInterface,
removeValueInterface,
deleteValueInterface,
retrieveValueInterface,
retrieveValuesInterface,
storageOptions,
addValuesInterface,
addArrayInputObj,
addArrayResultObj,
retrieveArrayObj,
deleteValuesInterface,
} from "./utils";

export const retrieveValue: retrieveValueInterface = (
Expand All @@ -19,6 +25,17 @@ export const retrieveValue: retrieveValueInterface = (
}
};

export const retrieveValues: retrieveValuesInterface = (
valueKeys: string[],
storageType: storageOptions
) => {
let result_obj: retrieveArrayObj = {};
valueKeys.forEach((element) => {
result_obj[element] = retrieveValue(element, storageType);
});
return result_obj;
};

export const addValue: addValueInterface = (
key: string,
value: string,
Expand All @@ -45,7 +62,18 @@ export const addValue: addValueInterface = (
}
};

export const deleteValue: removeValueInterface = (
export const addValues: addValuesInterface = (
keyValuePair: addArrayInputObj,
storageType: storageOptions
) => {
let result_obj: addArrayResultObj = {};
for (const key in keyValuePair) {
result_obj[key] = addValue(key, keyValuePair[key], storageType);
}
return result_obj;
};

export const deleteValue: deleteValueInterface = (
valueKey: string,
storageType: storageOptions
) => {
Expand All @@ -68,3 +96,14 @@ export const deleteValue: removeValueInterface = (
return false;
}
};

export const deleteValues: deleteValuesInterface = (
valueKeys: string[],
storageType: storageOptions
) => {
let result_obj: addArrayResultObj = {};
valueKeys.forEach((element) => {
result_obj[element] = deleteValue(element, storageType);
});
return result_obj;
};
30 changes: 27 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@ export interface retrieveValueInterface {
(valueKey: string, storageType: storageOptions): string | null;
}

export interface retrieveValuesInterface {
(valueKeys: string[], storageType: storageOptions): retrieveArrayObj;
}

export interface addValueInterface {
(key: string, value: string, storageType: storageOptions): boolean;
(key: string, value: string, storageType: storageOptions): boolean;
}

export interface removeValueInterface {
(valueKey: string, storageType: storageOptions): boolean;
export interface addValuesInterface {
(keyValuePair: addArrayInputObj, storageType: storageOptions): addArrayResultObj;
}

export interface deleteValueInterface {
(valueKey: string, storageType: storageOptions): boolean;
}

export interface deleteValuesInterface {
(valueKeys: string[], storageType: storageOptions): addArrayResultObj;
}

export type storageOptions = "local" | "session";

export type retrieveArrayObj = {
[key: string]: string | null;
};

export type addArrayResultObj = {
[key: string]: boolean;
};

export type addArrayInputObj = {
[key: string]: string;
};
97 changes: 92 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { addValue, deleteValue, retrieveValue } from "../src/index";
import {
addValue,
addValues,
deleteValue,
deleteValues,
retrieveValue,
retrieveValues,
} from "../src/index";

const addValuesToStorage = () => {
localStorage.setItem("key1", "value");
localStorage.setItem("key2", "value2");
sessionStorage.setItem("key1", "value");
sessionStorage.setItem("key2", "value2");
};

describe("Tests for the retrieve, add and delete methods", () => {
beforeEach(() => {
Expand All @@ -16,8 +30,7 @@ describe("Tests for the retrieve, add and delete methods", () => {

it("Return value using retrieve method with primed storages", async () => {
// Set some values to storage
localStorage.setItem("key1", "value");
sessionStorage.setItem("key1", "value");
addValuesToStorage();

const localValue = retrieveValue("key1", "local");
const sessionValue = retrieveValue("key1", "session");
Expand All @@ -36,8 +49,7 @@ describe("Tests for the retrieve, add and delete methods", () => {

it("Return true using delete method with primed storages", async () => {
// Set some values to storage
localStorage.setItem("key1", "value");
sessionStorage.setItem("key1", "value");
addValuesToStorage();

const localValue = deleteValue("key1", "local");
const sessionValue = deleteValue("key1", "session");
Expand All @@ -56,3 +68,78 @@ describe("Tests for the retrieve, add and delete methods", () => {
expect(sessionValue).toBe(true);
});
});

describe("Tests for the retrieve, add and delete methods with array and object inputs", () => {
beforeEach(() => {
localStorage.clear();
sessionStorage.clear();
});

it("Null return for empty storages using retrieve method", async () => {
const localValue = retrieveValues(["key1", "key2"], "local");
const sessionValue = retrieveValues(["key1", "key2"], "session");

expect(localValue.key1).toBe(null);
expect(localValue.key2).toBe(null);
expect(sessionValue.key1).toBe(null);
expect(sessionValue.key2).toBe(null);
});

it("Return value using retrieve method with primed storages", async () => {
// Set some values to storage
addValuesToStorage();

const localValue = retrieveValues(["key1", "key2"], "local");
const sessionValue = retrieveValues(["key1", "key2"], "session");

expect(localValue.key1).toBe("value");
expect(localValue.key2).toBe("value2");
expect(sessionValue.key1).toBe("value");
expect(sessionValue.key2).toBe("value2");
});

it("Return true for empty storages using delete method", async () => {
const localValue = deleteValues(["key1", "key2"], "local");
const sessionValue = deleteValues(["key1", "key2"], "session");

expect(localValue.key1).toBe(true);
expect(localValue.key2).toBe(true);
expect(sessionValue.key1).toBe(true);
expect(sessionValue.key2).toBe(true);
});

it("Return true using delete method with primed storages", async () => {
// Set some values to storage
addValuesToStorage();

const localValue = deleteValues(["key1", "key2"], "local");
const sessionValue = deleteValues(["key1", "key2"], "session");

expect(localValue.key1).toBe(true);
expect(localValue.key2).toBe(true);
expect(sessionValue.key1).toBe(true);
expect(sessionValue.key2).toBe(true);
});

it("Return true for successful value addition", async () => {
const localValue = addValues(
{ key1: "value", key2: "value2" },
"local"
);
const sessionValue = addValues(
{ key1: "value", key2: "value2" },
"session"
);

expect(localStorage.getItem("key1")).toBe("value");
expect(sessionStorage.getItem("key1")).toBe("value");
expect(localStorage.getItem("key2")).toBe("value2");
expect(sessionStorage.getItem("key2")).toBe("value2");
console.log(localValue);

expect(localValue.key1).toBe(true);
expect(localValue.key2).toBe(true);
expect(sessionValue.key1).toBe(true);
expect(sessionValue.key2).toBe(true);
});
});

0 comments on commit d7b0bd0

Please sign in to comment.