@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 +3 -4
- package/dist/index.cjs +11 -3
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
6
6
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
7
|
-
[](https://www.npmjs.com/package/@cacheable/utils)
|
|
8
|
+
[](https://www.npmjs.com/package/@cacheable/utils)
|
|
9
9
|
[](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(
|
|
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(
|
|
124
|
-
const
|
|
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
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
78
|
-
const
|
|
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
|
-
|
|
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;
|