mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-12 17:26:17 +00:00
72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
export function lastFindInArray (array, callback) {
|
|
for (let i = array.length - 1; i >= 0; --i) {
|
|
if (callback(array[i], i, array)) {
|
|
return array[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
function escapeHtmlCharacters (html) {
|
|
const matchHtmlRegExp = /["'&<>]/g
|
|
const escapes = ['"', '&', ''', '<', '>']
|
|
let match = null
|
|
const replaceAt = (str, index, replace) =>
|
|
str.substr(0, index) +
|
|
replace +
|
|
str.substr(index + replace.length - (replace.length - 1))
|
|
|
|
while ((match = matchHtmlRegExp.exec(html)) != null) {
|
|
const current = { char: match[0], index: match.index }
|
|
if (current.char === '&') {
|
|
let nextStr = ''
|
|
let nextIndex = current.index
|
|
let escapedStr = false
|
|
// maximum length of an escape string is 5. For example ('"')
|
|
while (nextStr.length <= 5) {
|
|
nextStr += html[nextIndex]
|
|
nextIndex++
|
|
if (escapes.indexOf(nextStr) !== -1) {
|
|
escapedStr = true
|
|
break
|
|
}
|
|
}
|
|
if (!escapedStr) {
|
|
// this & char is not a part of an escaped string
|
|
html = replaceAt(html, current.index, '&')
|
|
}
|
|
} else if (current.char === '"') {
|
|
html = replaceAt(html, current.index, '"')
|
|
} else if (current.char === "'") {
|
|
html = replaceAt(html, current.index, ''')
|
|
} else if (current.char === '<') {
|
|
html = replaceAt(html, current.index, '<')
|
|
} else if (current.char === '>') {
|
|
html = replaceAt(html, current.index, '>')
|
|
}
|
|
}
|
|
return html
|
|
}
|
|
|
|
export function isObjectEqual (a, b) {
|
|
const aProps = Object.getOwnPropertyNames(a)
|
|
const bProps = Object.getOwnPropertyNames(b)
|
|
|
|
if (aProps.length !== bProps.length) {
|
|
return false
|
|
}
|
|
|
|
for (var i = 0; i < aProps.length; i++) {
|
|
const propName = aProps[i]
|
|
if (a[propName] !== b[propName]) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
export default {
|
|
lastFindInArray,
|
|
escapeHtmlCharacters,
|
|
isObjectEqual
|
|
}
|