@mblaney/holster 1.0.7 → 1.0.8

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/src/utils.js CHANGED
@@ -1,18 +1,25 @@
1
1
  export const num = {
2
- is: n =>
3
- !(n instanceof Array) &&
4
- (n - parseFloat(n) + 1 >= 0 || Infinity === n || -Infinity === n),
2
+ is: n => {
3
+ if (n instanceof Array) return false
4
+
5
+ if (typeof n === "number") return !isNaN(n)
6
+
7
+ if (typeof n === "string") {
8
+ const parsed = parseFloat(n)
9
+ return !isNaN(parsed) && isFinite(parsed)
10
+ }
11
+
12
+ return false
13
+ },
5
14
  }
6
15
 
7
16
  export const obj = {
8
17
  is: o => {
9
- if (!o) return false
18
+ if (!o || typeof o !== "object") return false
10
19
 
11
- return (
12
- (o instanceof Object && o.constructor === Object) ||
13
- Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] ===
14
- "Object"
15
- )
20
+ const toString = Object.prototype.toString.call(o)
21
+ const match = toString.match(/^\[object (\w+)\]$/)
22
+ return o.constructor === Object || (match && match[1] === "Object")
16
23
  },
17
24
  map: (list, cb, o) => {
18
25
  var keys = Object.keys(list)
@@ -74,11 +81,13 @@ export const userPublicKey = "_holster_user_public_key"
74
81
  // graph converts objects to graph format with updated states,
75
82
  // with optional meta data to verify signed data.
76
83
  export const graph = (soul, data, sig, pub) => {
84
+ const timestamp = Date.now()
77
85
  const g = {[soul]: {_: {"#": soul, ">": {}}}}
86
+
78
87
  for (const [key, value] of Object.entries(data)) {
79
- if (key !== "_" && key != userPublicKey && key != userSignature) {
88
+ if (key !== "_" && key !== userPublicKey && key !== userSignature) {
80
89
  g[soul][key] = value
81
- g[soul]._[">"][key] = Date.now()
90
+ g[soul]._[">"][key] = timestamp
82
91
  }
83
92
  }
84
93
  // If a signature and public key are provided they also need to be stored on
@@ -87,9 +96,9 @@ export const graph = (soul, data, sig, pub) => {
87
96
  // broadcast the data as a put, which other devices will need to verify.
88
97
  if (sig && pub) {
89
98
  g[soul][userSignature] = sig
90
- g[soul]._[">"][userSignature] = Date.now()
99
+ g[soul]._[">"][userSignature] = timestamp
91
100
  g[soul][userPublicKey] = pub
92
- g[soul]._[">"][userPublicKey] = Date.now()
101
+ g[soul]._[">"][userPublicKey] = timestamp
93
102
  }
94
103
  return g
95
104
  }
@@ -121,7 +130,7 @@ export const match = (lex, key) => {
121
130
  export const text = {
122
131
  random: length => {
123
132
  var s = ""
124
- const c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz"
133
+ const c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
125
134
  if (!length) length = 24
126
135
  for (let i = 0; i < length; i++) {
127
136
  s += c.charAt(Math.floor(Math.random() * c.length))
package/src/wire.js CHANGED
@@ -164,7 +164,13 @@ const Wire = opt => {
164
164
  // across the network.
165
165
  const update = await Ham.mix(data, graph, opt.secure, listen)
166
166
  const none = Object.keys(update.now).length === 0
167
- if (none || !(await check(update.now, send, cb))) return
167
+ if (none) {
168
+ // Nothing to do.
169
+ cb(null)
170
+ return
171
+ }
172
+
173
+ if (!(await check(update.now, send, cb))) return
168
174
 
169
175
  store.put(update.now, cb)
170
176
  // Also put data on the wire spec.