event-storage 1.0.0 → 1.1.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/src/util.js CHANGED
@@ -1,6 +1,3 @@
1
- import fs from 'fs';
2
- import { mkdirpSync } from 'mkdirp';
3
-
4
1
  /**
5
2
  * Assert that actual and expected match or throw an Error with the given message appended by information about expected and actual value.
6
3
  *
@@ -62,28 +59,6 @@ function hash(str) {
62
59
  return hash >>> 0; // jshint ignore:line
63
60
  }
64
61
 
65
- /**
66
- * Build a buffer containing the file magic header and a JSON stringified metadata block, padded to be a multiple of 16 bytes long.
67
- *
68
- * @param {string} magic
69
- * @param {object} metadata
70
- * @returns {Buffer} A buffer containing the header data
71
- */
72
- function buildMetadataHeader(magic, metadata) {
73
- assertEqual(magic.length, 8, 'The header magic bytes length is wrong.');
74
- let metadataString = JSON.stringify(metadata);
75
- let metadataSize = Buffer.byteLength(metadataString, 'utf8');
76
- // 8 byte MAGIC, 4 byte metadata size, 1 byte line break
77
- const pad = (16 - ((8 + 4 + metadataSize + 1) % 16)) % 16;
78
- metadataString += ' '.repeat(pad) + "\n";
79
- metadataSize += pad + 1;
80
- const metadataBuffer = Buffer.allocUnsafe(8 + 4 + metadataSize);
81
- metadataBuffer.write(magic, 0, 8, 'utf8');
82
- metadataBuffer.writeUInt32BE(metadataSize, 8);
83
- metadataBuffer.write(metadataString, 8 + 4, metadataSize, 'utf8');
84
- return metadataBuffer;
85
- }
86
-
87
62
  /**
88
63
  * Do a binary search for number in the range 1-length with values retrieved via a provided getter.
89
64
  *
@@ -137,22 +112,6 @@ function wrapAndCheck(index, length) {
137
112
  return index;
138
113
  }
139
114
 
140
- /**
141
- * Ensure that the given directory exists.
142
- * @param {string} dirName
143
- * @return {boolean} true if the directory existed already
144
- */
145
- function ensureDirectory(dirName) {
146
- if (!fs.existsSync(dirName)) {
147
- try {
148
- mkdirpSync(dirName);
149
- } catch (e) {
150
- }
151
- return false;
152
- }
153
- return true;
154
- }
155
-
156
115
  /**
157
116
  * Perform a k-way merge over multiple streams, invoking a callback for each item in ascending key order.
158
117
  * Each stream object is mutated in place by the `advance` function.
@@ -179,40 +138,30 @@ function kWayMerge(streams, getKey, advance, visit) {
179
138
  }
180
139
 
181
140
  /**
182
- * Scan a directory for files whose names match a regex pattern, calling a callback for each match.
183
- * The `onEach` callback receives the first capturing group of the match (`match[1]`), or the full
184
- * match (`match[0]`) when no capturing group is defined in the pattern.
141
+ * Read a scalar value at a dot-notation path from an object.
142
+ * Returns `undefined` if any path segment is absent or an intermediate value is not an object.
185
143
  *
186
- * @param {string} directory The directory to scan.
187
- * @param {RegExp} regexPattern The pattern to match file names against.
188
- * @param {function(string)} onEach Called with the first capturing group (or full match) for each matching file name.
189
- * @param {function(Error?)} onDone Called when the scan is complete, or with an error if one occurred.
144
+ * @param {object} obj
145
+ * @param {string} dotPath Dot-separated property path, e.g. `'payload.type'`.
146
+ * @returns {*}
190
147
  */
191
- function scanForFiles(directory, regexPattern, onEach, onDone) {
192
- fs.readdir(directory, (err, files) => {
193
- if (err) {
194
- return onDone(err);
195
- }
196
- let match;
197
- for (let file of files) {
198
- if ((match = file.match(regexPattern)) !== null) {
199
- onEach(match[1] !== undefined ? match[1] : match[0]);
200
- }
201
- }
202
- onDone(null);
203
- });
148
+ function getPropertyAtPath(obj, dotPath) {
149
+ let current = obj;
150
+ const parts = dotPath.split('.');
151
+ for (const part of parts) {
152
+ if (current == null || typeof current !== 'object') return undefined;
153
+ current = current[part];
154
+ }
155
+ return current;
204
156
  }
205
157
 
206
-
207
158
  export {
208
159
  assert,
209
160
  assertEqual,
210
161
  hash,
211
162
  wrapAndCheck,
212
163
  binarySearch,
213
- buildMetadataHeader,
214
164
  alignTo,
215
- ensureDirectory,
216
- scanForFiles,
217
- kWayMerge
165
+ kWayMerge,
166
+ getPropertyAtPath
218
167
  };