Skip to content

Commit c43aae3

Browse files
committed
[docs] Hash examples
1 parent 498924c commit c43aae3

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/@types/common/docs.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@
286286
* for most purposes.
287287
* @param string The string to hash.
288288
* @returns A hash of the string.
289+
* @example
290+
* This example gets the hash of two similar strings.
291+
* ```js
292+
* import {getHash} from 'tinybase';
293+
* console.log(getHash('Hello, world!'));
294+
* // -> 3985698964
295+
* console.log(getHash('Hello, world?'));
296+
* // -> 3549480870
297+
* ```
289298
* @category Hash
290299
* @since v6.2.0
291300
*/
@@ -302,6 +311,21 @@
302311
* @param hash2 A second hash to add or remove from the first.
303312
* @returns The resulting hash of the two hashes added to or removed from each
304313
* other.
314+
* @example
315+
* This example adds two hashes together.
316+
* ```js
317+
* import {addOrRemoveHash} from 'tinybase';
318+
*
319+
* const hash1 = 123456789;
320+
* const hash2 = 987654321;
321+
* console.log(addOrRemoveHash(hash1, hash2));
322+
* // -> 1032168868
323+
*
324+
* console.log(addOrRemoveHash(1032168868, hash1));
325+
* // -> 987654321
326+
* console.log(addOrRemoveHash(1032168868, hash2));
327+
* // -> 123456789
328+
* ```
305329
* @category Hash
306330
* @since v6.2.0
307331
*/
@@ -313,6 +337,19 @@
313337
* @param tableHashes An object containing each Table Id and its hash.
314338
* @param tablesHlc The top-level HLC of the tabular part of the Store.
315339
* @returns A hash of the Tables.
340+
* @example
341+
* This example gets the hash of the tabular part of a Store.
342+
* ```js
343+
* import {getTablesHash} from 'tinybase';
344+
*
345+
* const tableHashes = {
346+
* pets: 2544679909, // hash of its contents
347+
* };
348+
* const tablesHlc = '03E3B------mmxrx';
349+
*
350+
* console.log(getTablesHash(tableHashes, tablesHlc));
351+
* // -> 1835775460
352+
* ```
316353
* @category Hash
317354
* @since v6.2.0
318355
*/
@@ -324,6 +361,17 @@
324361
* @param tableId The Id of the Table.
325362
* @param tableHash The hash of the Table.
326363
* @returns A hash of the Table based on its content and Id.
364+
* @example
365+
* This example gets the hash of a Table and its Id.
366+
* ```js
367+
* import {getTableInTablesHash} from 'tinybase';
368+
*
369+
* const tableId = 'pets';
370+
* const tableHash = 2544679909; // hash of its contents
371+
*
372+
* console.log(getTableInTablesHash(tableId, tableHash));
373+
* // -> 2778789628
374+
* ```
327375
* @category Hash
328376
* @since v6.2.0
329377
*/
@@ -335,6 +383,19 @@
335383
* @param rowHashes An object containing each Row Id and its hash.
336384
* @param tableHlc The HLC of the Table.
337385
* @returns A hash of the Table.
386+
* @example
387+
* This example gets the hash of a Table.
388+
* ```js
389+
* import {getTableHash} from 'tinybase';
390+
*
391+
* const rowHashes = {
392+
* fido: 703486916, // hash of its contents
393+
* };
394+
* const tableHlc = '03E3B------mmxrx';
395+
*
396+
* console.log(getTableHash(rowHashes, tableHlc));
397+
* // -> 2544679909
398+
* ```
338399
* @category Hash
339400
* @since v6.2.0
340401
*/
@@ -346,6 +407,17 @@
346407
* @param rowId The Id of the Row.
347408
* @param rowHash The hash of the Row.
348409
* @returns A hash of the Row based on its content and Id.
410+
* @example
411+
* This example gets the hash of a Row and its Id.
412+
* ```js
413+
* import {getRowInTableHash} from 'tinybase';
414+
*
415+
* const rowId = 'fido';
416+
* const rowHash = 703486916; // hash of its contents
417+
*
418+
* console.log(getRowInTableHash(rowId, rowHash));
419+
* // -> 1600649469
420+
* ```
349421
* @category Hash
350422
* @since v6.2.0
351423
*/
@@ -357,6 +429,19 @@
357429
* @param cellHashes An object containing each Cell Id and its hash.
358430
* @param rowHlc The HLC of the Row.
359431
* @returns A hash of the Row.
432+
* @example
433+
* This example gets the hash of a Row.
434+
* ```js
435+
* import {getRowHash} from 'tinybase';
436+
*
437+
* const cellHashes = {
438+
* 'species': 3002200796, // hash of 'dog' and '03E3B------mmxrx'
439+
* };
440+
* const rowHlc = '03E3B------mmxrx';
441+
*
442+
* console.log(getRowHash(cellHashes, rowHlc));
443+
* // -> 703486916
444+
* ```
360445
* @category Hash
361446
* @since v6.2.0
362447
*/
@@ -368,6 +453,17 @@
368453
* @param cellId The Id of the Cell.
369454
* @param cellHash The hash of the Cell.
370455
* @returns A hash of the Cell based on its content and Id.
456+
* @example
457+
* This example gets the hash of a Cell and its Id.
458+
* ```js
459+
* import {getCellInRowHash} from 'tinybase';
460+
*
461+
* const cellId = 'species';
462+
* const cellHash = '3002200796'; // hash of 'dog' and '03E3B------mmxrx'
463+
*
464+
* console.log(getCellInRowHash(cellId, cellHash));
465+
* // -> 3777304796
466+
* ```
371467
* @category Hash
372468
* @since v6.2.0
373469
*/
@@ -379,6 +475,17 @@
379475
* @param cell The Cell's value (or `undefined`).
380476
* @param cellHlc The HLC of the Cell.
381477
* @returns A hash of the Cell.
478+
* @example
479+
* This example gets the hash of a Cell and its HLC.
480+
* ```js
481+
* import {getCellHash} from 'tinybase';
482+
*
483+
* const cell = 'dog';
484+
* const cellHlc = '03E3B------mmxrx';
485+
*
486+
* console.log(getCellHash(cell, cellHlc));
487+
* // -> 3002200796
488+
* ```
382489
* @category Hash
383490
* @since v6.2.0
384491
*/
@@ -390,6 +497,19 @@
390497
* @param valueHashes An object containing each Value Id and its hash.
391498
* @param valuesHlc The HLC of the Values.
392499
* @returns A hash of the Values.
500+
* @example
501+
* This example gets the hash of a Values object.
502+
* ```js
503+
* import {getValuesHash} from 'tinybase';
504+
*
505+
* const valueHashes = {
506+
* meaningOfLife: 312420374, // hash of 42 and '03E3B------mmxrx'
507+
* };
508+
* const valuesHlc = '03E3B------mmxrx';
509+
*
510+
* console.log(getValuesHash(valueHashes, valuesHlc));
511+
* // -> 3680840875
512+
* ```
393513
* @category Hash
394514
* @since v6.2.0
395515
*/
@@ -401,6 +521,17 @@
401521
* @param valueId The Id of the Value.
402522
* @param valueHash The hash of the Value.
403523
* @returns A hash of the Value based on its content and Id.
524+
* @example
525+
* This example gets the hash of a Value and its Id.
526+
* ```js
527+
* import {getValueInValuesHash} from 'tinybase';
528+
*
529+
* const valueId = 'meaningOfLife';
530+
* const valueHash = 312420374; // hash of 42 and '03E3B------mmxrx'
531+
*
532+
* console.log(getValueInValuesHash(valueId, valueHash));
533+
* // -> 330198963
534+
* ```
404535
* @category Hash
405536
* @since v6.2.0
406537
*/
@@ -412,6 +543,17 @@
412543
* @param value The Value (or `undefined`).
413544
* @param valueHlc The HLC of the Value.
414545
* @returns A hash of the Value.
546+
* @example
547+
* This example gets the hash of a Value and its HLC.
548+
* ```js
549+
* import {getValueHash} from 'tinybase';
550+
*
551+
* const value = 42;
552+
* const valueHlc = '03E3B------mmxrx';
553+
*
554+
* console.log(getValueHash(value, valueHlc));
555+
* // -> 312420374
556+
* ```
415557
* @category Hash
416558
* @since v6.2.0
417559
*/

src/common/hash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const getHash: typeof getHashDecl = (string: string): Hash => {
3434
export const addOrRemoveHash: typeof addOrRemoveHashDecl = (
3535
hash1: Hash,
3636
hash2: Hash,
37-
): Hash => hash1 ^ hash2;
37+
): Hash => (hash1 ^ hash2) >>> 0;
3838

3939
export const getValuesHash: typeof getValuesHashDecl = (
4040
valueHashes: {[valueId: Id]: Hash},

0 commit comments

Comments
 (0)