Skip to content

Commit

Permalink
fix: support query parse in uri
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanba committed Sep 4, 2024
1 parent 80dcce7 commit a23732f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .changeset/old-ladybugs-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@difizen/mana-common': patch
'@difizen/mana-docs': patch
'@difizen/mana-app': patch
'@difizen/mana-core': patch
'@difizen/mana-l10n': patch
'@difizen/mana-observable': patch
'@difizen/mana-react': patch
'@difizen/mana-syringe': patch
'@difizen/mana-ui': patch
'@difizen/umi-plugin-mana': patch
---

fix: support query parse in uri
17 changes: 17 additions & 0 deletions packages/mana-common/src/uri.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ describe('URI', () => {
new URI('file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc').toString() ===
'file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc',
);
assert(
new URI('file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc', {
simpleMode: false,
}).query === 'KeyId=asgxdjasbcxjsc',
);
assert(
new URI('file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc', {
simpleMode: false,
}).getParsedQuery()['KeyId'] === 'asgxdjasbcxjsc',
);
assert(
new URI('file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc').toString() ===
URI.withQuery(
new URI('file:///a/b/c/example.png'),
URI.stringifyQuery({ KeyId: 'asgxdjasbcxjsc' }),
).toString(),
);
assert(
VscodeURI.parse('file:///a/b/c/example.png?KeyId=asgxdjasbcxjsc', false, {
simpleMode: false,
Expand Down
24 changes: 24 additions & 0 deletions packages/mana-common/src/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,28 @@ export class URI {
}
return !!Path.relative(left, right);
}

getParsedQuery(): { [key: string]: string } {
const queryString = this.query;
const query: Record<string, string> = {};
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split(
'&',
);
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
}

static stringifyQuery(query: { [key: string]: any }): string {
const values: string[] = [];
Object.keys(query).forEach((key) => {
const value = encodeURIComponent(query[key]);
if (value !== undefined) {
values.push(encodeURIComponent(key) + '=' + value);
}
});
return values.join('&');
}
}

0 comments on commit a23732f

Please sign in to comment.