-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Feat(variables-scss): Add debug option for exporter #DS-1505
- Loading branch information
1 parent
1bc17e5
commit 8a63bbf
Showing
3 changed files
with
63 additions
and
21 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
exporters/variables-scss/src/helpers/__tests__/safeStringify.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { safeStringify } from '../safeStringify'; | ||
|
||
type CircularObject = { [key: string]: CircularObject | unknown }; | ||
|
||
describe('safeStringify', () => { | ||
it('should stringify a simple object', () => { | ||
const obj = { key: 'value', number: 42 }; | ||
const result = safeStringify(obj); | ||
|
||
expect(result).toBe(JSON.stringify(obj, null, 2)); | ||
}); | ||
|
||
it('should handle circular references in objects', () => { | ||
const obj: CircularObject = { key: 'value' }; | ||
obj.self = obj; // Introduce circular reference | ||
|
||
const result = safeStringify(obj); | ||
|
||
expect(result).toContain('"self": "CIRCULAR_REFERENCE"'); | ||
}); | ||
|
||
it('should handle nested objects with no circular references', () => { | ||
const obj = { outer: { inner: { key: 'value' } } }; | ||
const result = safeStringify(obj); | ||
|
||
expect(result).toBe(JSON.stringify(obj, null, 2)); | ||
}); | ||
|
||
it('should handle arrays within objects', () => { | ||
const obj = { list: [1, 2, 3] }; | ||
const result = safeStringify(obj); | ||
|
||
expect(result).toBe(JSON.stringify(obj, null, 2)); | ||
}); | ||
|
||
it('should handle null values', () => { | ||
const obj = { key: null }; | ||
const result = safeStringify(obj); | ||
|
||
expect(result).toBe(JSON.stringify(obj, null, 2)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const safeStringify = (obj: object) => { | ||
let cache: string[] | null = []; | ||
const str = JSON.stringify( | ||
obj, | ||
(key, value) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (cache?.includes(value)) { | ||
return 'CIRCULAR_REFERENCE'; | ||
} | ||
cache?.push(value); | ||
} | ||
|
||
return value; | ||
}, | ||
2, | ||
); | ||
cache = null; | ||
|
||
return str; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters