Refactored cache observable factory and documented utilities

This commit is contained in:
squidfunk
2020-02-19 10:18:33 +01:00
parent 57817eac99
commit 74b02ad382
5 changed files with 42 additions and 27 deletions

View File

@@ -82,7 +82,14 @@ export function truncate(value: string, n: number): string {
* Round a number for display with source facts
*
* This is a reverse engineered implementation of GitHub's weird rounding
* algorithm for stars, forks and all other numbers. Probably incorrect.
* algorithm for stars, forks and all other numbers. While all numbers below
* `1,000` are returned as-is, bigger numbers are converted to fixed numbers
* in the following way:
*
* - `1,049` => `1k`
* - `1,050` => `1,1k`
* - `1,949` => `1,9k`
* - `1,950` => `2k`
*
* @param value - Original value
*
@@ -104,13 +111,13 @@ export function round(value: number): string {
*
* @param value - Value to be hashed
*
* @return Hash
* @return Hash as 32bit integer
*/
export function hash(value: string): number {
let k = 0
let h = 0
for (let i = 0, len = value.length; i < len; i++) {
k = ((k << 5) - k) + value.charCodeAt(i)
k |= 0 // Convert to 32bit integer
h = ((h << 5) - h) + value.charCodeAt(i)
h |= 0 // Convert to 32bit integer
}
return k
return h
}