refactor(core): Deduplicate isObjectLiteral, add docs and tests (#12332)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-12-20 18:41:05 +01:00
committed by GitHub
parent 06b86af735
commit 724e08562f
10 changed files with 62 additions and 17 deletions

View File

@@ -1,3 +1,5 @@
import { isObjectLiteral } from './utils';
/** A nodejs Buffer gone through JSON.stringify */
export type SerializedBuffer = {
type: 'Buffer';
@@ -9,10 +11,6 @@ export function toBuffer(serializedBuffer: SerializedBuffer): Buffer {
return Buffer.from(serializedBuffer.data);
}
function isObjectLiteral(item: unknown): item is { [key: string]: unknown } {
return typeof item === 'object' && item !== null && !Array.isArray(item);
}
export function isSerializedBuffer(candidate: unknown): candidate is SerializedBuffer {
return (
isObjectLiteral(candidate) &&

View File

@@ -0,0 +1,35 @@
import { isObjectLiteral } from '@/utils';
describe('isObjectLiteral', () => {
test.each([
['empty object literal', {}, true],
['object with properties', { foo: 'bar', num: 123 }, true],
['nested object literal', { nested: { foo: 'bar' } }, true],
['object with symbol key', { [Symbol.for('foo')]: 'bar' }, true],
['null', null, false],
['empty array', [], false],
['array with values', [1, 2, 3], false],
['number', 42, false],
['string', 'string', false],
['boolean', true, false],
['undefined', undefined, false],
['Date object', new Date(), false],
['RegExp object', new RegExp(''), false],
['Map object', new Map(), false],
['Set object', new Set(), false],
['arrow function', () => {}, false],
['regular function', function () {}, false],
['class instance', new (class TestClass {})(), false],
['object with custom prototype', Object.create({ customMethod: () => {} }), true],
['Object.create(null)', Object.create(null), false],
['Buffer', Buffer.from('test'), false],
['Serialized Buffer', Buffer.from('test').toJSON(), true],
['Promise', new Promise(() => {}), false],
])('should return %s for %s', (_, input, expected) => {
expect(isObjectLiteral(input)).toBe(expected);
});
it('should return false for Error objects', () => {
expect(isObjectLiteral(new Error())).toBe(false);
});
});

View File

@@ -25,3 +25,4 @@ export * from './node-execution-context';
export * from './PartialExecutionUtils';
export { ErrorReporter } from './error-reporter';
export * from './SerializedBuffer';
export { isObjectLiteral } from './utils';

View File

@@ -0,0 +1,18 @@
type ObjectLiteral = { [key: string | symbol]: unknown };
/**
* Checks if the provided value is a plain object literal (not null, not an array, not a class instance, and not a primitive).
* This function serves as a type guard.
*
* @param candidate - The value to check
* @returns {boolean} True if the value is an object literal, false otherwise
*/
export function isObjectLiteral(candidate: unknown): candidate is ObjectLiteral {
return (
typeof candidate === 'object' &&
candidate !== null &&
!Array.isArray(candidate) &&
// eslint-disable-next-line @typescript-eslint/ban-types
(Object.getPrototypeOf(candidate) as Object)?.constructor?.name === 'Object'
);
}