Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ build/Release
# Dependency directories

node_modules/
package-lock.json
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/utils/object/modify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ describe('Modifying objects', () => {
test('pick keys', () => {
expect(pick({ a: 1, b: 2 }, ['a'])).toEqual({ a: 1 })
})

test('handles duplicate keys', () => {
expect(pick({ a: 1, b: 2 }, ['a', 'a'])).toEqual({ a: 1 })
expect(exclude({ a: 1, b: 2 }, ['a', 'a'])).toEqual({ b: 2 })
})
})
6 changes: 4 additions & 2 deletions packages/lib/src/utils/object/modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ export function pick<T extends any, K extends (keyof T)[] = []>(
keys: K
): Pick<T, K[number]>
export function pick(object: any, keys: string[]) {
const keySet = new Set(keys)
return Object.fromEntries(
Object.entries(object).filter(([key]) => keys.includes(key))
Object.entries(object).filter(([key]) => keySet.has(key))
)
}

Expand All @@ -14,8 +15,9 @@ type ExcludeFunction = <T extends object, K extends (keyof T)[] = []>(
) => Omit<T, K[number]>

export const exclude: ExcludeFunction = (object, keys: any): any => {
const keySet = new Set(keys)
return Object.fromEntries(
Object.entries(object).filter(([key]) => !keys.includes(key))
Object.entries(object).filter(([key]) => !keySet.has(key))
)
}

Expand Down
51 changes: 27 additions & 24 deletions packages/lib/src/utils/string/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { pipe } from '../function/pipe'

const ESCAPE_MAP: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}
const UNESCAPE_MAP: Record<string, string> = {
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#39;': "'"
}
const ESCAPE_REGEX = new RegExp(
`(${Object.keys(ESCAPE_MAP).join('|')})`,
'g'
)
const UNESCAPE_REGEX = new RegExp(
`(${Object.keys(UNESCAPE_MAP).join('|')})`,
'g'
)

// TODO: refactor

export function normalizeNewLines(string_: string) {
Expand Down Expand Up @@ -39,35 +62,15 @@ export function normalizeNewLines(string_: string) {
}

export function escapeHtml(unsafeText: string): string {
const map: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}

return pipe(
Object.keys(map),
(keys) => keys.join('|'),
(match) => new RegExp(`(${match})`, 'g'),
(regex) => unsafeText.replaceAll(regex, (match) => map[match])
unsafeText,
(text) => text.replaceAll(ESCAPE_REGEX, (match) => ESCAPE_MAP[match])
)
}

export function unescapeHtml(unsafeText: string): string {
const map: Record<string, string> = {
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#39;': "'"
}

return pipe(
Object.keys(map),
(keys) => keys.join('|'),
(match) => new RegExp(`(${match})`, 'g'),
(regex) => unsafeText.replaceAll(regex, (match) => map[match])
unsafeText,
(text) => text.replaceAll(UNESCAPE_REGEX, (match) => UNESCAPE_MAP[match])
)
}