@ouroboros/browser 0.3.0 → 0.4.0

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/README.md CHANGED
@@ -5,12 +5,12 @@
5
5
  A library of classes to manage functionality in browsers.
6
6
 
7
7
  ## Installation
8
- npm
8
+
9
9
  ```bash
10
10
  npm install @ouroboros/browser
11
11
  ```
12
12
 
13
- ## Getting Started
13
+ ## clipboard
14
14
 
15
15
  Import clipboard into your code
16
16
 
@@ -24,6 +24,10 @@ or
24
24
  import { clipboard } from '@ouroboros/browser';
25
25
  ```
26
26
 
27
+ copy()
28
+
29
+ ## cookies
30
+
27
31
  Import Cookies into your code
28
32
 
29
33
  ```javascript
@@ -36,6 +40,10 @@ or
36
40
  import { cookies } from '@ouroboros/browser';
37
41
  ```
38
42
 
43
+ get(), remove(), set()
44
+
45
+ ## hash
46
+
39
47
  Import hash into your code
40
48
 
41
49
  ```javascript
@@ -48,6 +56,10 @@ or
48
56
  import { hash } from '@ouroboros/browser';
49
57
  ```
50
58
 
59
+ get(), set(), subscribe(), unsubscribe()
60
+
61
+ ## network
62
+
51
63
  Import network into your code
52
64
 
53
65
  ```javascript
@@ -60,6 +72,10 @@ or
60
72
  import { network } from '@ouroboros/browser';
61
73
  ```
62
74
 
75
+ default (network)
76
+
77
+ ## pageVisibility
78
+
63
79
  Import pageVisibility into your code
64
80
 
65
81
  ```javascript
@@ -72,6 +88,10 @@ or
72
88
  import { pageVisibility } from '@ouroboros/browser';
73
89
  ```
74
90
 
91
+ default (pageVisibility)
92
+
93
+ ## safeLocalStorage
94
+
75
95
  Import safeLocalStorage into your code
76
96
 
77
97
  ```javascript
@@ -82,4 +102,22 @@ or
82
102
 
