trilium/services/utils.js

37 lines
716 B
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
function newNoteId() {
2017-10-29 00:12:20 +08:00
return randomString(12);
}
2017-10-29 00:12:20 +08:00
const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function randomString(length, chars) {
let result = '';
for (let i = length; i > 0; --i) {
2017-10-29 00:12:20 +08:00
result += ALPHA_NUMERIC[Math.floor(Math.random() * ALPHA_NUMERIC.length)];
}
return result;
}
function nowTimestamp() {
2017-10-16 05:07:34 +08:00
return Math.floor(Date.now() / 1000);
}
function toBase64(plainText) {
return Buffer.from(plainText).toString('base64');
}
function fromBase64(encodedText) {
return Buffer.from(encodedText, 'base64');
}
module.exports = {
2017-10-29 00:12:20 +08:00
randomString,
nowTimestamp,
newNoteId,
toBase64,
fromBase64
};