@ouroboros/browser 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { default as clipboard } from './clipboard';
2
2
  export { default as cookies } from './cookies';
3
3
  export { default as hash } from './hash';
4
4
  export { default as pageVisibility } from './pageVisibility';
5
+ export { default as safeLocalStorage } from './safeLocalStorage';
package/index.js CHANGED
@@ -2,3 +2,4 @@ export { default as clipboard } from './clipboard';
2
2
  export { default as cookies } from './cookies';
3
3
  export { default as hash } from './hash';
4
4
  export { default as pageVisibility } from './pageVisibility';
5
+ export { default as safeLocalStorage } from './safeLocalStorage';
package/index.ts CHANGED
@@ -2,3 +2,4 @@ export { default as clipboard } from './clipboard';
2
2
  export { default as cookies } from './cookies';
3
3
  export { default as hash } from './hash';
4
4
  export { default as pageVisibility } from './pageVisibility';
5
+ export { default as safeLocalStorage } from './safeLocalStorage';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ouroboros/browser",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Package to handle common browser functionality.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,6 +19,7 @@
19
19
  "clipboard",
20
20
  "cookies",
21
21
  "hash",
22
+ "local storage",
22
23
  "page visibility"
23
24
  ],
24
25
  "author": "Chris Nasr <chris@ouroboroscoding.com>",
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Safe Local Storage
3
+ *
4
+ * Handles safely fetching values from localeStorage regardless if they exist
5
+ * or not
6
+ *
7
+ * @author Chris Nasr <chris@ouroboroscoding.com>
8
+ * @copyright Ouroboros Coding Inc.
9
+ * @created 2023-01-17
10
+ */
11
+ /**
12
+ * Bool
13
+ *
14
+ * Fetches a value from local storage or returns the default if no value is
15
+ * found. Assumes data is a boolean value
16
+ *
17
+ * @name bool
18
+ * @access public
19
+ * @param {string} name The name of the local var to fetch
20
+ * @param {string} defaultReturn The value to return if the var is not found
21
+ * @return {boolean}
22
+ */
23
+ export declare function bool(name: string, defaultReturn?: boolean): boolean;
24
+ /**
25
+ * json
26
+ *
27
+ * Fetches a value from local storage or returns the default if no value is
28
+ * found. Assumes data is stored in JSON
29
+ *
30
+ * @name json
31
+ * @access public
32
+ * @param {string} name The name of the local var to fetch
33
+ * @param {string} defaultReturn The value to return if the var is not found
34
+ * @return {any}
35
+ */
36
+ export declare function json(name: string, defaultReturn: any): any;
37
+ /**
38
+ * String
39
+ *
40
+ * Fetches a value from local storage or returns the default if no value is
41
+ * found
42
+ *
43
+ * @name string
44
+ * @access public
45
+ * @param {string} name The name of the local var to fetch
46
+ * @param {string} defaultReturn The value to return if the var is not found
47
+ * @return {string}
48
+ */
49
+ export declare function string(name: string, defaultReturn: string): string;
50
+ declare const safeLocaleStorage: {
51
+ bool: typeof bool;
52
+ json: typeof json;
53
+ string: typeof string;
54
+ };
55
+ export default safeLocaleStorage;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Safe Local Storage
3
+ *
4
+ * Handles safely fetching values from localeStorage regardless if they exist
5
+ * or not
6
+ *
7
+ * @author Chris Nasr <chris@ouroboroscoding.com>
8
+ * @copyright Ouroboros Coding Inc.
9
+ * @created 2023-01-17
10
+ */
11
+ /**
12
+ * Bool
13
+ *
14
+ * Fetches a value from local storage or returns the default if no value is
15
+ * found. Assumes data is a boolean value
16
+ *
17
+ * @name bool
18
+ * @access public
19
+ * @param {string} name The name of the local var to fetch
20
+ * @param {string} defaultReturn The value to return if the var is not found
21
+ * @return {boolean}
22
+ */
23
+ export function bool(name, defaultReturn = false) {
24
+ const value = localStorage.getItem(name);
25
+ return value === null ? defaultReturn : (value === '' ? false : true);
26
+ }
27
+ /**
28
+ * json
29
+ *
30
+ * Fetches a value from local storage or returns the default if no value is
31
+ * found. Assumes data is stored in JSON
32
+ *
33
+ * @name json
34
+ * @access public
35
+ * @param {string} name The name of the local var to fetch
36
+ * @param {string} defaultReturn The value to return if the var is not found
37
+ * @return {any}
38
+ */
39
+ export function json(name, defaultReturn) {
40
+ const value = localStorage.getItem(name);
41
+ return value === null ? defaultReturn : JSON.parse(value);
42
+ }
43
+ /**
44
+ * String
45
+ *
46
+ * Fetches a value from local storage or returns the default if no value is
47
+ * found
48
+ *
49
+ * @name string
50
+ * @access public
51
+ * @param {string} name The name of the local var to fetch
52
+ * @param {string} defaultReturn The value to return if the var is not found
53
+ * @return {string}
54
+ */
55
+ export function string(name, defaultReturn) {
56
+ const value = localStorage.getItem(name);
57
+ return value === null ? defaultReturn : value;
58
+ }
59
+ // Default export
60
+ const safeLocaleStorage = { bool, json, string };
61
+ export default safeLocaleStorage;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Safe Local Storage
3
+ *
4
+ * Handles safely fetching values from localeStorage regardless if they exist
5
+ * or not
6
+ *
7
+ * @author Chris Nasr <chris@ouroboroscoding.com>
8
+ * @copyright Ouroboros Coding Inc.
9
+ * @created 2023-01-17
10
+ */
11
+
12
+ /**
13
+ * Bool
14
+ *
15
+ * Fetches a value from local storage or returns the default if no value is
16
+ * found. Assumes data is a boolean value
17
+ *
18
+ * @name bool
19
+ * @access public
20
+ * @param {string} name The name of the local var to fetch
21
+ * @param {string} defaultReturn The value to return if the var is not found
22
+ * @return {boolean}
23
+ */
24
+ export function bool(name: string, defaultReturn=false): boolean {
25
+ const value = localStorage.getItem(name);
26
+ return value === null ? defaultReturn : (
27
+ value === '' ? false : true
28
+ )
29
+ }
30
+
31
+ /**
32
+ * json
33
+ *
34
+ * Fetches a value from local storage or returns the default if no value is
35
+ * found. Assumes data is stored in JSON
36
+ *
37
+ * @name json
38
+ * @access public
39
+ * @param {string} name The name of the local var to fetch
40
+ * @param {string} defaultReturn The value to return if the var is not found
41
+ * @return {any}
42
+ */
43
+ export function json(name: string, defaultReturn: any): any {
44
+ const value = localStorage.getItem(name);
45
+ return value === null ? defaultReturn : JSON.parse(value);
46
+ }
47
+
48
+ /**
49
+ * String
50
+ *
51
+ * Fetches a value from local storage or returns the default if no value is
52
+ * found
53
+ *
54
+ * @name string
55
+ * @access public
56
+ * @param {string} name The name of the local var to fetch
57
+ * @param {string} defaultReturn The value to return if the var is not found
58
+ * @return {string}
59
+ */
60
+ export function string(name: string, defaultReturn: string): string {
61
+ const value = localStorage.getItem(name);
62
+ return value === null ? defaultReturn : value;
63
+ }
64
+
65
+ // Default export
66
+ const safeLocaleStorage = { bool, json, string };
67
+ export default safeLocaleStorage;