react-native-onyx 1.0.131 → 2.0.2
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/API.md +83 -22
- package/README.md +3 -5
- package/dist/DevTools.d.ts +23 -0
- package/{lib → dist}/DevTools.js +16 -18
- package/dist/Logger.js +32 -0
- package/dist/MDTable.d.ts +36 -0
- package/{lib → dist}/MDTable.js +12 -17
- package/{lib → dist}/Onyx.js +278 -477
- package/dist/OnyxCache.d.ts +121 -0
- package/{lib → dist}/OnyxCache.js +16 -53
- package/{lib/Str.js → dist/Str.d.ts} +2 -11
- package/dist/Str.js +31 -0
- package/dist/SyncQueue.d.ts +32 -0
- package/{lib → dist}/SyncQueue.js +9 -11
- package/dist/batch.d.ts +2 -0
- package/dist/batch.js +4 -0
- package/dist/batch.native.d.ts +2 -0
- package/dist/batch.native.js +4 -0
- package/dist/compose.d.ts +19 -0
- package/{lib → dist}/compose.js +5 -8
- package/dist/createDeferredTask.d.ts +12 -0
- package/{lib → dist}/createDeferredTask.js +4 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +10 -0
- package/dist/metrics/PerformanceUtils.d.ts +14 -0
- package/{lib → dist}/metrics/PerformanceUtils.js +16 -16
- package/dist/metrics/index.d.ts +4 -0
- package/dist/metrics/index.js +14 -0
- package/dist/metrics/index.native.d.ts +43 -0
- package/{lib → dist}/metrics/index.native.js +80 -102
- package/{lib/storage/NativeStorage.js → dist/storage/NativeStorage.d.ts} +1 -2
- package/dist/storage/NativeStorage.js +7 -0
- package/dist/storage/WebStorage.d.ts +19 -0
- package/{lib → dist}/storage/WebStorage.js +24 -34
- package/dist/storage/__mocks__/index.d.ts +23 -0
- package/{lib → dist}/storage/__mocks__/index.js +17 -19
- package/{lib/storage/index.web.js → dist/storage/index.d.ts} +1 -2
- package/dist/storage/index.js +7 -0
- package/{lib/storage/index.native.js → dist/storage/index.native.d.ts} +1 -2
- package/dist/storage/index.native.js +7 -0
- package/dist/storage/providers/IDBKeyVal.d.ts +26 -0
- package/{lib → dist}/storage/providers/IDBKeyVal.js +38 -52
- package/dist/storage/providers/SQLiteStorage.d.ts +52 -0
- package/{lib → dist}/storage/providers/SQLiteStorage.js +27 -42
- package/{lib → dist}/utils.js +14 -27
- package/{lib → dist}/withOnyx.js +122 -159
- package/package.json +23 -54
- package/dist/web.development.js +0 -4308
- package/dist/web.development.js.map +0 -1
- package/dist/web.min.js +0 -2
- package/dist/web.min.js.map +0 -1
- package/lib/Logger.js +0 -31
- package/lib/batch.js +0 -3
- package/lib/batch.native.js +0 -3
- package/lib/index.d.ts +0 -6
- package/lib/index.js +0 -5
- package/lib/metrics/index.web.js +0 -10
- package/native.js +0 -11
- package/web.js +0 -12
- /package/{lib → dist}/Logger.d.ts +0 -0
- /package/{lib → dist}/Onyx.d.ts +0 -0
- /package/{lib → dist}/types.d.ts +0 -0
- /package/{lib → dist}/utils.d.ts +0 -0
- /package/{lib → dist}/withOnyx.d.ts +0 -0
package/API.md
CHANGED
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
## Functions
|
|
6
6
|
|
|
7
7
|
<dl>
|
|
8
|
+
<dt><a href="#sendActionToDevTools">sendActionToDevTools(method, key, value, mergedValue)</a></dt>
|
|
9
|
+
<dd><p>Sends an action to DevTools extension</p>
|
|
10
|
+
</dd>
|
|
11
|
+
<dt><a href="#maybeFlushBatchUpdates">maybeFlushBatchUpdates()</a> ⇒ <code>Promise</code></dt>
|
|
12
|
+
<dd><p>We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other.
|
|
13
|
+
This happens for example in the Onyx.update function, where we process API responses that might contain a lot of
|
|
14
|
+
update operations. Instead of calling the subscribers for each update operation, we batch them together which will
|
|
15
|
+
cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.</p>
|
|
16
|
+
</dd>
|
|
8
17
|
<dt><a href="#getSubsetOfData">getSubsetOfData(sourceData, selector, [withOnyxInstanceState])</a> ⇒ <code>Mixed</code></dt>
|
|
9
18
|
<dd><p>Uses a selector function to return a simplified version of sourceData</p>
|
|
10
19
|
</dd>
|
|
@@ -25,12 +34,6 @@ If the requested key is a collection, it will return an object with all the coll
|
|
|
25
34
|
<dt><a href="#disconnect">disconnect(connectionID, [keyToRemoveFromEvictionBlocklist])</a></dt>
|
|
26
35
|
<dd><p>Remove the listener for a react component</p>
|
|
27
36
|
</dd>
|
|
28
|
-
<dt><a href="#maybeFlushBatchUpdates">maybeFlushBatchUpdates()</a> ⇒ <code>Promise</code></dt>
|
|
29
|
-
<dd><p>We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other.
|
|
30
|
-
This happens for example in the Onyx.update function, where we process API responses that might contain a lot of
|
|
31
|
-
update operations. Instead of calling the subscribers for each update operation, we batch them together which will
|
|
32
|
-
cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.</p>
|
|
33
|
-
</dd>
|
|
34
37
|
<dt><a href="#scheduleSubscriberUpdate">scheduleSubscriberUpdate(key, value, [canUpdateSubscriber])</a> ⇒ <code>Promise</code></dt>
|
|
35
38
|
<dd><p>Schedules an update that will be appended to the macro task queue (so it doesn't update the subscribers immediately).</p>
|
|
36
39
|
</dd>
|
|
@@ -39,11 +42,15 @@ cause react to schedule the updates at once instead of after each other. This is
|
|
|
39
42
|
so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
|
|
40
43
|
subscriber callbacks receive the data in a different format than they normally expect and it breaks code.</p>
|
|
41
44
|
</dd>
|
|
42
|
-
<dt><a href="#broadcastUpdate">broadcastUpdate(key, value, hasChanged,
|
|
45
|
+
<dt><a href="#broadcastUpdate">broadcastUpdate(key, value, method, hasChanged, wasRemoved)</a> ⇒ <code>Promise</code></dt>
|
|
43
46
|
<dd><p>Notifys subscribers and writes current value to cache</p>
|
|
44
47
|
</dd>
|
|
45
48
|
<dt><a href="#hasPendingMergeForKey">hasPendingMergeForKey(key)</a> ⇒ <code>Boolean</code></dt>
|
|
46
49
|
<dd></dd>
|
|
50
|
+
<dt><a href="#removeNullValues">removeNullValues(key, value)</a> ⇒ <code>Mixed</code></dt>
|
|
51
|
+
<dd><p>Removes a key from storage if the value is null.
|
|
52
|
+
Otherwise removes all nested null values in objects and returns the object</p>
|
|
53
|
+
</dd>
|
|
47
54
|
<dt><a href="#set">set(key, value)</a> ⇒ <code>Promise</code></dt>
|
|
48
55
|
<dd><p>Write a value to our store with the given key</p>
|
|
49
56
|
</dd>
|
|
@@ -83,11 +90,41 @@ value will be saved to storage after the default value.</p>
|
|
|
83
90
|
<dt><a href="#setMemoryOnlyKeys">setMemoryOnlyKeys(keyList)</a></dt>
|
|
84
91
|
<dd><p>When set these keys will not be persisted to storage</p>
|
|
85
92
|
</dd>
|
|
93
|
+
<dt><a href="#onClear">onClear(callback)</a></dt>
|
|
94
|
+
<dd><p>Sets the callback to be called when the clear finishes executing.</p>
|
|
95
|
+
</dd>
|
|
96
|
+
<dt><a href="#subscribeToEvents">subscribeToEvents()</a></dt>
|
|
97
|
+
<dd><p>Subscribes to the Broadcast channel and executes actions based on the
|
|
98
|
+
types of events.</p>
|
|
99
|
+
</dd>
|
|
86
100
|
<dt><a href="#init">init([options])</a></dt>
|
|
87
101
|
<dd><p>Initialize the store with actions and listening for storage events</p>
|
|
88
102
|
</dd>
|
|
89
103
|
</dl>
|
|
90
104
|
|
|
105
|
+
<a name="sendActionToDevTools"></a>
|
|
106
|
+
|
|
107
|
+
## sendActionToDevTools(method, key, value, mergedValue)
|
|
108
|
+
Sends an action to DevTools extension
|
|
109
|
+
|
|
110
|
+
**Kind**: global function
|
|
111
|
+
|
|
112
|
+
| Param | Type | Description |
|
|
113
|
+
| --- | --- | --- |
|
|
114
|
+
| method | <code>string</code> | Onyx method from METHOD |
|
|
115
|
+
| key | <code>string</code> | Onyx key that was changed |
|
|
116
|
+
| value | <code>any</code> | contains the change that was made by the method |
|
|
117
|
+
| mergedValue | <code>any</code> | (optional) value that was written in the storage after a merge method was executed. |
|
|
118
|
+
|
|
119
|
+
<a name="maybeFlushBatchUpdates"></a>
|
|
120
|
+
|
|
121
|
+
## maybeFlushBatchUpdates() ⇒ <code>Promise</code>
|
|
122
|
+
We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other.
|
|
123
|
+
This happens for example in the Onyx.update function, where we process API responses that might contain a lot of
|
|
124
|
+
update operations. Instead of calling the subscribers for each update operation, we batch them together which will
|
|
125
|
+
cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.
|
|
126
|
+
|
|
127
|
+
**Kind**: global function
|
|
91
128
|
<a name="getSubsetOfData"></a>
|
|
92
129
|
|
|
93
130
|
## getSubsetOfData(sourceData, selector, [withOnyxInstanceState]) ⇒ <code>Mixed</code>
|
|
@@ -182,15 +219,6 @@ Remove the listener for a react component
|
|
|
182
219
|
```js
|
|
183
220
|
Onyx.disconnect(connectionID);
|
|
184
221
|
```
|
|
185
|
-
<a name="maybeFlushBatchUpdates"></a>
|
|
186
|
-
|
|
187
|
-
## maybeFlushBatchUpdates() ⇒ <code>Promise</code>
|
|
188
|
-
We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other.
|
|
189
|
-
This happens for example in the Onyx.update function, where we process API responses that might contain a lot of
|
|
190
|
-
update operations. Instead of calling the subscribers for each update operation, we batch them together which will
|
|
191
|
-
cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.
|
|
192
|
-
|
|
193
|
-
**Kind**: global function
|
|
194
222
|
<a name="scheduleSubscriberUpdate"></a>
|
|
195
223
|
|
|
196
224
|
## scheduleSubscriberUpdate(key, value, [canUpdateSubscriber]) ⇒ <code>Promise</code>
|
|
@@ -224,26 +252,41 @@ subscriber callbacks receive the data in a different format than they normally e
|
|
|
224
252
|
|
|
225
253
|
<a name="broadcastUpdate"></a>
|
|
226
254
|
|
|
227
|
-
## broadcastUpdate(key, value, hasChanged,
|
|
255
|
+
## broadcastUpdate(key, value, method, hasChanged, wasRemoved) ⇒ <code>Promise</code>
|
|
228
256
|
Notifys subscribers and writes current value to cache
|
|
229
257
|
|
|
230
258
|
**Kind**: global function
|
|
231
259
|
|
|
260
|
+
| Param | Type | Default |
|
|
261
|
+
| --- | --- | --- |
|
|
262
|
+
| key | <code>String</code> | |
|
|
263
|
+
| value | <code>\*</code> | |
|
|
264
|
+
| method | <code>String</code> | |
|
|
265
|
+
| hasChanged | <code>Boolean</code> | |
|
|
266
|
+
| wasRemoved | <code>Boolean</code> | <code>false</code> |
|
|
267
|
+
|
|
268
|
+
<a name="hasPendingMergeForKey"></a>
|
|
269
|
+
|
|
270
|
+
## hasPendingMergeForKey(key) ⇒ <code>Boolean</code>
|
|
271
|
+
**Kind**: global function
|
|
272
|
+
|
|
232
273
|
| Param | Type |
|
|
233
274
|
| --- | --- |
|
|
234
275
|
| key | <code>String</code> |
|
|
235
|
-
| value | <code>\*</code> |
|
|
236
|
-
| hasChanged | <code>Boolean</code> |
|
|
237
|
-
| method | <code>String</code> |
|
|
238
276
|
|
|
239
|
-
<a name="
|
|
277
|
+
<a name="removeNullValues"></a>
|
|
278
|
+
|
|
279
|
+
## removeNullValues(key, value) ⇒ <code>Mixed</code>
|
|
280
|
+
Removes a key from storage if the value is null.
|
|
281
|
+
Otherwise removes all nested null values in objects and returns the object
|
|
240
282
|
|
|
241
|
-
## hasPendingMergeForKey(key) ⇒ <code>Boolean</code>
|
|
242
283
|
**Kind**: global function
|
|
284
|
+
**Returns**: <code>Mixed</code> - The value without null values and a boolean "wasRemoved", which indicates if the key got removed completely
|
|
243
285
|
|
|
244
286
|
| Param | Type |
|
|
245
287
|
| --- | --- |
|
|
246
288
|
| key | <code>String</code> |
|
|
289
|
+
| value | <code>Mixed</code> |
|
|
247
290
|
|
|
248
291
|
<a name="set"></a>
|
|
249
292
|
|
|
@@ -367,6 +410,24 @@ When set these keys will not be persisted to storage
|
|
|
367
410
|
| --- | --- |
|
|
368
411
|
| keyList | <code>Array.<string></code> |
|
|
369
412
|
|
|
413
|
+
<a name="onClear"></a>
|
|
414
|
+
|
|
415
|
+
## onClear(callback)
|
|
416
|
+
Sets the callback to be called when the clear finishes executing.
|
|
417
|
+
|
|
418
|
+
**Kind**: global function
|
|
419
|
+
|
|
420
|
+
| Param | Type |
|
|
421
|
+
| --- | --- |
|
|
422
|
+
| callback | <code>function</code> |
|
|
423
|
+
|
|
424
|
+
<a name="subscribeToEvents"></a>
|
|
425
|
+
|
|
426
|
+
## subscribeToEvents()
|
|
427
|
+
Subscribes to the Broadcast channel and executes actions based on the
|
|
428
|
+
types of events.
|
|
429
|
+
|
|
430
|
+
**Kind**: global function
|
|
370
431
|
<a name="init"></a>
|
|
371
432
|
|
|
372
433
|
## init([options])
|
package/README.md
CHANGED
|
@@ -292,13 +292,13 @@ function signOut() {
|
|
|
292
292
|
`Onyx.get`, `Onyx.set`, and the rest of the API accesses the underlying storage
|
|
293
293
|
differently depending on the platform
|
|
294
294
|
|
|
295
|
-
Under the hood storage access calls are delegated to a [`StorageProvider`](lib/storage/index.
|
|
295
|
+
Under the hood storage access calls are delegated to a [`StorageProvider`](lib/storage/index.js)
|
|
296
296
|
Some platforms (like web and desktop) might use the same storage provider
|
|
297
297
|
|
|
298
298
|
If a platform needs to use a separate library (like using MMVK for react-native) it should be added in the following way:
|
|
299
299
|
1. Create a `StorageProvider.js` at [lib/storage/providers](lib/storage/providers)
|
|
300
300
|
Reference an existing [StorageProvider](lib/storage/providers/AsyncStorage.js) for the interface that has to be implemented
|
|
301
|
-
2. Update the factory at [lib/storage/index.
|
|
301
|
+
2. Update the factory at [lib/storage/index.js](lib/storage/index.js) and [lib/storage/index.native.js](lib/storage/index.native.js) to return the newly created Provider for the desired Platform(s)
|
|
302
302
|
|
|
303
303
|
# API Reference
|
|
304
304
|
|
|
@@ -433,9 +433,7 @@ The action logs use this naming convention:
|
|
|
433
433
|
`react-native` bundles source using the `metro` bundler. `metro` does not follow symlinks, so we can't use `npm link` to
|
|
434
434
|
link a local version of Onyx during development
|
|
435
435
|
|
|
436
|
-
To quickly test small changes you can directly go to `node_modules/react-native-onyx` in the parent project and
|
|
437
|
-
- tweak original source if you're testing over a react-native project
|
|
438
|
-
- tweak `dist/web.development.js` for non react-native-projects
|
|
436
|
+
To quickly test small changes you can directly go to `node_modules/react-native-onyx` in the parent project and tweak original source code.
|
|
439
437
|
|
|
440
438
|
To continuously work on Onyx we have to set up a task that copies content to parent project's `node_modules/react-native-onyx`:
|
|
441
439
|
1. Work on Onyx feature or a fix
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare const _default: DevTools;
|
|
2
|
+
export default _default;
|
|
3
|
+
declare class DevTools {
|
|
4
|
+
remoteDev: any;
|
|
5
|
+
state: {};
|
|
6
|
+
defaultState: {};
|
|
7
|
+
connectViaExtension(options: any): any;
|
|
8
|
+
/**
|
|
9
|
+
* Registers an action that updated the current state of the storage
|
|
10
|
+
*
|
|
11
|
+
* @param {string} type - name of the action
|
|
12
|
+
* @param {any} payload - data written to the storage
|
|
13
|
+
* @param {object} stateChanges - partial state that got updated after the changes
|
|
14
|
+
*/
|
|
15
|
+
registerAction(type: string, payload?: any, stateChanges?: object): void;
|
|
16
|
+
initState(initialState?: {}): void;
|
|
17
|
+
/**
|
|
18
|
+
* This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
|
|
19
|
+
*
|
|
20
|
+
* @param {string[]} keysToPreserve
|
|
21
|
+
*/
|
|
22
|
+
clearState(keysToPreserve?: string[]): void;
|
|
23
|
+
}
|
package/{lib → dist}/DevTools.js
RENAMED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const underscore_1 = __importDefault(require("underscore"));
|
|
3
7
|
const ERROR_LABEL = 'Onyx DevTools - Error: ';
|
|
4
|
-
|
|
5
8
|
/* eslint-disable no-underscore-dangle */
|
|
6
9
|
class DevTools {
|
|
7
10
|
constructor() {
|
|
@@ -9,18 +12,17 @@ class DevTools {
|
|
|
9
12
|
this.state = {};
|
|
10
13
|
this.defaultState = {};
|
|
11
14
|
}
|
|
12
|
-
|
|
13
15
|
connectViaExtension(options) {
|
|
14
16
|
try {
|
|
15
17
|
if ((options && options.remote) || typeof window === 'undefined' || !window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
16
18
|
return;
|
|
17
19
|
}
|
|
18
20
|
return window.__REDUX_DEVTOOLS_EXTENSION__.connect(options);
|
|
19
|
-
}
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
20
23
|
console.error(ERROR_LABEL, e);
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
|
-
|
|
24
26
|
/**
|
|
25
27
|
* Registers an action that updated the current state of the storage
|
|
26
28
|
*
|
|
@@ -33,17 +35,14 @@ class DevTools {
|
|
|
33
35
|
if (!this.remoteDev) {
|
|
34
36
|
return;
|
|
35
37
|
}
|
|
36
|
-
const newState = {
|
|
37
|
-
|
|
38
|
-
...stateChanges,
|
|
39
|
-
};
|
|
40
|
-
this.remoteDev.send({type, payload}, newState);
|
|
38
|
+
const newState = Object.assign(Object.assign({}, this.state), stateChanges);
|
|
39
|
+
this.remoteDev.send({ type, payload }, newState);
|
|
41
40
|
this.state = newState;
|
|
42
|
-
}
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
43
|
console.error(ERROR_LABEL, e);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
|
|
47
46
|
initState(initialState = {}) {
|
|
48
47
|
try {
|
|
49
48
|
if (!this.remoteDev) {
|
|
@@ -52,20 +51,19 @@ class DevTools {
|
|
|
52
51
|
this.remoteDev.init(initialState);
|
|
53
52
|
this.state = initialState;
|
|
54
53
|
this.defaultState = initialState;
|
|
55
|
-
}
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
56
|
console.error(ERROR_LABEL, e);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
|
|
60
59
|
/**
|
|
61
60
|
* This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
|
|
62
61
|
*
|
|
63
62
|
* @param {string[]} keysToPreserve
|
|
64
63
|
*/
|
|
65
64
|
clearState(keysToPreserve = []) {
|
|
66
|
-
const newState =
|
|
65
|
+
const newState = underscore_1.default.mapObject(this.state, (value, key) => (keysToPreserve.includes(key) ? value : this.defaultState[key]));
|
|
67
66
|
this.registerAction('CLEAR', undefined, newState);
|
|
68
67
|
}
|
|
69
68
|
}
|
|
70
|
-
|
|
71
|
-
export default new DevTools();
|
|
69
|
+
exports.default = new DevTools();
|
package/dist/Logger.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logAlert = exports.logInfo = exports.registerLogger = void 0;
|
|
4
|
+
// Logging callback
|
|
5
|
+
let logger = () => { };
|
|
6
|
+
/**
|
|
7
|
+
* Register the logging callback
|
|
8
|
+
*
|
|
9
|
+
* @param {Function} callback
|
|
10
|
+
*/
|
|
11
|
+
function registerLogger(callback) {
|
|
12
|
+
logger = callback;
|
|
13
|
+
}
|
|
14
|
+
exports.registerLogger = registerLogger;
|
|
15
|
+
/**
|
|
16
|
+
* Send an alert message to the logger
|
|
17
|
+
*
|
|
18
|
+
* @param {String} message
|
|
19
|
+
*/
|
|
20
|
+
function logAlert(message) {
|
|
21
|
+
logger({ message: `[Onyx] ${message}`, level: 'alert' });
|
|
22
|
+
}
|
|
23
|
+
exports.logAlert = logAlert;
|
|
24
|
+
/**
|
|
25
|
+
* Send an info message to the logger
|
|
26
|
+
*
|
|
27
|
+
* @param {String} message
|
|
28
|
+
*/
|
|
29
|
+
function logInfo(message) {
|
|
30
|
+
logger({ message: `[Onyx] ${message}`, level: 'info' });
|
|
31
|
+
}
|
|
32
|
+
exports.logInfo = logInfo;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default MDTable;
|
|
2
|
+
declare class MDTable {
|
|
3
|
+
/**
|
|
4
|
+
* Create a CSV string from the table data
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
toCSV(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Create a JSON string from the table data
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
toJSON(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Create a MD string from the table data
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
toString(): string;
|
|
18
|
+
}
|
|
19
|
+
declare namespace MDTable {
|
|
20
|
+
/**
|
|
21
|
+
* Table Factory helper
|
|
22
|
+
* @param {Object} options
|
|
23
|
+
* @param {string} [options.title] - optional title center above the table
|
|
24
|
+
* @param {string[]} options.heading - table column names
|
|
25
|
+
* @param {number[]} [options.leftAlignedCols=[]] - indexes of columns that should be left aligned
|
|
26
|
+
* Pass the columns that are non numeric here - the rest will be aligned to the right
|
|
27
|
+
* @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow`
|
|
28
|
+
* @returns {MDTable}
|
|
29
|
+
*/
|
|
30
|
+
function factory({ title, heading, leftAlignedCols, rows }: {
|
|
31
|
+
title?: string | undefined;
|
|
32
|
+
heading: string[];
|
|
33
|
+
leftAlignedCols?: number[] | undefined;
|
|
34
|
+
rows?: any[] | undefined;
|
|
35
|
+
}): MDTable;
|
|
36
|
+
}
|
package/{lib → dist}/MDTable.js
RENAMED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const ascii_table_1 = __importDefault(require("ascii-table"));
|
|
7
|
+
class MDTable extends ascii_table_1.default {
|
|
4
8
|
/**
|
|
5
9
|
* Create a CSV string from the table data
|
|
6
10
|
* @returns {string}
|
|
@@ -8,7 +12,6 @@ class MDTable extends AsciTable {
|
|
|
8
12
|
toCSV() {
|
|
9
13
|
return [this.getTitle(), this.getHeading(), ...this.getRows()].join('\n');
|
|
10
14
|
}
|
|
11
|
-
|
|
12
15
|
/**
|
|
13
16
|
* Create a JSON string from the table data
|
|
14
17
|
* @returns {string}
|
|
@@ -16,7 +19,6 @@ class MDTable extends AsciTable {
|
|
|
16
19
|
toJSON() {
|
|
17
20
|
return JSON.stringify(super.toJSON());
|
|
18
21
|
}
|
|
19
|
-
|
|
20
22
|
/**
|
|
21
23
|
* Create a MD string from the table data
|
|
22
24
|
* @returns {string}
|
|
@@ -27,20 +29,16 @@ class MDTable extends AsciTable {
|
|
|
27
29
|
const ascii = super.toString().replace(/-\|/g, () => {
|
|
28
30
|
/* we replace "----|" with "---:|" to align the data to the right in MD */
|
|
29
31
|
idx++;
|
|
30
|
-
|
|
31
32
|
if (idx < 0 || this.leftAlignedCols.includes(idx)) {
|
|
32
33
|
return '-|';
|
|
33
34
|
}
|
|
34
|
-
|
|
35
35
|
return ':|';
|
|
36
36
|
});
|
|
37
|
-
|
|
38
37
|
// strip the top and the bottom row (----) to make an MD table
|
|
39
38
|
const md = ascii.split('\n').slice(1, -1).join('\n');
|
|
40
39
|
return md;
|
|
41
40
|
}
|
|
42
41
|
}
|
|
43
|
-
|
|
44
42
|
/**
|
|
45
43
|
* Table Factory helper
|
|
46
44
|
* @param {Object} options
|
|
@@ -51,16 +49,13 @@ class MDTable extends AsciTable {
|
|
|
51
49
|
* @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow`
|
|
52
50
|
* @returns {MDTable}
|
|
53
51
|
*/
|
|
54
|
-
MDTable.factory = ({title, heading, leftAlignedCols = [], rows = []}) => {
|
|
55
|
-
const table = new MDTable({title, heading, rows});
|
|
52
|
+
MDTable.factory = ({ title, heading, leftAlignedCols = [], rows = [] }) => {
|
|
53
|
+
const table = new MDTable({ title, heading, rows });
|
|
56
54
|
table.leftAlignedCols = leftAlignedCols;
|
|
57
|
-
|
|
58
55
|
/* By default we want everything aligned to the right as most values are numbers
|
|
59
56
|
* we just override the columns that are not right aligned */
|
|
60
|
-
heading.forEach((name, idx) => table.setAlign(idx,
|
|
61
|
-
leftAlignedCols.forEach((idx) => table.setAlign(idx,
|
|
62
|
-
|
|
57
|
+
heading.forEach((name, idx) => table.setAlign(idx, ascii_table_1.default.RIGHT));
|
|
58
|
+
leftAlignedCols.forEach((idx) => table.setAlign(idx, ascii_table_1.default.LEFT));
|
|
63
59
|
return table;
|
|
64
60
|
};
|
|
65
|
-
|
|
66
|
-
export default MDTable;
|
|
61
|
+
exports.default = MDTable;
|