mirror of
https://github.com/networkmastered/obsidian-compress.git
synced 2026-07-22 05:41:28 +00:00
74 lines
No EOL
1.4 KiB
JavaScript
74 lines
No EOL
1.4 KiB
JavaScript
//input is b64 so doesnt contain specials.
|
|
const charMap = [
|
|
'á',
|
|
'é',
|
|
'í',
|
|
'ó',
|
|
'ú',
|
|
'ü',
|
|
'ñ',
|
|
'ç',
|
|
'¿',
|
|
'¡'
|
|
]
|
|
//En
|
|
function ENum(num) {
|
|
let encoded = ''
|
|
for (let digit of num.toString()) {
|
|
encoded += charMap[parseInt(digit)]
|
|
}
|
|
return encoded
|
|
}
|
|
//De
|
|
function DNum(encoded) {
|
|
let decoded = ''
|
|
for (let char of encoded) {
|
|
decoded += charMap.indexOf(char)
|
|
}
|
|
return decoded
|
|
}
|
|
//V2
|
|
export function encodeSafe(str) {
|
|
let count = 0
|
|
let typ = ""
|
|
let out = ""
|
|
str.split("").forEach((char) => {
|
|
if (char != typ && typ != "") {
|
|
if (count > 2) {
|
|
out += ENum(count) + typ
|
|
} else {
|
|
out += typ.repeat(count)
|
|
}
|
|
count = 0
|
|
}
|
|
typ = char
|
|
count++
|
|
})
|
|
if (typ != "" && count > 0) {
|
|
if (count > 2) {
|
|
out += ENum(count) + typ
|
|
} else {
|
|
out += typ.repeat(count)
|
|
}
|
|
}
|
|
if (decodeSafe(out) == str) {
|
|
return out
|
|
}
|
|
}
|
|
export function decodeSafe(str) {
|
|
let count = ""
|
|
let out = ""
|
|
str.split("").forEach((char) => {
|
|
if (charMap.includes(char)) {
|
|
count += char
|
|
} else {
|
|
if (count == "") {
|
|
out += char
|
|
} else {
|
|
out += char.repeat(DNum(count))
|
|
count = ""
|
|
}
|
|
}
|
|
})
|
|
return out
|
|
} |