diff options
author | Max Wu | 2018-03-03 16:25:30 +0800 |
---|---|---|
committer | Max Wu | 2018-03-03 16:26:19 +0800 |
commit | d08c9522c0dd414a6fed1671064701160d233603 (patch) | |
tree | d6ffeaadc516b2e75200a4d7e4f80fa2b5ab810e /public/js/utils.js | |
parent | fe429e9ac17b73638835b2ec1c5033043c5f9942 (diff) |
Update to migrate note url in the history of browser storage and cookie
Signed-off-by: Max Wu <jackymaxj@gmail.com>
Diffstat (limited to '')
-rw-r--r-- | public/js/utils.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/public/js/utils.js b/public/js/utils.js new file mode 100644 index 00000000..91e7f133 --- /dev/null +++ b/public/js/utils.js @@ -0,0 +1,32 @@ +import base64url from 'base64url' + +let uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i + +export function checkNoteIdValid (id) { + let result = id.match(uuidRegex) + if (result && result.length === 1) { + return true + } else { + return false + } +} + +export function encodeNoteId (id) { + // remove dashes in UUID and encode in url-safe base64 + let str = id.replace(/-/g, '') + let hexStr = Buffer.from(str, 'hex') + return base64url.encode(hexStr) +} + +export function decodeNoteId (encodedId) { + // decode from url-safe base64 + let id = base64url.toBuffer(encodedId).toString('hex') + // add dashes between the UUID string parts + let idParts = [] + idParts.push(id.substr(0, 8)) + idParts.push(id.substr(8, 4)) + idParts.push(id.substr(12, 4)) + idParts.push(id.substr(16, 4)) + idParts.push(id.substr(20, 12)) + return idParts.join('-') +} |