Skip to content

Commit 68405e8

Browse files
committed
feat: create util func to stringify object without quotes in props
1 parent d873051 commit 68405e8

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/graphql/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './generate-token.util';
44
export * from './get-number-of-arguments.util';
55
export * from './normalize-route-path.util';
66
export * from './remove-temp.util';
7+
export * from './object.util';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function stringifyWithoutQuotes(obj: object, includeSpaces?: boolean) {
2+
let result = includeSpaces
3+
? JSON.stringify(obj, null, 2)
4+
: JSON.stringify(obj);
5+
result = result
6+
.replace(/"([^"]+)":/g, '$1:')
7+
.replace(/(?<char>({|,|:))/g, '$<char> ');
8+
9+
if (!includeSpaces) {
10+
result = result.replace(/}/g, ' }');
11+
}
12+
return result;
13+
}

packages/graphql/tests/utils/function.utils.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getNumberOfArguments } from '../../lib/utils';
1+
import { getNumberOfArguments, stringifyWithoutQuotes } from '../../lib/utils';
22

33
describe('getNumberOfArguments', () => {
44
describe('when using function', () => {
@@ -145,3 +145,14 @@ describe('getNumberOfArguments', () => {
145145
});
146146
});
147147
});
148+
149+
describe('stringifyWithoutQuotes', () => {
150+
it('should stringify object correctly', () => {
151+
const obj = {
152+
name: '@tag',
153+
as: '@mytag',
154+
};
155+
156+
expect(stringifyWithoutQuotes(obj)).toBe('{ name: "@tag", as: "@mytag" }');
157+
});
158+
});

0 commit comments

Comments
 (0)