Skip to content

Commit

Permalink
Merge branch 'release/release/0.13.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 13, 2023
2 parents 5272084 + ca62d16 commit 5322257
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.13.8",
"version": "0.13.9",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
25 changes: 16 additions & 9 deletions src/common/data/url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { linkifyPlainText, toHumanReadableUrl } from './url'
import { encodeQuery, linkifyPlainText, toHumanReadableUrl } from './url'

describe('url', () => {
it('Split string to URLs', () => {
const sample
= 'https://example.com has <strange & fancy> some example.com at end http://example.com some'
const sample = 'https://example.com has <strange & fancy> some example.com at end http://example.com some'
expect(linkifyPlainText(sample)).toBe(
'<a target="_blank" href="https://example.com">example.com</a> has &lt;strange &amp; fancy&gt; some example.com at end <a target="_blank" href="http://example.com">example.com</a> some',
)
})

it('should make human readable url', () => {
expect(toHumanReadableUrl('example.com')).toEqual('example.com')
expect(toHumanReadableUrl('https://www.receipts-app.com/')).toEqual(
'receipts-app.com',
)
expect(toHumanReadableUrl('https://www.receipts-app.com/')).toEqual('receipts-app.com',)
expect(toHumanReadableUrl('https://pdfify.app/')).toEqual('pdfify.app')
expect(
toHumanReadableUrl('https://pdfify.app/more/subpage.html?query=123'),
).toEqual('pdfify.app/more/subpage.html?query=123')
expect(toHumanReadableUrl('https://pdfify.app/more/subpage.html?query=123')).toEqual('pdfify.app/more/subpage.html?query=123')
})

it('should build query', () => {
let data = {
a: 1,
b: '2',
c: null,
d: undefined,
e: 0
}
expect(encodeQuery(data)).toMatchInlineSnapshot('"a=1&b=2&e=0"')
expect(encodeQuery(data, (v) => !!v)).toMatchInlineSnapshot('"a=1&b=2"')
})
})
12 changes: 6 additions & 6 deletions src/common/data/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export function toHumanReadableUrl(url: string): string {

//

export function encodeQuery(data: Record<string, any>) {
export function encodeQuery(data: Record<string, any>, filterValue?: (value: any) => boolean) {
const pairs = []
for (let [key, value] of Object.entries(data)) {
if (value != null) {
if (!Array.isArray(value))
value = [value]
for (const v of value) {
if (v != null) {
pairs.push(
`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`,
)
}
if (filterValue && !filterValue(v))
continue
else if (v == null)
continue
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`)
}
}
}
Expand Down

0 comments on commit 5322257

Please sign in to comment.