@cacheable/utils 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/README.md CHANGED
@@ -4,8 +4,8 @@
4
4
 
5
5
  [![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)
6
6
  [![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
7
- [![npm](https://img.shields.io/npm/dm/cacheable.svg)](https://www.npmjs.com/package/cacheable)
8
- [![npm](https://img.shields.io/npm/v/cacheable)](https://www.npmjs.com/package/cacheable)
7
+ [![npm](https://img.shields.io/npm/dm/@cacheable/utils.svg)](https://www.npmjs.com/package/@cacheable/utils)
8
+ [![npm](https://img.shields.io/npm/v/@cacheable/utils)](https://www.npmjs.com/package/@cacheable/utils)
9
9
  [![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
10
10
 
11
11
  `@cacheable/utils` is a collecton of utility functions, helpers, and types for `cacheable` and other caching libraries. It provides a robust set of features to enhance caching capabilities, including:
@@ -108,11 +108,10 @@ If you want to get a number hash you can use the `hashToNumber` function:
108
108
  ```typescript
109
109
  import { hash, hashToNumber } from '@cacheable/utils';
110
110
 
111
- const hashValue = hash({foo: 'bar'}, HashAlgorithm.DJB2);
112
111
  const min = 0;
113
112
  const max = 10;
114
113
 
115
- const result = hashToNumber(hashValue, min, max);
114
+ const result = hashToNumber({foo: 'bar'}, min, max, HashAlgorithm.DJB2);
116
115
  console.log(result); // A number between 0 and 10 based on the hash value
117
116
  ```
118
117
 
package/dist/index.cjs CHANGED
@@ -120,10 +120,18 @@ function hash(object, algorithm = "sha256" /* SHA256 */) {
120
120
  hasher.update(objectString);
121
121
  return hasher.digest("hex");
122
122
  }
123
- function hashToNumber(hash2, min = 0, max = 10) {
124
- const hashNumber = Number.parseInt(hash2, 16);
123
+ function hashToNumber(object, min = 0, max = 10, algorithm = "sha256" /* SHA256 */) {
124
+ const hashResult = hash(object, algorithm);
125
+ const hashNumber = Number.parseInt(hashResult, 16);
125
126
  const range = max - min + 1;
126
- return min + hashNumber % range;
127
+ const result = min + hashNumber % range;
128
+ if (result < min) {
129
+ return min;
130
+ }
131
+ if (result > max) {
132
+ return max;
133
+ }
134
+ return result;
127
135
  }
128
136
  function djb2Hash(string_) {
129
137
  let hash2 = 5381;
package/dist/index.d.cts CHANGED
@@ -59,7 +59,7 @@ declare enum HashAlgorithm {
59
59
  * @returns {string} The hash of the object
60
60
  */
61
61
  declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
- declare function hashToNumber(hash: string, min?: number, max?: number): number;
62
+ declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
63
63
 
64
64
  /**
65
65
  * Converts a shorthand time string or number into milliseconds.
package/dist/index.d.ts CHANGED
@@ -59,7 +59,7 @@ declare enum HashAlgorithm {
59
59
  * @returns {string} The hash of the object
60
60
  */
61
61
  declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
- declare function hashToNumber(hash: string, min?: number, max?: number): number;
62
+ declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
63
63
 
64
64
  /**
65
65
  * Converts a shorthand time string or number into milliseconds.
package/dist/index.js CHANGED
@@ -74,10 +74,18 @@ function hash(object, algorithm = "sha256" /* SHA256 */) {
74
74
  hasher.update(objectString);
75
75
  return hasher.digest("hex");
76
76
  }
77
- function hashToNumber(hash2, min = 0, max = 10) {
78
- const hashNumber = Number.parseInt(hash2, 16);
77
+ function hashToNumber(object, min = 0, max = 10, algorithm = "sha256" /* SHA256 */) {
78
+ const hashResult = hash(object, algorithm);
79
+ const hashNumber = Number.parseInt(hashResult, 16);
79
80
  const range = max - min + 1;
80
- return min + hashNumber % range;
81
+ const result = min + hashNumber % range;
82
+ if (result < min) {
83
+ return min;
84
+ }
85
+ if (result > max) {
86
+ return max;
87
+ }
88
+ return result;
81
89
  }
82
90
  function djb2Hash(string_) {
83
91
  let hash2 = 5381;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/utils",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Cacheable Utilities for Caching Libraries",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",