@ouroboros/browser 0.3.1 → 0.4.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/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 (Subscribe)
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 (Subscribe)
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
+ size (Subscribe), compare(), greaterThan(), lessThan()
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, compare, greaterThan, lessThan } 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, compare, greaterThan, lessThan } 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, compare, greaterThan, lessThan } from './size';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ouroboros/browser",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "Package to handle common browser functionality.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -22,7 +22,8 @@
22
22
  "hash",
23
23
  "local storage",
24
24
  "network",
25
- "page visibility"
25
+ "page visibility",
26
+ "browser size"
26
27
  ],
27
28
  "author": "Chris Nasr <chris@ouroboroscoding.com>",
28
29
  "license": "MIT",
package/size.d.ts ADDED
@@ -0,0 +1,141 @@
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
+ import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
11
+ export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
12
+ export declare const SIZES: Record<SIZE, number>;
13
+ /**
14
+ * Size
15
+ *
16
+ * Extends the Subscribe class to be created once and exported
17
+ *
18
+ * @name Size
19
+ * @extends Subscribe
20
+ */
21
+ declare class Size extends Subscribe {
22
+ _callback: () => void;
23
+ /**
24
+ * Constructor
25
+ *
26
+ * Creates a new instance and returns it
27
+ *
28
+ * @name Size
29
+ * @access private
30
+ * @returns Size
31
+ */
32
+ constructor();
33
+ /**
34
+ * Compare
35
+ *
36
+ * Calls the module's `compare` method with the current size and the
37
+ * `against` param.
38
+ *
39
+ * @name compare
40
+ * @access public
41
+ * @param against The size to compare the current size against
42
+ * @returns number
43
+ */
44
+ compare(against: SIZE): number;
45
+ /**
46
+ * Get
47
+ *
48
+ * Returns the current size. Overrides the built in Subscribe.get to make
49
+ * sure we always have the latest, even if no one has ever subscribed
50
+ *
51
+ * @name get
52
+ * @access public
53
+ * @returns string
54
+ */
55
+ get(): SIZE;
56
+ /**
57
+ * Greater Than
58
+ *
59
+ * Calls the module's `greaterThan` method with the current size and the
60
+ * `against` param.
61
+ *
62
+ * @name greaterThan
63
+ * @access public
64
+ * @param against The size to compare the current size against
65
+ * @returns number
66
+ */
67
+ greaterThan(against: SIZE): boolean;
68
+ /**
69
+ * Less Than
70
+ *
71
+ * Calls the module's `lessThan` method with the current size and the
72
+ * `against` param.
73
+ *
74
+ * @name lessThan
75
+ * @access public
76
+ * @param against The size to compare the current size against
77
+ * @returns number
78
+ */
79
+ lessThan(against: SIZE): boolean;
80
+ /**
81
+ * Subscribe
82
+ *
83
+ * Overrides parent subscribe to handle adding the window event
84
+ *
85
+ * @name subscribe
86
+ * @access public
87
+ * @param callback The function to call when the state changes
88
+ * @returns object
89
+ */
90
+ subscribe(callback: SubscribeCallback): SubscribeReturn;
91
+ /**
92
+ * Unsubscribe
93
+ *
94
+ * Overrides parent unsubscribe to handle removing the window event
95
+ *
96
+ * @name unsubscribe
97
+ * @access public
98
+ * @param callback The callback to remove from the list
99
+ * @returns boolean
100
+ */
101
+ unsubscribe(callback: SubscribeCallback): boolean;
102
+ }
103
+ declare const size: Size;
104
+ export default size;
105
+ /**
106
+ * Compare
107
+ *
108
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
109
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
110
+ *
111
+ * @name compare
112
+ * @access public
113
+ * @param a The size to compare against `b`
114
+ * @param b The size to compare against `a`
115
+ * @returns -1 || 0 || 1
116
+ */
117
+ export declare function compare(a: SIZE, b: SIZE): 0 | 1 | -1;
118
+ /**
119
+ * Greater Than
120
+ *
121
+ * Returns true only if `a` is a larger width than `b`
122
+ *
123
+ * @name greaterThan
124
+ * @access public
125
+ * @param a The size to compare as greater than `b`
126
+ * @param b The size to compare against `a`
127
+ * @returns boolean
128
+ */
129
+ export declare function greaterThan(a: SIZE, b: SIZE): boolean;
130
+ /**
131
+ * Less Than
132
+ *
133
+ * Returns true only if `a` is a smaller width than `b`
134
+ *
135
+ * @name greaterThan
136
+ * @access public
137
+ * @param a The size to compare as less than `b`
138
+ * @param b The size to compare against `a`
139
+ * @returns boolean
140
+ */
141
+ export declare function lessThan(a: SIZE, b: SIZE): boolean;
package/size.js ADDED
@@ -0,0 +1,223 @@
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
+ // Ouroboros modules
11
+ import Subscribe from '@ouroboros/subscribe';
12
+ // The minimum pixels necessary for each size type
13
+ export const SIZES = {
14
+ xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
15
+ };
16
+ /**
17
+ * Calculate
18
+ *
19
+ * Uses the current client width to calculate the string representation
20
+ *
21
+ * @name calculate
22
+ * @access private
23
+ * @returns string
24
+ */
25
+ function calculate() {
26
+ if (document.documentElement.clientWidth < 600) {
27
+ return 'xs';
28
+ }
29
+ if (document.documentElement.clientWidth < 900) {
30
+ return 'sm';
31
+ }
32
+ if (document.documentElement.clientWidth < 1200) {
33
+ return 'md';
34
+ }
35
+ if (document.documentElement.clientWidth < 1536) {
36
+ return 'lg';
37
+ }
38
+ return 'xl';
39
+ }
40
+ /**
41
+ * Size
42
+ *
43
+ * Extends the Subscribe class to be created once and exported
44
+ *
45
+ * @name Size
46
+ * @extends Subscribe
47
+ */
48
+ class Size extends Subscribe {
49
+ // The window event callback
50
+ _callback;
51
+ /**
52
+ * Constructor
53
+ *
54
+ * Creates a new instance and returns it
55
+ *
56
+ * @name Size
57
+ * @access private
58
+ * @returns Size
59
+ */
60
+ constructor() {
61
+ // Get the current values and init the instance with them
62
+ super(calculate());
63
+ // Store the subscription callback
64
+ this._callback = () => {
65
+ this.set(calculate());
66
+ };
67
+ }
68
+ /**
69
+ * Compare
70
+ *
71
+ * Calls the module's `compare` method with the current size and the
72
+ * `against` param.
73
+ *
74
+ * @name compare
75
+ * @access public
76
+ * @param against The size to compare the current size against
77
+ * @returns number
78
+ */
79
+ compare(against) {
80
+ return compare(this.subscribeData, against);
81
+ }
82
+ /**
83
+ * Get
84
+ *
85
+ * Returns the current size. Overrides the built in Subscribe.get to make
86
+ * sure we always have the latest, even if no one has ever subscribed
87
+ *
88
+ * @name get
89
+ * @access public
90
+ * @returns string
91
+ */
92
+ get() {
93
+ const s = calculate();
94
+ if (s !== this.subscribeData) {
95
+ this.set(s);
96
+ }
97
+ return s;
98
+ }
99
+ /**
100
+ * Greater Than
101
+ *
102
+ * Calls the module's `greaterThan` method with the current size and the
103
+ * `against` param.
104
+ *
105
+ * @name greaterThan
106
+ * @access public
107
+ * @param against The size to compare the current size against
108
+ * @returns number
109
+ */
110
+ greaterThan(against) {
111
+ return greaterThan(this.subscribeData, against);
112
+ }
113
+ /**
114
+ * Less Than
115
+ *
116
+ * Calls the module's `lessThan` method with the current size and the
117
+ * `against` param.
118
+ *
119
+ * @name lessThan
120
+ * @access public
121
+ * @param against The size to compare the current size against
122
+ * @returns number
123
+ */
124
+ lessThan(against) {
125
+ return lessThan(this.subscribeData, against);
126
+ }
127
+ /**
128
+ * Subscribe
129
+ *
130
+ * Overrides parent subscribe to handle adding the window event
131
+ *
132
+ * @name subscribe
133
+ * @access public
134
+ * @param callback The function to call when the state changes
135
+ * @returns object
136
+ */
137
+ subscribe(callback) {
138
+ // If we have no current subscribers
139
+ if (this.subscribeCallbacks.length === 0) {
140
+ // Add the event listener
141
+ window.addEventListener('resize', this._callback);
142
+ }
143
+ // Call the parent subscribe and return
144
+ return super.subscribe(callback);
145
+ }
146
+ /**
147
+ * Unsubscribe
148
+ *
149
+ * Overrides parent unsubscribe to handle removing the window event
150
+ *
151
+ * @name unsubscribe
152
+ * @access public
153
+ * @param callback The callback to remove from the list
154
+ * @returns boolean
155
+ */
156
+ unsubscribe(callback) {
157
+ // Call the parent unsubscribe
158
+ const bRet = super.unsubscribe(callback);
159
+ // If it was successful and we have no more callbacks
160
+ if (bRet && this.subscribeCallbacks.length === 0) {
161
+ // Remove the event listener
162
+ window.removeEventListener('resize', this._callback);
163
+ }
164
+ // Return
165
+ return bRet;
166
+ }
167
+ }
168
+ // Create an instance of the class
169
+ const size = new Size();
170
+ // Default export
171
+ export default size;
172
+ /**
173
+ * Compare
174
+ *
175
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
176
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
177
+ *
178
+ * @name compare
179
+ * @access public
180
+ * @param a The size to compare against `b`
181
+ * @param b The size to compare against `a`
182
+ * @returns -1 || 0 || 1
183
+ */
184
+ export function compare(a, b) {
185
+ // If the current size is the same as the passed value
186
+ if (a === b)
187
+ return 0;
188
+ // Else, if the current value is less than the passed value
189
+ if (SIZES[a] < SIZES[b])
190
+ return -1;
191
+ // Else, the current value is more than the passed value
192
+ return 1;
193
+ }
194
+ /**
195
+ * Greater Than
196
+ *
197
+ * Returns true only if `a` is a larger width than `b`
198
+ *
199
+ * @name greaterThan
200
+ * @access public
201
+ * @param a The size to compare as greater than `b`
202
+ * @param b The size to compare against `a`
203
+ * @returns boolean
204
+ */
205
+ export function greaterThan(a, b) {
206
+ // If the current value is greater than the passed value
207
+ return SIZES[a] > SIZES[b];
208
+ }
209
+ /**
210
+ * Less Than
211
+ *
212
+ * Returns true only if `a` is a smaller width than `b`
213
+ *
214
+ * @name greaterThan
215
+ * @access public
216
+ * @param a The size to compare as less than `b`
217
+ * @param b The size to compare against `a`
218
+ * @returns boolean
219
+ */
220
+ export function lessThan(a, b) {
221
+ // If the `a` minimum value is less than the `b` minimum value
222
+ return SIZES[a] < SIZES[b];
223
+ }
package/size.ts ADDED
@@ -0,0 +1,249 @@
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
+ // Ouroboros modules
12
+ import Subscribe, {
13
+ SubscribeCallback,
14
+ SubscribeReturn
15
+ } from '@ouroboros/subscribe';
16
+
17
+ // Types
18
+ export type SIZE = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
19
+
20
+ // The minimum pixels necessary for each size type
21
+ export const SIZES: Record<SIZE, number> = {
22
+ xs: 0, sm: 600, md: 900, lg: 1200, xl: 1536
23
+ }
24
+
25
+ /**
26
+ * Calculate
27
+ *
28
+ * Uses the current client width to calculate the string representation
29
+ *
30
+ * @name calculate
31
+ * @access private
32
+ * @returns string
33
+ */
34
+ function calculate(): SIZE {
35
+ if(document.documentElement.clientWidth < 600) { return 'xs' }
36
+ if(document.documentElement.clientWidth < 900) { return 'sm' }
37
+ if(document.documentElement.clientWidth < 1200) { return 'md' }
38
+ if(document.documentElement.clientWidth < 1536) { return 'lg' }
39
+ return 'xl';
40
+ }
41
+
42
+ /**
43
+ * Size
44
+ *
45
+ * Extends the Subscribe class to be created once and exported
46
+ *
47
+ * @name Size
48
+ * @extends Subscribe
49
+ */
50
+ class Size extends Subscribe {
51
+
52
+ // The window event callback
53
+ _callback: () => void;
54
+
55
+ /**
56
+ * Constructor
57
+ *
58
+ * Creates a new instance and returns it
59
+ *
60
+ * @name Size
61
+ * @access private
62
+ * @returns Size
63
+ */
64
+ constructor() {
65
+
66
+ // Get the current values and init the instance with them
67
+ super(calculate());
68
+
69
+ // Store the subscription callback
70
+ this._callback = () => {
71
+ this.set(calculate());
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Compare
77
+ *
78
+ * Calls the module's `compare` method with the current size and the
79
+ * `against` param.
80
+ *
81
+ * @name compare
82
+ * @access public
83
+ * @param against The size to compare the current size against
84
+ * @returns number
85
+ */
86
+ compare(against: SIZE): number {
87
+ return compare(this.subscribeData, against);
88
+ }
89
+
90
+ /**
91
+ * Get
92
+ *
93
+ * Returns the current size. Overrides the built in Subscribe.get to make
94
+ * sure we always have the latest, even if no one has ever subscribed
95
+ *
96
+ * @name get
97
+ * @access public
98
+ * @returns string
99
+ */
100
+ get() {
101
+ const s = calculate();
102
+ if(s !== this.subscribeData) {
103
+ this.set(s);
104
+ }
105
+ return s;
106
+ }
107
+
108
+ /**
109
+ * Greater Than
110
+ *
111
+ * Calls the module's `greaterThan` method with the current size and the
112
+ * `against` param.
113
+ *
114
+ * @name greaterThan
115
+ * @access public
116
+ * @param against The size to compare the current size against
117
+ * @returns number
118
+ */
119
+ greaterThan(against: SIZE): boolean {
120
+ return greaterThan(this.subscribeData, against);
121
+ }
122
+
123
+ /**
124
+ * Less Than
125
+ *
126
+ * Calls the module's `lessThan` method with the current size and the
127
+ * `against` param.
128
+ *
129
+ * @name lessThan
130
+ * @access public
131
+ * @param against The size to compare the current size against
132
+ * @returns number
133
+ */
134
+ lessThan(against: SIZE): boolean {
135
+ return lessThan(this.subscribeData, against);
136
+ }
137
+
138
+ /**
139
+ * Subscribe
140
+ *
141
+ * Overrides parent subscribe to handle adding the window event
142
+ *
143
+ * @name subscribe
144
+ * @access public
145
+ * @param callback The function to call when the state changes
146
+ * @returns object
147
+ */
148
+ subscribe(callback: SubscribeCallback): SubscribeReturn {
149
+
150
+ // If we have no current subscribers
151
+ if(this.subscribeCallbacks.length === 0) {
152
+
153
+ // Add the event listener
154
+ window.addEventListener('resize', this._callback);
155
+ }
156
+
157
+ // Call the parent subscribe and return
158
+ return super.subscribe(callback);
159
+ }
160
+
161
+ /**
162
+ * Unsubscribe
163
+ *
164
+ * Overrides parent unsubscribe to handle removing the window event
165
+ *
166
+ * @name unsubscribe
167
+ * @access public
168
+ * @param callback The callback to remove from the list
169
+ * @returns boolean
170
+ */
171
+ unsubscribe(callback: SubscribeCallback): boolean {
172
+
173
+ // Call the parent unsubscribe
174
+ const bRet = super.unsubscribe(callback);
175
+
176
+ // If it was successful and we have no more callbacks
177
+ if(bRet && this.subscribeCallbacks.length === 0) {
178
+
179
+ // Remove the event listener
180
+ window.removeEventListener('resize', this._callback);
181
+ }
182
+
183
+ // Return
184
+ return bRet;
185
+ }
186
+ }
187
+
188
+ // Create an instance of the class
189
+ const size = new Size();
190
+ // Default export
191
+ export default size;
192
+
193
+ /**
194
+ * Compare
195
+ *
196
+ * Compares two widths and returns the standard -1,0,1 values as a result. -1
197
+ * if `a` is less than `b, 0 for equality, and 1 if `a` is greater than `b`.
198
+ *
199
+ * @name compare
200
+ * @access public
201
+ * @param a The size to compare against `b`
202
+ * @param b The size to compare against `a`
203
+ * @returns -1 || 0 || 1
204
+ */
205
+ export function compare(a: SIZE, b: SIZE) {
206
+
207
+ // If the current size is the same as the passed value
208
+ if(a === b) return 0;
209
+
210
+ // Else, if the current value is less than the passed value
211
+ if(SIZES[a] < SIZES[b]) return -1;
212
+
213
+ // Else, the current value is more than the passed value
214
+ return 1;
215
+ }
216
+
217
+ /**
218
+ * Greater Than
219
+ *
220
+ * Returns true only if `a` is a larger width than `b`
221
+ *
222
+ * @name greaterThan
223
+ * @access public
224
+ * @param a The size to compare as greater than `b`
225
+ * @param b The size to compare against `a`
226
+ * @returns boolean
227
+ */
228
+ export function greaterThan(a: SIZE, b: SIZE): boolean {
229
+
230
+ // If the current value is greater than the passed value
231
+ return SIZES[a] > SIZES[b];
232
+ }
233
+
234
+ /**
235
+ * Less Than
236
+ *
237
+ * Returns true only if `a` is a smaller width than `b`
238
+ *
239
+ * @name greaterThan
240
+ * @access public
241
+ * @param a The size to compare as less than `b`
242
+ * @param b The size to compare against `a`
243
+ * @returns boolean
244
+ */
245
+ export function lessThan(a: SIZE, b: SIZE): boolean {
246
+
247
+ // If the `a` minimum value is less than the `b` minimum value
248
+ return SIZES[a] < SIZES[b];
249
+ }