83
103
  ```javascript
84
104
  import { safeLocalStorage } from '@ouroboros/browser';
85
- ```
105
+ ```
106
+
107
+ bool(), json(), string()
108
+
109
+ ## size
110
+
111
+ Import size into your code
112
+
113
+ ```javascript
114
+ import size from '@ouroboros/browser/size';
115
+ ```
116
+
117
+ or
118
+
119
+ ```javascript
120
+ import { size } from '@ouroboros/browser';
121
+ ```
122
+
123
+ xs, sm, md, lg, xl, compare(), get(), greaterThan(), lessThan()
package/hash.js CHANGED
@@ -173,7 +173,7 @@ function subscribe(name, callback) {
173
173
  // If we don't already have the name
174
174
  if (!(name in oSubscribe)) {
175
175
  // Create a new subscribe instance
176
- oSubscribe[name] = new Subscribe();
176
+ oSubscribe[name] = new Subscribe(name in dHash ? dHash[name] : null);
177
177
  }
178
178
  // Add the callback and return
179
179
  return oSubscribe[name].subscribe(callback);
package/hash.ts CHANGED
@@ -220,7 +220,9 @@ function subscribe(name: string, callback: SubscribeCallback): SubscribeReturn {
220
220
  if(!(name in oSubscribe)) {
221
221
 
222
222
  // Create a new subscribe instance
223
- oSubscribe[name] = new Subscribe();
223
+ oSubscribe[name] = new Subscribe(
224
+ name in (dHash as HashType) ? (dHash as HashType)[name] : null
225
+ );
224
226
  }
225
227
 
226
228
  // Add the callback and return
package/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export { default as hash } from './hash';
4
4
  export { default as network } from './network';
5
5
  export { default as pageVisibility } from './pageVisibility';
6
6
  export { default as safeLocalStorage } from './safeLocalStorage';
7
+ export { default as size } from './size';
package/index.js CHANGED
@@ -4,3 +4,4 @@ export { default as hash } from './hash';
4
4
  export { default as network } from './network';
5
5
  export { default as pageVisibility } from './pageVisibility';
6
6
  export { default as safeLocalStorage } from './safeLocalStorage';
7
+ export { default as size } from './size';
package/index.ts CHANGED
@@ -3,4 +3,5 @@ export { default as cookies } from './cookies';
3
3
  export { default as hash } from './hash';
4
4
  export { default as network } from './network';
5
5
  export { default as pageVisibility } from './pageVisibility';
6
- export { default as safeLocalStorage } from './safeLocalStorage';
6
+ export { default as safeLocalStorage } from './safeLocalStorage';
7
+ export { default as size } from './size';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ouroboros/browser",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Package to handle common browser functionality.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/size.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+ export declare const xs = "xs";
11
+ export declare const sm = "sm";
12
+ export declare const md = "md";
13
+ export declare const lg = "lg";
14
+ export declare const xl = "xl";
15
+ export type SIZE = typeof xs | typeof sm | typeof md | typeof lg | typeof xl;
16
+ export declare const SIZES: Record<SIZE, number>;
17
+ /**
18
+ * Compare
19
+ *
20
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
21
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
22
+ *
23
+ * @name compare
24
+ * @access public
25
+ * @param against The size to compare the current size against
26
+ * @returns -1 || 0 || 1
27
+ */
28
+ export declare function compare(against: SIZE): 0 | 1 | -1;
29
+ /**
30
+ * Greater Than
31
+ *
32
+ * Returns true only if `a` is a larger width than `b`
33
+ *
34
+ * @name greaterThan
35
+ * @access public
36
+ * @param against The size to compare the current size against
37
+ * @returns boolean
38
+ */
39
+ export declare function greaterThan(against: SIZE): boolean;
40
+ /**
41
+ * Less Than
42
+ *
43
+ * Returns true only if `a` is a smaller width than `b`
44
+ *
45
+ * @name greaterThan
46
+ * @access public
47
+ * @param against The size to compare the current size against
48
+ * @returns boolean
49
+ */
50
+ export declare function lessThan(against: SIZE): boolean;
51
+ /**
52
+ * Get
53
+ *
54
+ * Returns the current width type based on the current client width
55
+ *
56
+ * @name get
57
+ * @access public
58
+ * @returns The current width
59
+ */
60
+ export declare function get(): SIZE;
61
+ declare const width: {
62
+ xs: string;
63
+ sm: string;
64
+ md: string;
65
+ lg: string;
66
+ xl: string;
67
+ compare: typeof compare;
68
+ greaterThan: typeof greaterThan;
69
+ lessThan: typeof lessThan;
70
+ get: typeof get;
71
+ };
72
+ export default width;
package/size.js ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+ // Constants
11
+ export const xs = 'xs';
12
+ export const sm = 'sm';
13
+ export const md = 'md';
14
+ export const lg = 'lg';
15
+ export const xl = 'xl';
16
+ // The minimum pixels necessary for each size type
17
+ export const SIZES = {
18
+ [xs]: 0, [sm]: 600, [md]: 900,
19
+ [lg]: 1200, [xl]: 1536
20
+ };
21
+ // The current size
22
+ let _current = null;
23
+ /**
24
+ * Init
25
+ *
26
+ * Called internally to setup the module
27
+ *
28
+ * @name _init
29
+ * @access private
30
+ */
31
+ function _init() {
32
+ // Const private function to calculate size from client width
33
+ const _calculate = () => {
34
+ if (document.documentElement.clientWidth < 600) {
35
+ return xs;
36
+ }
37
+ if (document.documentElement.clientWidth < 900) {
38
+ return sm;
39
+ }
40
+ if (document.documentElement.clientWidth < 1200) {
41
+ return md;
42
+ }
43
+ if (document.documentElement.clientWidth < 1536) {
44
+ return lg;
45
+ }
46
+ return xl;
47
+ };
48
+ // If we don't have the size
49
+ if (_current === null) {
50
+ _current = _calculate();
51
+ window.addEventListener('resize', () => {
52
+ _current = _calculate();
53
+ });
54
+ }
55
+ }
56
+ /**
57
+ * Compare
58
+ *
59
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
60
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
61
+ *
62
+ * @name compare
63
+ * @access public
64
+ * @param against The size to compare the current size against
65
+ * @returns -1 || 0 || 1
66
+ */
67
+ export function compare(against) {
68
+ // Init if necessary
69
+ if (_current === null)
70
+ _init();
71
+ // If the current size is the same as the passed value
72
+ if (_current === against)
73
+ return 0;
74
+ // Else, if the current value is less than the passed value
75
+ if (SIZES[_current] < SIZES[against])
76
+ return -1;
77
+ // Else, the current value is more than the passed value
78
+ return 1;
79
+ }
80
+ /**
81
+ * Greater Than
82
+ *
83
+ * Returns true only if `a` is a larger width than `b`
84
+ *
85
+ * @name greaterThan
86
+ * @access public
87
+ * @param against The size to compare the current size against
88
+ * @returns boolean
89
+ */
90
+ export function greaterThan(against) {
91
+ // Init if necessary
92
+ if (_current === null)
93
+ _init();
94
+ // If the current value is greater than the passed value
95
+ return SIZES[_current] > SIZES[against];
96
+ }
97
+ /**
98
+ * Less Than
99
+ *
100
+ * Returns true only if `a` is a smaller width than `b`
101
+ *
102
+ * @name greaterThan
103
+ * @access public
104
+ * @param against The size to compare the current size against
105
+ * @returns boolean
106
+ */
107
+ export function lessThan(against) {
108
+ // Init if necessary
109
+ if (_current === null)
110
+ _init();
111
+ // If the current value is less than the passed value
112
+ return SIZES[_current] < SIZES[against];
113
+ }
114
+ /**
115
+ * Get
116
+ *
117
+ * Returns the current width type based on the current client width
118
+ *
119
+ * @name get
120
+ * @access public
121
+ * @returns The current width
122
+ */
123
+ export function get() {
124
+ // Init if necessary
125
+ if (_current === null)
126
+ _init();
127
+ // Return the current size
128
+ return _current;
129
+ }
130
+ // Default export
131
+ const width = { xs, sm, md, lg, xl, compare, greaterThan, lessThan, get };
132
+ export default width;
package/size.ts ADDED
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Size
3
+ *
4
+ * JS Library to manage the current client width of the screen in specific sizes
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-09-19
9
+ */
10
+
11
+ // Constants
12
+ export const xs = 'xs';
13
+ export const sm = 'sm';
14
+ export const md = 'md';
15
+ export const lg = 'lg';
16
+ export const xl = 'xl';
17
+
18
+ // Types
19
+ export type SIZE = typeof xs | typeof sm | typeof md | typeof lg | typeof xl;
20
+
21
+ // The minimum pixels necessary for each size type
22
+ export const SIZES: Record<SIZE, number> = {
23
+ [xs]: 0, [sm]: 600, [md]: 900,
24
+ [lg]: 1200, [xl]: 1536
25
+ }
26
+
27
+ // The current size
28
+ let _current: SIZE | null = null;
29
+
30
+ /**
31
+ * Init
32
+ *
33
+ * Called internally to setup the module
34
+ *
35
+ * @name _init
36
+ * @access private
37
+ */
38
+ function _init(): void {
39
+
40
+ // Const private function to calculate size from client width
41
+ const _calculate = () => {
42
+ if(document.documentElement.clientWidth < 600) { return xs }
43
+ if(document.documentElement.clientWidth < 900) { return sm }
44
+ if(document.documentElement.clientWidth < 1200) { return md }
45
+ if(document.documentElement.clientWidth < 1536) { return lg }
46
+ return xl;
47
+ }
48
+
49
+ // If we don't have the size
50
+ if(_current === null) {
51
+ _current = _calculate();
52
+ window.addEventListener('resize', () => {
53
+ _current = _calculate();
54
+ });
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Compare
60
+ *
61
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
62
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
63
+ *
64
+ * @name compare
65
+ * @access public
66
+ * @param against The size to compare the current size against
67
+ * @returns -1 || 0 || 1
68
+ */
69
+ export function compare(against: SIZE) {
70
+
71
+ // Init if necessary
72
+ if(_current === null) _init();
73
+
74
+ // If the current size is the same as the passed value
75
+ if(_current === against) return 0;
76
+
77
+ // Else, if the current value is less than the passed value
78
+ if(SIZES[_current as SIZE] < SIZES[against]) return -1;
79
+
80
+ // Else, the current value is more than the passed value
81
+ return 1;
82
+ }
83
+
84
+ /**
85
+ * Greater Than
86
+ *
87
+ * Returns true only if `a` is a larger width than `b`
88
+ *
89
+ * @name greaterThan
90
+ * @access public
91
+ * @param against The size to compare the current size against
92
+ * @returns boolean
93
+ */
94
+ export function greaterThan(against: SIZE): boolean {
95
+
96
+ // Init if necessary
97
+ if(_current === null) _init();
98
+
99
+ // If the current value is greater than the passed value
100
+ return SIZES[_current as SIZE] > SIZES[against];
101
+ }
102
+
103
+ /**
104
+ * Less Than
105
+ *
106
+ * Returns true only if `a` is a smaller width than `b`
107
+ *
108
+ * @name greaterThan
109
+ * @access public
110
+ * @param against The size to compare the current size against
111
+ * @returns boolean
112
+ */
113
+ export function lessThan(against: SIZE): boolean {
114
+
115
+ // Init if necessary
116
+ if(_current === null) _init();
117
+
118
+ // If the current value is less than the passed value
119
+ return SIZES[_current as SIZE] < SIZES[against];
120
+ }
121
+
122
+ /**
123
+ * Get
124
+ *
125
+ * Returns the current width type based on the current client width
126
+ *
127
+ * @name get
128
+ * @access public
129
+ * @returns The current width
130
+ */
131
+ export function get(): SIZE {
132
+
133
+ // Init if necessary
134
+ if(_current === null) _init();
135
+
136
+ // Return the current size
137
+ return _current as SIZE;
138
+ }
139
+
140
+ // Default export
141
+ const width = { xs, sm, md, lg, xl, compare, greaterThan, lessThan, get };
142
+ export default width;