2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2022-02-16 09:02:31 +00:00
|
|
|
import type { Account, Doc, Ref } from './classes'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export function generateId<T extends Doc> (): Ref<T> {
|
|
|
|
return (timestamp() + random + count()) as Ref<T>
|
|
|
|
}
|
2021-09-10 15:54:13 +00:00
|
|
|
|
|
|
|
let currentAccount: Account
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function getCurrentAccount (): Account { return currentAccount }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @param account -
|
|
|
|
*/
|
|
|
|
export function setCurrentAccount (account: Account): void {
|
|
|
|
currentAccount = account
|
|
|
|
}
|
2022-02-16 09:02:31 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function escapeLikeForRegexp (value: string): string {
|
|
|
|
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|