feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
17
frontend/packages/arch/utils/src/index.ts
Normal file
17
frontend/packages/arch/utils/src/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { parseHashOrQuery } from './url/parse-hash-or-query';
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { parseHashOrQuery } from '../parse-hash-or-query';
|
||||
|
||||
const baseQuery = 'keyA=123&keyB=false&keyC=test&%3F!a=%3F!a';
|
||||
const expectResult = {
|
||||
keyA: '123',
|
||||
keyB: 'false',
|
||||
keyC: 'test',
|
||||
'?!a': '?!a',
|
||||
};
|
||||
|
||||
// 老版本 parseHashOrQuery 实现,验证一下输出一致
|
||||
const parseHashOrQueryOld = (hashFragmentOrQueryString: string) => {
|
||||
const targetString =
|
||||
hashFragmentOrQueryString.startsWith('#') ||
|
||||
hashFragmentOrQueryString.startsWith('?')
|
||||
? hashFragmentOrQueryString.slice(1)
|
||||
: hashFragmentOrQueryString;
|
||||
|
||||
const params: Record<string, string> = {};
|
||||
|
||||
const regex = /([^&=]+)=([^&]*)/g;
|
||||
|
||||
let matchResult: RegExpExecArray | null = null;
|
||||
|
||||
// eslint-disable-next-line no-cond-assign
|
||||
while ((matchResult = regex.exec(targetString))) {
|
||||
const [, key, value] = matchResult;
|
||||
params[decodeURIComponent(key)] = decodeURIComponent(value);
|
||||
}
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
describe('parseHashOrQuery', () => {
|
||||
it('parse query string starts with `?`', () => {
|
||||
const query = `?${baseQuery}`;
|
||||
const newRes = parseHashOrQuery(query);
|
||||
expect(newRes).toEqual(expectResult);
|
||||
expect(parseHashOrQueryOld(query)).toEqual(newRes);
|
||||
});
|
||||
|
||||
it('parse hash starts with `#`', () => {
|
||||
const query = `#${baseQuery}`;
|
||||
const newRes = parseHashOrQuery(query);
|
||||
expect(newRes).toEqual(expectResult);
|
||||
expect(parseHashOrQueryOld(query)).toEqual(newRes);
|
||||
});
|
||||
|
||||
it('parse plain string', () => {
|
||||
const newRes = parseHashOrQuery(baseQuery);
|
||||
expect(newRes).toEqual(expectResult);
|
||||
expect(parseHashOrQueryOld(baseQuery)).toEqual(newRes);
|
||||
});
|
||||
});
|
||||
35
frontend/packages/arch/utils/src/url/parse-hash-or-query.ts
Normal file
35
frontend/packages/arch/utils/src/url/parse-hash-or-query.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @deprecated use query-string or URLSearchParams instead
|
||||
* @param queryString query or hash string
|
||||
* @returns key value pair as the parse result, if a key show up more than ones in query, the last value will be taken
|
||||
*/
|
||||
export const parseHashOrQuery = (queryString: string) => {
|
||||
if (queryString.startsWith('?') || queryString.startsWith('#')) {
|
||||
queryString = queryString.slice(1);
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(queryString);
|
||||
const result: Record<string, string> = {};
|
||||
|
||||
for (const [key, value] of params.entries()) {
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
Reference in New Issue
Block a user