platform/packages/core/src/utils.ts
Ilya Sumbatyants b0ffc0ed46
Ranks (#653)
Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
2021-12-17 10:08:37 +01:00

92 lines
2.5 KiB
TypeScript

//
// Copyright © 2020, 2021 Anticrm Platform Contributors.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { LexoRank, LexoDecimal, LexoNumeralSystem36 } from 'lexorank'
import LexoRankBucket from 'lexorank/lib/lexoRank/lexoRankBucket'
import type { Doc, Ref, Account } from './classes'
function toHex (value: number, chars: number): string {
const result = value.toString(16)
if (result.length < chars) {
return '0'.repeat(chars - result.length) + result
}
return result
}
let counter = (Math.random() * (1 << 24)) | 0
const random = toHex((Math.random() * (1 << 24)) | 0, 6) + toHex((Math.random() * (1 << 16)) | 0, 4)
function timestamp (): string {
const time = (Date.now() / 1000) | 0
return toHex(time, 8)
}
function count (): string {
const val = counter++ & 0xffffff
return toHex(val, 6)
}
/**
* @public
* @returns
*/
export function generateId<T extends Doc> (): Ref<T> {
return (timestamp() + random + count()) as Ref<T>
}
let currentAccount: Account
/**
* @public
* @returns
*/
export function getCurrentAccount (): Account { return currentAccount }
/**
* @public
* @param account -
*/
export function setCurrentAccount (account: Account): void {
currentAccount = account
}
/**
* @public
*/
export const genRanks = (count: number): Generator<string, void, unknown> =>
(function * () {
const sys = new LexoNumeralSystem36()
const base = 36
const max = base ** 6
const gap = LexoDecimal.parse(Math.trunc(max / (count + 2)).toString(base), sys)
let cur = LexoDecimal.parse('0', sys)
for (let i = 0; i < count; i++) {
cur = cur.add(gap)
yield new LexoRank(LexoRankBucket.BUCKET_0, cur).toString()
}
})()
/**
* @public
*/
export const calcRank = (prev?: { rank: string }, next?: { rank: string }): string => {
const a = prev?.rank !== undefined ? LexoRank.parse(prev.rank) : LexoRank.min()
const b = next?.rank !== undefined ? LexoRank.parse(next.rank) : LexoRank.max()
return a.between(b).toString()
}