@jdeighan/coffee-utils 16.0.19 → 16.0.21

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/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "16.0.19",
4
+ "version": "16.0.21",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
8
8
  ".": "./src/lib/browser.js",
9
9
  "./server": "./src/lib/server.js",
10
10
  "./browser": "./src/lib/browser.js",
11
- "./prefs": "./src/lib/prefs.js",
12
11
  "./fs": "./src/lib/fs.js",
13
12
  "./log": "./src/lib/log.js",
14
13
  "./block": "./src/lib/block.js",
@@ -47,14 +46,14 @@
47
46
  },
48
47
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
49
48
  "dependencies": {
50
- "@jdeighan/base-utils": "^8.0.21",
49
+ "@jdeighan/base-utils": "^8.0.22",
51
50
  "cross-env": "^7.0.3",
52
51
  "n-readlines": "^1.0.1",
53
52
  "readline-sync": "^1.4.10",
54
53
  "svelte": "^4.1.1"
55
54
  },
56
55
  "devDependencies": {
57
- "@jdeighan/unit-tester": "^3.0.69",
56
+ "@jdeighan/unit-tester": "^3.0.70",
58
57
  "ava": "^6.0.1"
59
58
  }
60
59
  }
@@ -1,6 +1,7 @@
1
1
  # browser.coffee
2
2
 
3
3
  import {undef, defined, notdefined} from '@jdeighan/base-utils'
4
+ import {assert, croak} from '@jdeighan/base-utils/exceptions'
4
5
 
5
6
  audio = undef # audio context - create only when needed, then keep
6
7
 
@@ -24,15 +25,27 @@ export beep = (volume=100, freq=520, duration=200) =>
24
25
 
25
26
  # ---------------------------------------------------------------------------
26
27
 
27
- export getLocalStore = (key, defValue={}) =>
28
+ export localStorageAvailable = () ->
29
+
30
+ storage = window.localStorage
31
+ if notdefined(storage)
32
+ return false
33
+ try
34
+ x = '__storage_test__'
35
+ storage.setItem x, x
36
+ got = storage.getItem x
37
+ storage.removeItem x
38
+ return (got == x)
39
+ catch e
40
+ return false
41
+
42
+ # ---------------------------------------------------------------------------
28
43
 
29
- if (typeof localStorage == 'undefined')
30
- console.log "localStorage not available!"
31
- return undef
44
+ export getLocalStore = (key, defValue={}) =>
32
45
 
33
- value = localStorage.getItem(key)
34
- if defined(value)
35
- return JSON.parse(value)
46
+ assert localStorageAvailable(), "no localStorage"
47
+ if localStorage.hasOwnProperty(key)
48
+ return JSON.parse(localStorage.getItem(key))
36
49
  else
37
50
  localStorage.setItem key, JSON.stringify(defValue)
38
51
  return defValue
@@ -41,20 +54,6 @@ export getLocalStore = (key, defValue={}) =>
41
54
 
42
55
  export setLocalStore = (key, value) =>
43
56
 
44
- if (typeof localStorage == 'undefined')
45
- console.log "localStorage not available!"
46
- return undef
47
-
57
+ assert localStorageAvailable(), "no localStorage"
48
58
  localStorage.setItem key, JSON.stringify(value)
49
59
  return
50
-
51
- # ---------------------------------------------------------------------------
52
- # --- only here for backward compatibility
53
-
54
- export localStore = (key, value) =>
55
-
56
- if defined(value)
57
- setLocalStore key, value
58
- else
59
- getLocalStore key, undef
60
- return
@@ -8,6 +8,11 @@ import {
8
8
  notdefined
9
9
  } from '@jdeighan/base-utils';
10
10
 
11
+ import {
12
+ assert,
13
+ croak
14
+ } from '@jdeighan/base-utils/exceptions';
15
+
11
16
  audio = undef; // audio context - create only when needed, then keep
12
17
 
13
18
 
@@ -30,15 +35,29 @@ export var beep = (volume = 100, freq = 520, duration = 200) => {
30
35
  };
31
36
 
32
37
  // ---------------------------------------------------------------------------
33
- export var getLocalStore = (key, defValue = {}) => {
34
- var value;
35
- if (typeof localStorage === 'undefined') {
36
- console.log("localStorage not available!");
37
- return undef;
38
+ export var localStorageAvailable = function() {
39
+ var e, got, storage, x;
40
+ storage = window.localStorage;
41
+ if (notdefined(storage)) {
42
+ return false;
43
+ }
44
+ try {
45
+ x = '__storage_test__';
46
+ storage.setItem(x, x);
47
+ got = storage.getItem(x);
48
+ storage.removeItem(x);
49
+ return got === x;
50
+ } catch (error) {
51
+ e = error;
52
+ return false;
38
53
  }
39
- value = localStorage.getItem(key);
40
- if (defined(value)) {
41
- return JSON.parse(value);
54
+ };
55
+
56
+ // ---------------------------------------------------------------------------
57
+ export var getLocalStore = (key, defValue = {}) => {
58
+ assert(localStorageAvailable(), "no localStorage");
59
+ if (localStorage.hasOwnProperty(key)) {
60
+ return JSON.parse(localStorage.getItem(key));
42
61
  } else {
43
62
  localStorage.setItem(key, JSON.stringify(defValue));
44
63
  return defValue;
@@ -47,19 +66,6 @@ export var getLocalStore = (key, defValue = {}) => {
47
66
 
48
67
  // ---------------------------------------------------------------------------
49
68
  export var setLocalStore = (key, value) => {
50
- if (typeof localStorage === 'undefined') {
51
- console.log("localStorage not available!");
52
- return undef;
53
- }
69
+ assert(localStorageAvailable(), "no localStorage");
54
70
  localStorage.setItem(key, JSON.stringify(value));
55
71
  };
56
-
57
- // ---------------------------------------------------------------------------
58
- // --- only here for backward compatibility
59
- export var localStore = (key, value) => {
60
- if (defined(value)) {
61
- setLocalStore(key, value);
62
- } else {
63
- getLocalStore(key, undef);
64
- }
65
- };
@@ -1,12 +0,0 @@
1
- # prefs.coffee
2
-
3
- import {getLocalStore} from '@jdeighan/coffee-utils/browser'
4
-
5
- export hPrefs = getLocalStore 'hPrefs', {}
6
-
7
- # ---------------------------------------------------------------------------
8
-
9
- export setPref = (key, value) =>
10
-
11
- hPrefs[key] = value
12
- setLocalStore 'hPrefs', hPrefs
package/src/lib/prefs.js DELETED
@@ -1,13 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // prefs.coffee
3
- import {
4
- getLocalStore
5
- } from '@jdeighan/coffee-utils/browser';
6
-
7
- export var hPrefs = getLocalStore('hPrefs', {});
8
-
9
- // ---------------------------------------------------------------------------
10
- export var setPref = (key, value) => {
11
- hPrefs[key] = value;
12
- return setLocalStore('hPrefs', hPrefs);
13
- };